diff --git a/brathuhn.py b/brathuhn.py index 66f25e3..360993c 100644 --- a/brathuhn.py +++ b/brathuhn.py @@ -1,9 +1,12 @@ -import xmpp, os, sys, time +import xmpp, os, sys, time, traceback from config import * VERSION = "0.2" +MIN_CONNECT_INTERVAL=300 + +last_connect = 0 commands = dict() commands_private = dict() group_listeners = [] @@ -170,7 +173,7 @@ def __presenceCB(conn,msg): users[msg.getFrom()] = STATUS_ONLINE #Online else: - print msg.getFrom(),"is now",show + #print msg.getFrom(),"is now",show if not msg.getFrom() in users: for listener in status_listeners: listener(msg.getFrom(),msg,STATUS_JOIN) @@ -196,11 +199,13 @@ def connect_and_join(): """Connect to the server and join the group chat room from the config.""" global room_nick global room_to + global last_connect + last_connect=time.time() #connect to server con=cl.connect() if not con: print 'could not connect!' - sys.exit() + raise 'could not connect!' print 'connected' cl.RegisterHandler('message',__messageCB) @@ -210,7 +215,7 @@ def connect_and_join(): auth=cl.auth(NAME,PWD,resource=jid.getResource()) if not auth: print 'could not authenticate!' - sys.exit() + raise 'could not authenticate!' print 'auth' @@ -229,7 +234,26 @@ def connect_and_join(): cl.send(xmpp.Presence(to=room_to,status=NAME+" --(brathuhn bot framework (v"+VERSION+"), type !help)")) def loop(): + global last_connect """Loop forever and handle the event-queue.""" while True: - cl.Process(1) + try: + cl.Process(1) + except xmpp.protocol.Conflict: + print "detected another connection, quitting..." + sys.exit(0) + except KeyboardInterrupt: + print "keyboard interrupt, quitting..." + sys.exit(0) + except: + print "-------ERROR OCCURED-------" + exc_type, exc_value, exc_traceback = sys.exc_info() + traceback.print_tb(exc_traceback, file=sys.stdout) + if not cl.connected: + s=MIN_CONNECT_INTERVAL-(time.time()-last_connect) + if s>0: + time.sleep(s) + connect_and_join() + print "---------------------------" +