comparison src/plugins/plugin_xep_0095.py @ 993:301b342c697a

core: use of the new core.log module: /!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author Goffi <goffi@goffi.org>
date Sat, 19 Apr 2014 19:19:19 +0200
parents c6d8fc63b1db
children 069ad98b360d
comparison
equal deleted inserted replaced
992:f51a1895275c 993:301b342c697a
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from logging import debug, info, error 22 from sat.core.log import getLogger
23 log = getLogger(__name__)
23 from twisted.words.xish import domish 24 from twisted.words.xish import domish
24 from twisted.words.protocols.jabber import client 25 from twisted.words.protocols.jabber import client
25 import uuid 26 import uuid
26 27
27 from zope.interface import implements 28 from zope.interface import implements
50 51
51 52
52 class XEP_0095(object): 53 class XEP_0095(object):
53 54
54 def __init__(self, host): 55 def __init__(self, host):
55 info(_("Plugin XEP_0095 initialization")) 56 log.info(_("Plugin XEP_0095 initialization"))
56 self.host = host 57 self.host = host
57 self.si_profiles = {} # key: SI profile, value: callback 58 self.si_profiles = {} # key: SI profile, value: callback
58 59
59 def getHandler(self, profile): 60 def getHandler(self, profile):
60 return XEP_0095_handler(self) 61 return XEP_0095_handler(self)
67 68
68 def streamInit(self, iq_el, profile): 69 def streamInit(self, iq_el, profile):
69 """This method is called on stream initiation (XEP-0095 #3.2) 70 """This method is called on stream initiation (XEP-0095 #3.2)
70 @param iq_el: IQ element 71 @param iq_el: IQ element
71 @param profile: %(doc_profile)s""" 72 @param profile: %(doc_profile)s"""
72 info(_("XEP-0095 Stream initiation")) 73 log.info(_("XEP-0095 Stream initiation"))
73 iq_el.handled = True 74 iq_el.handled = True
74 si_el = iq_el.firstChildElement() 75 si_el = iq_el.firstChildElement()
75 si_id = si_el.getAttribute('id') 76 si_id = si_el.getAttribute('id')
76 si_mime_type = iq_el.getAttribute('mime-type', 'application/octet-stream') 77 si_mime_type = iq_el.getAttribute('mime-type', 'application/octet-stream')
77 si_profile = si_el.getAttribute('profile') 78 si_profile = si_el.getAttribute('profile')
108 def sendFailedError(self, iq_id, to_jid, profile): 109 def sendFailedError(self, iq_id, to_jid, profile):
109 """Helper method to send when we transfer failed 110 """Helper method to send when we transfer failed
110 @param iq_id: IQ id 111 @param iq_id: IQ id
111 @param to_jid: recipient 112 @param to_jid: recipient
112 @param profile: %(doc_profile)s""" 113 @param profile: %(doc_profile)s"""
113 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) 114 self.sendError(iq_id, to_jid, 500, 'cancel', {'custom': 'failed'}, profile=profile) # as there is no lerror code for failed transfer, we use 500 (undefined-condition)
114 115
115 def sendError(self, iq_id, to_jid, err_code, err_type='cancel', data={}, profile=C.PROF_KEY_NONE): 116 def sendError(self, iq_id, to_jid, err_code, err_type='cancel', data={}, profile=C.PROF_KEY_NONE):
116 """Send IQ error as a result 117 """Send IQ error as a result
117 @param iq_id: IQ id 118 @param iq_id: IQ id
118 @param to_jid: recipient 119 @param to_jid: recipient
152 @param feature_elt: domish element 'feature' containing stream method to use 153 @param feature_elt: domish element 'feature' containing stream method to use
153 @param misc_elts: list of domish element to add 154 @param misc_elts: list of domish element to add
154 @param profile: %(doc_profile)s""" 155 @param profile: %(doc_profile)s"""
155 _client = self.host.getClient(profile) 156 _client = self.host.getClient(profile)
156 assert(_client) 157 assert(_client)
157 info(_("sending stream initiation accept answer")) 158 log.info(_("sending stream initiation accept answer"))
158 result = domish.Element((None, 'iq')) 159 result = domish.Element((None, 'iq'))
159 result['type'] = 'result' 160 result['type'] = 'result'
160 result['id'] = iq_id 161 result['id'] = iq_id
161 result['to'] = to_jid 162 result['to'] = to_jid
162 si = result.addElement('si', NS_SI) 163 si = result.addElement('si', NS_SI)
174 @param mime_type: stream mime type 175 @param mime_type: stream mime type
175 @param profile: %(doc_profile)s 176 @param profile: %(doc_profile)s
176 @return: session id, offer""" 177 @return: session id, offer"""
177 current_jid, xmlstream = self.host.getJidNStream(profile_key) 178 current_jid, xmlstream = self.host.getJidNStream(profile_key)
178 if not xmlstream: 179 if not xmlstream:
179 error(_('Asking for an non-existant or not connected profile')) 180 log.error(_('Asking for an non-existant or not connected profile'))
180 return "" 181 return ""
181 182
182 offer = client.IQ(xmlstream, 'set') 183 offer = client.IQ(xmlstream, 'set')
183 sid = str(uuid.uuid4()) 184 sid = str(uuid.uuid4())
184 debug(_("Stream Session ID: %s") % offer["id"]) 185 log.debug(_("Stream Session ID: %s") % offer["id"])
185 186
186 offer["from"] = current_jid.full() 187 offer["from"] = current_jid.full()
187 offer["to"] = to_jid.full() 188 offer["to"] = to_jid.full()
188 si = offer.addElement('si', NS_SI) 189 si = offer.addElement('si', NS_SI)
189 si['id'] = sid 190 si['id'] = sid