Skip to content

Commit

Permalink
Don't JOIN/PART if using NOTICE
Browse files Browse the repository at this point in the history
Wait for an ack before quitting.
  • Loading branch information
rectalogic committed Sep 16, 2019
1 parent 1d9e80c commit 8737726
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions notify_irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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,
Expand Down

0 comments on commit 8737726

Please sign in to comment.