Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0100.py @ 283:68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
- plugin for XEP 0060, 0163 and 0107, based on Wokkel's pubsub
- XEP 0115 plugin has been fixed, so generateHash can be called when features have changed (can be usefull to filter PEP)
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 03 Feb 2011 18:06:25 +0100 |
parents | b1794cbb88e5 |
children | 7c79d4a8c9e6 |
rev | line source |
---|---|
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 SAT plugin for managing gateways (xep-0100) |
228 | 6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU General Public License for more details. |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from logging import debug, info, error |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.internet import protocol |
64 | 24 from twisted.words.protocols.jabber import client, jid |
39
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
25 from twisted.words.protocols.jabber import error as jab_error |
174
fbae69247b15
Core: plugin 0100: added connection lost management for disco info
Goffi <goffi@goffi.org>
parents:
169
diff
changeset
|
26 import twisted.internet.error |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 import pdb |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from wokkel import disco, iwokkel |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 PLUGIN_INFO = { |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 "name": "Gateways Plugin", |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 "import_name": "XEP_0100", |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 "type": "XEP", |
48 | 35 "protocols": ["XEP-0100"], |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
30
diff
changeset
|
36 "dependencies": ["XEP_0077"], |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 "main": "XEP_0100", |
69 | 38 "description": _("""Implementation of Gateways protocol""") |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 } |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 class XEP_0100(): |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 def __init__(self, host): |
69 | 44 info(_("Gateways plugin initialization")) |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 self.host = host |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 self.__gateways = {} #dict used to construct the answer to findGateways. Key = target jid |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
47 host.bridge.addMethod("findGateways", ".communication", in_sign='ss', out_sign='s', method=self.findGateways) |
169
06985b6ad23a
SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents:
102
diff
changeset
|
48 host.bridge.addMethod("gatewayRegister", ".request", in_sign='ssa(ss)s', out_sign='s', method=self.gatewayRegister) |
39
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
49 |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
50 def __inc_handled_items(self, request_id): |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
51 self.__gateways[request_id]['__handled_items']+=1 |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
52 |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
53 if self.__gateways[request_id]['__total_items'] == self.__gateways[request_id]['__handled_items']: |
69 | 54 debug (_("All items checked for id [%s]") % str(request_id)) |
39
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
55 |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
56 del self.__gateways[request_id]['__total_items'] |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
57 del self.__gateways[request_id]['__handled_items'] |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
58 self.host.actionResultExt(request_id,"DICT_DICT",self.__gateways[request_id]) |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
59 |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 def discoInfo(self, disco, entity, request_id): |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 """Find disco infos about entity, to check if it is a gateway""" |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 for identity in disco.identities: |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 if identity[0] == 'gateway': |
69 | 65 print (_("Found gateway (%(jid)s): %(identity)s") % {'jid':entity.full(), 'identity':disco.identities[identity]}) |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 self.__gateways[request_id][entity.full()] = { |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 'name':disco.identities[identity], |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 'type':identity[1] |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 } |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
39
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
71 self.__inc_handled_items(request_id) |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
72 |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
73 def discoInfoErr(self, failure, entity, request_id): |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
74 """Something is going wrong with disco""" |
174
fbae69247b15
Core: plugin 0100: added connection lost management for disco info
Goffi <goffi@goffi.org>
parents:
169
diff
changeset
|
75 failure.trap(jab_error.StanzaError,twisted.internet.error.ConnectionLost) |
fbae69247b15
Core: plugin 0100: added connection lost management for disco info
Goffi <goffi@goffi.org>
parents:
169
diff
changeset
|
76 error(_("Error when discovering [%(jid)s]: %(error)s") % {'jid':entity.full(), 'error':failure.getErrorMessage()}) |
39
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
77 self.__inc_handled_items(request_id) |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
37
diff
changeset
|
78 |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 |
64 | 80 def discoItems(self, disco, request_id, target, client): |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 """Look for items with disco protocol, and ask infos for each one""" |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
39
diff
changeset
|
82 #FIXME: target is used as we can't find the original iq node (parent is None) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
39
diff
changeset
|
83 # an other way would avoid this useless parameter (is there a way with wokkel ?) |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 if len(disco._items) == 0: |
69 | 85 debug (_("No gateway found")) |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 self.host.actionResultExt(request_id,"DICT_DICT",{}) |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 return |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
39
diff
changeset
|
89 self.__gateways[request_id] = {'__total_items':len(disco._items), '__handled_items':0, '__private__':{'target':target.full()}} |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 for item in disco._items: |
175
8537df794f74
Primitivus: gateways: current server is now shown in title
Goffi <goffi@goffi.org>
parents:
174
diff
changeset
|
91 #TODO: need to set a timeout for theses requests |
69 | 92 debug (_("item found: %s"), item.name) |
102 | 93 client.disco.requestInfo(item.entity).addCallback(self.discoInfo, entity=item.entity, request_id=request_id).addErrback(self.discoInfoErr, entity=item.entity, request_id=request_id) |
94 | |
95 def discoItemsErr(self, failure, request_id, target, client): | |
96 """Something is going wrong with disco""" | |
97 error(_("Error when discovering [%(target)s]: %(condition)s") % {'target':target.full(), 'condition':unicode(failure.value)}) | |
98 message_data={"reason": "connection error", "message":_(u"Error while trying to discover %(target)s gateways: %(error_mess)s") % {'target':target.full(), 'error_mess':unicode(failure.value)}} | |
99 self.host.bridge.actionResult("ERROR", request_id, message_data) | |
100 | |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 |
169
06985b6ad23a
SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents:
102
diff
changeset
|
102 def registrationSuccessful(self, target, profile): |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
30
diff
changeset
|
103 """Called when in_band registration is ok, we must now follow the rest of procedure""" |
69 | 104 debug (_("Registration successful, doing the rest")) |
169
06985b6ad23a
SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents:
102
diff
changeset
|
105 self.host.addContact(target, profile) |
06985b6ad23a
SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents:
102
diff
changeset
|
106 self.host.setPresence(target, profile) |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
30
diff
changeset
|
107 |
169
06985b6ad23a
SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents:
102
diff
changeset
|
108 def gatewayRegister(self, action, target, fields, profile_key='@DEFAULT@'): |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
30
diff
changeset
|
109 """Register gateway using in-band registration, then log-in to gateway""" |
169
06985b6ad23a
SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents:
102
diff
changeset
|
110 profile = self.host.memory.getProfileName(profile_key) |
06985b6ad23a
SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents:
102
diff
changeset
|
111 assert(profile) #FIXME: return an error here |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
30
diff
changeset
|
112 if action == 'SUBMIT': |
169
06985b6ad23a
SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents:
102
diff
changeset
|
113 self.host.plugins["XEP_0077"].addTrigger(target, self.registrationSuccessful, profile) |
06985b6ad23a
SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents:
102
diff
changeset
|
114 return self.host.plugins["XEP_0077"].in_band_submit(action, target, fields, profile) |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
30
diff
changeset
|
115 |
64 | 116 def findGateways(self, target, profile_key='@DEFAULT@'): |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 """Find gateways in the target JID, using discovery protocol |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 Return an id used for retrieving the list of gateways |
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 """ |
102 | 120 profile = self.host.memory.getProfileName(profile_key) |
64 | 121 client = self.host.getClient(profile_key) |
122 assert(client) | |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
39
diff
changeset
|
123 to_jid = jid.JID(target) |
102 | 124 debug (_("find gateways (target = %(target)s, profile = %(profile)s)") % {'target':to_jid.full(), 'profile':profile}) |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 request_id = self.host.get_next_id() |
102 | 126 client.disco.requestItems(to_jid).addCallback(self.discoItems, request_id=request_id, target = to_jid, client = client).addErrback(self.discoItemsErr, request_id=request_id, target = to_jid, client = client) |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 return request_id |