now detects if kicked, needs a test what happens when the server looses connection

master
Stefan Krulj 14 years ago
parent 40687330e3
commit fd1947fdd9
  1. 4
      main.py
  2. 37
      sys/brathuhn.py
  3. 4
      sys/config.py

@ -1,7 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys, os import sys, os, time
reload(sys) reload(sys)
sys.setdefaultencoding("utf-8") sys.setdefaultencoding("utf-8")
@ -37,5 +37,3 @@ print "PID:", os.getpid()
brathuhn.connect_and_join() brathuhn.connect_and_join()
brathuhn.loop() brathuhn.loop()

@ -1,14 +1,14 @@
import xmpp, os, sys, time, traceback, stream_ft import xmpp, os, sys, time, traceback, stream_ft
import threading, errno, select import threading, errno, select
import util import util, datetime
from config import * from config import *
VERSION = "0.2" VERSION = "0.2"
MIN_CONNECT_INTERVAL = 300 IGNORE_FIRST = 10
IGNORE_FIRST = 3
last_connect = 0 last_connect = 0
reconnect = False
commands = dict() commands = dict()
commands_private = dict() commands_private = dict()
group_listeners = [] group_listeners = []
@ -162,8 +162,10 @@ def __messageCB(conn,msg):
raise xmpp.NodeProcessed raise xmpp.NodeProcessed
def __presenceCB(conn,msg): def __presenceCB(conn,msg):
global reconnect
if msg.getType()=='unavailable': if msg.getType()=='unavailable':
nick=msg.getNick() nick=msg.getNick()
print room_nick
if nick==None: if nick==None:
if msg.getFrom() in users: if msg.getFrom() in users:
del users[msg.getFrom()] del users[msg.getFrom()]
@ -171,6 +173,9 @@ def __presenceCB(conn,msg):
if not msg.getFrom() in users: if not msg.getFrom() in users:
for listener in status_listeners: for listener in status_listeners:
listener(msg.getFrom(),msg,STATUS_OFFLINE) listener(msg.getFrom(),msg,STATUS_OFFLINE)
elif nick==NAME:
print "--got kicked"
reconnect = True
else: else:
if msg.getFrom() in users: if msg.getFrom() in users:
del users[msg.getFrom()] del users[msg.getFrom()]
@ -216,8 +221,6 @@ def __ignore_first(conn,msg):
__messageCB(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(): def connect_and_join():
"""Connect to the server and join the group chat room from the config.""" """Connect to the server and join the group chat room from the config."""
@ -227,6 +230,9 @@ def connect_and_join():
global cl global cl
global join_time global join_time
last_connect=time.time() last_connect=time.time()
jid=xmpp.protocol.JID(node=NAME,domain=SRV,resource=RESOURCE)
cl=xmpp.Client(jid.getDomain(),debug=[])
#connect to server #connect to server
con=cl.connect() con=cl.connect()
if not con: if not con:
@ -266,8 +272,19 @@ def loop():
"""Loop forever and handle the event-queue.""" """Loop forever and handle the event-queue."""
global last_connect global last_connect
global call_ats global call_ats
global reconnect
while True: while True:
try: 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) cl.Process(1)
to_remove = [] to_remove = []
for cid in call_ats: for cid in call_ats:
@ -291,13 +308,9 @@ def loop():
print_error() print_error()
def 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) 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 "---------------------------" print "---------------------------"
def callAt(func,_time,*args): 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 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 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 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 E.g this will call foo(arg1,arg2) in ~5 seconds
callAt(foo,time.time()+5,arg1,arg2) callAt(foo,time.time()+5,arg1,arg2)

@ -15,6 +15,10 @@ try:
except: except:
RESOURCE = 'bot' RESOURCE = 'bot'
try:
RECONNECT_INTERVAL = int(cp.get("Account","RECONNECT_INTERVAL"))
except:
RECONNECT_INTERVAL = 300
## ##
## General ## General

Loading…
Cancel
Save