2010-07-24 10:39:58 -05:00
|
|
|
#!/usr/bin/env python2.6
|
2010-07-24 09:54:38 -05:00
|
|
|
|
2010-07-24 10:39:58 -05:00
|
|
|
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
|
2010-07-24 09:54:38 -05:00
|
|
|
import os
|
2010-07-24 10:12:00 -05:00
|
|
|
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
|
|
|
#####
|
2010-07-25 07:34:54 -05:00
|
|
|
# sub_join_channel
|
|
|
|
# join a channel when told to by an admin
|
|
|
|
# interface assumptions: privmsg
|
2010-07-24 12:22:35 -05:00
|
|
|
#####
|
|
|
|
|
2010-07-25 07:54:11 -05:00
|
|
|
def sub_join_channel(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 07:34:54 -05:00
|
|
|
if what.split(' ')[0] == 'join' and admin_unlocked:
|
|
|
|
channel = what.split(' ')[1]
|
2010-07-25 07:58:52 -05:00
|
|
|
if irclib.is_channel(channel):
|
|
|
|
connection.join(channel)
|
|
|
|
connection.privmsg(replypath, 'joined ' + channel + '.')
|
2010-07-24 12:59:51 -05:00
|
|
|
|
2010-07-24 12:22:35 -05:00
|
|
|
#####
|
2010-07-25 07:34:54 -05:00
|
|
|
# sub_part_channel
|
|
|
|
# leave a channel when told to by an admin. optionally provide a message
|
|
|
|
# interface assumptions: privmsg
|
2010-07-24 12:22:35 -05:00
|
|
|
#####
|
|
|
|
|
2010-07-25 07:54:11 -05:00
|
|
|
def sub_part_channel(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 07:34:54 -05:00
|
|
|
if what.split(' ')[0] == 'part' and admin_unlocked:
|
2010-07-24 13:44:45 -05:00
|
|
|
channel = what.split(' ')[1]
|
2010-07-25 07:58:52 -05:00
|
|
|
if irclib.is_channel(channel):
|
|
|
|
connection.part(channel, ' '.join(what.split(' ')[2:]))
|
|
|
|
connection.privmsg(replypath, 'parted ' + channel + '.')
|
2010-07-25 07:34:54 -05:00
|
|
|
|
|
|
|
#####
|
|
|
|
# sub_quit_channel
|
|
|
|
# quit irc server. optionally provide a message
|
|
|
|
# interface assumptions: privmsg
|
|
|
|
#####
|
|
|
|
|
2010-07-25 07:54:11 -05:00
|
|
|
def sub_quit_channel(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 07:34:54 -05:00
|
|
|
if what.split(' ')[0] == 'quit' and admin_unlocked:
|
2010-07-25 07:54:11 -05:00
|
|
|
connection.privmsg(replypath, 'quitting')
|
2010-07-24 12:52:27 -05:00
|
|
|
connection.quit(' '.join(what.split(' ')[1:]))
|
2010-07-24 13:23:37 -05:00
|
|
|
with open('dr.botzo.cfg', 'w') as cfg:
|
|
|
|
config.write(cfg)
|
2010-07-25 07:34:54 -05:00
|
|
|
|
|
|
|
#####
|
|
|
|
# sub_handle_autojoin
|
|
|
|
# add/remove on channel autojoin list.
|
|
|
|
# interface assumptions: privmsg
|
|
|
|
#####
|
|
|
|
|
2010-07-25 07:54:11 -05:00
|
|
|
def sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 07:34:54 -05:00
|
|
|
if what.split(' ')[0] == 'autojoin' and admin_unlocked:
|
2010-07-24 13:36:13 -05:00
|
|
|
if what.split(' ')[1] == 'add':
|
|
|
|
try:
|
|
|
|
# get existing list
|
2010-07-24 13:44:45 -05:00
|
|
|
channel = what.split(' ')[2]
|
2010-07-25 07:58:52 -05:00
|
|
|
if irclib.is_channel(channel):
|
|
|
|
channelset = set(config.get('channels', 'autojoin').split(','))
|
|
|
|
channelset.add(channel)
|
|
|
|
config.set('channels', 'autojoin', ','.join(channelset))
|
|
|
|
connection.privmsg(replypath, 'added ' + channel + ' to autojoin')
|
2010-07-24 13:36:13 -05:00
|
|
|
except NoOptionError: pass
|
|
|
|
elif what.split(' ')[1] == 'remove':
|
|
|
|
try:
|
|
|
|
# get existing list
|
2010-07-24 13:44:45 -05:00
|
|
|
channel = what.split(' ')[2]
|
2010-07-25 07:58:52 -05:00
|
|
|
if irclib.is_channel(channel):
|
|
|
|
channelset = set(config.get('channels', 'autojoin').split(','))
|
|
|
|
channelset.discard(channel)
|
|
|
|
config.set('channels', 'autojoin', ','.join(channelset))
|
|
|
|
connection.privmsg(replypath, 'removed ' + channel + ' from autojoin')
|
2010-07-24 13:36:13 -05:00
|
|
|
except NoOptionError: pass
|
2010-07-24 10:47:33 -05:00
|
|
|
|
2010-07-25 07:34:54 -05:00
|
|
|
#####
|
|
|
|
# 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):
|
2010-07-25 07:54:11 -05:00
|
|
|
nick = irclib.nm_to_n(event.source())
|
|
|
|
userhost = irclib.nm_to_uh(event.source())
|
|
|
|
replypath = nick
|
2010-07-25 07:34:54 -05:00
|
|
|
what = event.arguments()[0]
|
2010-07-25 07:54:11 -05:00
|
|
|
|
2010-07-25 07:34:54 -05:00
|
|
|
admin_unlocked = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
if userhost == config.get('admin', 'userhost'):
|
|
|
|
admin_unlocked = True
|
|
|
|
except NoOptionError: pass
|
|
|
|
|
|
|
|
# admin commands
|
2010-07-25 07:54:11 -05:00
|
|
|
sub_join_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
sub_part_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
sub_quit_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 07:34:54 -05:00
|
|
|
|
2010-07-25 08:20:58 -05:00
|
|
|
#####
|
|
|
|
# on_pubmsg
|
|
|
|
# public messages in a channel where the bot is
|
|
|
|
#####
|
|
|
|
|
|
|
|
def on_pubmsg(connection, event):
|
|
|
|
nick = irclib.nm_to_n(event.source())
|
|
|
|
userhost = irclib.nm_to_uh(event.source())
|
|
|
|
replypath = event.target()
|
|
|
|
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, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
sub_part_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
|
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
|
2010-07-24 10:12:00 -05:00
|
|
|
try:
|
|
|
|
# load connection info
|
2010-07-25 08:24:04 -05:00
|
|
|
botserver = config.get('IRC', 'server')
|
|
|
|
botport = config.getint('IRC', 'port')
|
|
|
|
botnick = config.get('IRC', 'nick')
|
|
|
|
botircname = config.get('IRC', 'name')
|
2010-07-24 10:12:00 -05:00
|
|
|
except NoSectionError as e:
|
2010-07-24 10:14:47 -05:00
|
|
|
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()
|
2010-07-25 08:24:04 -05:00
|
|
|
server = irc.server().connect(botserver, botport, botnick, botircname)
|
2010-07-24 10:47:33 -05:00
|
|
|
|
|
|
|
# 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-25 08:20:58 -05:00
|
|
|
server.add_global_handler('pubmsg', on_pubmsg)
|
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
|