annotate src/plugins/plugin_xep_0100.py @ 606:21ddafccf32d

installation: added forgotten imaging dependency
author Goffi <goffi@goffi.org>
date Sat, 23 Feb 2013 17:50:58 +0100
parents e629371a28d3
children 84a6e83157c2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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)
572
ca13633d3b6b dates update
Goffi <goffi@goffi.org>
parents: 544
diff changeset
6 Copyright (C) 2009, 2010, 2011, 2012, 2013 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
480
2a072735e459 Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents: 459
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
25
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
480
2a072735e459 Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents: 459
diff changeset
16 GNU Affero General Public License for more details.
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
17
480
2a072735e459 Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents: 459
diff changeset
18 You should have received a copy of the GNU Affero General Public License
25
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
544
899bd6fc7580 plugin XEP-0100: fixed lackings profile arguments in some methods
Goffi <goffi@goffi.org>
parents: 480
diff changeset
23 from twisted.words.protocols.jabber import client as jabber_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
24 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
25 import twisted.internet.error
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
26
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
27 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
28 "name": "Gateways Plugin",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
29 "import_name": "XEP-0100",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
30 "type": "XEP",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
31 "protocols": ["XEP-0100"],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
32 "dependencies": ["XEP-0077"],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
33 "main": "XEP_0100",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
34 "description": _("""Implementation of Gateways protocol""")
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
35 }
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
36
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
37
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
38 class XEP_0100(object):
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 def __init__(self, host):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 65
diff changeset
41 info(_("Gateways plugin initialization"))
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.host = host
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
43 self.__gateways = {} # dict used to construct the answer to findGateways. Key = target jid
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 291
diff changeset
44 host.bridge.addMethod("findGateways", ".plugin", in_sign='ss', out_sign='s', method=self.findGateways)
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 291
diff changeset
45 host.bridge.addMethod("gatewayRegister", ".plugin", 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
46
544
899bd6fc7580 plugin XEP-0100: fixed lackings profile arguments in some methods
Goffi <goffi@goffi.org>
parents: 480
diff changeset
47 def __inc_handled_items(self, request_id, profile):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
48 self.__gateways[request_id]['__handled_items'] += 1
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 if self.__gateways[request_id]['__total_items'] == self.__gateways[request_id]['__handled_items']:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
51 debug(_("All items checked for id [%s]") % str(request_id))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
52
39
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
53 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
54 del self.__gateways[request_id]['__handled_items']
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
55 self.host.actionResultExt(request_id, "DICT_DICT", self.__gateways[request_id], profile)
39
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
56
544
899bd6fc7580 plugin XEP-0100: fixed lackings profile arguments in some methods
Goffi <goffi@goffi.org>
parents: 480
diff changeset
57 def discoInfo(self, disco, entity, request_id, profile):
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
58 """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
59
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
60 for identity in disco.identities:
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
61 if identity[0] == 'gateway':
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
62 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
63 self.__gateways[request_id][entity.full()] = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
64 'name': disco.identities[identity],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
65 'type': identity[1]
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
66 }
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
67
544
899bd6fc7580 plugin XEP-0100: fixed lackings profile arguments in some methods
Goffi <goffi@goffi.org>
parents: 480
diff changeset
68 self.__inc_handled_items(request_id, profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
69
544
899bd6fc7580 plugin XEP-0100: fixed lackings profile arguments in some methods
Goffi <goffi@goffi.org>
parents: 480
diff changeset
70 def discoInfoErr(self, failure, entity, request_id, profile):
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 """Something is going wrong with disco"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
72 failure.trap(jab_error.StanzaError, twisted.internet.error.ConnectionLost)
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
73 error(_("Error when discovering [%(jid)s]: %(error)s") % {'jid': entity.full(), 'error': failure.getErrorMessage()})
544
899bd6fc7580 plugin XEP-0100: fixed lackings profile arguments in some methods
Goffi <goffi@goffi.org>
parents: 480
diff changeset
74 self.__inc_handled_items(request_id, profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
75
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
76 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
77 """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
78 #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
79 # 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
80 if len(disco._items) == 0:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
81 debug(_("No gateway found"))
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
82 self.host.actionResultExt(request_id, "DICT_DICT", {})
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
83 return
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
84
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
85 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
86 for item in disco._items:
175
8537df794f74 Primitivus: gateways: current server is now shown in title
Goffi <goffi@goffi.org>
parents: 174
diff changeset
87 #TODO: need to set a timeout for theses requests
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
88 debug(_("item found: %s"), item.name)
544
899bd6fc7580 plugin XEP-0100: fixed lackings profile arguments in some methods
Goffi <goffi@goffi.org>
parents: 480
diff changeset
89 client.disco.requestInfo(item.entity).addCallback(self.discoInfo, entity=item.entity, request_id=request_id, profile=client.profile).addErrback(self.discoInfoErr, entity=item.entity, request_id=request_id, profile=client.profile)
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 69
diff changeset
90
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 69
diff changeset
91 def discoItemsErr(self, failure, request_id, target, client):
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 69
diff changeset
92 """Something is going wrong with disco"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
93 error(_("Error when discovering [%(target)s]: %(condition)s") % {'target': target.full(), 'condition': unicode(failure.value)})
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
94 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)}}
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 69
diff changeset
95 self.host.bridge.actionResult("ERROR", request_id, message_data)
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 69
diff changeset
96
169
06985b6ad23a SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents: 102
diff changeset
97 def registrationSuccessful(self, target, profile):
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
98 """Called when in_band registration is ok, we must now follow the rest of procedure"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
99 debug(_("Registration successful, doing the rest"))
169
06985b6ad23a SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents: 102
diff changeset
100 self.host.addContact(target, profile)
06985b6ad23a SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents: 102
diff changeset
101 self.host.setPresence(target, profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
102
169
06985b6ad23a SàT: plugin 0077 & 0100: fixed profile management
Goffi <goffi@goffi.org>
parents: 102
diff changeset
103 def gatewayRegister(self, action, target, fields, profile_key='@DEFAULT@'):
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
104 """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
105 profile = self.host.memory.getProfileName(profile_key)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
106 assert(profile) # FIXME: return an error here
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
107 if action == 'SUBMIT':
291
7c79d4a8c9e6 plugins: fixed bad import names
Goffi <goffi@goffi.org>
parents: 228
diff changeset
108 self.host.plugins["XEP-0077"].addTrigger(target, self.registrationSuccessful, profile)
7c79d4a8c9e6 plugins: fixed bad import names
Goffi <goffi@goffi.org>
parents: 228
diff changeset
109 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
110
544
899bd6fc7580 plugin XEP-0100: fixed lackings profile arguments in some methods
Goffi <goffi@goffi.org>
parents: 480
diff changeset
111 def findGateways(self, target, profile_key):
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
112 """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
113 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
114 """
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 69
diff changeset
115 profile = self.host.memory.getProfileName(profile_key)
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
116 client = self.host.getClient(profile_key)
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
117 assert(client)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents: 39
diff changeset
118 to_jid = jid.JID(target)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
119 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
120 request_id = self.host.get_next_id()
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
121 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
122 return request_id