comparison src/plugins/plugin_xep_0077.py @ 594:e629371a28d3

Fix pep8 support in src/plugins.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:35 +0100
parents beaf6bec2fcd
children 84a6e83157c2
comparison
equal deleted inserted replaced
593:70bae685d05c 594:e629371a28d3
27 from wokkel import data_form 27 from wokkel import data_form
28 28
29 NS_REG = 'jabber:iq:register' 29 NS_REG = 'jabber:iq:register'
30 30
31 PLUGIN_INFO = { 31 PLUGIN_INFO = {
32 "name": "XEP 0077 Plugin", 32 "name": "XEP 0077 Plugin",
33 "import_name": "XEP-0077", 33 "import_name": "XEP-0077",
34 "type": "XEP", 34 "type": "XEP",
35 "protocols": ["XEP-0077"], 35 "protocols": ["XEP-0077"],
36 "dependencies": [], 36 "dependencies": [],
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
41 class XEP_0077(object): 42 class XEP_0077(object):
42 43
43 def __init__(self, host): 44 def __init__(self, host):
44 info(_("Plugin XEP_0077 initialization")) 45 info(_("Plugin XEP_0077 initialization"))
45 self.host = host 46 self.host = host
46 self.triggers = {} #used by other protocol (e.g. XEP-0100) to finish registration. key = target_jid 47 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) 48 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) 49 host.bridge.addMethod("in_band_submit", ".plugin", in_sign='ssa(ss)s', out_sign='s', method=self.in_band_submit)
49 50
50 def addTrigger(self, target, cb, profile): 51 def addTrigger(self, target, cb, profile):
51 """Add a callback which is called when registration to target is successful""" 52 """Add a callback which is called when registration to target is successful"""
52 self.triggers[target] = (cb, profile) 53 self.triggers[target] = (cb, profile)
53 54
54 def reg_ok(self, answer, profile): 55 def reg_ok(self, answer, profile):
55 """Called after the first get IQ""" 56 """Called after the first get IQ"""
56 try: 57 try:
57 x_elem = filter (lambda x:x.name == "x", answer.firstChildElement().elements())[0] #We only want the "x" element (data form) 58 x_elem = filter(lambda x: x.name == "x", answer.firstChildElement().elements())[0] # We only want the "x" element (data form)
58 except IndexError: 59 except IndexError:
59 info(_("No data form found")) 60 info(_("No data form found"))
60 #TODO: manage registration without data form 61 #TODO: manage registration without data form
61 answer_data={"reason": "unmanaged", "message":_("This gateway can't be managed by SàT, sorry :(")} 62 answer_data = {"reason": "unmanaged", "message": _("This gateway can't be managed by SàT, sorry :(")}
62 answer_type = "ERROR" 63 answer_type = "ERROR"
63 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) 64 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile)
64 return 65 return
65 66
66 form = data_form.Form.fromElement(x_elem) 67 form = data_form.Form.fromElement(x_elem)
67 xml_data = dataForm2xml(form) 68 xml_data = dataForm2xml(form)
68 self.host.bridge.actionResult("XMLUI", answer['id'], {"target":answer["from"], "type":"registration", "xml":xml_data}, profile) 69 self.host.bridge.actionResult("XMLUI", answer['id'], {"target": answer["from"], "type": "registration", "xml": xml_data}, profile)
69 70
70 def reg_err(self, failure, profile): 71 def reg_err(self, failure, profile):
71 """Called when something is wrong with registration""" 72 """Called when something is wrong with registration"""
72 info (_("Registration failure: %s") % str(failure.value)) 73 info(_("Registration failure: %s") % str(failure.value))
73 answer_data = {} 74 answer_data = {}
74 answer_data['reason'] = 'unknown' 75 answer_data['reason'] = 'unknown'
75 answer_data={"message":"%s [code: %s]" % (failure.value.condition, unicode(failure.value))} 76 answer_data = {"message": "%s [code: %s]" % (failure.value.condition, unicode(failure.value))}
76 answer_type = "ERROR" 77 answer_type = "ERROR"
77 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) 78 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile)
78 79
79 def unregistrationAnswer(self, answer, profile): 80 def unregistrationAnswer(self, answer, profile):
80 debug (_("registration answer: %s") % answer.toXml()) 81 debug(_("registration answer: %s") % answer.toXml())
81 answer_type = "SUCCESS" 82 answer_type = "SUCCESS"
82 answer_data={"message":_("Your are now unregistred")} 83 answer_data = {"message": _("Your are now unregistred")}
83 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) 84 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile)
84 85
85 def unregistrationFailure(self, failure, profile): 86 def unregistrationFailure(self, failure, profile):
86 info (_("Unregistration failure: %s") % str(failure.value)) 87 info(_("Unregistration failure: %s") % str(failure.value))
87 answer_type = "ERROR" 88 answer_type = "ERROR"
88 answer_data = {} 89 answer_data = {}
89 answer_data['reason'] = 'unknown' 90 answer_data['reason'] = 'unknown'
90 answer_data={"message":_("Unregistration failed: %s") % failure.value.condition} 91 answer_data = {"message": _("Unregistration failed: %s") % failure.value.condition}
91 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) 92 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile)
92 93
93 def registrationAnswer(self, answer, profile): 94 def registrationAnswer(self, answer, profile):
94 debug (_("registration answer: %s") % answer.toXml()) 95 debug(_("registration answer: %s") % answer.toXml())
95 answer_type = "SUCCESS" 96 answer_type = "SUCCESS"
96 answer_data={"message":_("Registration successfull")} 97 answer_data = {"message": _("Registration successfull")}
97 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile) 98 self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile)
98 if self.triggers.has_key(answer["from"]): 99 if answer["from"] in self.triggers:
99 callback,profile = self.triggers[answer["from"]] 100 callback, profile = self.triggers[answer["from"]]
100 callback(answer["from"], profile) 101 callback(answer["from"], profile)
101 del self.triggers[answer["from"]] 102 del self.triggers[answer["from"]]
102 103
103 def registrationFailure(self, failure, profile): 104 def registrationFailure(self, failure, profile):
104 info (_("Registration failure: %s") % str(failure.value)) 105 info(_("Registration failure: %s") % str(failure.value))
105 print failure.value.stanza.toXml() 106 print failure.value.stanza.toXml()
106 answer_type = "ERROR" 107 answer_type = "ERROR"
107 answer_data = {} 108 answer_data = {}
108 if failure.value.condition == 'conflict': 109 if failure.value.condition == 'conflict':
109 answer_data['reason'] = 'conflict' 110 answer_data['reason'] = 'conflict'
110 answer_data={"message":_("Username already exists, please choose an other one")} 111 answer_data = {"message": _("Username already exists, please choose an other one")}
111 else: 112 else:
112 answer_data['reason'] = 'unknown' 113 answer_data['reason'] = 'unknown'
113 answer_data={"message":_("Registration failed")} 114 answer_data = {"message": _("Registration failed")}
114 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile) 115 self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile)
115 if failure.value.stanza["from"] in self.triggers.has_key: 116 if failure.value.stanza["from"] in self.triggers:
116 del self.triggers[failure.value.stanza["from"]] 117 del self.triggers[failure.value.stanza["from"]]
117 118
118 def in_band_submit(self, action, target, fields, profile): 119 def in_band_submit(self, action, target, fields, profile):
119 """Submit a form for registration, using data_form""" 120 """Submit a form for registration, using data_form"""
120 id, deferred = self.host.submitForm(action, target, fields, profile) 121 id, deferred = self.host.submitForm(action, target, fields, profile)
126 127
127 def in_band_register(self, target, profile_key='@DEFAULT@'): 128 def in_band_register(self, target, profile_key='@DEFAULT@'):
128 """register to a target JID""" 129 """register to a target JID"""
129 client = self.host.getClient(profile_key) 130 client = self.host.getClient(profile_key)
130 if not client: 131 if not client:
131 error (_('Asking for an non-existant or not connected profile')) 132 error(_('Asking for an non-existant or not connected profile'))
132 return "" 133 return ""
133 to_jid = jid.JID(target) 134 to_jid = jid.JID(target)
134 debug(_("Asking registration for [%s]") % to_jid.full()) 135 debug(_("Asking registration for [%s]") % to_jid.full())
135 reg_request=IQ(client.xmlstream,'get') 136 reg_request = IQ(client.xmlstream, 'get')
136 reg_request["from"]=client.jid.full() 137 reg_request["from"] = client.jid.full()
137 reg_request["to"] = to_jid.full() 138 reg_request["to"] = to_jid.full()
138 reg_request.addElement('query', NS_REG) 139 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]) 140 reg_request.send(to_jid.full()).addCallbacks(self.reg_ok, self.reg_err, callbackArgs=[client.profile], errbackArgs=[client.profile])
140 return reg_request["id"] 141 return reg_request["id"]