summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2010-05-02 21:18:09 +0200
committerMatthias Schiffer <matthias@gamezock.de>2010-05-02 21:19:21 +0200
commitd537d4b69b4547814f2d01bc18ce2c472b978647 (patch)
tree444f07b95e0e2333f9d5d264a8917dfbd7230f6a
parent0277a779c9dff841e04a08a5c39fe07faa92cfe8 (diff)
downloadcurunir-d537d4b69b4547814f2d01bc18ce2c472b978647.tar
curunir-d537d4b69b4547814f2d01bc18ce2c472b978647.zip
Added log module
-rw-r--r--config.py.default7
-rw-r--r--connection/xmpp.py2
-rw-r--r--curunir.py14
-rw-r--r--modules/help.py2
-rw-r--r--modules/log.py10
-rw-r--r--modules/mysql.py10
6 files changed, 38 insertions, 7 deletions
diff --git a/config.py.default b/config.py.default
index 92c067d..578f10d 100644
--- a/config.py.default
+++ b/config.py.default
@@ -6,5 +6,12 @@ config = {
'rooms': [],
'nick': '',
+ 'mysql': {
+ 'host': '',
+ 'user': '',
+ 'passwd': '',
+ 'db': ''
+ },
+
'modules': []
}
diff --git a/connection/xmpp.py b/connection/xmpp.py
index 423cd2a..4104af7 100644
--- a/connection/xmpp.py
+++ b/connection/xmpp.py
@@ -29,7 +29,7 @@ class MucHandler(MucRoomHandler):
if(user.same_as(self.room_state.me)):
return
- for mod in self.client.module_manager.modules:
+ for mod in self.client.module_manager.modules.itervalues():
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):
diff --git a/curunir.py b/curunir.py
index 7f6a699..99fd77f 100644
--- a/curunir.py
+++ b/curunir.py
@@ -6,14 +6,18 @@ from config import config
class ModuleManager:
def __init__(self, config):
self.config = config
- self.modules = []
+ self.modules = {}
for mod in config['modules']:
- self.load(mod)
+ self.get(mod)
- def load(self, name):
- mod = __import__('modules.' + name, globals(), locals(), ['Module'])
- self.modules.append(mod.Module(self))
+ def get(self, name):
+ if not name in self.modules:
+ mod = __import__('modules.' + name, globals(), locals(), ['Module'])
+ self.modules[name] = mod.Module(self)
+
+ return self.modules[name]
+
run = True
diff --git a/modules/help.py b/modules/help.py
index d239f84..71d1a56 100644
--- a/modules/help.py
+++ b/modules/help.py
@@ -12,7 +12,7 @@ class Module(ModuleBase):
if not re.match(r'!help(?:\W|\Z)', message_body):
return
- commands = reduce(lambda l, mod: l + mod.commands(), self.manager.modules, [])
+ commands = reduce(lambda l, mod: l + mod.commands(), self.manager.modules.itervalues(), [])
helpstring = reduce(lambda s, (c, h): s + c + ': ' + h + '\n', commands, '')
reply('Befehle:\n\n' + helpstring)
diff --git a/modules/log.py b/modules/log.py
new file mode 100644
index 0000000..3280d27
--- /dev/null
+++ b/modules/log.py
@@ -0,0 +1,10 @@
+from base import ModuleBase
+
+class Module(ModuleBase):
+ def __init__(self, manager):
+ ModuleBase.__init__(self, manager)
+ self.db = manager.get('mysql').db
+
+ def groupchat(self, room, message_from, message_body, reply):
+ cursor = self.db.cursor()
+ cursor.execute('INSERT INTO log (time, room, nick, text) VALUES (NOW(), %s, %s, %s)', (room, message_from, message_body))
diff --git a/modules/mysql.py b/modules/mysql.py
new file mode 100644
index 0000000..3b6c3b1
--- /dev/null
+++ b/modules/mysql.py
@@ -0,0 +1,10 @@
+from base import ModuleBase
+import MySQLdb
+
+class Module(ModuleBase):
+ def __init__(self, manager):
+ ModuleBase.__init__(self, manager)
+
+ conf = manager.config['mysql']
+
+ self.db = MySQLdb.connect(host = conf['host'], user = conf['user'], passwd = conf['passwd'], db = conf['db'])