2018-07-22 19:12:52 +02:00
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Presence handler
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2018-07-23 23:01:39 +02:00
|
|
|
import nbxmpp
|
|
|
|
|
2018-07-22 19:12:52 +02:00
|
|
|
from gajim.common import app
|
2018-10-04 23:55:35 +02:00
|
|
|
from gajim.common.i18n import _
|
2018-07-22 19:12:52 +02:00
|
|
|
from gajim.common.nec import NetworkEvent
|
2018-07-23 23:01:39 +02:00
|
|
|
from gajim.common.modules.user_nickname import parse_nickname
|
2018-07-22 19:12:52 +02:00
|
|
|
|
|
|
|
log = logging.getLogger('gajim.c.m.presence')
|
|
|
|
|
|
|
|
|
|
|
|
class Presence:
|
|
|
|
def __init__(self, con):
|
|
|
|
self._con = con
|
|
|
|
self._account = con.name
|
|
|
|
|
2018-07-23 23:01:39 +02:00
|
|
|
self.handlers = [
|
|
|
|
('presence', self._presence_received),
|
|
|
|
('presence', self._subscribe_received, 'subscribe'),
|
|
|
|
('presence', self._subscribed_received, 'subscribed'),
|
|
|
|
('presence', self._unsubscribe_received, 'unsubscribe'),
|
|
|
|
('presence', self._unsubscribed_received, 'unsubscribed'),
|
|
|
|
]
|
|
|
|
|
|
|
|
# keep the jids we auto added (transports contacts) to not send the
|
|
|
|
# SUBSCRIBED event to GUI
|
|
|
|
self.automatically_added = []
|
|
|
|
|
|
|
|
# list of jid to auto-authorize
|
|
|
|
self.jids_for_auto_auth = []
|
2018-07-22 19:12:52 +02:00
|
|
|
|
2018-09-11 22:25:55 +02:00
|
|
|
def _presence_received(self, _con, stanza):
|
2018-07-23 23:01:39 +02:00
|
|
|
if stanza.getType() in ('subscribe', 'subscribed',
|
|
|
|
'unsubscribe', 'unsubscribed'):
|
|
|
|
# Dont handle that here
|
|
|
|
return
|
|
|
|
|
2018-07-22 19:12:52 +02:00
|
|
|
log.info('Received from %s', stanza.getFrom())
|
2018-09-09 17:43:17 +02:00
|
|
|
if nbxmpp.isErrorNode(stanza):
|
|
|
|
log.info('Error:\n%s', stanza)
|
|
|
|
|
2018-07-22 19:12:52 +02:00
|
|
|
app.nec.push_incoming_event(
|
|
|
|
NetworkEvent('raw-pres-received',
|
|
|
|
conn=self._con,
|
|
|
|
stanza=stanza))
|
|
|
|
|
2018-09-11 22:25:55 +02:00
|
|
|
def _subscribe_received(self, _con, stanza):
|
2018-07-23 23:01:39 +02:00
|
|
|
from_ = stanza.getFrom()
|
|
|
|
jid = from_.getStripped()
|
|
|
|
fjid = str(from_)
|
|
|
|
status = stanza.getStatus()
|
|
|
|
is_transport = app.jid_is_transport(fjid)
|
|
|
|
auto_auth = app.config.get_per('accounts', self._account, 'autoauth')
|
|
|
|
user_nick = parse_nickname(stanza)
|
|
|
|
|
|
|
|
log.info('Received Subscribe: %s, transport: %s, auto_auth: %s, '
|
|
|
|
'user_nick: %s', from_, is_transport, auto_auth, user_nick)
|
|
|
|
if is_transport and fjid in self._con.agent_registrations:
|
|
|
|
self._con.agent_registrations[fjid]['sub_received'] = True
|
2018-09-11 22:25:55 +02:00
|
|
|
if not self._con.agent_registrations[fjid]['roster_push']:
|
2018-07-23 23:01:39 +02:00
|
|
|
# We'll reply after roster push result
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
|
|
|
if auto_auth or is_transport or jid in self.jids_for_auto_auth:
|
2018-09-01 17:07:30 +02:00
|
|
|
self.send_presence(fjid, 'subscribed')
|
2018-07-23 23:01:39 +02:00
|
|
|
|
|
|
|
if not status:
|
|
|
|
status = _('I would like to add you to my roster.')
|
|
|
|
|
|
|
|
app.nec.push_incoming_event(NetworkEvent(
|
|
|
|
'subscribe-presence-received',
|
|
|
|
conn=self._con,
|
|
|
|
jid=jid,
|
|
|
|
fjid=fjid,
|
|
|
|
status=status,
|
|
|
|
user_nick=user_nick,
|
|
|
|
is_transport=is_transport))
|
|
|
|
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
2018-09-11 22:25:55 +02:00
|
|
|
def _subscribed_received(self, _con, stanza):
|
2018-07-23 23:01:39 +02:00
|
|
|
from_ = stanza.getFrom()
|
|
|
|
jid = from_.getStripped()
|
|
|
|
resource = from_.getResource()
|
|
|
|
log.info('Received Subscribed: %s', from_)
|
|
|
|
if jid in self.automatically_added:
|
|
|
|
self.automatically_added.remove(jid)
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
|
|
|
app.nec.push_incoming_event(NetworkEvent(
|
|
|
|
'subscribed-presence-received',
|
|
|
|
conn=self._con, jid=jid, resource=resource))
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
2018-09-11 22:25:55 +02:00
|
|
|
@staticmethod
|
|
|
|
def _unsubscribe_received(_con, stanza):
|
2018-07-23 23:01:39 +02:00
|
|
|
log.info('Received Unsubscribe: %s', stanza.getFrom())
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
2018-09-11 22:25:55 +02:00
|
|
|
def _unsubscribed_received(self, _con, stanza):
|
2018-07-23 23:01:39 +02:00
|
|
|
from_ = stanza.getFrom()
|
|
|
|
jid = from_.getStripped()
|
|
|
|
log.info('Received Unsubscribed: %s', from_)
|
|
|
|
app.nec.push_incoming_event(NetworkEvent(
|
|
|
|
'unsubscribed-presence-received',
|
|
|
|
conn=self._con, jid=jid))
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
|
|
|
def subscribed(self, jid):
|
|
|
|
if not app.account_is_connected(self._account):
|
|
|
|
return
|
|
|
|
log.info('Subscribed: %s', jid)
|
2018-09-01 17:07:30 +02:00
|
|
|
self.send_presence(jid, 'subscribed')
|
2018-07-23 23:01:39 +02:00
|
|
|
|
|
|
|
def unsubscribed(self, jid):
|
|
|
|
if not app.account_is_connected(self._account):
|
|
|
|
return
|
|
|
|
log.info('Unsubscribed: %s', jid)
|
2018-09-01 17:07:30 +02:00
|
|
|
self.send_presence(jid, 'unsubscribed')
|
2018-07-23 23:01:39 +02:00
|
|
|
|
|
|
|
def unsubscribe(self, jid, remove_auth=True):
|
|
|
|
if not app.account_is_connected(self._account):
|
|
|
|
return
|
|
|
|
if remove_auth:
|
2018-09-12 00:01:54 +02:00
|
|
|
self._con.getRoster().del_item(jid)
|
2018-07-23 23:01:39 +02:00
|
|
|
jid_list = app.config.get_per('contacts')
|
|
|
|
for j in jid_list:
|
|
|
|
if j.startswith(jid):
|
|
|
|
app.config.del_per('contacts', j)
|
|
|
|
else:
|
|
|
|
log.info('Unsubscribe from %s', jid)
|
2018-09-12 00:01:54 +02:00
|
|
|
self._con.getRoster().unsubscribe(jid)
|
|
|
|
self._con.getRoster().set_item(jid)
|
2018-07-23 23:01:39 +02:00
|
|
|
|
2018-09-11 22:25:55 +02:00
|
|
|
def subscribe(self, jid, msg=None, name='', groups=None, auto_auth=False):
|
2018-07-23 23:01:39 +02:00
|
|
|
if not app.account_is_connected(self._account):
|
|
|
|
return
|
|
|
|
if groups is None:
|
|
|
|
groups = []
|
|
|
|
|
|
|
|
log.info('Request Subscription to %s', jid)
|
|
|
|
|
|
|
|
if auto_auth:
|
|
|
|
self.jids_for_auto_auth.append(jid)
|
|
|
|
|
|
|
|
infos = {'jid': jid}
|
|
|
|
if name:
|
|
|
|
infos['name'] = name
|
|
|
|
iq = nbxmpp.Iq('set', nbxmpp.NS_ROSTER)
|
|
|
|
query = iq.setQuery()
|
|
|
|
item = query.addChild('item', attrs=infos)
|
|
|
|
for group in groups:
|
|
|
|
item.addChild('group').setData(group)
|
|
|
|
self._con.connection.send(iq)
|
|
|
|
|
2018-09-09 17:43:17 +02:00
|
|
|
self.send_presence(jid, 'subscribe', status=msg)
|
2018-07-23 23:01:39 +02:00
|
|
|
|
2018-09-01 17:07:30 +02:00
|
|
|
def get_presence(self, to=None, typ=None, priority=None,
|
|
|
|
show=None, status=None, nick=None, caps=True,
|
|
|
|
sign=None, idle_time=None):
|
|
|
|
presence = nbxmpp.Presence(to, typ, priority, show, status)
|
|
|
|
if nick is not None:
|
|
|
|
nick_tag = presence.setTag('nick', namespace=nbxmpp.NS_NICK)
|
|
|
|
nick_tag.setData(nick)
|
|
|
|
|
|
|
|
if sign:
|
|
|
|
presence.setTag(nbxmpp.NS_SIGNED + ' x').setData(sign)
|
|
|
|
|
|
|
|
if idle_time is not None:
|
|
|
|
idle_node = presence.setTag('idle', namespace=nbxmpp.NS_IDLE)
|
|
|
|
idle_node.setAttr('since', idle_time)
|
|
|
|
|
2018-09-01 20:34:56 +02:00
|
|
|
if not self._con.avatar_conversion:
|
|
|
|
# XEP-0398 not supported by server so
|
|
|
|
# we add the avatar sha to our presence
|
|
|
|
self._con.get_module('VCardAvatars').add_update_node(presence)
|
2018-09-01 17:07:30 +02:00
|
|
|
|
|
|
|
if caps:
|
|
|
|
attrs = {'hash': 'sha-1',
|
|
|
|
'node': 'http://gajim.org',
|
|
|
|
'ver': app.caps_hash[self._account]}
|
|
|
|
presence.setTag('c', namespace=nbxmpp.NS_CAPS, attrs=attrs)
|
2018-07-23 23:01:39 +02:00
|
|
|
|
|
|
|
return presence
|
|
|
|
|
2018-09-01 17:07:30 +02:00
|
|
|
def send_presence(self, *args, **kwargs):
|
|
|
|
if not app.account_is_connected(self._account):
|
|
|
|
return
|
|
|
|
presence = self.get_presence(*args, **kwargs)
|
|
|
|
log.debug('Send presence:\n%s', presence)
|
|
|
|
self._con.connection.send(presence)
|
|
|
|
|
2018-07-22 19:12:52 +02:00
|
|
|
|
|
|
|
def parse_show(stanza):
|
|
|
|
show = stanza.getShow()
|
|
|
|
type_ = parse_type(stanza)
|
|
|
|
if show is None and type_ is None:
|
|
|
|
return 'online'
|
|
|
|
|
|
|
|
if type_ == 'unavailable':
|
|
|
|
return 'offline'
|
|
|
|
|
|
|
|
if show not in (None, 'chat', 'away', 'xa', 'dnd'):
|
|
|
|
log.warning('Invalid show element: %s', stanza)
|
|
|
|
if type_ is None:
|
|
|
|
return 'online'
|
|
|
|
return 'offline'
|
|
|
|
|
|
|
|
if show is None:
|
|
|
|
return 'online'
|
|
|
|
return show
|
|
|
|
|
|
|
|
|
|
|
|
def parse_type(stanza):
|
|
|
|
type_ = stanza.getType()
|
|
|
|
if type_ not in (None, 'unavailable', 'error', 'subscribe',
|
|
|
|
'subscribed', 'unsubscribe', 'unsubscribed'):
|
|
|
|
log.warning('Invalid type: %s', stanza)
|
|
|
|
return None
|
|
|
|
return type_
|
|
|
|
|
|
|
|
|
|
|
|
def get_instance(*args, **kwargs):
|
|
|
|
return Presence(*args, **kwargs), 'Presence'
|