comparison src/plugins/plugin_xep_0203.py @ 1955:633b5c21aefd

backend, frontend: messages refactoring (huge commit, not finished): /!\ database schema has been modified, do a backup before updating message have been refactored, here are the main changes: - languages are now handled - all messages have an uid (internal to SàT) - message updating is anticipated - subject is now first class - new naming scheme is used newMessage => messageNew, getHistory => historyGet, sendMessage => messageSend - minimal compatibility refactoring in quick_frontend/Primitivus, better refactoring should follow - threads handling - delayed messages are saved into history - info messages may also be saved in history (e.g. to keep track of people joining/leaving a room) - duplicate messages should be avoided - historyGet return messages in right order, no need to sort again - plugins have been updated to follow new features, some of them need to be reworked (e.g. OTR) - XEP-0203 (Delayed Delivery) is now fully handled in core, the plugin just handle disco and creation of a delay element - /!\ jp and Libervia are currently broken, as some features of Primitivus It has been put in one huge commit to avoid breaking messaging between changes. This is the main part of message refactoring, other commits will follow to take profit of the new features/behaviour.
author Goffi <goffi@goffi.org>
date Tue, 24 May 2016 22:11:04 +0200
parents 2daf7b4c6756
children 1d3f73e065e1
comparison
equal deleted inserted replaced
1943:ccfe45302a5c 1955:633b5c21aefd
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
26 from wokkel import disco, iwokkel, delay 25 from wokkel import disco, iwokkel, delay
27 try: 26 try:
28 from twisted.words.protocols.xmlstream import XMPPHandler 27 from twisted.words.protocols.xmlstream import XMPPHandler
29 except ImportError: 28 except ImportError:
30 from wokkel.subprotocols import XMPPHandler 29 from wokkel.subprotocols import XMPPHandler
47 class XEP_0203(object): 46 class XEP_0203(object):
48 47
49 def __init__(self, host): 48 def __init__(self, host):
50 log.info(_("Delayed Delivery plugin initialization")) 49 log.info(_("Delayed Delivery plugin initialization"))
51 self.host = host 50 self.host = host
52 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
53
54 51
55 def getHandler(self, profile): 52 def getHandler(self, profile):
56 return XEP_0203_handler(self, profile) 53 return XEP_0203_handler(self, profile)
57 54
58 def delay(self, stamp, sender=None, desc='', parent=None): 55 def delay(self, stamp, sender=None, desc='', parent=None):
69 elt.addContent(desc) 66 elt.addContent(desc)
70 if parent: 67 if parent:
71 parent.addChild(elt) 68 parent.addChild(elt)
72 return elt 69 return elt
73 70
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
98 71
99 class XEP_0203_handler(XMPPHandler): 72 class XEP_0203_handler(XMPPHandler):
100 implements(iwokkel.IDisco) 73 implements(iwokkel.IDisco)
101 74
102 def __init__(self, plugin_parent, profile): 75 def __init__(self, plugin_parent, profile):