annotate src/plugins/plugin_xep_0334.py @ 2160:e67e8cd24141

core (tools/common): data objects first draft: this module aims is to help manipulate complex data from bridge, mainly for the template system. It is in common and not only in frontends as it may be used in some case by backend, if it needs to use template system in the future.
author Goffi <goffi@goffi.org>
date Tue, 21 Feb 2017 21:01:40 +0100
parents 33c8c4973743
children 8b37a62336c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
3
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT plugin for Delayed Delivery (XEP-0334)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
7
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
12
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
17
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
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
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
22 from sat.core.log import getLogger
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
23 log = getLogger(__name__)
2132
c0577837680a core: replaced SkipHistory exception by a key in mess_data:
Goffi <goffi@goffi.org>
parents: 2131
diff changeset
24 from sat.core.constants import Const as C
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
25
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
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
27
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
28 from wokkel import disco, iwokkel
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
29 try:
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
30 from twisted.words.protocols.xmlstream import XMPPHandler
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
31 except ImportError:
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
32 from wokkel.subprotocols import XMPPHandler
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
33 from zope.interface import implements
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
34 from textwrap import dedent
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
35
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
36
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
37 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
38 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
39 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
40 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
41 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
42 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
43 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
44 C.PI_DESCRIPTION: D_(u"""Implementation of Message Processing Hints"""),
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
45 C.PI_USAGE: dedent(D_(u"""\
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
46 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
47 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
48 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
49
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
50 }
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
51
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
52 NS_HINTS = u'urn:xmpp:hints'
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
53
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
54
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
55 class XEP_0334(object):
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
56 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
57 HINT_NO_STORE = u'no-store'
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
58 HINT_NO_COPY = u'no-copy'
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
59 HINT_STORE = u'store'
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
60 HINTS = (HINT_NO_PERMANENT_STORE, HINT_NO_STORE, HINT_NO_COPY, HINT_STORE)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
61
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
62 def __init__(self, host):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
63 log.info(_("Message Processing Hints plugin initialization"))
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
64 self.host = host
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2132
diff changeset
65 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
66 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
67
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2132
diff changeset
68 def getHandler(self, client):
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
69 return XEP_0334_handler()
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
70
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
71 def addHint(self, mess_data, hint):
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
72 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
73 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
74 return
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
75 hints = mess_data.setdefault('hints', set())
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
76 if hint in self.HINTS:
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
77 hints.add(hint)
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
78 else:
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
79 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
80
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
81 def _sendPostXmlTreatment(self, mess_data):
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
82 if 'hints' in mess_data:
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
83 for hint in mess_data['hints']:
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
84 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
85 return mess_data
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
86
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2132
diff changeset
87 def sendMessageTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments):
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
88 """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
89 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
90 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
91 self.addHint(hint)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
92
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
93 post_xml_treatments.addCallback(self._sendPostXmlTreatment)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
94 return True
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
95
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
96 def _receivedSkipHistory(self, mess_data):
2132
c0577837680a core: replaced SkipHistory exception by a key in mess_data:
Goffi <goffi@goffi.org>
parents: 2131
diff changeset
97 mess_data[u'history'] = C.HISTORY_SKIP
c0577837680a core: replaced SkipHistory exception by a key in mess_data:
Goffi <goffi@goffi.org>
parents: 2131
diff changeset
98 return mess_data
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
99
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
100 def messageReceivedTrigger(self, client, message_elt, post_treat):
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
101 """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
102 for elt in message_elt.elements():
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
103 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
104 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
105 post_treat.addCallback(self._receivedSkipHistory)
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
106 break
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
107 return True
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
108
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
109
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
110 class XEP_0334_handler(XMPPHandler):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
111 implements(iwokkel.IDisco)
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
112
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
113 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
114 return [disco.DiscoFeature(NS_HINTS)]
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
115
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
116 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
117 return []