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:
parent
df91495652
commit
565aff193c
252
dr.botzo.py
252
dr.botzo.py
|
@ -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] + '\'')
|
connection.privmsg(replypath, 'last saw ' + query + ' at ' + converted.astimezone(tzlocal()).strftime("%Y/%m/%d %H:%M:%S %Z") + ' saying \'' + seendata[2] + '\'')
|
||||||
except NoOptionError: pass
|
except NoOptionError: pass
|
||||||
|
|
||||||
#####
|
class IrcAdmin(Module):
|
||||||
# sub_join_channel
|
"""all kinds of miscellaneous irc stuff
|
||||||
# join a channel when told to by an admin
|
"""
|
||||||
#####
|
|
||||||
|
|
||||||
def sub_join_channel(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
def __init__(self, config, server):
|
||||||
whats = what.split(' ')
|
super(IrcAdmin, self).__init__(config, server)
|
||||||
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 register_handlers(self, server):
|
||||||
# sub_part_channel
|
server.add_global_handler('welcome', self.on_connect)
|
||||||
# leave a channel when told to by an admin. optionally provide a message
|
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):
|
def on_connect(self, connection, event):
|
||||||
whats = what.split(' ')
|
"""handler for when the bot has connected to IRC
|
||||||
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)
|
|
||||||
|
|
||||||
#####
|
# user modes
|
||||||
# sub_quit_channel
|
try:
|
||||||
# quit irc server. optionally provide a message
|
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):
|
# join the specified channels
|
||||||
whats = what.split(' ')
|
try:
|
||||||
if whats[0] == 'quit' and admin_unlocked:
|
autojoins = self.config.get('channels', 'autojoin').split(',')
|
||||||
connection.privmsg(replypath, 'quitting')
|
for channel in autojoins:
|
||||||
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]
|
|
||||||
if irclib.is_channel(channel):
|
if irclib.is_channel(channel):
|
||||||
channelset = set(config.get('channels', 'autojoin').split(','))
|
connection.join(channel)
|
||||||
channelset.add(channel)
|
except NoOptionError: pass
|
||||||
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
|
|
||||||
|
|
||||||
#####
|
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
# sub_save_config
|
# this could be more sophisticated, but i'm too lazy to do a real port
|
||||||
# save the config file
|
# 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):
|
def sub_join_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
whats = what.split(' ')
|
whats = what.split(' ')
|
||||||
if whats[0] == 'save' and admin_unlocked:
|
if whats[0] == 'join' and admin_unlocked and len(whats) >= 2:
|
||||||
with open('dr.botzo.cfg', 'w') as cfg:
|
channel = whats[1]
|
||||||
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:
|
|
||||||
if irclib.is_channel(channel):
|
if irclib.is_channel(channel):
|
||||||
connection.join(channel)
|
connection.join(channel)
|
||||||
except NoOptionError: pass
|
connection.privmsg(replypath, 'joined ' + channel)
|
||||||
|
|
||||||
#####
|
def sub_part_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
# on_privmsg
|
whats = what.split(' ')
|
||||||
# private messages to the bot
|
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):
|
def sub_quit_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
nick = irclib.nm_to_n(event.source())
|
whats = what.split(' ')
|
||||||
userhost = irclib.nm_to_uh(event.source())
|
if whats[0] == 'quit' and admin_unlocked:
|
||||||
replypath = nick
|
connection.privmsg(replypath, 'quitting')
|
||||||
what = event.arguments()[0]
|
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:
|
def sub_save_config(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
if userhost == config.get('admin', 'userhost'):
|
whats = what.split(' ')
|
||||||
admin_unlocked = True
|
if whats[0] == 'save' and admin_unlocked:
|
||||||
except NoOptionError: pass
|
with open('dr.botzo.cfg', 'w') as cfg:
|
||||||
|
self.config.write(cfg)
|
||||||
|
connection.privmsg(replypath, 'saved config file')
|
||||||
|
|
||||||
# admin commands
|
def sub_change_nick(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
sub_join_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
whats = what.split(' ')
|
||||||
sub_part_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
if whats[0] == 'nick' and admin_unlocked and len(whats) >= 2:
|
||||||
sub_quit_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
newnick = whats[1]
|
||||||
sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
connection.nick(newnick)
|
||||||
sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
self.config.set('IRC', 'nick', newnick)
|
||||||
sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
connection.privmsg(replypath, 'changed nickname')
|
||||||
|
|
||||||
#####
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
#####
|
#####
|
||||||
# init
|
# init
|
||||||
|
@ -495,13 +425,9 @@ irclib.DEBUG = config.getboolean('IRC', 'debug')
|
||||||
irc = irclib.IRC()
|
irc = irclib.IRC()
|
||||||
server = irc.server().connect(botserver, botport, botnick, botircname)
|
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)
|
count = Countdown(config, server)
|
||||||
dice = Dice(config, server)
|
dice = Dice(config, server)
|
||||||
|
admin = IrcAdmin(config, server)
|
||||||
gt = GoogleTranslate(config, server)
|
gt = GoogleTranslate(config, server)
|
||||||
seen = Seen(config, server)
|
seen = Seen(config, server)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue