2010-05-02 06:20:00 +02:00
|
|
|
from connection.xmpp import XMPPConnection
|
|
|
|
|
2010-05-09 21:49:15 +02:00
|
|
|
import signal, sys, traceback
|
2010-05-02 15:03:32 +02:00
|
|
|
from config import config
|
2010-05-02 06:20:00 +02:00
|
|
|
|
2010-05-02 20:28:17 +02:00
|
|
|
class ModuleManager:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
2010-05-09 21:49:15 +02:00
|
|
|
self._modules = {}
|
2010-05-02 20:28:17 +02:00
|
|
|
|
|
|
|
for mod in config['modules']:
|
2010-05-02 21:18:09 +02:00
|
|
|
self.get(mod)
|
2010-05-02 20:28:17 +02:00
|
|
|
|
2010-05-02 21:18:09 +02:00
|
|
|
def get(self, name):
|
2010-05-09 21:49:15 +02:00
|
|
|
if not name in self._modules:
|
2010-05-02 21:18:09 +02:00
|
|
|
mod = __import__('modules.' + name, globals(), locals(), ['Module'])
|
2010-05-09 21:49:15 +02:00
|
|
|
self._modules[name] = mod.Module(self)
|
2010-05-02 21:18:09 +02:00
|
|
|
|
2010-05-09 21:49:15 +02:00
|
|
|
return self._modules[name]
|
|
|
|
|
|
|
|
def commands(self):
|
|
|
|
return reduce(lambda l, mod: l + mod.commands(), self._modules.itervalues(), [])
|
|
|
|
|
|
|
|
def helptexts(self):
|
|
|
|
return reduce(lambda l, mod: l + mod.helptexts(), self._modules.itervalues(), [])
|
|
|
|
|
|
|
|
def groupchat(self, room, nick, text, handler):
|
|
|
|
for mod in self._modules.itervalues():
|
|
|
|
try:
|
|
|
|
mod.groupchat(room, nick, text, handler)
|
|
|
|
except:
|
|
|
|
handler.reply(traceback.format_exc(5))
|
|
|
|
|
|
|
|
def join(self, room, nick, show, status, handler):
|
|
|
|
for mod in self._modules.itervalues():
|
|
|
|
try:
|
|
|
|
mod.join(room, nick, show, status, handler)
|
|
|
|
except:
|
|
|
|
handler.reply(traceback.format_exc(5))
|
|
|
|
|
|
|
|
def leave(self, room, nick, show, status, handler):
|
|
|
|
for mod in self._modules.itervalues():
|
|
|
|
try:
|
|
|
|
mod.leave(room, nick, show, status, handler)
|
|
|
|
except:
|
|
|
|
handler.reply(traceback.format_exc(5))
|
|
|
|
|
|
|
|
def topic(self, room, nick, text, handler):
|
|
|
|
for mod in self._modules.itervalues():
|
|
|
|
try:
|
|
|
|
mod.leave(room, nick, text, handler)
|
|
|
|
except:
|
|
|
|
handler.reply(traceback.format_exc(5))
|
|
|
|
|
2010-05-02 21:18:09 +02:00
|
|
|
|
2010-05-02 20:28:17 +02:00
|
|
|
|
2010-05-02 06:20:00 +02:00
|
|
|
run = True
|
|
|
|
|
|
|
|
def exithandler(signum, frame):
|
|
|
|
global run
|
|
|
|
run = False
|
|
|
|
|
2010-05-03 03:37:21 +02:00
|
|
|
print 'Starting Curunir 0.1'
|
|
|
|
|
2010-05-02 06:20:00 +02:00
|
|
|
signal.signal(signal.SIGINT, exithandler)
|
|
|
|
signal.signal(signal.SIGTERM, exithandler)
|
|
|
|
signal.signal(signal.SIGQUIT, exithandler)
|
|
|
|
|
2010-05-03 03:37:21 +02:00
|
|
|
print 'Loading modules...'
|
|
|
|
|
2010-05-02 20:28:17 +02:00
|
|
|
modman = ModuleManager(config)
|
|
|
|
|
2010-05-02 06:20:00 +02:00
|
|
|
while run:
|
2010-05-09 21:49:15 +02:00
|
|
|
connection = XMPPConnection(config, modman)
|
|
|
|
print 'Connecting...'
|
|
|
|
connection.connect()
|
|
|
|
|
|
|
|
try:
|
|
|
|
while run:
|
|
|
|
act = connection.stream.loop_iter(1)
|
|
|
|
if not act:
|
|
|
|
connection.idle()
|
|
|
|
|
|
|
|
connection.disconnect()
|
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
|
|
|
connection.disconnect()
|