dr.botzo/dr.botzo.py

85 lines
2.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python2.6
2010-07-24 09:54:38 -05:00
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
2010-07-24 09:54:38 -05:00
import os
import sys
2010-07-24 09:54:38 -05:00
2010-07-24 10:47:33 -05:00
import irclib
2010-07-24 12:22:35 -05:00
#####
# on_connect
2010-07-24 10:47:33 -05:00
# handler for when the bot has connected to IRC
2010-07-24 12:22:35 -05:00
#####
2010-07-24 10:47:33 -05:00
def on_connect(connection, event):
# user modes
2010-07-24 11:45:46 -05:00
try:
usermode = config.get('IRC', 'usermode')
connection.mode(nick, usermode)
except NoOptionError: pass
2010-07-24 10:47:33 -05:00
# join the specified channel
# TODO: support multiple
if irclib.is_channel(channel):
connection.join(channel)
2010-07-24 12:22:35 -05:00
#####
# on_privmsg
# private messages to the bot
#####
def on_privmsg(connection, event):
2010-07-24 12:42:25 -05:00
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
if what.split(' ')[0] == 'join' and admin_unlocked:
connection.join(what.split(' ')[1])
elif what.split(' ')[0] == 'part' and admin_unlocked:
connection.part(what.split(' ')[1], ' '.join(what.split(' ')[2:]))
2010-07-24 10:47:33 -05:00
#####
# init
#####
# read config file
2010-07-24 11:34:19 -05:00
config = ConfigParser({'debug': 'false'})
2010-07-24 09:54:38 -05:00
config.read([os.path.expanduser('~/.dr.botzo.cfg'), 'dr.botzo.cfg'])
2010-07-24 11:34:19 -05:00
# load necessary options
try:
# load connection info
server = config.get('IRC', 'server')
port = config.getint('IRC', 'port')
channel = config.get('IRC', 'channel')
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))
2010-07-24 09:54:38 -05:00
2010-07-24 11:34:19 -05:00
# load additional options
irclib.DEBUG = config.getboolean('IRC', 'debug')
2010-07-24 10:47:33 -05:00
# 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)
2010-07-24 12:22:35 -05:00
server.add_global_handler('privmsg', on_privmsg)
2010-07-24 10:47:33 -05:00
# loop forever
irc.process_forever()
2010-07-24 09:54:38 -05:00
# vi:tabstop=4:expandtab:autoindent