From 87377266635a9b7093c715a5ebb8a073b810cd9d Mon Sep 17 00:00:00 2001 From: Andrew Wason Date: Fri, 13 Sep 2019 13:44:24 -0400 Subject: [PATCH] Don't JOIN/PART if using NOTICE Wait for an ack before quitting. --- notify_irc.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/notify_irc.py b/notify_irc.py index c777961..8e365ad 100755 --- a/notify_irc.py +++ b/notify_irc.py @@ -2,9 +2,12 @@ import sys import argparse +import logging import pydle +log = logging.getLogger(__name__) + class NotifyIRC(pydle.Client): def __init__(self, channel, channel_key, notification, use_notice=False, **kwargs): @@ -13,25 +16,37 @@ def __init__(self, channel, channel_key, notification, use_notice=False, **kwarg self.channel_key = channel_key self.notification = notification self.use_notice = use_notice + self.future = None async def on_connect(self): await super().on_connect() - await self.join(self.channel, self.channel_key) + if self.use_notice: + await self.notice(self.channel, self.notification) + # Need to issue a command and await the response before we quit, + # otherwise we are disconnected before the notice is processed + self.future = self.eventloop.create_future() + await self.rawmsg("VERSION") + await self.future + await self.quit() + else: + await self.join(self.channel, self.channel_key) async def on_join(self, channel, user): await super().on_join(channel, user) if user != self.nickname: return - if self.use_notice: - await self.notice(self.channel, self.notification) - else: - await self.message(self.channel, self.notification) + await self.message(self.channel, self.notification) await self.part(self.channel) async def on_part(self, channel, user, message=None): await super().on_part(channel, user, message) await self.quit() + async def on_raw_351(self, message): + """VERSION response""" + if self.future: + self.future.set_result(None) + def get_args(): parser = argparse.ArgumentParser() @@ -49,11 +64,13 @@ def get_args(): "--notice", action="store_true", help="Use NOTICE instead if PRIVMSG" ) parser.add_argument("--message", required=True) + parser.add_argument("--verbose", action="store_true") return parser.parse_args() def main(): args = get_args() + logging.basicConfig(level=logging.DEBUG if args.verbose else logging.WARNING) client = NotifyIRC( channel=args.channel,