comparison sat/plugins/plugin_xep_0359.py @ 2696:5849dcaab99d

plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
author Goffi <goffi@goffi.org>
date Sat, 01 Dec 2018 09:57:25 +0100
parents
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2695:c5543fba97e8 2696:5849dcaab99d
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for Message Archive Management (XEP-0359)
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 from sat.core.constants import Const as C
22 from sat.core import exceptions
23 from sat.core.i18n import _
24 from sat.core.log import getLogger
25 from twisted.words.protocols.jabber import xmlstream
26 from zope.interface import implements
27 from wokkel import disco
28
29 log = getLogger(__name__)
30
31
32 PLUGIN_INFO = {
33 C.PI_NAME: u"Unique and Stable Stanza IDs",
34 C.PI_IMPORT_NAME: u"XEP-0359",
35 C.PI_TYPE: u"XEP",
36 C.PI_PROTOCOLS: [u"XEP-0359"],
37 C.PI_MAIN: u"XEP_0359",
38 C.PI_HANDLER: u"yes",
39 C.PI_DESCRIPTION: _(u"""Implementation of Unique and Stable Stanza IDs"""),
40 }
41
42 NS_SID = u"urn:xmpp:sid:0"
43
44
45 class XEP_0359(object):
46
47 def __init__(self, host):
48 log.info(_(u"Unique and Stable Stanza IDs plugin initialization"))
49 self.host = host
50 host.registerNamespace(u"stanza_id", NS_SID)
51 host.trigger.add(u"message_parse", self._message_parseTrigger)
52
53 def _message_parseTrigger(self, client, message_elt, mess_data):
54 """Check if message has a stanza-id"""
55 stanza_id = self.getStanzaId(message_elt, client.jid.userhostJID())
56 if stanza_id is not None:
57 mess_data[u'extra'][u'stanza_id'] = stanza_id
58 return True
59
60 def getStanzaId(self, element, by):
61 """Return stanza-id if found in element
62
63 @param element(domish.Element): element to parse
64 @param by(jid.JID): entity which should have set a stanza-id
65 @return (unicode, None): stanza-id if found
66 """
67 stanza_id = None
68 for stanza_elt in element.elements(NS_SID, u"stanza-id"):
69 if stanza_elt.getAttribute(u"by") == by.full():
70 if stanza_id is not None:
71 # we must not have more than one element (§3 #4)
72 raise exceptions.DataError(
73 u"More than one corresponding stanza-id found!")
74 stanza_id = stanza_elt.getAttribute(u"id")
75 # we don't break to be sure that there is no more than one element
76 # with this "by" attribute
77
78 return stanza_id
79
80 def addStanzaId(self, client, element, stanza_id, by=None):
81 """Add a <stanza-id/> to a stanza
82
83 @param element(domish.Element): stanza where the <stanza-id/> must be added
84 @param stanza_id(unicode): id to use
85 @param by(jid.JID, None): jid to use or None to use client.jid
86 """
87 sid_elt = element.addElement((NS_SID, u"stanza-id"))
88 sid_elt[u"by"] = client.jid.userhost() if by is None else by.userhost()
89 sid_elt[u"id"] = stanza_id
90
91 def getHandler(self, client):
92 return XEP_0359_handler()
93
94
95 class XEP_0359_handler(xmlstream.XMPPHandler):
96 implements(disco.IDisco)
97
98 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
99 return [disco.DiscoFeature(NS_SID)]
100
101 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
102 return []