54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
import re
|
|
|
|
import yaml
|
|
from irc.client import Event, NickMask, ServerConnection # type: ignore
|
|
from pinhook import bot, plugin # type: ignore
|
|
|
|
RELAY_REGEX = re.compile(r'^<([^@]+@(?:tilde|bread))>\s?(.*)$')
|
|
|
|
|
|
class BreadBot(bot.Bot):
|
|
|
|
def on_welcome(self, c: ServerConnection, e: Event) -> None:
|
|
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: ServerConnection, e: Event) -> None:
|
|
# 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
|
|
|
|
return super().process_event(c, e)
|
|
|
|
|
|
def main() -> None:
|
|
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()
|