diff options
Diffstat (limited to 'butterfly/connection.py')
-rw-r--r-- | butterfly/connection.py | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/butterfly/connection.py b/butterfly/connection.py index d5f3c07..872a699 100644 --- a/butterfly/connection.py +++ b/butterfly/connection.py @@ -33,6 +33,7 @@ from butterfly.handle import ButterflyHandleFactory, network_to_extension from butterfly.contacts import ButterflyContacts from butterfly.channel_manager import ButterflyChannelManager from butterfly.mail_notification import ButterflyMailNotification +from butterfly.channel.sasl import ButterflySASLChannel __all__ = ['ButterflyConnection'] @@ -80,8 +81,12 @@ class ButterflyConnection(telepathy.server.Connection, self._manager = weakref.proxy(manager) self._new_client(use_http=self._try_http) - self._account = (parameters['account'].encode('utf-8'), - parameters['password'].encode('utf-8')) + + password = parameters.get('password', None) + if password is not None: + password = password.encode('utf-8') + self._account = (parameters['account'].encode('utf-8'), password) + self._channel_manager = ButterflyChannelManager(self, protocol) # Call parent initializers @@ -103,6 +108,8 @@ class ButterflyConnection(telepathy.server.Connection, self._initial_presence = papyon.Presence.INVISIBLE self._initial_personal_message = None + self._sasl_channel = None + logger.info("Connection to the account %s created" % account) except Exception, e: import traceback @@ -265,9 +272,34 @@ class ButterflyConnection(telepathy.server.Connection, return self.ensure_handle(handle_type, handle_name, contact=contact) def Connect(self): - if self._status == telepathy.CONNECTION_STATUS_DISCONNECTED: - logger.info("Connecting") - self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_NONE_SPECIFIED + if self._status != telepathy.CONNECTION_STATUS_DISCONNECTED: + return + + logger.info("Connecting") + + self.StatusChanged(telepathy.CONNECTION_STATUS_CONNECTING, + telepathy.CONNECTION_STATUS_REASON_REQUESTED) + self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_NONE_SPECIFIED + + def prompt_cb(password, error, message): + if error is not None: + logger.warning('SASL channel failed: %s: %s' % (error, message)) + + self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED + self._disconnected() + else: + self._account = (self._account[0], str(password).encode('utf-8')) + self._msn_client.login(*self._account) + + if self._account[1] is None: + # pop up a SASL channel asking for the password + props = self._generate_props(telepathy.CHANNEL_TYPE_SERVER_AUTHENTICATION, + telepathy.server.NoneHandle(), False) + + self._sasl_channel = ButterflySASLChannel(self, None, props, 'PasswordChannel') + self._sasl_channel.prompt(prompt_cb) + else: + # we have the password already self._msn_client.login(*self._account) def Disconnect(self): @@ -282,6 +314,10 @@ class ButterflyConnection(telepathy.server.Connection, logger.info("Disconnected") self.StatusChanged(telepathy.CONNECTION_STATUS_DISCONNECTED, self.__disconnect_reason) + + if self._sasl_channel is not None: + self._sasl_channel.Close() + self._channel_manager.close() self._manager.disconnected(self) @@ -337,8 +373,8 @@ class ButterflyConnection(telepathy.server.Connection, # papyon.event.ClientEventInterface def on_client_state_changed(self, state): if state == papyon.event.ClientState.CONNECTING: - self.StatusChanged(telepathy.CONNECTION_STATUS_CONNECTING, - telepathy.CONNECTION_STATUS_REASON_REQUESTED) + # connection status is already CONNECTING + pass elif state == papyon.event.ClientState.SYNCHRONIZED: handle = self.ensure_handle(telepathy.HANDLE_TYPE_LIST, 'subscribe') props = self._generate_props(telepathy.CHANNEL_TYPE_CONTACT_LIST, |