breadbot/breadbot/__main__.py

55 lines
1.4 KiB
Python

#!/usr/bin/env python3
import argparse
import re
import yaml
from irc.client import NickMask
from pinhook import plugin, bot
RELAY_REGEX = re.compile(r'^<([^@]+@(?:tilde|bread))>\s?(.*)$')
class BreadBot(bot.Bot):
def on_welcome(self, c, e):
c.mode(self.bot_nick, '+B')
super().on_welcome(c, e)
for channel in self.chanlist:
self.process_output(c, channel, plugin.message('bread'))
def process_event(self, c, e):
# Handle BreadRelay by suppressing it
if e.source.nick == 'BreadRelay' and e.arguments:
match = RELAY_REGEX.match(e.arguments[0])
if match:
nick, e.arguments[0] = match.groups()
# e.source.nick is a property, and e.source is a string
# in the nick!userhost format, so we reconstruct it
e.source = NickMask(f'{nick}!{e.source.userhost}')
# Ignore messages sent by the bot itself
if e.source.nick == self.bot_nick:
return
print(e.source, e.arguments)
return super().process_event(c, e)
def main():
parser = argparse.ArgumentParser(
description='Breadpunk Bread Bot',
)
parser.add_argument(
'config',
type=argparse.FileType('r'),
help='Configuration file',
)
args = parser.parse_args()
config = yaml.safe_load(args.config)
bot = BreadBot(**config)
bot.start()
if __name__ == '__main__':
main()