From 8017b2d9f1eceaf31aa95f87ee356ab8c114cde3 Mon Sep 17 00:00:00 2001 From: "louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13" Date: Sat, 8 Jan 2011 23:20:27 +0000 Subject: Handle authentication and connection errors. fixed #1994 --- src/connection.py | 9 ++++++--- src/core.py | 40 ++++++++++++++++++++++++++++++++++------ src/poezio.py | 3 ++- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/connection.py b/src/connection.py index 8e082087..66ae9e55 100644 --- a/src/connection.py +++ b/src/connection.py @@ -50,7 +50,7 @@ class Connection(sleekxmpp.ClientXMPP): sleekxmpp.ClientXMPP.__init__(self, jid, password, ssl=True) self.registerPlugin('xep_0030') self.registerPlugin('xep_0045') - + def start(self): # TODO, try multiple servers # With anon auth. @@ -58,10 +58,13 @@ class Connection(sleekxmpp.ClientXMPP): custom_host = config.get('custom_host', '') custom_port = config.get('custom_port', -1) if custom_host and custom_port != -1: - self.connect((custom_host, custom_port)) + res = self.connect((custom_host, custom_port), reattempt=False) else: - self.connect() + res = self.connect(reattempt=False) + if not res: + return False self.process(threaded=True) + return True # Global connection object connection = Connection() diff --git a/src/core.py b/src/core.py index 7ab4917d..de16bcae 100644 --- a/src/core.py +++ b/src/core.py @@ -147,7 +147,10 @@ class Core(object): } # Add handlers - self.xmpp.add_event_handler("session_start", self.on_connected) + self.xmpp.add_event_handler('connected', self.on_connected) + 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("session_start", self.on_session_start) self.xmpp.add_event_handler("groupchat_presence", self.on_groupchat_presence) self.xmpp.add_event_handler("groupchat_message", self.on_groupchat_message) self.xmpp.add_event_handler("groupchat_subject", self.on_groupchat_subject) @@ -157,6 +160,9 @@ class Core(object): self.xmpp.add_event_handler("roster_update", self.on_roster_update) self.xmpp.add_event_handler("changed_status", self.on_presence) + self.information(_('Welcome to poezio!')) + self.refresh_window() + def on_exception(self, typ, value, trace): """ When an exception in raised, open a special tab @@ -240,14 +246,36 @@ class Core(object): if tab and isinstance(tab, tabs.ConversationTab): self.add_message_to_text_buffer(tab.get_room(), msg) + def on_failed_connection(self): + """ + We cannot contact the remote server + """ + self.information(_("Connection to remote server failed")) + + def on_disconnected(self, event): + """ + When we are disconnected from remote server + """ + self.information(_("Disconnected from server.")) + + def on_failed_auth(self, event): + """ + Authentication failed + """ + self.information(_("Authentication failed.")) def on_connected(self, event): + """ + Remote host responded, but we are not yet authenticated + """ + self.information(_("Connected to server.")) + + def on_session_start(self, event): """ Called when we are connected and authenticated """ - self.information(_("Welcome on Poezio \o/!")) + self.information(_("Authentication success.")) self.information(_("Your JID is %s") % self.xmpp.boundjid.full) - if not self.xmpp.anon: # request the roster self.xmpp.getRoster() @@ -598,15 +626,15 @@ class Core(object): """ main loop waiting for the user to press a key """ - self.refresh_window() + curses.ungetch('\n') # FIXME while self.running: - self.doupdate() - char=read_char(self.stdscr) + char = read_char(self.stdscr) # search for keyboard shortcut if char in list(self.key_func.keys()): self.key_func[char]() else: self.do_command(char) + self.doupdate() def current_tab(self): """ diff --git a/src/poezio.py b/src/poezio.py index 64a62d04..8362cd19 100644 --- a/src/poezio.py +++ b/src/poezio.py @@ -34,7 +34,8 @@ if __name__ == '__main__': signal.signal(signal.SIGINT, signal.SIG_IGN) # ignore ctrl-c if options.debug: logging.basicConfig(filename=options.debug,level=logging.DEBUG) - connection.start() # Connect to remote server + if not connection.start(): # Connect to remote server + core.on_failed_connection() # Disable any display of non-wanted text on the terminal # by redirecting stderr to /dev/null # sys.stderr = open('/dev/null', 'a') -- cgit v1.2.3