Improve getting own jid

We get our full JID on the bind event.
After that it is saved in the `registered_name` attr on the Connection
Object.

In case the bind never occured we get the bare JID from config.
This commit is contained in:
Philipp Hörist 2017-07-27 18:55:13 +02:00
parent 914935f864
commit 1e313cc7e2
2 changed files with 11 additions and 11 deletions

View File

@ -671,6 +671,8 @@ class Connection(CommonConnection, ConnectionHandlers):
self.try_connecting_for_foo_secs = 45 self.try_connecting_for_foo_secs = 45
# holds the actual hostname to which we are connected # holds the actual hostname to which we are connected
self.connected_hostname = None self.connected_hostname = None
# Holds the full jid we received on the bind event
self.registered_name = None
self.redirected = None self.redirected = None
self.last_time_to_reconnect = None self.last_time_to_reconnect = None
self.new_account_info = None self.new_account_info = None
@ -754,19 +756,16 @@ class Connection(CommonConnection, ConnectionHandlers):
def check_jid(self, jid): def check_jid(self, jid):
return helpers.parse_jid(jid) return helpers.parse_jid(jid)
def get_own_jid(self, full=False): def get_own_jid(self):
""" """
Return our own jid as JID Return the last full JID we received on a bind event.
If full = True, this raises an exception if we cant provide In case we were never connected it returns the bare JID from config.
the full JID
""" """
if self.connection: if self.registered_name:
full_jid = self.connection._registered_name # This returns the full jid we received on the bind event
return nbxmpp.JID(full_jid) return self.registered_name
else: else:
if full: # This returns the bare jid
raise exceptions.GajimGeneralException(
'We are not connected, full JID unknown.')
return nbxmpp.JID(gajim.get_jid_from_account(self.name)) return nbxmpp.JID(gajim.get_jid_from_account(self.name))
def reconnect(self): def reconnect(self):
@ -1465,6 +1464,7 @@ class Connection(CommonConnection, ConnectionHandlers):
return return
if hasattr(con, 'Resource'): if hasattr(con, 'Resource'):
self.server_resource = con.Resource self.server_resource = con.Resource
self.registered_name = con._registered_name
if gajim.config.get_per('accounts', self.name, 'anonymous_auth'): if gajim.config.get_per('accounts', self.name, 'anonymous_auth'):
# Get jid given by server # Get jid given by server
old_jid = gajim.get_jid_from_account(self.name) old_jid = gajim.get_jid_from_account(self.name)

View File

@ -1146,7 +1146,7 @@ class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
self.encrypted = False self.encrypted = False
account = self.conn.name account = self.conn.name
if self.stanza.getFrom() == self.conn.get_own_jid(full=True): if self.stanza.getFrom() == self.conn.get_own_jid():
# Drop messages sent from our own full jid # Drop messages sent from our own full jid
# It can happen that when we sent message to our own bare jid # It can happen that when we sent message to our own bare jid
# that the server routes that message back to us # that the server routes that message back to us