Improve reconnecting ability
And increase timeout to 5 seconds.
This commit is contained in:
parent
2349db325d
commit
33a9780d97
37
hybridbot.py
37
hybridbot.py
@ -112,19 +112,22 @@ class IRCBot:
|
||||
|
||||
|
||||
class XMPPBot:
|
||||
def __init__(self, opts, inter):
|
||||
def __init__(self, opts, inter, timeout=5.0):
|
||||
self.client = slixmpp.ClientXMPP(opts['jid'], opts['passwd'])
|
||||
self.opts = opts
|
||||
self.nick = opts['nick']
|
||||
self.pure_nick = self.nick
|
||||
self.muc = opts['muc']
|
||||
self.inter = inter
|
||||
self.timeout = timeout
|
||||
self.muc_is_joined = False
|
||||
|
||||
def register_handlers(self):
|
||||
self.client.add_event_handler('failed_all_auth',
|
||||
self.on_failed_all_auth)
|
||||
self.client.add_event_handler('session_start', self.on_session_start)
|
||||
self.client.add_event_handler('session_end', self.on_session_end)
|
||||
self.client.add_event_handler('got_online', self.on_got_online)
|
||||
self.client.add_event_handler('disconnected', self.on_disconnected)
|
||||
self.client.add_event_handler('groupchat_message', self.on_message)
|
||||
self.client.add_event_handler('muc::%s::presence' % self.muc,
|
||||
self.on_presence)
|
||||
@ -132,10 +135,15 @@ class XMPPBot:
|
||||
def connect(self):
|
||||
self.client.connect()
|
||||
|
||||
def join_muc(self):
|
||||
@asyncio.coroutine
|
||||
def join_muc_loop(self):
|
||||
muc_plugin = self.client.plugin['xep_0045']
|
||||
while not self.muc_is_joined:
|
||||
muc_plugin.join_muc(self.muc, self.nick)
|
||||
yield from asyncio.sleep(self.timeout)
|
||||
|
||||
muc_plugin.join_muc(self.muc, self.nick)
|
||||
def join_muc(self):
|
||||
asyncio.async(self.join_muc_loop())
|
||||
|
||||
def on_failed_all_auth(event):
|
||||
# print('could not connect!', file=sys.stderr)
|
||||
@ -146,12 +154,17 @@ class XMPPBot:
|
||||
def on_session_start(self, event):
|
||||
# print('connected with %s' %con, file=sys.stderr)
|
||||
print('Connected to XMPP')
|
||||
self.client.send_presence()
|
||||
self.client.get_roster()
|
||||
self.client.send_presence()
|
||||
|
||||
def on_got_online(self, event):
|
||||
self.join_muc()
|
||||
|
||||
def on_session_end(self, event):
|
||||
time.sleep(2.0)
|
||||
@asyncio.coroutine
|
||||
def on_disconnected(self, event):
|
||||
self.muc_is_joined = False
|
||||
print('Connection lost, reattempting in %d seconds' % self.timeout)
|
||||
yield from asyncio.sleep(self.timeout)
|
||||
self.connect()
|
||||
|
||||
def on_message(self, event):
|
||||
@ -160,6 +173,7 @@ class XMPPBot:
|
||||
|
||||
self.inter.relay_message('xmpp', nick, body)
|
||||
|
||||
@asyncio.coroutine
|
||||
def on_presence(self, event):
|
||||
try:
|
||||
muc_plugin = self.client.plugin['xep_0045']
|
||||
@ -171,7 +185,11 @@ class XMPPBot:
|
||||
if not nick:
|
||||
nick = muc_plugin.get_nick(self.muc, event['from'])
|
||||
|
||||
if typ == 'error':
|
||||
if typ == 'available':
|
||||
self.muc_is_joined = True
|
||||
|
||||
elif typ == 'error':
|
||||
self.muc_is_joined = False
|
||||
if event['error']['code'] == '409':
|
||||
self.nick = self.nick + '_'
|
||||
self.join_muc()
|
||||
@ -181,8 +199,9 @@ class XMPPBot:
|
||||
if nick in self.inter.get_xmpp_users():
|
||||
self.inter.remove_xmpp_user(nick)
|
||||
else:
|
||||
self.muc_is_joined = False
|
||||
self.nick = self.pure_nick
|
||||
time.sleep(0.5)
|
||||
yield from asyncio.sleep(0.5)
|
||||
self.join_muc()
|
||||
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user