xmpp: Fix issues with reconnecting
This commit is contained in:
parent
60dfc17c67
commit
b702c1af45
37
hybridbot.py
37
hybridbot.py
@ -5,13 +5,13 @@ import re
|
|||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from threading import Thread
|
|
||||||
from irc.bot import SingleServerIRCBot
|
|
||||||
import sleekxmpp
|
import sleekxmpp
|
||||||
if sys.version_info.major >= 3:
|
if sys.version_info.major >= 3:
|
||||||
from configparser import SafeConfigParser
|
from configparser import SafeConfigParser
|
||||||
else:
|
else:
|
||||||
from ConfigParser import SafeConfigParser
|
from ConfigParser import SafeConfigParser
|
||||||
|
from threading import Thread
|
||||||
|
from irc.bot import SingleServerIRCBot
|
||||||
|
|
||||||
|
|
||||||
class IRCBot:
|
class IRCBot:
|
||||||
@ -122,17 +122,23 @@ class XMPPBot:
|
|||||||
|
|
||||||
def register_handlers(self):
|
def register_handlers(self):
|
||||||
self.client.add_event_handler('session_start', self.on_session_start)
|
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('groupchat_message', self.on_message)
|
self.client.add_event_handler('groupchat_message', self.on_message)
|
||||||
self.client.add_event_handler('muc::%s::presence' % self.muc,
|
self.client.add_event_handler('muc::%s::presence' % self.muc,
|
||||||
self.on_presence)
|
self.on_presence)
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
if not self.client.connect():
|
||||||
|
# sys.stderr.write('could not connect!\n')
|
||||||
|
sys.stderr.write('Could not connect to server, or password ' +
|
||||||
|
'mismatch!\n')
|
||||||
|
sys.exit(1)
|
||||||
|
# sys.stderr.write('connected with %s\n'%con)
|
||||||
|
|
||||||
def join_muc(self):
|
def join_muc(self):
|
||||||
muc_plugin = self.client.plugin['xep_0045']
|
muc_plugin = self.client.plugin['xep_0045']
|
||||||
|
|
||||||
if self.muc in muc_plugin.getJoinedRooms():
|
muc_plugin.joinMUC(self.muc, self.nick)
|
||||||
muc_plugin.leaveMUC(self.muc, self.nick,
|
|
||||||
msg='Replaced by new connection')
|
|
||||||
muc_plugin.joinMUC(self.muc, self.nick, wait=True)
|
|
||||||
|
|
||||||
def on_session_start(self, event):
|
def on_session_start(self, event):
|
||||||
print('Connected to XMPP')
|
print('Connected to XMPP')
|
||||||
@ -140,6 +146,10 @@ class XMPPBot:
|
|||||||
self.client.send_presence()
|
self.client.send_presence()
|
||||||
self.join_muc()
|
self.join_muc()
|
||||||
|
|
||||||
|
def on_session_end(self, event):
|
||||||
|
time.sleep(2.0)
|
||||||
|
self.connect()
|
||||||
|
|
||||||
def on_message(self, event):
|
def on_message(self, event):
|
||||||
body = event['body']
|
body = event['body']
|
||||||
nick = event['mucnick']
|
nick = event['mucnick']
|
||||||
@ -192,15 +202,9 @@ class XMPPBot:
|
|||||||
def start(self):
|
def start(self):
|
||||||
self.client.register_plugin('xep_0045') # XMPP MUC.
|
self.client.register_plugin('xep_0045') # XMPP MUC.
|
||||||
self.client.register_plugin('xep_0199') # XMPP Ping.
|
self.client.register_plugin('xep_0199') # XMPP Ping.
|
||||||
if self.client.connect():
|
self.register_handlers()
|
||||||
# sys.stderr.write('connected with %s\n'%con)
|
self.connect()
|
||||||
self.register_handlers()
|
self.client.process(block=True)
|
||||||
self.client.process(block=True)
|
|
||||||
else:
|
|
||||||
# sys.stderr.write('could not connect!\n')
|
|
||||||
sys.stderr.write('Could not connect to server, or password ' +
|
|
||||||
'mismatch!\n')
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
class Intermedia:
|
class Intermedia:
|
||||||
@ -320,8 +324,7 @@ if __name__ == '__main__':
|
|||||||
config.read('config.ini')
|
config.read('config.ini')
|
||||||
|
|
||||||
if not config.sections():
|
if not config.sections():
|
||||||
sys.stderr.write('Error: Configuration file does not exist ' +
|
sys.stderr.write('Error: Configuration file does not exist or is empty.\n')
|
||||||
'or is empty.\n')
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
shared_opts['prefix'] = config.get('Shared', 'prefix')
|
shared_opts['prefix'] = config.get('Shared', 'prefix')
|
||||||
|
Loading…
Reference in New Issue
Block a user