comparison src/plugins/plugin_xep_0095.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
41 NS_SI = 'http://jabber.org/protocol/si' 41 NS_SI = 'http://jabber.org/protocol/si'
42 SI_REQUEST = IQ_SET + '/si[@xmlns="' + NS_SI + '"]' 42 SI_REQUEST = IQ_SET + '/si[@xmlns="' + NS_SI + '"]'
43 SI_PROFILE_HEADER = "http://jabber.org/protocol/si/profile/" 43 SI_PROFILE_HEADER = "http://jabber.org/protocol/si/profile/"
44 44
45 PLUGIN_INFO = { 45 PLUGIN_INFO = {
46 "name": "XEP 0095 Plugin", 46 "name": "XEP 0095 Plugin",
47 "import_name": "XEP-0095", 47 "import_name": "XEP-0095",
48 "type": "XEP", 48 "type": "XEP",
49 "protocols": ["XEP-0095"], 49 "protocols": ["XEP-0095"],
50 "main": "XEP_0095", 50 "main": "XEP_0095",
51 "handler": "yes", 51 "handler": "yes",
52 "description": _("""Implementation of Stream Initiation""") 52 "description": _("""Implementation of Stream Initiation""")
53 } 53 }
54
54 55
55 class XEP_0095(object): 56 class XEP_0095(object):
56 57
57 def __init__(self, host): 58 def __init__(self, host):
58 info(_("Plugin XEP_0095 initialization")) 59 info(_("Plugin XEP_0095 initialization"))
59 self.host = host 60 self.host = host
60 self.si_profiles = {} #key: SI profile, value: callback 61 self.si_profiles = {} # key: SI profile, value: callback
61 62
62 def getHandler(self, profile): 63 def getHandler(self, profile):
63 return XEP_0095_handler(self) 64 return XEP_0095_handler(self)
64 65
65 def registerSIProfile(self, si_profile, callback): 66 def registerSIProfile(self, si_profile, callback):
66 """Add a callback for a SI Profile 67 """Add a callback for a SI Profile
67 param si_profile: SI profile name (e.g. file-transfer) 68 param si_profile: SI profile name (e.g. file-transfer)
68 param callback: method to call when the profile name is asked""" 69 param callback: method to call when the profile name is asked"""
69 self.si_profiles[si_profile] = callback 70 self.si_profiles[si_profile] = callback
70 71
71 def streamInit(self, iq_el, profile): 72 def streamInit(self, iq_el, profile):
72 """This method is called on stream initiation (XEP-0095 #3.2) 73 """This method is called on stream initiation (XEP-0095 #3.2)
73 @param iq_el: IQ element 74 @param iq_el: IQ element
74 @param profile: %(doc_profile)s""" 75 @param profile: %(doc_profile)s"""
75 info (_("XEP-0095 Stream initiation")) 76 info(_("XEP-0095 Stream initiation"))
76 iq_el.handled=True 77 iq_el.handled = True
77 si_el = iq_el.firstChildElement() 78 si_el = iq_el.firstChildElement()
78 si_id = si_el.getAttribute('id') 79 si_id = si_el.getAttribute('id')
79 si_mime_type = iq_el.getAttribute('mime-type', 'application/octet-stream') 80 si_mime_type = iq_el.getAttribute('mime-type', 'application/octet-stream')
80 si_profile = si_el.getAttribute('profile') 81 si_profile = si_el.getAttribute('profile')
81 si_profile_key = si_profile[len(SI_PROFILE_HEADER):] if si_profile.startswith(SI_PROFILE_HEADER) else si_profile 82 si_profile_key = si_profile[len(SI_PROFILE_HEADER):] if si_profile.startswith(SI_PROFILE_HEADER) else si_profile
82 if self.si_profiles.has_key(si_profile_key): 83 if si_profile_key in self.si_profiles:
83 #We know this SI profile, we call the callback 84 #We know this SI profile, we call the callback
84 self.si_profiles[si_profile_key](iq_el['id'], iq_el['from'], si_id, si_mime_type, si_el, profile) 85 self.si_profiles[si_profile_key](iq_el['id'], iq_el['from'], si_id, si_mime_type, si_el, profile)
85 else: 86 else:
86 #We don't know this profile, we send an error 87 #We don't know this profile, we send an error
87 self.sendBadProfileError(iq_el['id'], iq_el['from'], profile) 88 self.sendBadProfileError(iq_el['id'], iq_el['from'], profile)
88 89
89 def sendRejectedError(self, iq_id, to_jid, reason = 'Offer Declined', profile='@NONE@'): 90 def sendRejectedError(self, iq_id, to_jid, reason='Offer Declined', profile='@NONE@'):
90 """Helper method to send when the stream is rejected 91 """Helper method to send when the stream is rejected
91 @param iq_id: IQ id 92 @param iq_id: IQ id
92 @param to_jid: recipient 93 @param to_jid: recipient
93 @param reason: human readable reason (string) 94 @param reason: human readable reason (string)
94 @param profile: %(doc_profile)s""" 95 @param profile: %(doc_profile)s"""
95 self.sendError(iq_id, to_jid, 403, 'cancel', {'text':reason}, profile=profile) 96 self.sendError(iq_id, to_jid, 403, 'cancel', {'text': reason}, profile=profile)
96 97
97 def sendBadProfileError(self, iq_id, to_jid, profile): 98 def sendBadProfileError(self, iq_id, to_jid, profile):
98 """Helper method to send when we don't know the SI profile 99 """Helper method to send when we don't know the SI profile
99 @param iq_id: IQ id 100 @param iq_id: IQ id
100 @param to_jid: recipient 101 @param to_jid: recipient
111 def sendFailedError(self, iq_id, to_jid, profile): 112 def sendFailedError(self, iq_id, to_jid, profile):
112 """Helper method to send when we transfer failed 113 """Helper method to send when we transfer failed
113 @param iq_id: IQ id 114 @param iq_id: IQ id
114 @param to_jid: recipient 115 @param to_jid: recipient
115 @param profile: %(doc_profile)s""" 116 @param profile: %(doc_profile)s"""
116 self.sendError(iq_id, to_jid, 500, 'cancel', {'custom':'failed'}, profile=profile) #as there is no error code for failed transfer, we use 500 (undefined-condition) 117 self.sendError(iq_id, to_jid, 500, 'cancel', {'custom': 'failed'}, profile=profile) # as there is no error code for failed transfer, we use 500 (undefined-condition)
117 118
118 def sendError(self, iq_id, to_jid, err_code, err_type='cancel', data={}, profile='@NONE@'): 119 def sendError(self, iq_id, to_jid, err_code, err_type='cancel', data={}, profile='@NONE@'):
119 """Send IQ error as a result 120 """Send IQ error as a result
120 @param iq_id: IQ id 121 @param iq_id: IQ id
121 @param to_jid: recipient 122 @param to_jid: recipient
129 result = domish.Element((None, 'iq')) 130 result = domish.Element((None, 'iq'))
130 result['type'] = 'result' 131 result['type'] = 'result'
131 result['id'] = iq_id 132 result['id'] = iq_id
132 result['to'] = to_jid 133 result['to'] = to_jid
133 error_el = result.addElement('error') 134 error_el = result.addElement('error')
134 error_el['err_code'] = str(err_code) 135 error_el['err_code'] = str(err_code)
135 error_el['type'] = err_type 136 error_el['type'] = err_type
136 if err_code==400 and err_type=='cancel': 137 if err_code == 400 and err_type == 'cancel':
137 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','bad-request')) 138 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas', 'bad-request'))
138 error_el.addElement((NS_SI,'no-valid-streams')) 139 error_el.addElement((NS_SI, 'no-valid-streams'))
139 elif err_code==400 and err_type=='modify': 140 elif err_code == 400 and err_type == 'modify':
140 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','bad-request')) 141 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas', 'bad-request'))
141 error_el.addElement((NS_SI,'bad-profile')) 142 error_el.addElement((NS_SI, 'bad-profile'))
142 elif err_code==403 and err_type=='cancel': 143 elif err_code == 403 and err_type == 'cancel':
143 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','forbidden')) 144 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas', 'forbidden'))
144 if data.has_key('text'): 145 if 'text' in data:
145 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','text'), content=data['text']) 146 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas', 'text'), content=data['text'])
146 elif err_code==500 and err_type=='cancel': 147 elif err_code == 500 and err_type == 'cancel':
147 condition_el = error_el.addElement((NS_SI,'undefined-condition')) 148 condition_el = error_el.addElement((NS_SI, 'undefined-condition'))
148 if data.has_key('custom') and data['custom']=='failed': 149 if 'custom' in data and data['custom'] == 'failed':
149 condition_el.addContent('Stream failed') 150 condition_el.addContent('Stream failed')
150 151
151 _client.xmlstream.send(result) 152 _client.xmlstream.send(result)
152 153
153 def acceptStream(self, iq_id, to_jid, feature_elt, misc_elts=[], profile='@NONE@'): 154 def acceptStream(self, iq_id, to_jid, feature_elt, misc_elts=[], profile='@NONE@'):
156 @param feature_elt: domish element 'feature' containing stream method to use 157 @param feature_elt: domish element 'feature' containing stream method to use
157 @param misc_elts: list of domish element to add 158 @param misc_elts: list of domish element to add
158 @param profile: %(doc_profile)s""" 159 @param profile: %(doc_profile)s"""
159 _client = self.host.getClient(profile) 160 _client = self.host.getClient(profile)
160 assert(_client) 161 assert(_client)
161 info (_("sending stream initiation accept answer")) 162 info(_("sending stream initiation accept answer"))
162 result = domish.Element((None, 'iq')) 163 result = domish.Element((None, 'iq'))
163 result['type'] = 'result' 164 result['type'] = 'result'
164 result['id'] = iq_id 165 result['id'] = iq_id
165 result['to'] = to_jid 166 result['to'] = to_jid
166 si = result.addElement('si', NS_SI) 167 si = result.addElement('si', NS_SI)
178 @param mime_type: stream mime type 179 @param mime_type: stream mime type
179 @param profile: %(doc_profile)s 180 @param profile: %(doc_profile)s
180 @return: session id, offer""" 181 @return: session id, offer"""
181 current_jid, xmlstream = self.host.getJidNStream(profile_key) 182 current_jid, xmlstream = self.host.getJidNStream(profile_key)
182 if not xmlstream: 183 if not xmlstream:
183 error (_('Asking for an non-existant or not connected profile')) 184 error(_('Asking for an non-existant or not connected profile'))
184 return "" 185 return ""
185 186
186 offer = client.IQ(xmlstream,'set') 187 offer = client.IQ(xmlstream, 'set')
187 sid = str(uuid.uuid4()) 188 sid = str(uuid.uuid4())
188 debug (_("Stream Session ID: %s") % offer["id"]) 189 debug(_("Stream Session ID: %s") % offer["id"])
189 190
190 offer["from"] = current_jid.full() 191 offer["from"] = current_jid.full()
191 offer["to"] = to_jid.full() 192 offer["to"] = to_jid.full()
192 si=offer.addElement('si',NS_SI) 193 si = offer.addElement('si', NS_SI)
193 si['id'] = sid 194 si['id'] = sid
194 si["mime-type"] = mime_type 195 si["mime-type"] = mime_type
195 si["profile"] = si_profile 196 si["profile"] = si_profile
196 for elt in misc_elts: 197 for elt in misc_elts:
197 si.addChild(elt) 198 si.addChild(elt)
207 def __init__(self, plugin_parent): 208 def __init__(self, plugin_parent):
208 self.plugin_parent = plugin_parent 209 self.plugin_parent = plugin_parent
209 self.host = plugin_parent.host 210 self.host = plugin_parent.host
210 211
211 def connectionInitialized(self): 212 def connectionInitialized(self):
212 self.xmlstream.addObserver(SI_REQUEST, self.plugin_parent.streamInit, profile = self.parent.profile) 213 self.xmlstream.addObserver(SI_REQUEST, self.plugin_parent.streamInit, profile=self.parent.profile)
213 214
214 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): 215 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
215 return [disco.DiscoFeature(NS_SI)] + [disco.DiscoFeature("http://jabber.org/protocol/si/profile/%s" % profile_name) for profile_name in self.plugin_parent.si_profiles] 216 return [disco.DiscoFeature(NS_SI)] + [disco.DiscoFeature("http://jabber.org/protocol/si/profile/%s" % profile_name) for profile_name in self.plugin_parent.si_profiles]
216 217
217 def getDiscoItems(self, requestor, target, nodeIdentifier=''): 218 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
218 return [] 219 return []
219