Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0334.py @ 2131:628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
- wrong elements were used in this plugging, it could not work properly
- use constants for hints
- add addHint method so other plugins can add a hint easily
- hints can be added by frontends in mess_data['extra']['hints'] (serialized dict) or by other plugins using addHint
- if history is skipped on message reception, mess_data['extra']['history']['skipped'] is set
- introduced PLUGIN_INFO['usage'] for usage information for developers
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 05 Feb 2017 14:55:19 +0100 |
parents | 633b5c21aefd |
children | c0577837680a |
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) | |
1766 | 5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
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 |
23 log = getLogger(__name__) | |
24 | |
25 from sat.core import exceptions | |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
26 from sat.tools.common import data_format |
1279 | 27 |
28 from wokkel import disco, iwokkel | |
29 try: | |
30 from twisted.words.protocols.xmlstream import XMPPHandler | |
31 except ImportError: | |
32 from wokkel.subprotocols import XMPPHandler | |
33 from twisted.python import failure | |
34 from zope.interface import implements | |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
35 from textwrap import dedent |
1279 | 36 |
37 | |
38 PLUGIN_INFO = { | |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
39 "name": u"Message Processing Hints", |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
40 "import_name": u"XEP-0334", |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
41 "type": u"XEP", |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
42 "protocols": [u"XEP-0334"], |
1279 | 43 "main": "XEP_0334", |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
44 "handler": u"yes", |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
45 "description": D_(u"""Implementation of Message Processing Hints"""), |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
46 "usage": dedent(D_(u"""\ |
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]). |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
49 Will set mess_data['extra']['history'] to 'skipped' when no store is requested and message is not saved in history.""")) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
50 |
1279 | 51 } |
52 | |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
53 NS_HINTS = u'urn:xmpp:hints' |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
54 |
1279 | 55 |
56 class XEP_0334(object): | |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
57 HINT_NO_PERMANENT_STORE = u'no-permanent-store' |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
58 HINT_NO_STORE = u'no-store' |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
59 HINT_NO_COPY = u'no-copy' |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
60 HINT_STORE = u'store' |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
61 HINTS = (HINT_NO_PERMANENT_STORE, HINT_NO_STORE, HINT_NO_COPY, HINT_STORE) |
1279 | 62 |
63 def __init__(self, host): | |
64 log.info(_("Message Processing Hints plugin initialization")) | |
65 self.host = host | |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
66 host.trigger.add("messageSend", self.messageSendTrigger) |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
67 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000) |
1279 | 68 |
69 def getHandler(self, profile): | |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
70 return XEP_0334_handler() |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
71 |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
72 def addHint(self, mess_data, hint): |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
73 if hint == self.HINT_NO_COPY and not mess_data['to'].resource: |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
74 log.error(u"{hint} can only be used with full jids! Ignoring it.".format(hint=hint)) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
75 return |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
76 hints = mess_data.setdefault('hints', set()) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
77 if hint in self.HINTS: |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
78 hints.add(hint) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
79 else: |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
80 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
|
81 |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
82 def _sendPostXmlTreatment(self, mess_data): |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
83 if 'hints' in mess_data: |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
84 for hint in mess_data['hints']: |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
85 mess_data[u'xml'].addElement((NS_HINTS, hint)) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
86 return mess_data |
1279 | 87 |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
88 def messageSendTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments): |
1279 | 89 """Add the hints element to the message to be sent""" |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
90 if u'hints' in mess_data[u'extra']: |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
91 for hint in data_format.dict2iter(u'hints', mess_data[u'extra'], pop=True): |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
92 self.addHint(hint) |
1279 | 93 |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
94 post_xml_treatments.addCallback(self._sendPostXmlTreatment) |
1279 | 95 return True |
96 | |
2131
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
97 def _receivedSkipHistory(self, mess_data): |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
98 mess_data[u'extra'][u'history'] == u'skipped' |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
99 raise failure.Failure(exceptions.SkipHistory()) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
100 |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
101 |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
102 def messageReceivedTrigger(self, client, message_elt, post_treat): |
1279 | 103 """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
|
104 for elt in message_elt.elements(): |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
105 if elt.uri == NS_HINTS and elt.name in (self.HINT_NO_PERMANENT_STORE, self.HINT_NO_STORE): |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
106 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
|
107 post_treat.addCallback(self._receivedSkipHistory) |
628c1c95f442
plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
108 break |
1279 | 109 return True |
110 | |
111 | |
112 class XEP_0334_handler(XMPPHandler): | |
113 implements(iwokkel.IDisco) | |
114 | |
115 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
|
116 return [disco.DiscoFeature(NS_HINTS)] |
1279 | 117 |
118 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
119 return [] |