diff --git a/main.py b/main.py index 9c1fe18..25f41a5 100755 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -import sys, os +import sys, os, time reload(sys) sys.setdefaultencoding("utf-8") @@ -37,5 +37,3 @@ print "PID:", os.getpid() brathuhn.connect_and_join() brathuhn.loop() - - diff --git a/sys/brathuhn.py b/sys/brathuhn.py index 34e55c4..52f65ea 100644 --- a/sys/brathuhn.py +++ b/sys/brathuhn.py @@ -1,14 +1,14 @@ import xmpp, os, sys, time, traceback, stream_ft import threading, errno, select -import util +import util, datetime from config import * VERSION = "0.2" -MIN_CONNECT_INTERVAL = 300 -IGNORE_FIRST = 3 +IGNORE_FIRST = 10 last_connect = 0 +reconnect = False commands = dict() commands_private = dict() group_listeners = [] @@ -162,8 +162,10 @@ def __messageCB(conn,msg): raise xmpp.NodeProcessed def __presenceCB(conn,msg): + global reconnect if msg.getType()=='unavailable': nick=msg.getNick() + print room_nick if nick==None: if msg.getFrom() in users: del users[msg.getFrom()] @@ -171,6 +173,9 @@ def __presenceCB(conn,msg): if not msg.getFrom() in users: for listener in status_listeners: listener(msg.getFrom(),msg,STATUS_OFFLINE) + elif nick==NAME: + print "--got kicked" + reconnect = True else: if msg.getFrom() in users: del users[msg.getFrom()] @@ -216,8 +221,6 @@ def __ignore_first(conn,msg): __messageCB(conn,msg) -jid=xmpp.protocol.JID(node=NAME,domain=SRV,resource=RESOURCE) -cl=xmpp.Client(jid.getDomain(),debug=[]) def connect_and_join(): """Connect to the server and join the group chat room from the config.""" @@ -227,6 +230,9 @@ def connect_and_join(): global cl global join_time last_connect=time.time() + + jid=xmpp.protocol.JID(node=NAME,domain=SRV,resource=RESOURCE) + cl=xmpp.Client(jid.getDomain(),debug=[]) #connect to server con=cl.connect() if not con: @@ -266,8 +272,19 @@ def loop(): """Loop forever and handle the event-queue.""" global last_connect global call_ats + global reconnect while True: try: + + if reconnect or cl.connected: #TODO check if it works + print "--reconnect--" + s=RECONNECT_INTERVAL-(time.time()-last_connect) + if s>0: + print "--reconnect interval not met (%dsek), sleeping[sek] %d" % (RECONNECT_INTERVAL,s) + time.sleep(s) + reconnect = False + print "--wake--" + connect_and_join() cl.Process(1) to_remove = [] for cid in call_ats: @@ -291,13 +308,9 @@ def loop(): print_error() def print_error(): - print "-------ERROR OCCURED-------" + print "-------ERROR OCCURED-------", datetime.datetime.now.strftime("%Y-%h-%d, %H:%M:%S") + print"--time: ", traceback.print_exc(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 "---------------------------" def callAt(func,_time,*args): @@ -305,7 +318,7 @@ def callAt(func,_time,*args): guaranteed that it will happen after the specified time. The call happens in the same thread, so no synchronization is needed. You can append any number of arguments you need. Returns the call id, needed to cancel a call - + E.g this will call foo(arg1,arg2) in ~5 seconds callAt(foo,time.time()+5,arg1,arg2) diff --git a/sys/config.py b/sys/config.py index 8d75934..8cbda5d 100644 --- a/sys/config.py +++ b/sys/config.py @@ -15,6 +15,10 @@ try: except: RESOURCE = 'bot' +try: + RECONNECT_INTERVAL = int(cp.get("Account","RECONNECT_INTERVAL")) +except: + RECONNECT_INTERVAL = 300 ## ## General