Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0077.py @ 587:952322b1d490
Remove trailing whitespaces.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 18 Jan 2013 17:55:34 +0100 |
parents | ca13633d3b6b |
children | beaf6bec2fcd |
comparison
equal
deleted
inserted
replaced
586:6a718ede8be1 | 587:952322b1d490 |
---|---|
37 "main": "XEP_0077", | 37 "main": "XEP_0077", |
38 "description": _("""Implementation of in-band registration""") | 38 "description": _("""Implementation of in-band registration""") |
39 } | 39 } |
40 | 40 |
41 class XEP_0077(): | 41 class XEP_0077(): |
42 | 42 |
43 def __init__(self, host): | 43 def __init__(self, host): |
44 info(_("Plugin XEP_0077 initialization")) | 44 info(_("Plugin XEP_0077 initialization")) |
45 self.host = host | 45 self.host = host |
46 self.triggers = {} #used by other protocol (e.g. XEP-0100) to finish registration. key = target_jid | 46 self.triggers = {} #used by other protocol (e.g. XEP-0100) to finish registration. key = target_jid |
47 host.bridge.addMethod("in_band_register", ".plugin", in_sign='ss', out_sign='s', method=self.in_band_register) | 47 host.bridge.addMethod("in_band_register", ".plugin", in_sign='ss', out_sign='s', method=self.in_band_register) |
48 host.bridge.addMethod("in_band_submit", ".plugin", in_sign='ssa(ss)s', out_sign='s', method=self.in_band_submit) | 48 host.bridge.addMethod("in_band_submit", ".plugin", in_sign='ssa(ss)s', out_sign='s', method=self.in_band_submit) |
49 | 49 |
50 def addTrigger(self, target, cb, profile): | 50 def addTrigger(self, target, cb, profile): |
51 """Add a callback which is called when registration to target is successful""" | 51 """Add a callback which is called when registration to target is successful""" |
52 self.triggers[target] = (cb, profile) | 52 self.triggers[target] = (cb, profile) |
53 | 53 |
54 def reg_ok(self, answer, profile): | 54 def reg_ok(self, answer, profile): |
55 """Called after the first get IQ""" | 55 """Called after the first get IQ""" |
56 try: | 56 try: |
57 x_elem = filter (lambda x:x.name == "x", answer.firstChildElement().elements())[0] #We only want the "x" element (data form) | 57 x_elem = filter (lambda x:x.name == "x", answer.firstChildElement().elements())[0] #We only want the "x" element (data form) |
58 except IndexError: | 58 except IndexError: |
60 #TODO: manage registration without data form | 60 #TODO: manage registration without data form |
61 answer_data={"reason": "unmanaged", "message":_("This gateway can't be managed by SàT, sorry :(")} | 61 answer_data={"reason": "unmanaged", "message":_("This gateway can't be managed by SàT, sorry :(")} |
62 answer_type = "ERROR" | 62 answer_type = "ERROR" |
63 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) | 63 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) |
64 return | 64 return |
65 | 65 |
66 form = data_form.Form.fromElement(x_elem) | 66 form = data_form.Form.fromElement(x_elem) |
67 xml_data = dataForm2xml(form) | 67 xml_data = dataForm2xml(form) |
68 self.host.bridge.actionResult("XMLUI", answer['id'], {"target":answer["from"], "type":"registration", "xml":xml_data}, profile) | 68 self.host.bridge.actionResult("XMLUI", answer['id'], {"target":answer["from"], "type":"registration", "xml":xml_data}, profile) |
69 | 69 |
70 def reg_err(self, failure, profile): | 70 def reg_err(self, failure, profile): |
73 answer_data = {} | 73 answer_data = {} |
74 answer_data['reason'] = 'unknown' | 74 answer_data['reason'] = 'unknown' |
75 answer_data={"message":"%s [code: %s]" % (failure.value.condition, unicode(failure.value))} | 75 answer_data={"message":"%s [code: %s]" % (failure.value.condition, unicode(failure.value))} |
76 answer_type = "ERROR" | 76 answer_type = "ERROR" |
77 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) | 77 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) |
78 | 78 |
79 def unregistrationAnswer(self, answer, profile): | 79 def unregistrationAnswer(self, answer, profile): |
80 debug (_("registration answer: %s") % answer.toXml()) | 80 debug (_("registration answer: %s") % answer.toXml()) |
81 answer_type = "SUCCESS" | 81 answer_type = "SUCCESS" |
82 answer_data={"message":_("Your are now unregistred")} | 82 answer_data={"message":_("Your are now unregistred")} |
83 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) | 83 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) |
84 | 84 |
85 def unregistrationFailure(self, failure, profile): | 85 def unregistrationFailure(self, failure, profile): |
86 info (_("Unregistration failure: %s") % str(failure.value)) | 86 info (_("Unregistration failure: %s") % str(failure.value)) |
87 answer_type = "ERROR" | 87 answer_type = "ERROR" |
88 answer_data = {} | 88 answer_data = {} |
89 answer_data['reason'] = 'unknown' | 89 answer_data['reason'] = 'unknown' |
90 answer_data={"message":_("Unregistration failed: %s") % failure.value.condition} | 90 answer_data={"message":_("Unregistration failed: %s") % failure.value.condition} |
91 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) | 91 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) |
92 | 92 |
93 def registrationAnswer(self, answer, profile): | 93 def registrationAnswer(self, answer, profile): |
94 debug (_("registration answer: %s") % answer.toXml()) | 94 debug (_("registration answer: %s") % answer.toXml()) |
95 answer_type = "SUCCESS" | 95 answer_type = "SUCCESS" |
96 answer_data={"message":_("Registration successfull")} | 96 answer_data={"message":_("Registration successfull")} |
97 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) | 97 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) |
98 if self.triggers.has_key(answer["from"]): | 98 if self.triggers.has_key(answer["from"]): |
99 callback,profile = self.triggers[answer["from"]] | 99 callback,profile = self.triggers[answer["from"]] |
100 callback(answer["from"], profile) | 100 callback(answer["from"], profile) |
101 del self.triggers[answer["from"]] | 101 del self.triggers[answer["from"]] |
102 | 102 |
103 def registrationFailure(self, failure, profile): | 103 def registrationFailure(self, failure, profile): |
104 info (_("Registration failure: %s") % str(failure.value)) | 104 info (_("Registration failure: %s") % str(failure.value)) |
105 print failure.value.stanza.toXml() | 105 print failure.value.stanza.toXml() |
106 answer_type = "ERROR" | 106 answer_type = "ERROR" |
107 answer_data = {} | 107 answer_data = {} |
118 def in_band_submit(self, action, target, fields, profile): | 118 def in_band_submit(self, action, target, fields, profile): |
119 """Submit a form for registration, using data_form""" | 119 """Submit a form for registration, using data_form""" |
120 id, deferred = self.host.submitForm(action, target, fields, profile) | 120 id, deferred = self.host.submitForm(action, target, fields, profile) |
121 if action == 'CANCEL': | 121 if action == 'CANCEL': |
122 deferred.addCallbacks(self.unregistrationAnswer, self.unregistrationFailure, callbackArgs=[profile], errbackArgs=[profile]) | 122 deferred.addCallbacks(self.unregistrationAnswer, self.unregistrationFailure, callbackArgs=[profile], errbackArgs=[profile]) |
123 else: | 123 else: |
124 deferred.addCallbacks(self.registrationAnswer, self.registrationFailure, callbackArgs=[profile], errbackArgs=[profile]) | 124 deferred.addCallbacks(self.registrationAnswer, self.registrationFailure, callbackArgs=[profile], errbackArgs=[profile]) |
125 return id | 125 return id |
126 | 126 |
127 def in_band_register(self, target, profile_key='@DEFAULT@'): | 127 def in_band_register(self, target, profile_key='@DEFAULT@'): |
128 """register to a target JID""" | 128 """register to a target JID""" |
129 client = self.host.getClient(profile_key) | 129 client = self.host.getClient(profile_key) |
130 if not client: | 130 if not client: |
131 error (_('Asking for an non-existant or not connected profile')) | 131 error (_('Asking for an non-existant or not connected profile')) |
135 reg_request=IQ(client.xmlstream,'get') | 135 reg_request=IQ(client.xmlstream,'get') |
136 reg_request["from"]=client.jid.full() | 136 reg_request["from"]=client.jid.full() |
137 reg_request["to"] = to_jid.full() | 137 reg_request["to"] = to_jid.full() |
138 reg_request.addElement('query', NS_REG) | 138 reg_request.addElement('query', NS_REG) |
139 reg_request.send(to_jid.full()).addCallbacks(self.reg_ok, self.reg_err, callbackArgs=[client.profile], errbackArgs=[client.profile]) | 139 reg_request.send(to_jid.full()).addCallbacks(self.reg_ok, self.reg_err, callbackArgs=[client.profile], errbackArgs=[client.profile]) |
140 return reg_request["id"] | 140 return reg_request["id"] |