Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0334.py @ 2730:b4c0a5bec729
quick frontend(chat): use unicode on jid.JID instance in self.host.bridge.messageEncryptionGet:
jid.JID is causing trouble when going trough bridge (with pb bridge), this patch avoid it by using unicode.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 27 Dec 2018 11:40:04 +0100 |
parents | 5060cbeec01e |
children | 003b8b4b56a7 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
1279 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # SAT plugin for Delayed Delivery (XEP-0334) | |
2483 | 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
1766 | 6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) |
1279 | 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 | |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
21 from sat.core.i18n import _, D_ |
1279 | 22 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
23 |
1279 | 24 log = getLogger(__name__) |
2132
c0577837680a
core: replaced SkipHistory exception by a key in mess_data:
Goffi <goffi@goffi.org>
parents:
2131
diff
changeset
|
25 from sat.core.constants import Const as C |
1279 | 26 |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
27 from sat.tools.common import data_format |
1279 | 28 |
29 from wokkel import disco, iwokkel | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
30 |
2698 | 31 from twisted.words.protocols.jabber import xmlstream |
1279 | 32 from zope.interface import implements |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
33 from textwrap import dedent |
1279 | 34 |
35 | |
36 PLUGIN_INFO = { | |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
37 C.PI_NAME: u"Message Processing Hints", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
38 C.PI_IMPORT_NAME: u"XEP-0334", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
39 C.PI_TYPE: u"XEP", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
40 C.PI_PROTOCOLS: [u"XEP-0334"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
41 C.PI_MAIN: "XEP_0334", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
42 C.PI_HANDLER: u"yes", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
43 C.PI_DESCRIPTION: D_(u"""Implementation of Message Processing Hints"""), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
44 C.PI_USAGE: dedent( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
45 D_( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
46 u"""\ |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
47 Frontends can use HINT_* constants in mess_data['extra'] in a serialized 'hints' dict. |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
48 Internal plugins can use directly addHint([HINT_* constant]). |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
49 Will set mess_data['extra']['history'] to 'skipped' when no store is requested and message is not saved in history.""" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
50 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 ), |
1279 | 52 } |
53 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 NS_HINTS = u"urn:xmpp:hints" |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
55 |
1279 | 56 |
57 class XEP_0334(object): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
58 HINT_NO_PERMANENT_STORE = u"no-permanent-store" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 HINT_NO_STORE = u"no-store" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
60 HINT_NO_COPY = u"no-copy" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
61 HINT_STORE = u"store" |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
62 HINTS = (HINT_NO_PERMANENT_STORE, HINT_NO_STORE, HINT_NO_COPY, HINT_STORE) |
1279 | 63 |
64 def __init__(self, host): | |
65 log.info(_("Message Processing Hints plugin initialization")) | |
66 self.host = host | |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2132
diff
changeset
|
67 host.trigger.add("sendMessage", self.sendMessageTrigger) |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
68 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000) |
1279 | 69 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2132
diff
changeset
|
70 def getHandler(self, client): |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
71 return XEP_0334_handler() |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
72 |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
73 def addHint(self, mess_data, hint): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 if hint == self.HINT_NO_COPY and not mess_data["to"].resource: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 log.error( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 u"{hint} can only be used with full jids! Ignoring it.".format(hint=hint) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 ) |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
78 return |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 hints = mess_data.setdefault("hints", set()) |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
80 if hint in self.HINTS: |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
81 hints.add(hint) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
82 else: |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
83 log.error(u"Unknown hint: {}".format(hint)) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
84 |
2652
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
85 def addHintElements(self, message_elt, hints): |
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
86 """Add hints elements to message stanza |
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
87 |
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
88 @param message_elt(domish.Element): stanza where hints must be added |
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
89 @param hints(iterable(unicode)): hints to add |
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
90 """ |
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
91 for hint in hints: |
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
92 message_elt.addElement((NS_HINTS, hint)) |
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
93 |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
94 def _sendPostXmlTreatment(self, mess_data): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
95 if "hints" in mess_data: |
2652
baccc27d5c5c
plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
96 self.addHintElements(mess_data[u"xml"], mess_data[u"hints"]) |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
97 return mess_data |
1279 | 98 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 def sendMessageTrigger( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 self, client, mess_data, pre_xml_treatments, post_xml_treatments |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 ): |
1279 | 102 """Add the hints element to the message to be sent""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 if u"hints" in mess_data[u"extra"]: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 for hint in data_format.dict2iter(u"hints", mess_data[u"extra"], pop=True): |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
105 self.addHint(hint) |
1279 | 106 |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
107 post_xml_treatments.addCallback(self._sendPostXmlTreatment) |
1279 | 108 return True |
109 | |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
110 def _receivedSkipHistory(self, mess_data): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
111 mess_data[u"history"] = C.HISTORY_SKIP |
2132
c0577837680a
core: replaced SkipHistory exception by a key in mess_data:
Goffi <goffi@goffi.org>
parents:
2131
diff
changeset
|
112 return mess_data |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
113 |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
114 def messageReceivedTrigger(self, client, message_elt, post_treat): |
1279 | 115 """Check for hints in the received message""" |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
116 for elt in message_elt.elements(): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
117 if elt.uri == NS_HINTS and elt.name in ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 self.HINT_NO_PERMANENT_STORE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
119 self.HINT_NO_STORE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 ): |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
121 log.debug(u"history will be skipped for this message, as requested") |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
122 post_treat.addCallback(self._receivedSkipHistory) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
123 break |
1279 | 124 return True |
125 | |
126 | |
2698 | 127 class XEP_0334_handler(xmlstream.XMPPHandler): |
1279 | 128 implements(iwokkel.IDisco) |
129 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
130 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
131 return [disco.DiscoFeature(NS_HINTS)] |
1279 | 132 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
133 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
1279 | 134 return [] |