summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2010-05-02 20:28:17 +0200
committerMatthias Schiffer <matthias@gamezock.de>2010-05-02 20:31:34 +0200
commit0277a779c9dff841e04a08a5c39fe07faa92cfe8 (patch)
tree6a935f30a31a925c2c404dbbc119040b3b9e0acd
parent41fec54c305d4a81245e9e294c7fc29fed9b7b00 (diff)
downloadcurunir-0277a779c9dff841e04a08a5c39fe07faa92cfe8.tar
curunir-0277a779c9dff841e04a08a5c39fe07faa92cfe8.zip
Added modules
-rw-r--r--.gitignore1
-rw-r--r--config.py5
-rw-r--r--config.py.default10
-rw-r--r--connection/xmpp.py32
-rw-r--r--curunir.py20
-rw-r--r--modules/__init__.py0
-rw-r--r--modules/base.py12
-rw-r--r--modules/help.py18
8 files changed, 77 insertions, 21 deletions
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
--- /dev/null
+++ b/modules/__init__.py
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)