dr.botzo/modules/IrcAdmin.py

147 lines
6.5 KiB
Python
Raw Normal View History

2010-07-27 20:35:01 -05:00
from ConfigParser import NoOptionError
from Module import Module
from irclib import irclib
import traceback
class IrcAdmin(Module):
# All kinds of miscellaneous irc stuff
def __init__(self, config, server, modlist, rehash):
super(IrcAdmin, self).__init__(config, server, modlist, rehash)
modlist.append(self)
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 on_connect(self, connection, event):
"""handler for when the bot has connected to IRC
"""
# user modes
try:
usermode = self.config.get('IRC', 'usermode')
connection.mode(self.botnick, usermode)
except NoOptionError: pass
# join the specified channels
try:
autojoins = self.config.get('channels', 'autojoin').split(',')
for channel in autojoins:
if irclib.is_channel(channel):
connection.join(channel)
except NoOptionError: pass
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)
self.sub_rehash(connection, event, nick, userhost, replypath, what, admin_unlocked)
def sub_rehash(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'rehash' and admin_unlocked:
self.rehash()
replystr = 'rehashed modules'
#traceback.print_stack()
if replypath is None:
return replystr
else:
connection.privmsg(replypath, replystr)
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)
replystr = 'joined ' + channel
if replypath is None:
return replystr
else:
connection.privmsg(replypath, replystr)
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:]))
replystr = 'parted ' + channel
if replypath is None:
return replystr
else:
connection.privmsg(replypath, replystr)
def sub_quit_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'quit' and admin_unlocked:
if replypath is not None:
connection.privmsg(replypath, 'quitting')
connection.quit(' '.join(whats[1:]))
with open('dr.botzo.cfg', 'w') as cfg:
self.config.write(cfg)
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))
replystr = 'added ' + channel + ' to autojoin'
if replypath is None:
return replystr
else:
connection.privmsg(replypath, replystr)
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))
replystr = 'removed ' + channel + ' from autojoin'
if replypath is None:
return replystr
else:
connection.privmsg(replypath, replystr)
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)
replystr = 'saved config file'
if replypath is None:
return replystr
else:
connection.privmsg(replypath, replystr)
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)
replystr = 'changed nickname'
if replypath is None:
return replystr
else:
connection.privmsg(replypath, replystr)