Add more convenient get_own_jid() method

This commit is contained in:
Philipp Hörist 2017-07-23 09:55:13 +02:00
parent 85d220c80e
commit 06d890eea7
3 changed files with 17 additions and 6 deletions

View File

@ -753,6 +753,21 @@ 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):
"""
Return our own jid as JID
If full = True, this raises an exception if we cant provide
the full JID
"""
if self.connection:
full_jid = self.connection._registered_name
return nbxmpp.JID(full_jid)
else:
if full:
raise exceptions.GajimGeneralException(
'We are not connected, full JID unknown.')
return nbxmpp.JID(gajim.get_jid_from_account(self.name))
def reconnect(self): def reconnect(self):
# Do not try to reco while we are already trying # Do not try to reco while we are already trying
self.time_to_reconnect = None self.time_to_reconnect = None

View File

@ -1131,8 +1131,7 @@ class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
self.encrypted = False self.encrypted = False
account = self.conn.name account = self.conn.name
our_full_jid = gajim.get_jid_from_account(account, full=True) if self.stanza.getFrom() == self.conn.get_own_jid(full=True):
if self.stanza.getFrom() == our_full_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

View File

@ -420,16 +420,13 @@ def jid_is_transport(jid):
return True return True
return False return False
def get_jid_from_account(account_name, full=False): def get_jid_from_account(account_name):
""" """
Return the jid we use in the given account Return the jid we use in the given account
""" """
name = config.get_per('accounts', account_name, 'name') name = config.get_per('accounts', account_name, 'name')
hostname = config.get_per('accounts', account_name, 'hostname') hostname = config.get_per('accounts', account_name, 'hostname')
jid = name + '@' + hostname jid = name + '@' + hostname
if full:
resource = connections[account_name].server_resource
jid += '/' + resource
return jid return jid
def get_our_jids(): def get_our_jids():