annotate src/plugins/plugin_xep_0203.py @ 1900:d1615f79dfe8

core (tools/config): fixed fixConfigOption: C.DEFAULT_CONFIG was used so the default values were written if not set in the config file used, resulting in a overriding of values set in config files of higher level (e.g. ~/.config/sat/sat.conf could override /etc/sat.conf values).
author Goffi <goffi@goffi.org>
date Wed, 09 Mar 2016 12:11:35 +0100
parents d17772b0fe22
children 2daf7b4c6756
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1273
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
1 #!/usr/bin/python
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
3
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT plugin for Delayed Delivery (XEP-0203)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1743
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1743
diff changeset
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
1273
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
7
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
12
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
17
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
20
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
21 from sat.core.i18n import _
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
22 from sat.core.log import getLogger
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
23 log = getLogger(__name__)
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
24
1743
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
25 from calendar import timegm
1273
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
26 from wokkel import disco, iwokkel, delay
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
27 try:
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
28 from twisted.words.protocols.xmlstream import XMPPHandler
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
29 except ImportError:
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
30 from wokkel.subprotocols import XMPPHandler
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
31 from zope.interface import implements
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
32
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
33
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
34 NS_DD = 'urn:xmpp:delay'
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
35
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
36 PLUGIN_INFO = {
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
37 "name": "Delayed Delivery",
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
38 "import_name": "XEP-0203",
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
39 "type": "XEP",
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
40 "protocols": ["XEP-0203"],
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
41 "main": "XEP_0203",
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
42 "handler": "yes",
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
43 "description": _("""Implementation of Delayed Delivery""")
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
44 }
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
45
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
46
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
47 class XEP_0203(object):
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
48
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
49 def __init__(self, host):
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
50 log.info(_("Delayed Delivery plugin initialization"))
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
51 self.host = host
1743
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
52 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
53
1273
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
54
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
55 def getHandler(self, profile):
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
56 return XEP_0203_handler(self, profile)
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
57
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
58 def delay(self, stamp, sender=None, desc='', parent=None):
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
59 """Build a delay element, eventually append it to the given parent element.
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
60
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
61 @param stamp (datetime): offset-aware timestamp of the original sending.
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
62 @param sender (JID): entity that originally sent or delayed the message.
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
63 @param desc (unicode): optional natural language description.
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
64 @param parent (domish.Element): add the delay element to this element.
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
65 @return: the delay element (domish.Element)
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
66 """
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
67 elt = delay.Delay(stamp, sender).toElement()
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
68 if desc:
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
69 elt.addContent(desc)
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
70 if parent:
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
71 parent.addChild(elt)
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
72 return elt
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
73
1743
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
74 def messagePostTreat(self, data, timestamp):
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
75 """Set the timestamp of a received message.
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
76
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
77 @param data (dict): data send by MessageReceived trigger through post_treat deferred
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
78 @param timestamp (int): original timestamp of a delayed message
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
79 @return: dict
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
80 """
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
81 data['extra']['timestamp'] = unicode(timestamp)
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
82 return data
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
83
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
84 def messageReceivedTrigger(self, message, post_treat, profile):
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
85 """Process a delay element from incoming message.
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
86
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
87 @param message (domish.Element): message element
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
88 @param post_treat (Deferred): deferred instance to add post treatments
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
89 """
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
90 try:
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
91 delay_ = delay.Delay.fromElement([elm for elm in message.elements() if elm.name == 'delay'][0])
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
92 except IndexError:
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
93 return True
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
94 else:
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
95 timestamp = timegm(delay_.stamp.utctimetuple())
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
96 post_treat.addCallback(self.messagePostTreat, timestamp)
4c48468ead4c plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
97 return True
1273
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
98
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
99 class XEP_0203_handler(XMPPHandler):
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
100 implements(iwokkel.IDisco)
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
101
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
102 def __init__(self, plugin_parent, profile):
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
103 self.plugin_parent = plugin_parent
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
104 self.host = plugin_parent.host
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
105 self.profile = profile
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
106
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
107 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
108 return [disco.DiscoFeature(NS_DD)]
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
109
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
110 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
f7977b2b727e plugin XEP-0203: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
111 return []