From fd171fb52c84832961f4249377c95944579cf2d2 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 17 Aug 2018 18:01:44 -0500 Subject: [PATCH] irc: add AP message relay --- viera/actor.py | 11 ++++++++++- viera/irc.py | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/viera/actor.py b/viera/actor.py index a9696e7..4c95a61 100644 --- a/viera/actor.py +++ b/viera/actor.py @@ -85,10 +85,15 @@ def strip_html(data): return cgi.escape(no_tags) -from .authreqs import check_reqs +from .authreqs import check_reqs, get_irc_bot async def handle_create(actor, data, request): + # for now, we only care about Notes + if data['object']['type'] != Note: + return + + # strip the HTML if present content = strip_html(data['object']['content']).split() # check if the message is an authorization token for linking identities together @@ -96,6 +101,10 @@ async def handle_create(actor, data, request): if check_reqs(content, actor): return + # fetch our IRC bot + bot = get_irc_bot() + bot.relay_message(actor, data['object'], content) + async def handle_follow(actor, data, request): message = { diff --git a/viera/irc.py b/viera/irc.py index 9477c8a..11d97ff 100644 --- a/viera/irc.py +++ b/viera/irc.py @@ -204,6 +204,18 @@ class IRCProtocol(asyncio.Protocol): logging.debug('> %r', m) self.transport.write(m.to_message().encode('utf-8') + b'\r\n') + def relay_message(self, actor, obj, content): + fmt = "\x02{name}\x02: {content} [{url}]" + + msgcontent = content[0:256] + if len(content) > 256: + msgcontent += '...' + + message = fmt.format(name=actor['name'], content=msgcontent, url=obj['id']) + target = ','.join(IRC_CONFIG['relay_channels']) + + self.say(target, message) + async def irc_bot(): loop = asyncio.get_event_loop()