From 659bc4c3aa6fcb3c7593971cd10008ff5ffca82c Mon Sep 17 00:00:00 2001 From: Alexei Sorokin Date: Mon, 26 Jun 2017 19:17:42 +0300 Subject: [PATCH] Enable ping keepalive --- hybridbot.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/hybridbot.py b/hybridbot.py index e6b3bdf..d732011 100755 --- a/hybridbot.py +++ b/hybridbot.py @@ -15,7 +15,6 @@ class IRCBot: self.client = SingleServerIRCBot([(opts['server'], opts['port'])], opts['nick'], opts['nick']) self.conn = self.client.connection - self.opts = opts self.nick = opts['nick'] self.pure_nick = self.nick self.chan = opts['chan'] @@ -114,13 +113,20 @@ class IRCBot: class XMPPBot: def __init__(self, opts, inter, timeout=5.0): self.client = slixmpp.ClientXMPP(opts['jid'], opts['passwd']) - self.opts = opts + + self.client.register_plugin('xep_0199') # XMPP Ping. + self.client_ping = self.client.plugin['xep_0199'] + self.client.register_plugin('xep_0045') # XMPP MUC. + self.client_muc = self.client.plugin['xep_0045'] + + self.muc_is_joined = False + self.client_ping.timeout = self.timeout = timeout + self.client_ping.keepalive = True + 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', @@ -136,7 +142,6 @@ class XMPPBot: self.client.connect() def join_muc(self): - muc_plugin = self.client.plugin['xep_0045'] @asyncio.coroutine def loop_cycle(): if self._join_muc_block > 0: @@ -144,7 +149,7 @@ class XMPPBot: self._join_muc_block += 1 while not self.muc_is_joined: - muc_plugin.join_muc(self.muc, self.nick) + self.client_muc.join_muc(self.muc, self.nick) yield from asyncio.sleep(self.timeout) self._join_muc_block -= 1 @@ -198,7 +203,7 @@ class XMPPBot: elif typ == 'error': self.muc_is_joined = False if event['error']['code'] == '409': - self.nick = self.nick + '_' + self.nick += '_' self.join_muc() elif typ == 'unavailable': @@ -231,8 +236,6 @@ class XMPPBot: @asyncio.coroutine def run(self): - self.client.register_plugin('xep_0045') # XMPP MUC. - self.client.register_plugin('xep_0199') # XMPP Ping. self.register_handlers() self.connect()