move join/part/quit/autojoin to separate functions. part 1 of making things generic

This commit is contained in:
Brian S. Stephan 2010-07-25 07:34:54 -05:00
parent bad1a82dc4
commit 317d0d575d

View File

@ -6,6 +6,86 @@ import sys
import irclib
#####
# sub_join_channel
# join a channel when told to by an admin
# interface assumptions: privmsg
#####
def sub_join_channel(connection, event, admin_unlocked):
nick = event.source().split('!')[0]
userhost = event.source().split('!')[1]
what = event.arguments()[0]
if what.split(' ')[0] == 'join' and admin_unlocked:
channel = what.split(' ')[1]
connection.join(channel)
connection.privmsg(nick, 'joined ' + channel + '.')
#####
# sub_part_channel
# leave a channel when told to by an admin. optionally provide a message
# interface assumptions: privmsg
#####
def sub_part_channel(connection, event, admin_unlocked):
nick = event.source().split('!')[0]
userhost = event.source().split('!')[1]
what = event.arguments()[0]
if what.split(' ')[0] == 'part' and admin_unlocked:
channel = what.split(' ')[1]
connection.part(channel, ' '.join(what.split(' ')[2:]))
connection.privmsg(nick, 'parted ' + channel + '.')
#####
# sub_quit_channel
# quit irc server. optionally provide a message
# interface assumptions: privmsg
#####
def sub_quit_channel(connection, event, admin_unlocked):
nick = event.source().split('!')[0]
userhost = event.source().split('!')[1]
what = event.arguments()[0]
if what.split(' ')[0] == 'quit' and admin_unlocked:
connection.privmsg(nick, 'quitting')
connection.quit(' '.join(what.split(' ')[1:]))
with open('dr.botzo.cfg', 'w') as cfg:
config.write(cfg)
#####
# sub_handle_autojoin
# add/remove on channel autojoin list.
# interface assumptions: privmsg
#####
def sub_autojoin_manipulate(connection, event, admin_unlocked):
nick = event.source().split('!')[0]
userhost = event.source().split('!')[1]
what = event.arguments()[0]
if what.split(' ')[0] == 'autojoin' and admin_unlocked:
if what.split(' ')[1] == 'add':
try:
# get existing list
channel = what.split(' ')[2]
channelset = set(config.get('channels', 'autojoin').split(','))
channelset.add(channel)
config.set('channels', 'autojoin', ','.join(channelset))
connection.privmsg(nick, 'added ' + channel + ' to autojoin')
except NoOptionError: pass
elif what.split(' ')[1] == 'remove':
try:
# get existing list
channel = what.split(' ')[2]
channelset = set(config.get('channels', 'autojoin').split(','))
channelset.discard(channel)
config.set('channels', 'autojoin', ','.join(channelset))
connection.privmsg(nick, 'removed ' + channel + ' from autojoin')
except NoOptionError: pass
#####
# on_connect
# handler for when the bot has connected to IRC
@ -43,43 +123,10 @@ def on_privmsg(connection, event):
except NoOptionError: pass
# admin commands
# join channel
if what.split(' ')[0] == 'join' and admin_unlocked:
channel = what.split(' ')[1]
connection.join(channel)
connection.privmsg(nick, 'joined ' + channel + '.')
# part channel, with message
elif what.split(' ')[0] == 'part' and admin_unlocked:
channel = what.split(' ')[1]
connection.part(channel, ' '.join(what.split(' ')[2:]))
connection.privmsg(nick, 'parted ' + channel + '.')
# quit server, with message
elif what.split(' ')[0] == 'quit' and admin_unlocked:
connection.privmsg(nick, 'quitting')
connection.quit(' '.join(what.split(' ')[1:]))
with open('dr.botzo.cfg', 'w') as cfg:
config.write(cfg)
# add/remove channel to/from autojoin list
elif what.split(' ')[0] == 'autojoin' and admin_unlocked:
if what.split(' ')[1] == 'add':
try:
# get existing list
channel = what.split(' ')[2]
channelset = set(config.get('channels', 'autojoin').split(','))
channelset.add(channel)
config.set('channels', 'autojoin', ','.join(channelset))
connection.privmsg(nick, 'added ' + channel + ' to autojoin')
except NoOptionError: pass
elif what.split(' ')[1] == 'remove':
try:
# get existing list
channel = what.split(' ')[2]
channelset = set(config.get('channels', 'autojoin').split(','))
channelset.discard(channel)
config.set('channels', 'autojoin', ','.join(channelset))
connection.privmsg(nick, 'removed ' + channel + ' from autojoin')
except NoOptionError: pass
sub_join_channel(connection, event, admin_unlocked)
sub_part_channel(connection, event, admin_unlocked)
sub_quit_channel(connection, event, admin_unlocked)
sub_autojoin_manipulate(connection, event, admin_unlocked)
#####
# init