import logging from ircbot.lib import Plugin, is_admin from ircbot.models import IrcChannel log = logging.getLogger('ircbot.ircplugins.ircmgmt') class ChannelManagement(Plugin): """Have IRC commands to do IRC things (join channels, quit, etc.).""" def start(self): """Set up the handlers.""" self.connection.reactor.add_global_regex_handler('pubmsg', r'^!join\s+([\S]+)', getattr(self, 'handle_join'), -20) self.connection.reactor.add_global_regex_handler('privmsg', r'^!join\s+([\S]+)', getattr(self, 'handle_join'), -20) self.connection.reactor.add_global_regex_handler('pubmsg', r'^!part\s+([\S]+)', getattr(self, 'handle_part'), -20) self.connection.reactor.add_global_regex_handler('privmsg', r'^!part\s+([\S]+)', getattr(self, 'handle_part'), -20) self.connection.reactor.add_global_regex_handler('pubmsg', r'^!quit\s*(.*)', getattr(self, 'handle_quit'), -20) self.connection.reactor.add_global_regex_handler('privmsg', r'^!quit\s*(.*)', getattr(self, 'handle_quit'), -20) super(ChannelManagement, self).start() def stop(self): """Tear down handlers.""" self.connection.reactor.remove_global_regex_handler('pubmsg', getattr(self, 'handle_join')) self.connection.reactor.remove_global_regex_handler('privmsg', getattr(self, 'handle_join')) self.connection.reactor.remove_global_regex_handler('pubmsg', getattr(self, 'handle_part')) self.connection.reactor.remove_global_regex_handler('privmsg', getattr(self, 'handle_part')) self.connection.reactor.remove_global_regex_handler('pubmsg', getattr(self, 'handle_quit')) self.connection.reactor.remove_global_regex_handler('privmsg', getattr(self, 'handle_quit')) super(ChannelManagement, self).stop() def handle_join(self, connection, event, match): """Handle the join command.""" if is_admin(event.source): channel = match.group(1) # put it in the database if it isn't already chan_mod, c = IrcChannel.objects.get_or_create(name=channel) log.debug(u"joining channel %s", channel) self.connection.join(channel) return self.bot.reply(event, "Joined channel {0:s}.".format(channel)) def handle_part(self, connection, event, match): """Handle the join command.""" if is_admin(event.source): channel = match.group(1) # put it in the database if it isn't already chan_mod, c = IrcChannel.objects.get_or_create(name=channel) log.debug(u"parting channel %s", channel) self.connection.part(channel) return self.bot.reply(event, "Parted channel {0:s}.".format(channel)) def handle_quit(self, connection, event, match): """Handle the join command.""" if is_admin(event.source): self.bot.die(msg=match.group(1)) plugin = ChannelManagement