diff options
-rw-r--r-- | connection/xmpp.py | 32 | ||||
-rw-r--r-- | modules/base.py | 14 | ||||
-rw-r--r-- | modules/ddate.py | 6 | ||||
-rw-r--r-- | modules/help.py | 7 | ||||
-rw-r--r-- | modules/log.py | 22 | ||||
-rw-r--r-- | modules/mysql.py | 2 | ||||
-rw-r--r-- | modules/topic.py | 25 |
7 files changed, 96 insertions, 12 deletions
diff --git a/connection/xmpp.py b/connection/xmpp.py index 4104af7..4facfe8 100644 --- a/connection/xmpp.py +++ b/connection/xmpp.py @@ -25,16 +25,44 @@ class MucHandler(MucRoomHandler): MucRoomHandler.__init__(self) self.client = client + def reply(self, text): + self.room_state.send_message(text) + + def get_topic(self): + return self.room_state.subject + + def set_topic(self, topic): + self.room_state.set_subject(topic) + def message_received(self, user, stanza): - if(user.same_as(self.room_state.me)): + if(user == None or user.same_as(self.room_state.me) or stanza.get_body() == None): return 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) + mod.groupchat(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_body(), self) def user_joined(self, user, stanza): if(not self.room_state.joined or user.same_as(self.room_state.me)): return + + for mod in self.client.module_manager.modules.itervalues(): + mod.join(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), stanza.get_status(), self) + + def user_left(self, user, stanza): + if(not self.room_state.joined or user.same_as(self.room_state.me)): + return + + for mod in self.client.module_manager.modules.itervalues(): + mod.leave(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), stanza.get_status(), self) + + def subject_changed(self, user, stanza): + topic = '' + if stanza.get_subject() != None: + topic = stanza.get_subject() + + for mod in self.client.module_manager.modules.itervalues(): + mod.topic(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, topic, self) + class PresenceHandler(object): implements(IPresenceHandlersProvider) diff --git a/modules/base.py b/modules/base.py index 9d54c40..b839e9b 100644 --- a/modules/base.py +++ b/modules/base.py @@ -5,8 +5,20 @@ class ModuleBase: def commands(self): return [] + def helptexts(self): + return [] + def message(self, message_type, message_from, message_subject, message_body, reply): pass - def groupchat(self, room, message_from, message_body, reply): + def groupchat(self, room, nick, text, handler): + pass + + def join(self, room, nick, show, status, handler): + pass + + def leave(self, room, nick, show, status, handler): + pass + + def topic(self, room, nick, text, handler): pass diff --git a/modules/ddate.py b/modules/ddate.py index eee8a29..5c6fd77 100644 --- a/modules/ddate.py +++ b/modules/ddate.py @@ -8,8 +8,8 @@ class Module(ModuleBase): def commands(self): return [('!ddate', 'Das heutige Datum im einzigen relevaten Kalender-System.')] - def groupchat(self, room, message_from, message_body, reply): - if not re.match(r'!ddate(?:\W|\Z)', message_body): + def groupchat(self, room, nick, text, handler): + if not re.match(r'!ddate(?:\W|\Z)', text): return - reply(subprocess.Popen(['ddate'], stdout=subprocess.PIPE).communicate()[0].strip()) + handler.reply(subprocess.Popen(['ddate'], stdout=subprocess.PIPE).communicate()[0].strip()) diff --git a/modules/help.py b/modules/help.py index 71d1a56..ed3e4d3 100644 --- a/modules/help.py +++ b/modules/help.py @@ -8,11 +8,12 @@ class Module(ModuleBase): 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): + def groupchat(self, room, nick, text, handler): + if not re.match(r'!help(?:\W|\Z)', text): return commands = reduce(lambda l, mod: l + mod.commands(), self.manager.modules.itervalues(), []) helpstring = reduce(lambda s, (c, h): s + c + ': ' + h + '\n', commands, '') + helptexts = '\n'.join(reduce(lambda l, mod: l + mod.helptexts(), self.manager.modules.itervalues(), [])) - reply('Befehle:\n\n' + helpstring) + handler.reply(('Befehle:\n' + helpstring + '\n' + helptexts).strip()) diff --git a/modules/log.py b/modules/log.py index f97ad90..04ea721 100644 --- a/modules/log.py +++ b/modules/log.py @@ -5,7 +5,25 @@ class Module(ModuleBase): ModuleBase.__init__(self, manager) self.db = manager.get('mysql').db - def groupchat(self, room, message_from, message_body, reply): + def helptexts(self): + return ['Chatlogs werden auch erstellt.'] + + def groupchat(self, room, nick, text, handler): + cursor = self.db.cursor() + cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `text`) VALUES ("message", NOW(), %s, %s, %s)', (room, nick, text)) + cursor.close() + + def join(self, room, nick, show, status, handler): + cursor = self.db.cursor() + cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `show`, `text`) VALUES ("join", NOW(), %s, %s, %s, %s)', (room, nick, show, status)) + cursor.close() + + def leave(self, room, nick, show, status, handler): + cursor = self.db.cursor() + cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `show`, `text`) VALUES ("leave", NOW(), %s, %s, %s, %s)', (room, nick, show, status)) + cursor.close() + + def topic(self, room, nick, text, handler): cursor = self.db.cursor() - cursor.execute('INSERT INTO log (time, room, nick, text) VALUES (NOW(), %s, %s, %s)', (room, message_from, message_body)) + cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `text`) VALUES ("topic", NOW(), %s, %s, %s)', (room, nick, text)) cursor.close() diff --git a/modules/mysql.py b/modules/mysql.py index 3b6c3b1..2db2817 100644 --- a/modules/mysql.py +++ b/modules/mysql.py @@ -7,4 +7,4 @@ class Module(ModuleBase): conf = manager.config['mysql'] - self.db = MySQLdb.connect(host = conf['host'], user = conf['user'], passwd = conf['passwd'], db = conf['db']) + self.db = MySQLdb.connect(host = conf['host'], user = conf['user'], passwd = conf['passwd'], db = conf['db'], use_unicode = True, charset = 'utf8') diff --git a/modules/topic.py b/modules/topic.py new file mode 100644 index 0000000..bc8c83e --- /dev/null +++ b/modules/topic.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +from base import ModuleBase +import re + +class Module(ModuleBase): + def __init__(self, manager): + ModuleBase.__init__(self, manager) + + def commands(self): + return [('!topic <Topic>', 'Fügt einen Text zum Thema des Chatraums hinzu')] + + def groupchat(self, room, nick, text, handler): + if not re.match(r'!topic\W', text): + return + + topic = re.sub(r'!topic\W+', '', text) + if topic == '': + return + + oldtopic = handler.get_topic() + if oldtopic != '' and oldtopic != None: + topic += ' | ' + oldtopic + + handler.set_topic(topic) |