Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0203.py @ 1743:4c48468ead4c
plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59):
- this patch reveals a bug in primitivus Chat.printMessage relative to MUC history (the test to discard duplicates is wrong)
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 11 Dec 2015 11:25:29 +0100 |
parents | 069ad98b360d |
children | d17772b0fe22 |
comparison
equal
deleted
inserted
replaced
1742:244a605623d6 | 1743:4c48468ead4c |
---|---|
20 | 20 |
21 from sat.core.i18n import _ | 21 from sat.core.i18n import _ |
22 from sat.core.log import getLogger | 22 from sat.core.log import getLogger |
23 log = getLogger(__name__) | 23 log = getLogger(__name__) |
24 | 24 |
25 from calendar import timegm | |
25 from wokkel import disco, iwokkel, delay | 26 from wokkel import disco, iwokkel, delay |
26 try: | 27 try: |
27 from twisted.words.protocols.xmlstream import XMPPHandler | 28 from twisted.words.protocols.xmlstream import XMPPHandler |
28 except ImportError: | 29 except ImportError: |
29 from wokkel.subprotocols import XMPPHandler | 30 from wokkel.subprotocols import XMPPHandler |
46 class XEP_0203(object): | 47 class XEP_0203(object): |
47 | 48 |
48 def __init__(self, host): | 49 def __init__(self, host): |
49 log.info(_("Delayed Delivery plugin initialization")) | 50 log.info(_("Delayed Delivery plugin initialization")) |
50 self.host = host | 51 self.host = host |
52 host.trigger.add("MessageReceived", self.messageReceivedTrigger) | |
53 | |
51 | 54 |
52 def getHandler(self, profile): | 55 def getHandler(self, profile): |
53 return XEP_0203_handler(self, profile) | 56 return XEP_0203_handler(self, profile) |
54 | 57 |
55 def delay(self, stamp, sender=None, desc='', parent=None): | 58 def delay(self, stamp, sender=None, desc='', parent=None): |
66 elt.addContent(desc) | 69 elt.addContent(desc) |
67 if parent: | 70 if parent: |
68 parent.addChild(elt) | 71 parent.addChild(elt) |
69 return elt | 72 return elt |
70 | 73 |
74 def messagePostTreat(self, data, timestamp): | |
75 """Set the timestamp of a received message. | |
76 | |
77 @param data (dict): data send by MessageReceived trigger through post_treat deferred | |
78 @param timestamp (int): original timestamp of a delayed message | |
79 @return: dict | |
80 """ | |
81 data['extra']['timestamp'] = unicode(timestamp) | |
82 return data | |
83 | |
84 def messageReceivedTrigger(self, message, post_treat, profile): | |
85 """Process a delay element from incoming message. | |
86 | |
87 @param message (domish.Element): message element | |
88 @param post_treat (Deferred): deferred instance to add post treatments | |
89 """ | |
90 try: | |
91 delay_ = delay.Delay.fromElement([elm for elm in message.elements() if elm.name == 'delay'][0]) | |
92 except IndexError: | |
93 return True | |
94 else: | |
95 timestamp = timegm(delay_.stamp.utctimetuple()) | |
96 post_treat.addCallback(self.messagePostTreat, timestamp) | |
97 return True | |
71 | 98 |
72 class XEP_0203_handler(XMPPHandler): | 99 class XEP_0203_handler(XMPPHandler): |
73 implements(iwokkel.IDisco) | 100 implements(iwokkel.IDisco) |
74 | 101 |
75 def __init__(self, plugin_parent, profile): | 102 def __init__(self, plugin_parent, profile): |