dr.botzo/dr.botzo.py

169 lines
4.9 KiB
Python
Executable File

#!/usr/bin/env python2.6
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
import os
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
#####
def on_connect(connection, event):
# user modes
try:
usermode = config.get('IRC', 'usermode')
connection.mode(nick, usermode)
except NoOptionError: pass
# join the specified channels
try:
autojoins = config.get('channels', 'autojoin').split(',')
for channel in autojoins:
if irclib.is_channel(channel):
connection.join(channel)
except NoOptionError: pass
#####
# on_privmsg
# private messages to the bot
#####
def on_privmsg(connection, event):
nick = event.source().split('!')[0]
userhost = event.source().split('!')[1]
what = event.arguments()[0]
admin_unlocked = False
try:
if userhost == config.get('admin', 'userhost'):
admin_unlocked = True
except NoOptionError: pass
# admin commands
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
#####
# read config file
config = ConfigParser({'debug': 'false'})
config.read([os.path.expanduser('~/.dr.botzo.cfg'), 'dr.botzo.cfg'])
# load necessary options
try:
# load connection info
server = config.get('IRC', 'server')
port = config.getint('IRC', 'port')
nick = config.get('IRC', 'nick')
ircname = config.get('IRC', 'name')
except NoSectionError as e:
sys.exit("Aborted due to error with necessary configuration: " + str(e))
except NoOptionError as e:
sys.exit("Aborted due to error with necessary configuration: " + str(e))
# load additional options
irclib.DEBUG = config.getboolean('IRC', 'debug')
# start up the IRC bot
# create IRC and server objects and connect
irc = irclib.IRC()
server = irc.server().connect(server, port, nick, ircname)
# install handlers
server.add_global_handler("welcome", on_connect)
server.add_global_handler('privmsg', on_privmsg)
# loop forever
irc.process_forever()
# vi:tabstop=4:expandtab:autoindent