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-25 09:34:28 -05:00
|
|
|
from datetime import datetime
|
2010-07-24 09:54:38 -05:00
|
|
|
import os
|
2010-07-25 17:39:22 -05:00
|
|
|
import random
|
2010-07-25 08:52:48 -05:00
|
|
|
import re
|
2010-07-24 10:12:00 -05:00
|
|
|
import sys
|
2010-07-24 09:54:38 -05:00
|
|
|
|
2010-07-25 11:53:07 -05:00
|
|
|
from dateutil.parser import *
|
2010-07-25 11:32:09 -05:00
|
|
|
from dateutil.relativedelta import *
|
|
|
|
from dateutil.tz import *
|
2010-07-24 10:47:33 -05:00
|
|
|
import irclib
|
|
|
|
|
2010-07-25 20:14:53 -05:00
|
|
|
class Module(object):
|
2010-07-25 20:09:02 -05:00
|
|
|
"""Base class used for creating classes that have real functionality.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, config, server):
|
|
|
|
"""Constructor for a feature module. Inheritors should not do anything special
|
|
|
|
here, instead they should implement register_handlers and do, or else this will
|
|
|
|
be a very uneventful affair.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.config = config
|
|
|
|
self.register_handlers(server)
|
|
|
|
|
|
|
|
def register_handlers(self, server):
|
|
|
|
"""This is called by __init__ and sets up server.add_global_handlers. Classes
|
|
|
|
inheriting from Module should implement this and set up the appropriate handlers,
|
|
|
|
e.g.:
|
|
|
|
|
|
|
|
server.add_global_handler('privmsg', self.on_privmsg)
|
|
|
|
|
|
|
|
Module.on_pubmsg and Module.on_privmsg are defined so far, the rest, you're on your
|
|
|
|
own.
|
|
|
|
"""
|
|
|
|
|
|
|
|
print "looks like someone forgot to implement register_handlers!"
|
|
|
|
|
|
|
|
def on_pubmsg(self, connection, event):
|
|
|
|
"""Does some variable setup and initial sanity checking before calling Module.do,
|
|
|
|
which should be implemented by subclasses and what can be ultimately responsible
|
|
|
|
for the work. Of course, you are free to reimplement on_pubmsg on your own too.
|
|
|
|
"""
|
|
|
|
|
|
|
|
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 == self.config.get('admin', 'userhost'):
|
|
|
|
admin_unlocked = True
|
|
|
|
except NoOptionError: pass
|
|
|
|
|
|
|
|
# only do commands if the bot has been addressed directly
|
|
|
|
addressed_pattern = '^' + connection.get_nickname() + '[:,]?\s+'
|
|
|
|
addressed_re = re.compile(addressed_pattern)
|
|
|
|
|
|
|
|
if not addressed_re.match(what):
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
what = addressed_re.sub('', what)
|
|
|
|
|
|
|
|
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
|
|
|
|
def on_privmsg(self, connection, event):
|
|
|
|
"""Does some variable setup and initial sanity checking before calling Module.do,
|
|
|
|
which should be implemented by subclasses and what can be ultimately responsible
|
|
|
|
for the work. Of course, you are free to reimplement on_privmsg on your own too.
|
|
|
|
"""
|
|
|
|
|
|
|
|
nick = irclib.nm_to_n(event.source())
|
|
|
|
userhost = irclib.nm_to_uh(event.source())
|
|
|
|
replypath = nick
|
|
|
|
what = event.arguments()[0]
|
|
|
|
|
|
|
|
admin_unlocked = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
if userhost == self.config.get('admin', 'userhost'):
|
|
|
|
admin_unlocked = True
|
|
|
|
except NoOptionError: pass
|
|
|
|
|
|
|
|
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
|
|
|
|
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
|
|
|
"""Implement this method in your subclass to have a fairly-automatic hook into
|
|
|
|
IRC functionality. This is called by the default on_pubmsg and on_privmsg
|
|
|
|
"""
|
|
|
|
|
|
|
|
print "looks like someone forgot to implement do!"
|
|
|
|
|
2010-07-25 20:14:53 -05:00
|
|
|
class GoogleTranslate(Module):
|
|
|
|
"""Class that translates text via Google Translate.
|
|
|
|
|
|
|
|
http://code.google.com/apis/ajaxlanguage/documentation/
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, config, server):
|
|
|
|
super(GoogleTranslate, self).__init__(config, server)
|
|
|
|
|
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
|
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 10:43:27 -05:00
|
|
|
whats = what.split(' ')
|
|
|
|
if whats[0] == 'join' and admin_unlocked and len(whats) >= 2:
|
|
|
|
channel = whats[1]
|
2010-07-25 07:58:52 -05:00
|
|
|
if irclib.is_channel(channel):
|
|
|
|
connection.join(channel)
|
2010-07-25 10:44:10 -05:00
|
|
|
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
|
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 10:43:27 -05:00
|
|
|
whats = what.split(' ')
|
|
|
|
if whats[0] == 'part' and admin_unlocked and len(whats) >= 2:
|
|
|
|
channel = whats[1]
|
2010-07-25 07:58:52 -05:00
|
|
|
if irclib.is_channel(channel):
|
2010-07-25 10:43:27 -05:00
|
|
|
connection.part(channel, ' '.join(whats[2:]))
|
2010-07-25 10:44:10 -05:00
|
|
|
connection.privmsg(replypath, 'parted ' + channel)
|
2010-07-25 07:34:54 -05:00
|
|
|
|
|
|
|
#####
|
|
|
|
# sub_quit_channel
|
|
|
|
# quit irc server. optionally provide a message
|
|
|
|
#####
|
|
|
|
|
2010-07-25 07:54:11 -05:00
|
|
|
def sub_quit_channel(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 10:43:27 -05:00
|
|
|
whats = what.split(' ')
|
|
|
|
if whats[0] == 'quit' and admin_unlocked:
|
2010-07-25 07:54:11 -05:00
|
|
|
connection.privmsg(replypath, 'quitting')
|
2010-07-25 10:43:27 -05:00
|
|
|
connection.quit(' '.join(whats[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.
|
|
|
|
#####
|
|
|
|
|
2010-07-25 07:54:11 -05:00
|
|
|
def sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 10:43:27 -05:00
|
|
|
whats = what.split(' ')
|
|
|
|
if whats[0] == 'autojoin' and admin_unlocked and len(whats) >= 3:
|
|
|
|
if whats[1] == 'add':
|
2010-07-24 13:36:13 -05:00
|
|
|
try:
|
|
|
|
# get existing list
|
2010-07-25 10:43:27 -05:00
|
|
|
channel = whats[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
|
2010-07-25 10:43:27 -05:00
|
|
|
elif whats[1] == 'remove':
|
2010-07-24 13:36:13 -05:00
|
|
|
try:
|
|
|
|
# get existing list
|
2010-07-25 10:43:27 -05:00
|
|
|
channel = whats[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 09:34:28 -05:00
|
|
|
#####
|
|
|
|
# sub_add_to_seen
|
|
|
|
# when someone says a pubmsg, keep it in the config
|
|
|
|
#####
|
|
|
|
|
|
|
|
def sub_add_to_seen(connection, event, nick, userhost, what):
|
|
|
|
if not config.has_section('seen'):
|
|
|
|
config.add_section('seen')
|
|
|
|
|
2010-07-25 10:08:30 -05:00
|
|
|
config.set('seen', nick, userhost + '|:|' + datetime.utcnow().isoformat() + '|:|' + what)
|
2010-07-25 09:34:28 -05:00
|
|
|
|
|
|
|
#####
|
|
|
|
# sub_report_seen
|
|
|
|
# report when a person has been seen, based on config
|
|
|
|
#####
|
|
|
|
|
|
|
|
def sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 10:43:27 -05:00
|
|
|
whats = what.split(' ')
|
|
|
|
if whats[0] == 'seen' and len(whats) >= 2:
|
|
|
|
query = whats[1]
|
2010-07-25 12:59:54 -05:00
|
|
|
if query != 'debug':
|
|
|
|
try:
|
|
|
|
seendata = config.get('seen', query).split('|:|')
|
|
|
|
converted = datetime.strptime(seendata[1], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=tzutc())
|
|
|
|
connection.privmsg(replypath, 'last saw ' + query + ' at ' + converted.astimezone(tzlocal()).strftime("%Y/%m/%d %H:%M:%S %Z") + ' saying \'' + seendata[2] + '\'')
|
|
|
|
except NoOptionError: pass
|
2010-07-25 09:34:28 -05:00
|
|
|
|
2010-07-25 10:09:48 -05:00
|
|
|
#####
|
|
|
|
# sub_save_config
|
|
|
|
# save the config file
|
|
|
|
#####
|
|
|
|
|
|
|
|
def sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 10:43:27 -05:00
|
|
|
whats = what.split(' ')
|
|
|
|
if whats[0] == 'save' and admin_unlocked:
|
2010-07-25 10:09:48 -05:00
|
|
|
with open('dr.botzo.cfg', 'w') as cfg:
|
|
|
|
config.write(cfg)
|
|
|
|
connection.privmsg(replypath, 'saved config file')
|
|
|
|
|
2010-07-25 10:24:15 -05:00
|
|
|
#####
|
|
|
|
# sub_change_nick
|
|
|
|
# change the bot's nickname
|
|
|
|
#####
|
|
|
|
|
|
|
|
def sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 10:43:27 -05:00
|
|
|
whats = what.split(' ')
|
|
|
|
if whats[0] == 'nick' and admin_unlocked and len(whats) >= 2:
|
|
|
|
newnick = whats[1]
|
2010-07-25 10:24:15 -05:00
|
|
|
connection.nick(newnick)
|
2010-07-25 10:28:50 -05:00
|
|
|
config.set('IRC', 'nick', newnick)
|
2010-07-25 10:24:15 -05:00
|
|
|
connection.privmsg(replypath, 'changed nickname')
|
|
|
|
|
2010-07-25 11:53:07 -05:00
|
|
|
#####
|
2010-07-25 12:40:57 -05:00
|
|
|
# sub_countdown
|
2010-07-25 11:53:07 -05:00
|
|
|
# add a countdown item to the bot
|
|
|
|
#####
|
|
|
|
|
2010-07-25 12:40:57 -05:00
|
|
|
def sub_countdown(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 11:53:07 -05:00
|
|
|
whats = what.split(' ')
|
2010-07-25 12:40:57 -05:00
|
|
|
if whats[0] == 'countdown' and len(whats) >= 2:
|
|
|
|
if whats[1] == 'add' and len(whats) >= 4:
|
2010-07-25 11:53:07 -05:00
|
|
|
item = whats[2]
|
2010-07-25 12:03:35 -05:00
|
|
|
target = parse(' '.join(whats[3:]), default=datetime.now().replace(tzinfo=tzlocal()))
|
2010-07-25 11:53:07 -05:00
|
|
|
if not config.has_section('countdown'):
|
|
|
|
config.add_section('countdown')
|
|
|
|
|
|
|
|
config.set('countdown', item, target.astimezone(tzutc()).isoformat())
|
2010-07-25 15:28:29 -05:00
|
|
|
connection.privmsg(replypath, 'added countdown item ' + whats[2])
|
2010-07-25 15:27:46 -05:00
|
|
|
elif whats[1] == 'remove':
|
|
|
|
try:
|
|
|
|
if config.remove_option('countdown', whats[2]):
|
|
|
|
connection.privmsg(replypath, 'removed countdown item ' + whats[2])
|
|
|
|
except NoSectionError: pass
|
2010-07-25 12:54:22 -05:00
|
|
|
elif whats[1] == 'list':
|
|
|
|
try:
|
2010-07-25 12:57:52 -05:00
|
|
|
cdlist = config.options('countdown')
|
|
|
|
cdlist.remove('debug')
|
|
|
|
cdlist.sort()
|
|
|
|
liststr = ', '.join(cdlist)
|
2010-07-25 12:54:22 -05:00
|
|
|
connection.privmsg(replypath, liststr)
|
|
|
|
except NoSectionError: pass
|
2010-07-25 12:40:57 -05:00
|
|
|
else:
|
|
|
|
try:
|
2010-07-25 12:47:34 -05:00
|
|
|
timestr = config.get('countdown', whats[1])
|
|
|
|
time = parse(timestr)
|
2010-07-25 12:40:57 -05:00
|
|
|
rdelta = relativedelta(time, datetime.now().replace(tzinfo=tzlocal()))
|
|
|
|
relstr = whats[1] + ' will occur in '
|
|
|
|
if rdelta.years != 0:
|
|
|
|
relstr += str(rdelta.years) + ' years '
|
|
|
|
if rdelta.months != 0:
|
|
|
|
relstr += str(rdelta.months) + ' months '
|
|
|
|
if rdelta.days != 0:
|
|
|
|
relstr += str(rdelta.days) + ' days '
|
|
|
|
if rdelta.hours != 0:
|
|
|
|
relstr += str(rdelta.hours) + ' hours '
|
|
|
|
if rdelta.minutes != 0:
|
|
|
|
relstr += str(rdelta.minutes) + ' minutes '
|
|
|
|
if rdelta.seconds != 0:
|
|
|
|
relstr += str(rdelta.seconds) + ' seconds'
|
2010-07-25 12:50:25 -05:00
|
|
|
#relstr += ' (' + timestr + ')'
|
2010-07-25 12:40:57 -05:00
|
|
|
connection.privmsg(replypath, relstr)
|
|
|
|
except NoOptionError: pass
|
2010-07-25 11:53:07 -05:00
|
|
|
|
2010-07-25 17:39:22 -05:00
|
|
|
#####
|
|
|
|
# sub_dice
|
|
|
|
# roll dice, primarily for roleplaying games
|
|
|
|
#####
|
|
|
|
|
|
|
|
def sub_dice(connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-25 19:21:20 -05:00
|
|
|
overallroll = what
|
2010-07-25 17:39:22 -05:00
|
|
|
|
2010-07-25 19:21:20 -05:00
|
|
|
rolls = re.split(';\s*', overallroll)
|
|
|
|
for roll in rolls:
|
|
|
|
pattern = '^(?:(\d+)#)?(?:(\d+)/)?(\d+)?d(\d+)(?:(\+|\-)(\d+))?(?:\s+(.*))?'
|
|
|
|
regex = re.compile(pattern)
|
|
|
|
matches = regex.search(roll)
|
2010-07-25 17:39:22 -05:00
|
|
|
|
2010-07-25 19:21:20 -05:00
|
|
|
if matches is not None:
|
|
|
|
# set variables including defaults for unspecified stuff
|
|
|
|
faces = int(matches.group(4))
|
|
|
|
comment = matches.group(7)
|
2010-07-25 17:39:22 -05:00
|
|
|
|
2010-07-25 19:21:20 -05:00
|
|
|
if matches.group(1) is None:
|
|
|
|
times = 1
|
2010-07-25 17:39:22 -05:00
|
|
|
else:
|
2010-07-25 19:21:20 -05:00
|
|
|
times = int(matches.group(1))
|
2010-07-25 17:39:22 -05:00
|
|
|
|
2010-07-25 19:21:20 -05:00
|
|
|
if matches.group(3) is None:
|
|
|
|
dice = 1
|
|
|
|
else:
|
|
|
|
dice = int(matches.group(3))
|
|
|
|
|
|
|
|
if matches.group(2) is None:
|
|
|
|
top = dice
|
|
|
|
else:
|
|
|
|
top = int(matches.group(2))
|
2010-07-25 17:39:22 -05:00
|
|
|
|
2010-07-25 19:21:20 -05:00
|
|
|
if matches.group(5) is None or matches.group(6) is None:
|
|
|
|
modifier = 0
|
|
|
|
else:
|
|
|
|
if str(matches.group(5)) == '-':
|
|
|
|
modifier = -1 * int(matches.group(6))
|
|
|
|
else:
|
|
|
|
modifier = int(matches.group(6))
|
|
|
|
|
|
|
|
result = ''
|
|
|
|
if comment is not None:
|
|
|
|
result += comment + ': '
|
|
|
|
|
|
|
|
for t in range(times):
|
|
|
|
ressubstr = ""
|
|
|
|
rolls = []
|
|
|
|
for d in range(dice):
|
|
|
|
rolls.append(str(random.randint(1, faces)))
|
|
|
|
rolls.sort()
|
|
|
|
rolls.reverse()
|
|
|
|
ressubstr = ','.join(rolls[0:top])
|
|
|
|
sum = 0
|
|
|
|
for r in rolls[0:top]:
|
|
|
|
sum += int(r)
|
|
|
|
sumplus = sum + modifier
|
|
|
|
result += str(sumplus) + ' [' + ressubstr
|
|
|
|
if modifier != 0:
|
|
|
|
if modifier > 0:
|
|
|
|
result += ' + ' + str(modifier)
|
|
|
|
else:
|
|
|
|
result += ' - ' + str(-1 * modifier)
|
|
|
|
result += ']'
|
|
|
|
|
|
|
|
if t != times-1:
|
|
|
|
result += ', '
|
|
|
|
|
|
|
|
connection.privmsg(replypath, result)
|
2010-07-25 17:39:22 -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')
|
2010-07-25 08:52:48 -05:00
|
|
|
connection.mode(botnick, usermode)
|
2010-07-25 07:34:54 -05:00
|
|
|
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 10:09:48 -05:00
|
|
|
sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 10:24:15 -05:00
|
|
|
sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 07:34:54 -05:00
|
|
|
|
2010-07-25 09:34:28 -05:00
|
|
|
# standard commands
|
|
|
|
sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 12:40:57 -05:00
|
|
|
sub_countdown(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 17:39:22 -05:00
|
|
|
sub_dice(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 09:34:28 -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
|
|
|
|
|
2010-07-25 09:34:28 -05:00
|
|
|
sub_add_to_seen(connection, event, nick, userhost, what)
|
|
|
|
|
2010-07-25 08:52:48 -05:00
|
|
|
# only do commands if the bot has been addressed directly
|
2010-07-25 10:24:15 -05:00
|
|
|
addressed_pattern = '^' + connection.get_nickname() + '[:,]?\s+'
|
2010-07-25 08:52:48 -05:00
|
|
|
addressed_re = re.compile(addressed_pattern)
|
|
|
|
|
|
|
|
if not addressed_re.match(what):
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
what = addressed_re.sub('', what)
|
|
|
|
|
2010-07-25 08:20:58 -05:00
|
|
|
# admin commands
|
|
|
|
sub_join_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
sub_part_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 08:53:41 -05:00
|
|
|
sub_quit_channel(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 08:20:58 -05:00
|
|
|
sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 10:09:48 -05:00
|
|
|
sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 10:24:15 -05:00
|
|
|
sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 08:20:58 -05:00
|
|
|
|
2010-07-25 09:34:28 -05:00
|
|
|
# standard commands
|
|
|
|
sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 12:40:57 -05:00
|
|
|
sub_countdown(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 17:39:22 -05:00
|
|
|
sub_dice(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
2010-07-25 09:34:28 -05:00
|
|
|
|
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
|
|
|
|
2010-07-25 20:09:02 -05:00
|
|
|
# test
|
2010-07-25 20:14:53 -05:00
|
|
|
gt = GoogleTranslate(config, server)
|
2010-07-25 20:09:02 -05:00
|
|
|
|
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
|