h/bot.py

130 lines
3.7 KiB
Python
Raw Normal View History

2008-06-09 11:59:56 +00:00
#!/usr/bin/python
2009-03-15 03:06:36 +00:00
network = "localhost"
2008-06-09 11:59:56 +00:00
nickname = "skybot"
2009-03-15 03:06:36 +00:00
channel = "#skybot"
2008-06-09 11:59:56 +00:00
2009-03-15 03:06:36 +00:00
import sys
import os
import glob
import imp
import re
import thread
import Queue
import copy
2008-06-09 11:59:56 +00:00
2009-03-15 03:06:36 +00:00
import irc
import yaml
2008-06-09 11:59:56 +00:00
2009-03-15 03:06:36 +00:00
os.chdir(sys.path[0]) #do stuff relative to the installation directory
2008-06-09 11:59:56 +00:00
class Bot(object):
def __init__(self):
self.plugins = {}
2009-03-15 03:06:36 +00:00
self.commands = [] # fn, name, func, args
self.listens = {}
self.filters = [] #fn, name, func
2008-06-09 11:59:56 +00:00
self.daemons = {}
bot = Bot()
2009-03-15 03:06:36 +00:00
bot.nickname = nickname
bot.channel = channel
bot.network = network
2008-06-09 11:59:56 +00:00
print 'Loading plugins'
2009-03-15 03:06:36 +00:00
magic_re = re.compile(r'^\s*#(command|filter)(?:: +(\S+) *(\S.*)?)?\s*$')
2008-06-09 11:59:56 +00:00
for filename in glob.glob("plugins/*.py"):
shortname = os.path.splitext(os.path.basename(filename))[0]
try:
2009-03-15 03:06:36 +00:00
bot.plugins[shortname] = imp.load_source(shortname, filename)
plugin = bot.plugins[shortname]
source = open(filename).read().split('\n')
#this is a nasty hack, but it simplifies registration
funcs = [x for x in dir(plugin)
if str(type(getattr(plugin,x))) == "<type 'function'>"]
for func in funcs:
#read the line before the function definition, looking for a
# comment that tells the bot about what it does
func = getattr(plugin, func)
lineno = func.func_code.co_firstlineno
if lineno == 1:
continue #can't have a line before the first line...
m = magic_re.match(source[lineno-2])
if m:
typ, nam, rest = m.groups()
if nam is None:
nam = func.__name__
if rest is None:
rest = '\s*(.*)'
if typ == 'command':
args = {'name': nam, 'hook': nam + rest}
bot.commands.append((filename, nam, func, args))
if typ == 'filter':
bot.filters.append((filename, nam, func))
2008-06-09 11:59:56 +00:00
except Exception, e:
print e
2009-03-15 03:06:36 +00:00
if bot.daemons:
print 'Running daemons'
for daemon in bot.daemons.itervalues():
thread.start_new_thread(daemon, ())
2008-06-09 11:59:56 +00:00
print 'Connecting to IRC'
bot.irc = irc.irc(network, nickname)
bot.irc.join(channel)
bot.commandprefix = '^(?:\.|'+nickname+'[:,]*\s*)'
print 'Running main loop'
class Input(object):
def __init__(self, raw, prefix, command,
params, nick, user, host, paraml, msg):
self.raw = raw
self.prefix = prefix
self.command = command
self.params = params
self.nick = nick
self.user = user
self.host = host
self.paraml = paraml
self.msg = msg
class FakeBot(object):
2009-03-15 03:06:36 +00:00
def __init__(self, bot, input, func):
2008-06-09 11:59:56 +00:00
self.bot = bot
self.input = input
2009-03-15 03:06:36 +00:00
self.msg = bot.irc.msg
self.cmd = bot.irc.cmd
self.func = func
if input.command == "PRIVMSG":
self.chan = input.paraml[0]
2008-06-09 11:59:56 +00:00
def say(self, msg):
self.bot.irc.msg(input.paraml[0], msg)
def reply(self, msg):
self.say(input.nick + ': ' + msg)
2009-03-15 03:06:36 +00:00
def run(self):
out = self.func(self, self.input)
if out is not None:
self.reply(unicode(out))
2008-06-09 11:59:56 +00:00
print bot.commands
2009-03-15 03:06:36 +00:00
print bot.filters
2008-06-09 11:59:56 +00:00
while True:
try:
out = bot.irc.out.get(timeout=1)
2009-03-15 03:06:36 +00:00
for fn, name, func, args in bot.commands:
2008-06-09 11:59:56 +00:00
input = Input(*out)
2009-03-15 03:06:36 +00:00
for fn, nam, filt in bot.filters:
2008-06-09 11:59:56 +00:00
input = filt(bot, func, args, input)
2009-03-15 03:06:36 +00:00
if input == None:
2008-06-09 11:59:56 +00:00
break
2009-03-15 03:06:36 +00:00
if input == None:
break
thread.start_new_thread(FakeBot(bot, input, func).run, ())
2008-06-09 11:59:56 +00:00
except Queue.Empty:
pass