convert all the miscellaneous irc interactions to an IrcAdmin Module. now everything is a module, and i can remove some other hooks. ugliest diff to date.

This commit is contained in:
Brian S. Stephan 2010-07-25 22:06:51 -05:00
parent df91495652
commit 565aff193c
1 changed files with 89 additions and 163 deletions

View File

@ -291,179 +291,109 @@ class Seen(Module):
connection.privmsg(replypath, 'last saw ' + query + ' at ' + converted.astimezone(tzlocal()).strftime("%Y/%m/%d %H:%M:%S %Z") + ' saying \'' + seendata[2] + '\'')
except NoOptionError: pass
#####
# sub_join_channel
# join a channel when told to by an admin
#####
class IrcAdmin(Module):
"""all kinds of miscellaneous irc stuff
"""
def sub_join_channel(connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'join' and admin_unlocked and len(whats) >= 2:
channel = whats[1]
if irclib.is_channel(channel):
connection.join(channel)
connection.privmsg(replypath, 'joined ' + channel)
def __init__(self, config, server):
super(IrcAdmin, self).__init__(config, server)
#####
# sub_part_channel
# leave a channel when told to by an admin. optionally provide a message
#####
def register_handlers(self, server):
server.add_global_handler('welcome', self.on_connect)
server.add_global_handler('pubmsg', self.on_pubmsg)
server.add_global_handler('privmsg', self.on_privmsg)
def sub_part_channel(connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'part' and admin_unlocked and len(whats) >= 2:
channel = whats[1]
if irclib.is_channel(channel):
connection.part(channel, ' '.join(whats[2:]))
connection.privmsg(replypath, 'parted ' + channel)
def on_connect(self, connection, event):
"""handler for when the bot has connected to IRC
"""
#####
# sub_quit_channel
# quit irc server. optionally provide a message
#####
# user modes
try:
usermode = self.config.get('IRC', 'usermode')
connection.mode(botnick, usermode)
except NoOptionError: pass
def sub_quit_channel(connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'quit' and admin_unlocked:
connection.privmsg(replypath, 'quitting')
connection.quit(' '.join(whats[1:]))
with open('dr.botzo.cfg', 'w') as cfg:
config.write(cfg)
#####
# sub_handle_autojoin
# add/remove on channel autojoin list.
#####
def sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'autojoin' and admin_unlocked and len(whats) >= 3:
if whats[1] == 'add':
try:
# get existing list
channel = whats[2]
# join the specified channels
try:
autojoins = self.config.get('channels', 'autojoin').split(',')
for channel in autojoins:
if irclib.is_channel(channel):
channelset = set(config.get('channels', 'autojoin').split(','))
channelset.add(channel)
config.set('channels', 'autojoin', ','.join(channelset))
connection.privmsg(replypath, 'added ' + channel + ' to autojoin')
except NoOptionError: pass
elif whats[1] == 'remove':
try:
# get existing list
channel = whats[2]
if irclib.is_channel(channel):
channelset = set(config.get('channels', 'autojoin').split(','))
channelset.discard(channel)
config.set('channels', 'autojoin', ','.join(channelset))
connection.privmsg(replypath, 'removed ' + channel + ' from autojoin')
except NoOptionError: pass
connection.join(channel)
except NoOptionError: pass
#####
# sub_save_config
# save the config file
#####
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
# this could be more sophisticated, but i'm too lazy to do a real port
# right now.
# TODO: sophisticate. also, document all of these
self.sub_join_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
self.sub_part_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
self.sub_quit_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
self.sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked)
self.sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked)
self.sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked)
def sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'save' and admin_unlocked:
with open('dr.botzo.cfg', 'w') as cfg:
config.write(cfg)
connection.privmsg(replypath, 'saved config file')
#####
# sub_change_nick
# change the bot's nickname
#####
def sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'nick' and admin_unlocked and len(whats) >= 2:
newnick = whats[1]
connection.nick(newnick)
config.set('IRC', 'nick', newnick)
connection.privmsg(replypath, 'changed nickname')
#####
# on_connect
# handler for when the bot has connected to IRC
#####
def on_connect(connection, event):
# user modes
try:
usermode = config.get('IRC', 'usermode')
connection.mode(botnick, usermode)
except NoOptionError: pass
# join the specified channels
try:
autojoins = config.get('channels', 'autojoin').split(',')
for channel in autojoins:
def sub_join_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'join' and admin_unlocked and len(whats) >= 2:
channel = whats[1]
if irclib.is_channel(channel):
connection.join(channel)
except NoOptionError: pass
connection.privmsg(replypath, 'joined ' + channel)
#####
# on_privmsg
# private messages to the bot
#####
def sub_part_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'part' and admin_unlocked and len(whats) >= 2:
channel = whats[1]
if irclib.is_channel(channel):
connection.part(channel, ' '.join(whats[2:]))
connection.privmsg(replypath, 'parted ' + channel)
def on_privmsg(connection, event):
nick = irclib.nm_to_n(event.source())
userhost = irclib.nm_to_uh(event.source())
replypath = nick
what = event.arguments()[0]
def sub_quit_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'quit' and admin_unlocked:
connection.privmsg(replypath, 'quitting')
connection.quit(' '.join(whats[1:]))
with open('dr.botzo.cfg', 'w') as cfg:
self.config.write(cfg)
admin_unlocked = False
def sub_autojoin_manipulate(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'autojoin' and admin_unlocked and len(whats) >= 3:
if whats[1] == 'add':
try:
# get existing list
channel = whats[2]
if irclib.is_channel(channel):
channelset = set(self.config.get('channels', 'autojoin').split(','))
channelset.add(channel)
self.config.set('channels', 'autojoin', ','.join(channelset))
connection.privmsg(replypath, 'added ' + channel + ' to autojoin')
except NoOptionError: pass
elif whats[1] == 'remove':
try:
# get existing list
channel = whats[2]
if irclib.is_channel(channel):
channelset = set(self.config.get('channels', 'autojoin').split(','))
channelset.discard(channel)
self.config.set('channels', 'autojoin', ','.join(channelset))
connection.privmsg(replypath, 'removed ' + channel + ' from autojoin')
except NoOptionError: pass
try:
if userhost == config.get('admin', 'userhost'):
admin_unlocked = True
except NoOptionError: pass
def sub_save_config(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'save' and admin_unlocked:
with open('dr.botzo.cfg', 'w') as cfg:
self.config.write(cfg)
connection.privmsg(replypath, 'saved config file')
# admin commands
sub_join_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_part_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_quit_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked)
#####
# on_pubmsg
# public messages in a channel where the bot is
#####
def on_pubmsg(connection, event):
nick = irclib.nm_to_n(event.source())
userhost = irclib.nm_to_uh(event.source())
replypath = event.target()
what = event.arguments()[0]
admin_unlocked = False
try:
if userhost == config.get('admin', 'userhost'):
admin_unlocked = True
except NoOptionError: pass
# only do commands if the bot has been addressed directly
addressed_pattern = '^' + connection.get_nickname() + '[:,]?\s+'
addressed_re = re.compile(addressed_pattern)
if not addressed_re.match(what):
return
else:
what = addressed_re.sub('', what)
# admin commands
sub_join_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_part_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_quit_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked)
sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked)
def sub_change_nick(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'nick' and admin_unlocked and len(whats) >= 2:
newnick = whats[1]
connection.nick(newnick)
self.config.set('IRC', 'nick', newnick)
connection.privmsg(replypath, 'changed nickname')
#####
# init
@ -495,13 +425,9 @@ irclib.DEBUG = config.getboolean('IRC', 'debug')
irc = irclib.IRC()
server = irc.server().connect(botserver, botport, botnick, botircname)
# install handlers
server.add_global_handler("welcome", on_connect)
server.add_global_handler('privmsg', on_privmsg)
server.add_global_handler('pubmsg', on_pubmsg)
count = Countdown(config, server)
dice = Dice(config, server)
admin = IrcAdmin(config, server)
gt = GoogleTranslate(config, server)
seen = Seen(config, server)