summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2010-05-09 21:49:15 +0200
committerMatthias Schiffer <matthias@gamezock.de>2010-05-09 21:49:15 +0200
commit1384d1976fa984b6b254c93677363f67944c0c7a (patch)
tree698faff3ef045df7f2a8e3bd75e6b1e04a397b28
parente5cd4bb20328110561ee9956f948b97c378cf671 (diff)
downloadcurunir-1384d1976fa984b6b254c93677363f67944c0c7a.tar
curunir-1384d1976fa984b6b254c93677363f67944c0c7a.zip
Made everything much more error resistant
-rw-r--r--connection/xmpp.py12
-rw-r--r--curunir.py68
-rw-r--r--modules/__init__.py21
-rw-r--r--modules/base.py24
-rw-r--r--modules/credits.py2
-rw-r--r--modules/ddate.py2
-rw-r--r--modules/help.py6
-rw-r--r--modules/log.py12
-rw-r--r--modules/mensa.py2
-rw-r--r--modules/mysql.py20
-rw-r--r--modules/topic.py2
11 files changed, 109 insertions, 62 deletions
diff --git a/connection/xmpp.py b/connection/xmpp.py
index 454f3df..dd1c061 100644
--- a/connection/xmpp.py
+++ b/connection/xmpp.py
@@ -38,8 +38,7 @@ class MucHandler(MucRoomHandler):
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)
+ self.client.module_manager.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)):
@@ -49,8 +48,7 @@ class MucHandler(MucRoomHandler):
if stanza.get_status() != None:
status = stanza.get_status()
- for mod in self.client.module_manager.modules.itervalues():
- mod.join(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self)
+ self.client.module_manager.join(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self)
def user_left(self, user, stanza):
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:
status = stanza.get_status()
- for mod in self.client.module_manager.modules.itervalues():
- mod.leave(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self)
+ self.client.module_manager.leave(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), 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)
+ self.client.module_manager.topic(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, topic, self)
class PresenceHandler(object):
diff --git a/curunir.py b/curunir.py
index 3c0d933..5383fe3 100644
--- a/curunir.py
+++ b/curunir.py
@@ -1,22 +1,57 @@
from connection.xmpp import XMPPConnection
-import signal, sys
+import signal, sys, traceback
from config import config
class ModuleManager:
def __init__(self, config):
self.config = config
- self.modules = {}
+ self._modules = {}
for mod in config['modules']:
self.get(mod)
def get(self, name):
- if not name in self.modules:
+ if not name in self._modules:
mod = __import__('modules.' + name, globals(), locals(), ['Module'])
- self.modules[name] = mod.Module(self)
+ self._modules[name] = mod.Module(self)
- return self.modules[name]
+ 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))
+
run = True
@@ -35,13 +70,18 @@ print 'Loading modules...'
modman = ModuleManager(config)
-connection = XMPPConnection(config, modman)
-print 'Connecting...'
-connection.connect()
-
while run:
- act = connection.stream.loop_iter(1)
- if not act:
- connection.idle()
-
-connection.disconnect()
+ connection = XMPPConnection(config, modman)
+ print 'Connecting...'
+ connection.connect()
+
+ try:
+ while run:
+ act = connection.stream.loop_iter(1)
+ if not act:
+ connection.idle()
+
+ connection.disconnect()
+ except:
+ traceback.print_exc(file=sys.stdout)
+ connection.disconnect()
diff --git a/modules/__init__.py b/modules/__init__.py
index e69de29..49a460a 100644
--- a/modules/__init__.py
+++ b/modules/__init__.py
@@ -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
diff --git a/modules/base.py b/modules/base.py
deleted file mode 100644
index b839e9b..0000000
--- a/modules/base.py
+++ /dev/null
@@ -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
diff --git a/modules/credits.py b/modules/credits.py
index 29c00f7..d6f0f66 100644
--- a/modules/credits.py
+++ b/modules/credits.py
@@ -1,4 +1,4 @@
-from base import ModuleBase
+from . import ModuleBase
import re
class Module(ModuleBase):
diff --git a/modules/ddate.py b/modules/ddate.py
index 5c6fd77..e687d76 100644
--- a/modules/ddate.py
+++ b/modules/ddate.py
@@ -1,4 +1,4 @@
-from base import ModuleBase
+from . import ModuleBase
import re, subprocess
class Module(ModuleBase):
diff --git a/modules/help.py b/modules/help.py
index ed3e4d3..99f05a4 100644
--- a/modules/help.py
+++ b/modules/help.py
@@ -1,4 +1,4 @@
-from base import ModuleBase
+from . import ModuleBase
import re
class Module(ModuleBase):
@@ -12,8 +12,8 @@ class Module(ModuleBase):
if not re.match(r'!help(?:\W|\Z)', text):
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, '')
- 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())
diff --git a/modules/log.py b/modules/log.py
index 04ea721..ff8093e 100644
--- a/modules/log.py
+++ b/modules/log.py
@@ -1,29 +1,29 @@
-from base import ModuleBase
+from . import ModuleBase
class Module(ModuleBase):
def __init__(self, manager):
ModuleBase.__init__(self, manager)
- self.db = manager.get('mysql').db
+ self.mysql = manager.get('mysql')
def helptexts(self):
return ['Chatlogs werden auch erstellt.']
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.close()
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.close()
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.close()
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.close()
diff --git a/modules/mensa.py b/modules/mensa.py
index 2dd71dc..96f2275 100644
--- a/modules/mensa.py
+++ b/modules/mensa.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-from base import ModuleBase
+from . import ModuleBase
import re
from datetime import datetime, date, timedelta, time
import urllib
diff --git a/modules/mysql.py b/modules/mysql.py
index 2db2817..5ae9e93 100644
--- a/modules/mysql.py
+++ b/modules/mysql.py
@@ -1,10 +1,24 @@
-from base import ModuleBase
+from . import ModuleBase
import MySQLdb
class Module(ModuleBase):
def __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()
diff --git a/modules/topic.py b/modules/topic.py
index 81e1802..d878a91 100644
--- a/modules/topic.py
+++ b/modules/topic.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-from base import ModuleBase
+from . import ModuleBase
import re
class Module(ModuleBase):