Fix all the connect() reconnect() stuff

The /reconnect command should now properly work everytime.
This commit is contained in:
Florent Le Coz 2014-07-30 17:24:41 +02:00
parent a404195a0a
commit ecf22cb4a4
4 changed files with 21 additions and 13 deletions

View file

@ -807,6 +807,9 @@ def command_quit(self, arg=''):
""" """
/quit /quit
""" """
if not self.xmpp.is_connected():
self.exit()
return
if len(arg.strip()) != 0: if len(arg.strip()) != 0:
msg = arg msg = arg
else: else:
@ -820,8 +823,7 @@ def command_quit(self, arg=''):
self.save_config() self.save_config()
self.plugin_manager.disable_plugins() self.plugin_manager.disable_plugins()
self.disconnect(msg) self.disconnect(msg)
self.running = False self.xmpp.add_event_handler("disconnected", self.exit, disposable=True)
self.reset_curses()
def command_destroy_room(self, arg=''): def command_destroy_room(self, arg=''):
""" """

View file

@ -9,6 +9,7 @@ import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
import asyncio
import collections import collections
import curses import curses
import os import os
@ -189,6 +190,7 @@ class Core(object):
# Add handlers # Add handlers
self.xmpp.add_event_handler('connected', self.on_connected) self.xmpp.add_event_handler('connected', self.on_connected)
self.xmpp.add_event_handler('connection_failed', self.on_failed_connection)
self.xmpp.add_event_handler('disconnected', self.on_disconnected) self.xmpp.add_event_handler('disconnected', self.on_disconnected)
self.xmpp.add_event_handler('failed_auth', self.on_failed_auth) self.xmpp.add_event_handler('failed_auth', self.on_failed_auth)
self.xmpp.add_event_handler('no_auth', self.on_no_auth) self.xmpp.add_event_handler('no_auth', self.on_no_auth)
@ -427,13 +429,8 @@ class Core(object):
if config.get('enable_user_gaming', True): if config.get('enable_user_gaming', True):
self.xmpp.plugin['xep_0196'].stop(block=False) self.xmpp.plugin['xep_0196'].stop(block=False)
self.plugin_manager.disable_plugins() self.plugin_manager.disable_plugins()
self.disconnect('') self.disconnect('%s received' % signals.get(sig))
self.running = False self.xmpp.add_event_handler("disconnected", self.exit, disposable=True)
try:
self.reset_curses()
except: # too bad
pass
sys.exit()
def autoload_plugins(self): def autoload_plugins(self):
""" """
@ -470,6 +467,10 @@ class Core(object):
self.refresh_window() self.refresh_window()
self.xmpp.plugin['xep_0012'].begin_idle(jid=self.xmpp.boundjid) self.xmpp.plugin['xep_0012'].begin_idle(jid=self.xmpp.boundjid)
def exit(self, event=None):
log.debug("exit(%s)" % (event,))
asyncio.get_event_loop().stop()
def on_exception(self, typ, value, trace): def on_exception(self, typ, value, trace):
""" """
When an exception is raised, just reset curses and call When an exception is raised, just reset curses and call
@ -786,7 +787,9 @@ class Core(object):
tab.command_part(msg) tab.command_part(msg)
self.xmpp.disconnect() self.xmpp.disconnect()
if reconnect: if reconnect:
self.xmpp.start() # Add a one-time event to reconnect as soon as we are
# effectively disconnected
self.xmpp.add_event_handler('disconnected', lambda event: self.xmpp.connect(), disposable=True)
def send_message(self, msg): def send_message(self, msg):
""" """

View file

@ -812,11 +812,11 @@ def on_groupchat_presence(self, presence):
### Connection-related handlers ### ### Connection-related handlers ###
def on_failed_connection(self): def on_failed_connection(self, error):
""" """
We cannot contact the remote server We cannot contact the remote server
""" """
self.information(_("Connection to remote server failed"), _('Error')) self.information(_("Connection to remote server failed: %s" % (error,)), _('Error'))
def on_disconnected(self, event): def on_disconnected(self, event):
""" """

View file

@ -241,7 +241,10 @@ class RosterInfoTab(Tab):
""" """
/reconnect /reconnect
""" """
self.core.disconnect(reconnect=True) if self.core.xmpp.is_connected():
self.core.disconnect(reconnect=True)
else:
self.core.xmpp.connect()
def command_disconnect(self, args=None): def command_disconnect(self, args=None):
""" """