annotate src/plugins/plugin_xep_0334.py @ 1992:d5befe7253aa

plugin XEP-0045: fixed typo in text commands
author Goffi <goffi@goffi.org>
date Sat, 02 Jul 2016 20:55:31 +0200
parents 633b5c21aefd
children 628c1c95f442
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
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
21 from sat.core.i18n import _
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__)
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
24
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
25 from sat.core import exceptions
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
26
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
27 from wokkel import disco, iwokkel
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
28 try:
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
29 from twisted.words.protocols.xmlstream import XMPPHandler
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
30 except ImportError:
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
31 from wokkel.subprotocols import XMPPHandler
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
32 from twisted.python import failure
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
33 from zope.interface import implements
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
34
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 NS_MPH = 'urn:xmpp:hints'
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
37
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
38 PLUGIN_INFO = {
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
39 "name": "Message Processing Hints",
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
40 "import_name": "XEP-0334",
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
41 "type": "XEP",
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
42 "protocols": ["XEP-0334"],
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
43 "main": "XEP_0334",
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
44 "handler": "yes",
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
45 "description": _("""Implementation of Message Processing Hints""")
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
46 }
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
47
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
48
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
49 class XEP_0334(object):
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 def __init__(self, host):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
52 log.info(_("Message Processing Hints plugin initialization"))
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
53 self.host = host
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
54 host.trigger.add("messageSend", self.messageSendTrigger)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
55 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
56
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
57 def getHandler(self, profile):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
58 return XEP_0334_handler(self, profile)
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
59
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
60 def messageSendTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments):
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
61 """Add the hints element to the message to be sent"""
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
62 hints = []
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
63 for key in ('no-permanent-storage', 'no-storage', 'no-copy'):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
64 if mess_data['extra'].get(key, None):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
65 hints.append(key)
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
66
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
67 def treatment(mess_data):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
68 message = mess_data['xml']
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
69 for key in hints:
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
70 message.addElement((NS_MPH, key))
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
71 if key in ('no-permanent-storage', 'no-storage'):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
72 mess_data['extra']['no_storage'] = True
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
73 # TODO: the core doesn't process this 'no_storage' info yet
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
74 # it will be added after the frontends refactorization
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
75 return mess_data
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
76
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
77 if hints:
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
78 post_xml_treatments.addCallback(treatment)
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
79 return True
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
80
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
81 def messageReceivedTrigger(self, client, message, post_treat):
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
82 """Check for hints in the received message"""
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
83 hints = []
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
84 for key in ('no-permanent-storage', 'no-storage'):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
85 try:
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
86 message.elements(uri=NS_MPH, name=key).next()
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
87 hints.append(key)
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
88 except StopIteration:
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
89 pass
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
90
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
91 def post_treat_hints(data):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
92 raise failure.Failure(exceptions.SkipHistory())
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
93
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
94 if hints:
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
95 post_treat.addCallback(post_treat_hints)
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
96 return True
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
97
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
98
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
99 class XEP_0334_handler(XMPPHandler):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
100 implements(iwokkel.IDisco)
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
101
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
102 def __init__(self, plugin_parent, profile):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
103 self.plugin_parent = plugin_parent
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
104 self.host = plugin_parent.host
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
105 self.profile = profile
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
106
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
107 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
108 return [disco.DiscoFeature(NS_MPH)]
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 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
111 return []