comparison src/plugins/plugin_xep_0077.py @ 223:86d249b6d9b7

Files reorganisation
author Goffi <goffi@goffi.org>
date Wed, 29 Dec 2010 01:06:29 +0100
parents plugins/plugin_xep_0077.py@06985b6ad23a
children b1794cbb88e5
comparison
equal deleted inserted replaced
222:3198bfd66daa 223:86d249b6d9b7
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 SAT plugin for managing xep-0077
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from logging import debug, info, error
23 from twisted.words.protocols.jabber import client, jid
24 from twisted.words.protocols.jabber import error as jab_error
25 from twisted.words.protocols.jabber.xmlstream import IQ
26 from twisted.internet import reactor
27 from sat.tools.xml_tools import dataForm2xml
28 import pdb
29
30 from wokkel import data_form
31
32 NS_REG = 'jabber:iq:register'
33
34 PLUGIN_INFO = {
35 "name": "XEP 0077 Plugin",
36 "import_name": "XEP_0077",
37 "type": "XEP",
38 "protocols": ["XEP-0077"],
39 "dependencies": [],
40 "main": "XEP_0077",
41 "description": _("""Implementation of in-band registration""")
42 }
43
44 class XEP_0077():
45
46 def __init__(self, host):
47 info(_("Plugin XEP_0077 initialization"))
48 self.host = host
49 self.triggers = {} #used by other protocol (e.g. XEP-0100) to finish registration. key = target_jid
50 host.bridge.addMethod("in_band_register", ".communication", in_sign='ss', out_sign='s', method=self.in_band_register)
51 host.bridge.addMethod("in_band_submit", ".request", in_sign='sa(ss)', out_sign='s', method=self.in_band_submit)
52
53 def addTrigger(self, target, cb, profile):
54 """Add a callback which is called when registration to target is successful"""
55 self.triggers[target] = (cb, profile)
56
57 def reg_ok(self, answer):
58 """Called after the first get IQ"""
59 try:
60 x_elem = filter (lambda x:x.name == "x", answer.firstChildElement().elements())[0] #We only want the "x" element (data form)
61 except IndexError:
62 info(_("No data form found"))
63 #TODO: manage registration without data form
64 answer_data={"reason": "unmanaged", "message":_("This gateway can't be managed by SàT, sorry :(")}
65 answer_type = "ERROR"
66 self.host.bridge.actionResult(answer_type, answer['id'], answer_data)
67 return
68
69 form = data_form.Form.fromElement(x_elem)
70 xml_data = dataForm2xml(form)
71 self.host.bridge.actionResult("XMLUI", answer['id'], {"target":answer["from"], "type":"registration", "xml":xml_data})
72
73 def reg_err(self, failure):
74 """Called when something is wrong with registration"""
75 info (_("Registration failure: %s") % str(failure.value))
76 answer_data = {}
77 answer_data['reason'] = 'unknown'
78 answer_data={"message":"%s [code: %s]" % (failure.value.condition, unicode(failure.value))}
79 answer_type = "ERROR"
80 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data)
81
82 def unregistrationAnswer(self, answer):
83 debug (_("registration answer: %s") % answer.toXml())
84 answer_type = "SUCCESS"
85 answer_data={"message":_("Your are now unregistred")}
86 self.host.bridge.actionResult(answer_type, answer['id'], answer_data)
87
88 def unregistrationFailure(self, failure):
89 info (_("Unregistration failure: %s") % str(failure.value))
90 answer_type = "ERROR"
91 answer_data = {}
92 answer_data['reason'] = 'unknown'
93 answer_data={"message":_("Unregistration failed: %s") % failure.value.condition}
94 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data)
95
96 def registrationAnswer(self, answer):
97 debug (_("registration answer: %s") % answer.toXml())
98 answer_type = "SUCCESS"
99 answer_data={"message":_("Registration successfull")}
100 self.host.bridge.actionResult(answer_type, answer['id'], answer_data)
101 if self.triggers.has_key(answer["from"]):
102 callback,profile = self.triggers[answer["from"]]
103 callback(answer["from"], profile)
104 del self.triggers[answer["from"]]
105
106 def registrationFailure(self, failure):
107 info (_("Registration failure: %s") % str(failure.value))
108 print failure.value.stanza.toXml()
109 answer_type = "ERROR"
110 answer_data = {}
111 if failure.value.condition == 'conflict':
112 answer_data['reason'] = 'conflict'
113 answer_data={"message":_("Username already exists, please choose an other one")}
114 else:
115 answer_data['reason'] = 'unknown'
116 answer_data={"message":_("Registration failed")}
117 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data)
118 if self.triggers.has_key(answer["from"]):
119 del self.triggers[answer["from"]]
120
121 def in_band_submit(self, action, target, fields, profile):
122 """Submit a form for registration, using data_form"""
123 id, deferred = self.host.submitForm(action, target, fields, profile)
124 if action == 'CANCEL':
125 deferred.addCallbacks(self.unregistrationAnswer, self.unregistrationFailure)
126 else:
127 deferred.addCallbacks(self.registrationAnswer, self.registrationFailure)
128 return id
129
130 def in_band_register(self, target, profile_key='@DEFAULT@'):
131 """register to a target JID"""
132 current_jid, xmlstream = self.host.getJidNStream(profile_key)
133 if not xmlstream:
134 error (_('Asking for an non-existant or not connected profile'))
135 return ""
136 to_jid = jid.JID(target)
137 debug(_("Asking registration for [%s]") % to_jid.full())
138 reg_request=IQ(xmlstream,'get')
139 reg_request["from"]=current_jid.full()
140 reg_request["to"] = to_jid.full()
141 query=reg_request.addElement('query', NS_REG)
142 reg_request.send(to_jid.full()).addCallbacks(self.reg_ok, self.reg_err)
143 return reg_request["id"]