Made everything much more error resistant

This commit is contained in:
Matthias Schiffer 2010-05-09 21:49:15 +02:00
parent e5cd4bb203
commit 1384d1976f
11 changed files with 109 additions and 62 deletions

View file

@ -38,8 +38,7 @@ class MucHandler(MucRoomHandler):
if(user == None or user.same_as(self.room_state.me) or stanza.get_body() == None): if(user == None or user.same_as(self.room_state.me) or stanza.get_body() == None):
return return
for mod in self.client.module_manager.modules.itervalues(): self.client.module_manager.groupchat(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_body(), self)
mod.groupchat(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_body(), self)
def user_joined(self, user, stanza): def user_joined(self, user, stanza):
if(not self.room_state.joined or user.same_as(self.room_state.me)): if(not self.room_state.joined or user.same_as(self.room_state.me)):
@ -49,8 +48,7 @@ class MucHandler(MucRoomHandler):
if stanza.get_status() != None: if stanza.get_status() != None:
status = stanza.get_status() status = stanza.get_status()
for mod in self.client.module_manager.modules.itervalues(): self.client.module_manager.join(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self)
mod.join(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self)
def user_left(self, user, stanza): def user_left(self, user, stanza):
if(not self.room_state.joined or user.same_as(self.room_state.me)): if(not self.room_state.joined or user.same_as(self.room_state.me)):
@ -60,16 +58,14 @@ class MucHandler(MucRoomHandler):
if stanza.get_status() != None: if stanza.get_status() != None:
status = stanza.get_status() status = stanza.get_status()
for mod in self.client.module_manager.modules.itervalues(): self.client.module_manager.leave(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self)
mod.leave(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self)
def subject_changed(self, user, stanza): def subject_changed(self, user, stanza):
topic = '' topic = ''
if stanza.get_subject() != None: if stanza.get_subject() != None:
topic = stanza.get_subject() topic = stanza.get_subject()
for mod in self.client.module_manager.modules.itervalues(): self.client.module_manager.topic(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, topic, self)
mod.topic(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, topic, self)
class PresenceHandler(object): class PresenceHandler(object):

View file

@ -1,22 +1,57 @@
from connection.xmpp import XMPPConnection from connection.xmpp import XMPPConnection
import signal, sys import signal, sys, traceback
from config import config from config import config
class ModuleManager: class ModuleManager:
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
self.modules = {} self._modules = {}
for mod in config['modules']: for mod in config['modules']:
self.get(mod) self.get(mod)
def get(self, name): def get(self, name):
if not name in self.modules: if not name in self._modules:
mod = __import__('modules.' + name, globals(), locals(), ['Module']) mod = __import__('modules.' + name, globals(), locals(), ['Module'])
self.modules[name] = mod.Module(self) self._modules[name] = mod.Module(self)
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))
return self.modules[name]
run = True run = True
@ -35,13 +70,18 @@ print 'Loading modules...'
modman = ModuleManager(config) modman = ModuleManager(config)
while run:
connection = XMPPConnection(config, modman) connection = XMPPConnection(config, modman)
print 'Connecting...' print 'Connecting...'
connection.connect() connection.connect()
try:
while run: while run:
act = connection.stream.loop_iter(1) act = connection.stream.loop_iter(1)
if not act: if not act:
connection.idle() connection.idle()
connection.disconnect() connection.disconnect()
except:
traceback.print_exc(file=sys.stdout)
connection.disconnect()

View file

@ -0,0 +1,21 @@
class ModuleBase:
def __init__(self, manager):
self.manager = manager
def commands(self):
return []
def helptexts(self):
return []
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

View file

@ -1,24 +0,0 @@
class ModuleBase:
def __init__(self, manager):
self.manager = manager
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, 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

View file

@ -1,4 +1,4 @@
from base import ModuleBase from . import ModuleBase
import re import re
class Module(ModuleBase): class Module(ModuleBase):

View file

@ -1,4 +1,4 @@
from base import ModuleBase from . import ModuleBase
import re, subprocess import re, subprocess
class Module(ModuleBase): class Module(ModuleBase):

View file

@ -1,4 +1,4 @@
from base import ModuleBase from . import ModuleBase
import re import re
class Module(ModuleBase): class Module(ModuleBase):
@ -12,8 +12,8 @@ class Module(ModuleBase):
if not re.match(r'!help(?:\W|\Z)', text): if not re.match(r'!help(?:\W|\Z)', text):
return return
commands = reduce(lambda l, mod: l + mod.commands(), self.manager.modules.itervalues(), []) commands = self.manager.commands()
helpstring = reduce(lambda s, (c, h): s + c + ': ' + h + '\n', commands, '') 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(), [])) helptexts = '\n'.join(self.manager.helptexts())
handler.reply(('Befehle:\n' + helpstring + '\n' + helptexts).strip()) handler.reply(('Befehle:\n' + helpstring + '\n' + helptexts).strip())

View file

@ -1,29 +1,29 @@
from base import ModuleBase from . import ModuleBase
class Module(ModuleBase): class Module(ModuleBase):
def __init__(self, manager): def __init__(self, manager):
ModuleBase.__init__(self, manager) ModuleBase.__init__(self, manager)
self.db = manager.get('mysql').db self.mysql = manager.get('mysql')
def helptexts(self): def helptexts(self):
return ['Chatlogs werden auch erstellt.'] return ['Chatlogs werden auch erstellt.']
def groupchat(self, room, nick, text, handler): def groupchat(self, room, nick, text, handler):
cursor = self.db.cursor() cursor = self.mysql.cursor()
cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `text`) VALUES ("message", NOW(), %s, %s, %s)', (room, nick, text)) cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `text`) VALUES ("message", NOW(), %s, %s, %s)', (room, nick, text))
cursor.close() cursor.close()
def join(self, room, nick, show, status, handler): def join(self, room, nick, show, status, handler):
cursor = self.db.cursor() cursor = self.mysql.cursor()
cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `show`, `text`) VALUES ("join", NOW(), %s, %s, %s, %s)', (room, nick, show, status)) cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `show`, `text`) VALUES ("join", NOW(), %s, %s, %s, %s)', (room, nick, show, status))
cursor.close() cursor.close()
def leave(self, room, nick, show, status, handler): def leave(self, room, nick, show, status, handler):
cursor = self.db.cursor() cursor = self.mysql.cursor()
cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `show`, `text`) VALUES ("leave", NOW(), %s, %s, %s, %s)', (room, nick, show, status)) cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `show`, `text`) VALUES ("leave", NOW(), %s, %s, %s, %s)', (room, nick, show, status))
cursor.close() cursor.close()
def topic(self, room, nick, text, handler): def topic(self, room, nick, text, handler):
cursor = self.db.cursor() cursor = self.mysql.cursor()
cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `text`) VALUES ("topic", NOW(), %s, %s, %s)', (room, nick, text)) cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `text`) VALUES ("topic", NOW(), %s, %s, %s)', (room, nick, text))
cursor.close() cursor.close()

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from base import ModuleBase from . import ModuleBase
import re import re
from datetime import datetime, date, timedelta, time from datetime import datetime, date, timedelta, time
import urllib import urllib

View file

@ -1,10 +1,24 @@
from base import ModuleBase from . import ModuleBase
import MySQLdb import MySQLdb
class Module(ModuleBase): class Module(ModuleBase):
def __init__(self, manager): def __init__(self, manager):
ModuleBase.__init__(self, manager) ModuleBase.__init__(self, manager)
conf = manager.config['mysql'] self.conf = manager.config['mysql']
self.db = MySQLdb.connect(host = conf['host'], user = conf['user'], passwd = conf['passwd'], db = conf['db'], use_unicode = True, charset = 'utf8') self.db = False
self._connect()
def _connect(self):
if self.db:
self.db.close()
self.db = MySQLdb.connect(host = self.conf['host'], user = self.conf['user'], passwd = self.conf['passwd'], db = self.conf['db'], use_unicode = True, charset = 'utf8')
def cursor(self):
try:
self.db.ping()
return self.db.cursor()
except MySQLdb.OperationalError:
self._connect()
return self.db.cursor()

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from base import ModuleBase from . import ModuleBase
import re import re
class Module(ModuleBase): class Module(ModuleBase):