comparison src/plugins/plugin_exp_pipe.py @ 1409:3265a2639182

massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
author souliane <souliane@mailoo.org>
date Thu, 16 Apr 2015 14:57:57 +0200
parents 069ad98b360d
children 697effba0310
comparison
equal deleted inserted replaced
1408:8a7145138330 1409:3265a2639182
66 log.info(_("SI Pipe Transfer: TimeOut reached for id %s") % approval_id) 66 log.info(_("SI Pipe Transfer: TimeOut reached for id %s") % approval_id)
67 try: 67 try:
68 client = self.host.getClient(profile) 68 client = self.host.getClient(profile)
69 del client._pipe_waiting_for_approval[approval_id] 69 del client._pipe_waiting_for_approval[approval_id]
70 except KeyError: 70 except KeyError:
71 log.warning(_("kill id called on a non existant approval id")) 71 log.warning(_(u"kill id called on a non existant approval id"))
72 72
73 def transferRequest(self, iq_id, from_jid, si_id, si_mime_type, si_el, profile): 73 def transferRequest(self, iq_id, from_jid, si_id, si_mime_type, si_el, profile):
74 """Called when a pipe transfer is requested 74 """Called when a pipe transfer is requested
75 @param iq_id: id of the iq request 75 @param iq_id: id of the iq request
76 @param from_jid: jid of the sender 76 @param from_jid: jid of the sender
83 client = self.host.getClient(profile) 83 client = self.host.getClient(profile)
84 pipe_elts = filter(lambda elt: elt.name == 'pipe', si_el.elements()) 84 pipe_elts = filter(lambda elt: elt.name == 'pipe', si_el.elements())
85 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_el) 85 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_el)
86 86
87 if not pipe_elts: 87 if not pipe_elts:
88 log.warning(_("No pipe element found")) 88 log.warning(_(u"No pipe element found"))
89 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile) 89 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
90 return 90 return
91 91
92 if feature_elts: 92 if feature_elts:
93 feature_el = feature_elts[0] 93 feature_el = feature_elts[0]
94 data_form.Form.fromElement(feature_el.firstChildElement()) 94 data_form.Form.fromElement(feature_el.firstChildElement())
95 try: 95 try:
96 stream_method = self.host.plugins["XEP-0020"].negociate(feature_el, 'stream-method', self.managed_stream_m) 96 stream_method = self.host.plugins["XEP-0020"].negociate(feature_el, 'stream-method', self.managed_stream_m)
97 except KeyError: 97 except KeyError:
98 log.warning(_("No stream method found")) 98 log.warning(_(u"No stream method found"))
99 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile) 99 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
100 return 100 return
101 if not stream_method: 101 if not stream_method:
102 log.warning(_("Can't find a valid stream method")) 102 log.warning(_(u"Can't find a valid stream method"))
103 self.host.plugins["XEP-0095"].sendFailedError(iq_id, from_jid, profile) 103 self.host.plugins["XEP-0095"].sendFailedError(iq_id, from_jid, profile)
104 return 104 return
105 else: 105 else:
106 log.warning(_("No feature element found")) 106 log.warning(_(u"No feature element found"))
107 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile) 107 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
108 return 108 return
109 109
110 #if we are here, the transfer can start, we just need user's agreement 110 #if we are here, the transfer can start, we just need user's agreement
111 data = {"id": iq_id, "from": from_jid} 111 data = {"id": iq_id, "from": from_jid}
124 if timeout.active(): 124 if timeout.active():
125 timeout.cancel() 125 timeout.cancel()
126 try: 126 try:
127 dest_path = frontend_data['dest_path'] 127 dest_path = frontend_data['dest_path']
128 except KeyError: 128 except KeyError:
129 log.error(_('dest path not found in frontend_data')) 129 log.error(_(u'dest path not found in frontend_data'))
130 del(client._pipe_waiting_for_approval[sid]) 130 del(client._pipe_waiting_for_approval[sid])
131 return 131 return
132 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE: 132 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
133 file_obj = open(dest_path, 'w+') 133 file_obj = open(dest_path, 'w+')
134 self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed, profile) 134 self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed, profile)
135 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE: 135 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
136 file_obj = open(dest_path, 'w+') 136 file_obj = open(dest_path, 'w+')
137 self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed, profile) 137 self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed, profile)
138 else: 138 else:
139 log.error(_("Unknown stream method, this should not happen at this stage, cancelling transfer")) 139 log.error(_(u"Unknown stream method, this should not happen at this stage, cancelling transfer"))
140 del(client._pipe_waiting_for_approval[sid]) 140 del(client._pipe_waiting_for_approval[sid])
141 return 141 return
142 142
143 #we can send the iq result 143 #we can send the iq result
144 feature_elt = self.host.plugins["XEP-0020"].chooseOption({'stream-method': stream_method}) 144 feature_elt = self.host.plugins["XEP-0020"].chooseOption({'stream-method': stream_method})
145 misc_elts = [] 145 misc_elts = []
146 misc_elts.append(domish.Element((PROFILE, "file"))) 146 misc_elts.append(domish.Element((PROFILE, "file")))
147 self.host.plugins["XEP-0095"].acceptStream(data["id"], data['from'], feature_elt, misc_elts, profile) 147 self.host.plugins["XEP-0095"].acceptStream(data["id"], data['from'], feature_elt, misc_elts, profile)
148 else: 148 else:
149 log.debug(_("Transfer [%s] refused"), sid) 149 log.debug(_(u"Transfer [%s] refused"), sid)
150 self.host.plugins["XEP-0095"].sendRejectedError(data["id"], data['from'], profile=profile) 150 self.host.plugins["XEP-0095"].sendRejectedError(data["id"], data['from'], profile=profile)
151 del(client._pipe_waiting_for_approval[sid]) 151 del(client._pipe_waiting_for_approval[sid])
152 152
153 def _transferSucceeded(self, sid, file_obj, stream_method, profile): 153 def _transferSucceeded(self, sid, file_obj, stream_method, profile):
154 """Called by the stream method when transfer successfuly finished 154 """Called by the stream method when transfer successfuly finished
162 """Called when something went wrong with the transfer 162 """Called when something went wrong with the transfer
163 @param id: stream id 163 @param id: stream id
164 @param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR""" 164 @param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR"""
165 client = self.host.getClient(profile) 165 client = self.host.getClient(profile)
166 data, timeout, stream_method, failed_methods, profile = client._pipe_waiting_for_approval[sid] 166 data, timeout, stream_method, failed_methods, profile = client._pipe_waiting_for_approval[sid]
167 log.warning(_('Transfer %(id)s failed with stream method %(s_method)s') % {'id': sid, 167 log.warning(_(u'Transfer %(id)s failed with stream method %(s_method)s') % {'id': sid,
168 's_method': stream_method}) 168 's_method': stream_method})
169 # filepath = file_obj.name 169 # filepath = file_obj.name
170 file_obj.close() 170 file_obj.close()
171 #TODO: session remenber (within a time limit) when a stream method fail, and avoid that stream method with full jid for the rest of the session 171 #TODO: session remenber (within a time limit) when a stream method fail, and avoid that stream method with full jid for the rest of the session
172 log.warning(_("All stream methods failed, can't transfer the file")) 172 log.warning(_(u"All stream methods failed, can't transfer the file"))
173 del(client._pipe_waiting_for_approval[sid]) 173 del(client._pipe_waiting_for_approval[sid])
174 174
175 def pipeCb(self, filepath, sid, profile, IQ): 175 def pipeCb(self, filepath, sid, profile, IQ):
176 if IQ['type'] == "error": 176 if IQ['type'] == "error":
177 stanza_err = jabber.error.exceptionFromStanza(IQ) 177 stanza_err = jabber.error.exceptionFromStanza(IQ)
178 if stanza_err.code == '403' and stanza_err.condition == 'forbidden': 178 if stanza_err.code == '403' and stanza_err.condition == 'forbidden':
179 log.debug(_("Pipe transfer refused by %s") % IQ['from']) 179 log.debug(_(u"Pipe transfer refused by %s") % IQ['from'])
180 self.host.bridge.newAlert(_("The contact %s refused your pipe stream") % IQ['from'], _("Pipe stream refused"), "INFO", profile) 180 self.host.bridge.newAlert(_("The contact %s refused your pipe stream") % IQ['from'], _("Pipe stream refused"), "INFO", profile)
181 else: 181 else:
182 log.warning(_("Error during pipe stream transfer with %s") % IQ['from']) 182 log.warning(_(u"Error during pipe stream transfer with %s") % IQ['from'])
183 self.host.bridge.newAlert(_("Something went wrong during the pipe stream session intialisation with %s") % IQ['from'], _("Pipe stream error"), "ERROR", profile) 183 self.host.bridge.newAlert(_("Something went wrong during the pipe stream session intialisation with %s") % IQ['from'], _("Pipe stream error"), "ERROR", profile)
184 return 184 return
185 185
186 si_elt = IQ.firstChildElement() 186 si_elt = IQ.firstChildElement()
187 187
188 if IQ['type'] != "result" or not si_elt or si_elt.name != "si": 188 if IQ['type'] != "result" or not si_elt or si_elt.name != "si":
189 log.error(_("Protocol error during file transfer")) 189 log.error(_(u"Protocol error during file transfer"))
190 return 190 return
191 191
192 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_elt) 192 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_elt)
193 if not feature_elts: 193 if not feature_elts:
194 log.warning(_("No feature element")) 194 log.warning(_(u"No feature element"))
195 return 195 return
196 196
197 choosed_options = self.host.plugins["XEP-0020"].getChoosedOptions(feature_elts[0]) 197 choosed_options = self.host.plugins["XEP-0020"].getChoosedOptions(feature_elts[0])
198 try: 198 try:
199 stream_method = choosed_options["stream-method"] 199 stream_method = choosed_options["stream-method"]
200 except KeyError: 200 except KeyError:
201 log.warning(_("No stream method choosed")) 201 log.warning(_(u"No stream method choosed"))
202 return 202 return
203 203
204 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE: 204 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
205 #fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender 205 #fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender
206 #file_obj = os.fdopen(fd, 'r') 206 #file_obj = os.fdopen(fd, 'r')
210 #fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender 210 #fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender
211 #file_obj = os.fdopen(fd, 'r') 211 #file_obj = os.fdopen(fd, 'r')
212 file_obj = open(filepath, 'r') # XXX: we have to be sure that filepath is well opened, as reading can block it 212 file_obj = open(filepath, 'r') # XXX: we have to be sure that filepath is well opened, as reading can block it
213 self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, None, self.sendSuccessCb, self.sendFailureCb, None, profile) 213 self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, None, self.sendSuccessCb, self.sendFailureCb, None, profile)
214 else: 214 else:
215 log.warning(_("Invalid stream method received")) 215 log.warning(_(u"Invalid stream method received"))
216 216
217 def pipeOut(self, to_jid, filepath, data={}, profile_key=C.PROF_KEY_NONE): 217 def pipeOut(self, to_jid, filepath, data={}, profile_key=C.PROF_KEY_NONE):
218 """send a file using EXP-PIPE 218 """send a file using EXP-PIPE
219 @to_jid: recipient 219 @to_jid: recipient
220 @filepath: absolute path to the named pipe to send 220 @filepath: absolute path to the named pipe to send
222 @param profile_key: %(doc_profile_key)s 222 @param profile_key: %(doc_profile_key)s
223 @return: an unique id to identify the transfer 223 @return: an unique id to identify the transfer
224 """ 224 """
225 profile = self.host.memory.getProfileName(profile_key) 225 profile = self.host.memory.getProfileName(profile_key)
226 if not profile: 226 if not profile:
227 log.warning(_("Trying to send a file from an unknown profile")) 227 log.warning(_(u"Trying to send a file from an unknown profile"))
228 return "" 228 return ""
229 feature_elt = self.host.plugins["XEP-0020"].proposeFeatures({'stream-method': self.managed_stream_m}) 229 feature_elt = self.host.plugins["XEP-0020"].proposeFeatures({'stream-method': self.managed_stream_m})
230 230
231 pipe_transfer_elts = [] 231 pipe_transfer_elts = []
232 232
241 log.info(_('Transfer %s successfuly finished') % sid) 241 log.info(_('Transfer %s successfuly finished') % sid)
242 file_obj.close() 242 file_obj.close()
243 243
244 def sendFailureCb(self, sid, file_obj, stream_method, reason, profile): 244 def sendFailureCb(self, sid, file_obj, stream_method, reason, profile):
245 file_obj.close() 245 file_obj.close()
246 log.warning(_('Transfer %(id)s failed with stream method %(s_method)s %(profile)s') % {'id': sid, "s_method": stream_method, "profile": profile}) 246 log.warning(_(u'Transfer %(id)s failed with stream method %(s_method)s %(profile)s') % {'id': sid, "s_method": stream_method, "profile": profile})