annotate plugins/plugin_xep_0100.py @ 39:2e3411a6baad

Wix: external server management in gateways manager, SàT: bug fixes in gateway management - SàT: from twisted.words.protocols.jabber.error module imported as jab_error to avoir name colision with error log method - XEP_0100 plugin: added errback for disco info - XEP_0077 plugin: registration callback now look for "x" element (data form: see XEP-0004), and send an error message if it can't find it. - Wix: added fields for browsing external server gateways
author Goffi <goffi@goffi.org>
date Thu, 17 Dec 2009 17:29:02 +1100
parents a61beb21d16d
children 874de3020e1c
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)
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
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
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 client, jid, xmlstream
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
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import pdb
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
27
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from wokkel import disco, iwokkel
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
29
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
30 PLUGIN_INFO = {
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
31 "name": "Gateways Plugin",
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
32 "import_name": "XEP_0100",
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
33 "type": "XEP",
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
34 "dependencies": ["XEP_0077"],
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "main": "XEP_0100",
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "description": """Implementation of Gateways protocol"""
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
37 }
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
38
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
39 class XEP_0100():
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 def __init__(self, host):
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
42 info("Gateways plugin initialization")
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.host = host
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.__gateways = {} #dict used to construct the answer to findGateways. Key = target jid
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
45 host.bridge.addMethod("findGateways", ".communication", in_sign='s', out_sign='s', method=self.findGateways)
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
46 host.bridge.addMethod("gatewayRegister", ".request", in_sign='ssa(ss)', 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
47
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
48 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
49 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
50
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
51 if self.__gateways[request_id]['__total_items'] == 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
52 debug ("All items checked for id [%s]" % str(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
53
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]['__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
55 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
56 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
57
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def discoInfo(self, disco, entity, request_id):
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
59 """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
60
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
61 for identity in disco.identities:
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
62 if identity[0] == 'gateway':
30
d6b613764dd7 new plugin for xep 0077 (In-Band registration): first draft
Goffi <goffi@goffi.org>
parents: 25
diff changeset
63 print ("Found gateway (%s): %s" % (entity.full(), disco.identities[identity]))
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.__gateways[request_id][entity.full()] = {
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
65 'name':disco.identities[identity],
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
66 'type':identity[1]
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
67 }
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
68
39
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
69 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
70
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
71 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
72 """Something is going wrong with disco"""
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
73 failure.trap(jab_error.StanzaError)
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
74 error("Error when discovering [%s]: %s" % (entity.full(), failure.value.condition))
2e3411a6baad Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents: 37
diff changeset
75 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
76
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
77
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
78 def discoItems(self, disco, request_id):
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
79 """Look for items with disco protocol, and ask infos for each one"""
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
80
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
81 if len(disco._items) == 0:
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
82 debug ("No gateway found")
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.host.actionResultExt(request_id,"DICT_DICT",{})
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
85
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.__gateways[request_id] = {'__total_items':len(disco._items), '__handled_items':0}
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
87 for item in disco._items:
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
88 debug ("item found: %s", item.name)
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.host.disco.requestInfo(item.entity).addCallback(self.discoInfo, entity=item.entity, request_id=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
90 self.host.disco.requestInfo(item.entity).addErrback(self.discoInfoErr, entity=item.entity, request_id=request_id)
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
91
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
92 def registrationSuccessful(self, target):
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
93 """Called when in_band registration is ok, we must now follow the rest of procedure"""
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
94 print "Registration successful, doing the rest"
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
95 self.host.addContact(target)
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
96 self.host.setPresence(target)
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
97
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
98 def gatewayRegister(self, action, target, fields):
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
99 """Register gateway using in-band registration, then log-in to gateway"""
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
100 if action == 'SUBMIT':
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
101 self.host.plugins["XEP_0077"].addTrigger(target, self.registrationSuccessful)
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
102 return self.host.plugins["XEP_0077"].in_band_submit(action, target, fields)
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 30
diff changeset
103
25
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
104 def findGateways(self, target):
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
105 """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
106 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
107 """
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
108 debug ("find gateways (target = %s)" % target)
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
109 request_id = self.host.get_next_id()
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
110 self.host.disco.requestItems(jid.JID(target)).addCallback(self.discoItems, request_id=request_id)
53e921c8a357 new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
diff changeset
111 return request_id