From 0277a779c9dff841e04a08a5c39fe07faa92cfe8 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 2 May 2010 20:28:17 +0200 Subject: Added modules --- .gitignore | 1 + config.py | 5 ----- config.py.default | 10 ++++++++++ connection/xmpp.py | 32 ++++++++++++++++++-------------- curunir.py | 20 ++++++++++++++++++-- modules/__init__.py | 0 modules/base.py | 12 ++++++++++++ modules/help.py | 18 ++++++++++++++++++ 8 files changed, 77 insertions(+), 21 deletions(-) delete mode 100644 config.py create mode 100644 config.py.default create mode 100644 modules/__init__.py create mode 100644 modules/base.py create mode 100644 modules/help.py diff --git a/.gitignore b/.gitignore index b261d8d..f8cd567 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *~ *.pyo +config.py diff --git a/config.py b/config.py deleted file mode 100644 index 11be75e..0000000 --- a/config.py +++ /dev/null @@ -1,5 +0,0 @@ -config = { - 'jid': '', - 'password': '', - 'cacertfile': '' - } diff --git a/config.py.default b/config.py.default new file mode 100644 index 0000000..92c067d --- /dev/null +++ b/config.py.default @@ -0,0 +1,10 @@ +config = { + 'jid': '', + 'password': '', + 'cacertfile': '', + + 'rooms': [], + 'nick': '', + + 'modules': [] + } diff --git a/connection/xmpp.py b/connection/xmpp.py index 2c45291..423cd2a 100644 --- a/connection/xmpp.py +++ b/connection/xmpp.py @@ -4,6 +4,7 @@ from pyxmpp.client import Client from pyxmpp.jabber.muc import * from pyxmpp.interface import implements from pyxmpp.interfaces import * +from functools import partial class MessageHandler: implements(IMessageHandlersProvider) @@ -17,25 +18,23 @@ class MessageHandler: ] def message(self, stanza): - return Message( - to_jid=stanza.get_from(), - from_jid=stanza.get_to(), - stanza_type=stanza.get_type(), - subject=stanza.get_subject(), - body=stanza.get_body()) - + pass + class MucHandler(MucRoomHandler): + def __init__(self, client): + MucRoomHandler.__init__(self) + self.client = client + def message_received(self, user, stanza): if(user.same_as(self.room_state.me)): return - self.room_state.send_message(stanza.get_body()[::-1]) + for mod in self.client.module_manager.modules: + mod.groupchat(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_body(), self.room_state.send_message) def user_joined(self, user, stanza): if(not self.room_state.joined or user.same_as(self.room_state.me)): return - - self.room_state.send_message('Willkommen, ' + user.nick) class PresenceHandler(object): implements(IPresenceHandlersProvider) @@ -112,9 +111,12 @@ class VersionHandler(object): class XMPPConnection(Client): - def __init__(self, config): - Client.__init__(self, JID(config['jid']), config['password'], tls_settings = TLSSettings(require = True, cacert_file = config['cacertfile'])) - + def __init__(self, config, modman): + self.config = config + self.module_manager = modman + + Client.__init__(self, JID(config['jid']), config['password'], tls_settings = TLSSettings(require = True, cacert_file = config['cacertfile']), keepalive = 60) + self.interface_providers = [ MessageHandler(self), PresenceHandler(self), @@ -125,4 +127,6 @@ class XMPPConnection(Client): Client.authorized(self) self.room_manager = MucRoomManager(self.stream) self.room_manager.set_handlers() - self.room_manager.join(JID('c3hl@conference.jabber.ccc.de'), 'curunir', MucHandler(), history_maxstanzas=0) + + for room in self.config['rooms']: + self.room_manager.join(JID(room), self.config['nick'], MucHandler(self), history_maxstanzas=0) diff --git a/curunir.py b/curunir.py index 7d14410..7f6a699 100644 --- a/curunir.py +++ b/curunir.py @@ -3,6 +3,18 @@ from connection.xmpp import XMPPConnection import signal, sys from config import config +class ModuleManager: + def __init__(self, config): + self.config = config + self.modules = [] + + for mod in config['modules']: + self.load(mod) + + def load(self, name): + mod = __import__('modules.' + name, globals(), locals(), ['Module']) + self.modules.append(mod.Module(self)) + run = True def exithandler(signum, frame): @@ -13,10 +25,14 @@ signal.signal(signal.SIGINT, exithandler) signal.signal(signal.SIGTERM, exithandler) signal.signal(signal.SIGQUIT, exithandler) -connection = XMPPConnection(config) +modman = ModuleManager(config) + +connection = XMPPConnection(config, modman) connection.connect() while run: - connection.stream.loop_iter(1) + act = connection.stream.loop_iter(1) + if not act: + connection.idle() connection.disconnect() diff --git a/modules/__init__.py b/modules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/base.py b/modules/base.py new file mode 100644 index 0000000..9d54c40 --- /dev/null +++ b/modules/base.py @@ -0,0 +1,12 @@ +class ModuleBase: + def __init__(self, manager): + self.manager = manager + + def commands(self): + return [] + + def message(self, message_type, message_from, message_subject, message_body, reply): + pass + + def groupchat(self, room, message_from, message_body, reply): + pass diff --git a/modules/help.py b/modules/help.py new file mode 100644 index 0000000..d239f84 --- /dev/null +++ b/modules/help.py @@ -0,0 +1,18 @@ +from base import ModuleBase +import re + +class Module(ModuleBase): + def __init__(self, manager): + ModuleBase.__init__(self, manager) + + def commands(self): + return [('!help', 'Zeigt diese Hilfe an...')] + + def groupchat(self, room, message_from, message_body, reply): + if not re.match(r'!help(?:\W|\Z)', message_body): + return + + commands = reduce(lambda l, mod: l + mod.commands(), self.manager.modules, []) + helpstring = reduce(lambda s, (c, h): s + c + ': ' + h + '\n', commands, '') + + reply('Befehle:\n\n' + helpstring) -- cgit v1.2.3