2018-07-03 00:10:55 +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/>.
|
|
|
|
|
|
|
|
# XEP-0163: Personal Eventing Protocol
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
from typing import Any
|
|
|
|
from typing import Dict
|
|
|
|
from typing import List
|
|
|
|
from typing import Tuple
|
|
|
|
|
2018-07-03 00:10:55 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import nbxmpp
|
|
|
|
|
|
|
|
from gajim.common import app
|
2018-07-04 22:55:16 +02:00
|
|
|
from gajim.common.exceptions import StanzaMalformed
|
2018-07-03 00:10:55 +02:00
|
|
|
from gajim.common.nec import NetworkIncomingEvent
|
|
|
|
from gajim.common.const import PEPHandlerType, PEPEventType
|
2018-09-13 23:56:12 +02:00
|
|
|
from gajim.common.types import ConnectionT
|
|
|
|
from gajim.common.types import PEPHandlersDict # pylint: disable=unused-import
|
|
|
|
from gajim.common.types import PEPNotifyCallback
|
|
|
|
from gajim.common.types import PEPRetractCallback
|
2018-07-03 00:10:55 +02:00
|
|
|
|
|
|
|
log = logging.getLogger('gajim.c.m.pep')
|
|
|
|
|
|
|
|
|
|
|
|
class PEP:
|
2018-09-13 23:56:12 +02:00
|
|
|
def __init__(self, con: ConnectionT) -> None:
|
2018-07-03 00:10:55 +02:00
|
|
|
self._con = con
|
|
|
|
self._account = con.name
|
|
|
|
|
|
|
|
self.handlers = [
|
|
|
|
('message', self._pep_event_received,
|
|
|
|
'headline', nbxmpp.NS_PUBSUB_EVENT)
|
|
|
|
]
|
|
|
|
|
2018-07-22 12:18:24 +02:00
|
|
|
self.supported = False
|
2018-09-13 23:56:12 +02:00
|
|
|
self._pep_handlers = {} # type: PEPHandlersDict
|
|
|
|
self._store_publish_modules = [] # type: List[Any]
|
|
|
|
|
|
|
|
def pass_disco(self,
|
|
|
|
from_: nbxmpp.JID,
|
|
|
|
identities: List[Dict[str, str]],
|
|
|
|
_features: List[str],
|
|
|
|
_data: List[nbxmpp.DataForm],
|
|
|
|
_node: str) -> None:
|
2018-07-22 12:18:24 +02:00
|
|
|
for identity in identities:
|
|
|
|
if identity['category'] == 'pubsub':
|
|
|
|
if identity.get('type') == 'pep':
|
|
|
|
log.info('Discovered PEP support: %s', from_)
|
|
|
|
self.supported = True
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def register_pep_handler(
|
|
|
|
self,
|
|
|
|
namespace: str,
|
|
|
|
notify_handler: PEPNotifyCallback,
|
|
|
|
retract_handler: PEPRetractCallback) -> None:
|
2018-07-03 00:10:55 +02:00
|
|
|
if namespace in self._pep_handlers:
|
|
|
|
self._pep_handlers[namespace].append(
|
|
|
|
(notify_handler, retract_handler))
|
|
|
|
else:
|
|
|
|
self._pep_handlers[namespace] = [(notify_handler, retract_handler)]
|
|
|
|
if notify_handler:
|
2018-09-13 23:56:12 +02:00
|
|
|
module_instance = notify_handler.__self__ # type: ignore
|
2018-07-04 22:55:16 +02:00
|
|
|
if module_instance.store_publish:
|
2018-07-03 00:10:55 +02:00
|
|
|
if module_instance not in self._store_publish_modules:
|
|
|
|
self._store_publish_modules.append(module_instance)
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def _pep_event_received(self,
|
|
|
|
_con: ConnectionT,
|
|
|
|
stanza: nbxmpp.Message) -> None:
|
2018-07-03 00:10:55 +02:00
|
|
|
jid = stanza.getFrom()
|
|
|
|
event = stanza.getTag('event', namespace=nbxmpp.NS_PUBSUB_EVENT)
|
|
|
|
items = event.getTag('items')
|
|
|
|
if items is None:
|
|
|
|
log.warning('Malformed PEP event (no items node): %s', stanza)
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
|
|
|
namespace = items.getAttr('node')
|
|
|
|
if namespace is None:
|
|
|
|
log.warning('Malformed PEP event (no node attr): %s', stanza)
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
|
|
|
log.info('PEP notification received: %s %s', jid, namespace)
|
|
|
|
|
|
|
|
handlers = self._pep_handlers.get(namespace, None)
|
|
|
|
if handlers is None:
|
|
|
|
# Old Fallback
|
|
|
|
from gajim.common.connection_handlers_events import PEPReceivedEvent as OldPEPReceivedEvent
|
|
|
|
app.nec.push_incoming_event(
|
|
|
|
OldPEPReceivedEvent(None, conn=self._con, stanza=stanza))
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
else:
|
|
|
|
# Check if this is a retraction
|
|
|
|
retract = items.getTag('retract')
|
|
|
|
if retract is not None:
|
|
|
|
for handler in handlers:
|
|
|
|
handler[PEPHandlerType.RETRACT](jid, retract.getID())
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
|
|
|
# Check if we have items
|
|
|
|
items_ = items.getTags('item')
|
|
|
|
if items_ is None:
|
|
|
|
log.warning('Malformed PEP event received: %s', stanza)
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
for handler in handlers:
|
|
|
|
handler[PEPHandlerType.NOTIFY](jid, items_[0])
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def send_stored_publish(self) -> None:
|
2018-07-03 00:10:55 +02:00
|
|
|
for module in self._store_publish_modules:
|
|
|
|
module.send_stored_publish()
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def reset_stored_publish(self) -> None:
|
2018-07-03 00:10:55 +02:00
|
|
|
for module in self._store_publish_modules:
|
|
|
|
module.reset_stored_publish()
|
|
|
|
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
class AbstractPEPData:
|
|
|
|
|
|
|
|
type_ = PEPEventType.ABSTRACT
|
|
|
|
|
|
|
|
def __init__(self, data: Any) -> None:
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def as_markup_text(self) -> str: # pylint: disable=no-self-use
|
|
|
|
'''SHOULD be implemented by subclasses'''
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def __eq__(self, other: Any) -> bool:
|
|
|
|
return other == self.type_
|
|
|
|
|
|
|
|
def __bool__(self) -> bool:
|
|
|
|
return self.data is not None
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return str(self.data)
|
|
|
|
|
|
|
|
|
2018-07-04 22:55:16 +02:00
|
|
|
class AbstractPEPModule:
|
2018-09-13 23:56:12 +02:00
|
|
|
|
|
|
|
name = ''
|
|
|
|
namespace = ''
|
|
|
|
pep_class = AbstractPEPData
|
|
|
|
store_publish = True
|
|
|
|
_log = log
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
con: ConnectionT,
|
|
|
|
account: str) -> None:
|
2018-07-04 22:55:16 +02:00
|
|
|
self._account = account
|
|
|
|
self._con = con
|
2018-07-03 00:10:55 +02:00
|
|
|
|
2018-07-04 22:55:16 +02:00
|
|
|
self._stored_publish = None
|
2018-07-03 00:10:55 +02:00
|
|
|
|
2018-07-04 22:55:16 +02:00
|
|
|
self._con.get_module('PEP').register_pep_handler(
|
|
|
|
self.namespace,
|
|
|
|
self._pep_notify_received,
|
|
|
|
self._pep_retract_received)
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def _pep_notify_received(self, jid: nbxmpp.JID, item: nbxmpp.Node) -> None:
|
2018-07-04 22:55:16 +02:00
|
|
|
try:
|
|
|
|
data = self._extract_info(item)
|
|
|
|
except StanzaMalformed as error:
|
|
|
|
log.warning('%s, %s: %s', jid, error, item)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._log.info('Received: %s %s', jid, data)
|
|
|
|
self._push_event(jid, self.pep_class(data))
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def _pep_retract_received(self, jid: nbxmpp.JID, id_: str) -> None:
|
2018-07-04 22:55:16 +02:00
|
|
|
self._log.info('Retract: %s %s', jid, id_)
|
|
|
|
self._push_event(jid, self.pep_class(None))
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def _extract_info(self, item: nbxmpp.Node) -> Any:
|
2018-07-04 22:55:16 +02:00
|
|
|
'''To be implemented by subclasses'''
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def _build_node(self, data: Any) -> nbxmpp.Node:
|
2018-07-04 22:55:16 +02:00
|
|
|
'''To be implemented by subclasses'''
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def _push_event(self, jid: nbxmpp.JID, user_pep: Any) -> None:
|
2018-07-06 20:12:50 +02:00
|
|
|
self._notification_received(jid, user_pep)
|
2018-07-04 22:55:16 +02:00
|
|
|
app.nec.push_incoming_event(
|
|
|
|
PEPReceivedEvent(None, conn=self._con,
|
|
|
|
jid=str(jid),
|
|
|
|
pep_type=self.name,
|
|
|
|
user_pep=user_pep))
|
2018-07-03 00:10:55 +02:00
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def _notification_received(self, jid: nbxmpp.JID, user_pep: Any) -> None:
|
2018-07-04 22:55:16 +02:00
|
|
|
for contact in app.contacts.get_contacts(self._account, str(jid)):
|
2018-07-03 00:10:55 +02:00
|
|
|
if user_pep:
|
|
|
|
contact.pep[self.name] = user_pep
|
|
|
|
else:
|
|
|
|
contact.pep.pop(self.name, None)
|
|
|
|
|
2018-07-04 22:55:16 +02:00
|
|
|
if jid == self._con.get_own_jid().getStripped():
|
2018-07-03 00:10:55 +02:00
|
|
|
if user_pep:
|
2018-07-04 22:55:16 +02:00
|
|
|
self._con.pep[self.name] = user_pep
|
2018-07-03 00:10:55 +02:00
|
|
|
else:
|
2018-07-04 22:55:16 +02:00
|
|
|
self._con.pep.pop(self.name, None)
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def send_stored_publish(self) -> None:
|
2018-07-04 22:55:16 +02:00
|
|
|
if self._stored_publish is not None:
|
|
|
|
self._log.info('Send stored publish')
|
2018-07-05 00:28:09 +02:00
|
|
|
self.send(self._stored_publish)
|
2018-07-04 22:55:16 +02:00
|
|
|
self._stored_publish = None
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def reset_stored_publish(self) -> None:
|
2018-07-04 22:55:16 +02:00
|
|
|
self._log.info('Reset stored publish')
|
|
|
|
self._stored_publish = None
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def send(self, data: Any) -> None:
|
2018-07-22 12:18:24 +02:00
|
|
|
if not self._con.get_module('PEP').supported:
|
2018-07-04 22:55:16 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if self._con.connected == 1:
|
|
|
|
# We are connecting, save activity and send it later
|
|
|
|
self._stored_publish = data
|
|
|
|
return
|
|
|
|
|
|
|
|
if data:
|
|
|
|
self._log.info('Send: %s', data)
|
|
|
|
else:
|
|
|
|
self._log.info('Remove')
|
|
|
|
|
|
|
|
item = self._build_node(data)
|
|
|
|
|
|
|
|
self._con.get_module('PubSub').send_pb_publish(
|
|
|
|
'', self.namespace, item, 'current')
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def retract(self) -> None:
|
2018-07-22 12:18:24 +02:00
|
|
|
if not self._con.get_module('PEP').supported:
|
2018-07-04 22:55:16 +02:00
|
|
|
return
|
|
|
|
self.send(None)
|
|
|
|
self._con.get_module('PubSub').send_pb_retract(
|
|
|
|
'', self.namespace, 'current')
|
2018-07-03 00:10:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PEPReceivedEvent(NetworkIncomingEvent):
|
|
|
|
name = 'pep-received'
|
2018-07-07 13:52:44 +02:00
|
|
|
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def get_instance(*args: Any, **kwargs: Any) -> Tuple[PEP, str]:
|
2018-07-07 13:52:44 +02:00
|
|
|
return PEP(*args, **kwargs), 'PEP'
|