dr.botzo/dr.botzo.py

85 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python2.6
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
import os
import sys
import irclib
#####
# 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 channel
# TODO: support multiple
if irclib.is_channel(channel):
connection.join(channel)
#####
# 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
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:]))
#####
# 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')
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))
# 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