annotate src/plugins/plugin_xep_0033.py @ 2116:766dbbec56f2

core (memory/cache): geFilePath now return None when uid is empty
author Goffi <goffi@goffi.org>
date Sun, 15 Jan 2017 16:00:40 +0100
parents a2bc5089c2eb
children 6e509ee853a8
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
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
3
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT plugin for Extended Stanza Addressing (xep-0033)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1552
diff changeset
5 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
6
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # (at your option) any later version.
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
11
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
16
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
19
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 750
diff changeset
20 from sat.core.i18n import _
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
21 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
22 log = getLogger(__name__)
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 999
diff changeset
23 from sat.core import exceptions
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
24 from wokkel import disco, iwokkel
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
25 from zope.interface import implements
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber.jid import JID
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 999
diff changeset
27 from twisted.python import failure
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
28 import copy
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
29 try:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
30 from twisted.words.protocols.xmlstream import XMPPHandler
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
31 except ImportError:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
32 from wokkel.subprotocols import XMPPHandler
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
33 from twisted.words.xish import domish
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
34 from twisted.internet import defer
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
35
1374
0befb14ecf62 renamed tools.misc to tools.trigger
Goffi <goffi@goffi.org>
parents: 1205
diff changeset
36 from sat.tools import trigger
749
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
37 from time import time
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
38
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
39 # TODO: fix Prosody "addressing" plugin to leave the concerned bcc according to the spec:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
40 #
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
41 # http://xmpp.org/extensions/xep-0033.html#addr-type-bcc
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
42 # "This means that the server MUST remove these addresses before the stanza is delivered to anyone other than the given bcc addressee or the multicast service of the bcc addressee."
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
43 #
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
44 # http://xmpp.org/extensions/xep-0033.html#multicast
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
45 # "Each 'bcc' recipient MUST receive only the <address type='bcc'/> associated with that addressee."
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
46
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
47 # TODO: fix Prosody "addressing" plugin to determine itself if remote servers supports this XEP
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
48
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
49
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
50 NS_XMPP_CLIENT = "jabber:client"
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
51 NS_ADDRESS = "http://jabber.org/protocol/address"
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
52 ATTRIBUTES = ["jid", "uri", "node", "desc", "delivered", "type"]
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
53 ADDRESS_TYPES = ["to", "cc", "bcc", "replyto", "replyroom", "noreply"]
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
54
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
55 PLUGIN_INFO = {
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
56 "name": "Extended Stanza Addressing Protocol Plugin",
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
57 "import_name": "XEP-0033",
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
58 "type": "XEP",
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
59 "protocols": ["XEP-0033"],
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
60 "dependencies": [],
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
61 "main": "XEP_0033",
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
62 "handler": "yes",
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
63 "description": _("""Implementation of Extended Stanza Addressing""")
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
64 }
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
65
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
66
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
67 class XEP_0033(object):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
68 """
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
69 Implementation for XEP 0033
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
70 """
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
71 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
72 log.info(_("Extended Stanza Addressing plugin initialization"))
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
73 self.host = host
749
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
74 self.internal_data = {}
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
75 host.trigger.add("messageSend", self.messageSendTrigger, trigger.TriggerManager.MIN_PRIORITY)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
76 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
77
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
78 def messageSendTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments):
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
79 """Process the XEP-0033 related data to be sent"""
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
80 profile = client.profile
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
81
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
82 def treatment(mess_data):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
83 if not 'address' in mess_data['extra']:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
84 return mess_data
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
85
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
86 def discoCallback(entities):
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
87 if not entities:
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 999
diff changeset
88 log.warning(_("XEP-0033 is being used but the server doesn't support it!"))
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
89 raise failure.Failure(exceptions.CancelError(u'Cancelled by XEP-0033'))
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
90 if mess_data["to"] not in entities:
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
91 expected = _(' or ').join([entity.userhost() for entity in entities])
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
92 log.warning(_(u"Stanzas using XEP-0033 should be addressed to %(expected)s, not %(current)s!") % {'expected': expected, 'current': mess_data["to"]})
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
93 log.warning(_(u"TODO: addressing has been fixed by the backend... fix it in the frontend!"))
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
94 mess_data["to"] = list(entities)[0].userhostJID()
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
95 element = mess_data['xml'].addElement('addresses', NS_ADDRESS)
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
96 entries = [entry.split(':') for entry in mess_data['extra']['address'].split('\n') if entry != '']
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
97 for type_, jid_ in entries:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
98 element.addChild(domish.Element((None, 'address'), None, {'type': type_, 'jid': jid_}))
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
99 # when the prosody plugin is completed, we can immediately return mess_data from here
750
c8b9f675ac17 plugin XEP-0033: avoid the controlled error to explode (use return Failure(...) instead of raise)
souliane <souliane@mailoo.org>
parents: 749
diff changeset
100 self.sendAndStoreMessage(mess_data, entries, profile)
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 999
diff changeset
101 log.debug("XEP-0033 took over")
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
102 raise failure.Failure(exceptions.CancelError(u'Cancelled by XEP-0033'))
1552
e0bde0d0b321 core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
103 d = self.host.findFeaturesSet([NS_ADDRESS], profile=profile)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
104 d.addCallbacks(discoCallback, lambda dummy: discoCallback(None))
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
105 return d
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
106
922
c897c8d321b3 core: sendMessageTrigger now manage pre and post treatments, which happen before or after XML generation
Goffi <goffi@goffi.org>
parents: 844
diff changeset
107 post_xml_treatments.addCallback(treatment)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
108 return True
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
109
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
110 def sendAndStoreMessage(self, mess_data, entries, profile):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
111 """Check if target servers support XEP-0033, send and store the messages
750
c8b9f675ac17 plugin XEP-0033: avoid the controlled error to explode (use return Failure(...) instead of raise)
souliane <souliane@mailoo.org>
parents: 749
diff changeset
112 @return: a friendly failure to let the core know that we sent the message already
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
113
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
114 Later we should be able to remove this method because:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
115 # XXX: sending the messages should be done by the local server
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
116 # FIXME: for now we duplicate the messages in the history for each recipient, this should change
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
117 # FIXME: for now we duplicate the echoes to the sender, this should also change
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
118 Ideas:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
119 - fix Prosody plugin to check if target server support the feature
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
120 - redesign the database to save only one entry to the database
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
121 - change the messageNew signal to eventually pass more than one recipient
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
122 """
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
123 def send(mess_data, skip_send=False):
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
124 client = self.host.profiles[profile]
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
125 d = defer.Deferred()
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
126 if not skip_send:
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
127 d.addCallback(self.host.messageSendToStream, client)
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
128 d.addCallback(self.host.messageAddToHistory, client)
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
129 d.addCallback(self.host.messageSendToBridge, client)
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
130 d.addErrback(lambda failure: failure.trap(exceptions.CancelError))
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
131 return d.callback(mess_data)
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
132
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
133 def discoCallback(entities, to_jid_s):
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
134 history_data = copy.deepcopy(mess_data)
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
135 history_data['to'] = JID(to_jid_s)
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
136 history_data['xml']['to'] = to_jid_s
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
137 if entities:
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
138 if entities not in self.internal_data[timestamp]:
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
139 sent_data = copy.deepcopy(mess_data)
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
140 sent_data['to'] = JID(JID(to_jid_s).host)
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
141 sent_data['xml']['to'] = JID(to_jid_s).host
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
142 send(sent_data)
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
143 self.internal_data[timestamp].append(entities)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
144 # we still need to fill the history and signal the echo...
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
145 send(history_data, skip_send=True)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
146 else:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
147 # target server misses the addressing feature
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
148 send(history_data)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
149
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
150 def errback(failure, to_jid):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
151 discoCallback(None, to_jid)
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
152
749
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
153 timestamp = time()
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
154 self.internal_data[timestamp] = []
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
155 defer_list = []
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
156 for type_, jid_ in entries:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
157 d = defer.Deferred()
1552
e0bde0d0b321 core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
158 d.addCallback(self.host.findFeaturesSet, jid_=JID(JID(jid_).host), profile=profile)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
159 d.addCallbacks(discoCallback, errback, callbackArgs=[jid_], errbackArgs=[jid_])
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
160 d.callback([NS_ADDRESS])
749
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
161 defer_list.append(d)
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
162 d = defer.Deferred().addCallback(lambda dummy: self.internal_data.pop(timestamp))
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
163 defer.DeferredList(defer_list).chainDeferred(d)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
164
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
165 def messageReceivedTrigger(self, client, message, post_treat):
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
166 """In order to save the addressing information in the history"""
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
167 def post_treat_addr(data, addresses):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
168 data['extra']['addresses'] = ""
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
169 for address in addresses:
787
dd656d745d6a test: added tests for XEP-0033
souliane <souliane@mailoo.org>
parents: 785
diff changeset
170 # Depending how message has been constructed, we could get here
dd656d745d6a test: added tests for XEP-0033
souliane <souliane@mailoo.org>
parents: 785
diff changeset
171 # some noise like "\n " instead of an address element.
dd656d745d6a test: added tests for XEP-0033
souliane <souliane@mailoo.org>
parents: 785
diff changeset
172 if isinstance(address, domish.Element):
dd656d745d6a test: added tests for XEP-0033
souliane <souliane@mailoo.org>
parents: 785
diff changeset
173 data['extra']['addresses'] += '%s:%s\n' % (address['type'], address['jid'])
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
174 return data
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
175
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
176 try:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
177 addresses = message.elements(NS_ADDRESS, 'addresses').next()
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
178 except StopIteration:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
179 pass # no addresses
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
180 else:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
181 post_treat.addCallback(post_treat_addr, addresses.children)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
182 return True
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
183
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
184 def getHandler(self, profile):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
185 return XEP_0033_handler(self, profile)
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
186
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
187
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
188 class XEP_0033_handler(XMPPHandler):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
189 implements(iwokkel.IDisco)
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
190
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
191 def __init__(self, plugin_parent, profile):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
192 self.plugin_parent = plugin_parent
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
193 self.host = plugin_parent.host
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
194 self.profile = profile
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
195
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
196 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
197 return [disco.DiscoFeature(NS_ADDRESS)]
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
198
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
199 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
200 return []