Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0203.py @ 1933:16e65f15f31f
plugin adhoc_dbus, jp (cmd/adhoc): fixes adHocDBusAddAuto when no bus is found
fix bug 134
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 17 Apr 2016 13:37:40 +0200 |
parents | d17772b0fe22 |
children | 2daf7b4c6756 |
rev | line source |
---|---|
1273 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for Delayed Delivery (XEP-0203) | |
1766 | 5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) | |
1273 | 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 | |
21 from sat.core.i18n import _ | |
22 from sat.core.log import getLogger | |
23 log = getLogger(__name__) | |
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 | 26 from wokkel import disco, iwokkel, delay |
27 try: | |
28 from twisted.words.protocols.xmlstream import XMPPHandler | |
29 except ImportError: | |
30 from wokkel.subprotocols import XMPPHandler | |
31 from zope.interface import implements | |
32 | |
33 | |
34 NS_DD = 'urn:xmpp:delay' | |
35 | |
36 PLUGIN_INFO = { | |
37 "name": "Delayed Delivery", | |
38 "import_name": "XEP-0203", | |
39 "type": "XEP", | |
40 "protocols": ["XEP-0203"], | |
41 "main": "XEP_0203", | |
42 "handler": "yes", | |
43 "description": _("""Implementation of Delayed Delivery""") | |
44 } | |
45 | |
46 | |
47 class XEP_0203(object): | |
48 | |
49 def __init__(self, host): | |
50 log.info(_("Delayed Delivery plugin initialization")) | |
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 | 54 |
55 def getHandler(self, profile): | |
56 return XEP_0203_handler(self, profile) | |
57 | |
58 def delay(self, stamp, sender=None, desc='', parent=None): | |
59 """Build a delay element, eventually append it to the given parent element. | |
60 | |
61 @param stamp (datetime): offset-aware timestamp of the original sending. | |
62 @param sender (JID): entity that originally sent or delayed the message. | |
63 @param desc (unicode): optional natural language description. | |
64 @param parent (domish.Element): add the delay element to this element. | |
65 @return: the delay element (domish.Element) | |
66 """ | |
67 elt = delay.Delay(stamp, sender).toElement() | |
68 if desc: | |
69 elt.addContent(desc) | |
70 if parent: | |
71 parent.addChild(elt) | |
72 return elt | |
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 | 98 |
99 class XEP_0203_handler(XMPPHandler): | |
100 implements(iwokkel.IDisco) | |
101 | |
102 def __init__(self, plugin_parent, profile): | |
103 self.plugin_parent = plugin_parent | |
104 self.host = plugin_parent.host | |
105 self.profile = profile | |
106 | |
107 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
108 return [disco.DiscoFeature(NS_DD)] | |
109 | |
110 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
111 return [] |