annotate src/plugins/plugin_exp_pipe.py @ 412:62b17854254e

database integration: first draft - using SQLite as backend /!\ Not finished yet, break execution. SàT CAN'T LAUNCH PROPERLY IN THE CURRENT STATE
author Goffi <goffi@goffi.org>
date Sun, 30 Oct 2011 23:13:40 +0100
parents b2caa2615c4c
children cf005701624b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
3
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for managing pipes (experimental)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
7
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
12
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
17
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
21
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, warning, error
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import protocol
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import client, jid
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import error as jab_error
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import os, os.path
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.internet import reactor
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import pdb
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
30
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from zope.interface import implements
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
32
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from wokkel import disco, iwokkel, data_form
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
34
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
35 IQ_SET = '/iq[@type="set"]'
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
36 PROFILE_NAME = "pipe-transfer"
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
37 PROFILE = "http://jabber.org/protocol/si/profile/" + PROFILE_NAME
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
38
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
39 PLUGIN_INFO = {
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
40 "name": "Pipe Plugin",
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
41 "import_name": "EXP-PIPE",
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
42 "type": "EXP",
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
43 "protocols": ["EXP-PIPE"],
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "dependencies": ["XEP-0020", "XEP-0095", "XEP-0065", "XEP-0047"],
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "main": "Exp_Pipe",
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "handler": "no",
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "description": _("""Implementation of SI Pipe Transfer""")
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
48 }
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
49
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
50 class Exp_Pipe():
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
51 """This is a modified version of XEP-0096 to work with named pipes instead of files"""
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
52
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def __init__(self, host):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
54 info(_("Plugin Pipe initialization"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.host = host
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self._waiting_for_approval = {} #key = id, value = [transfer data, IdelayedCall Reactor timeout,
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
57 # current stream method, [failed stream methods], profile]
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.managed_stream_m = [self.host.plugins["XEP-0065"].NAMESPACE,
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.host.plugins["XEP-0047"].NAMESPACE] #Stream methods managed
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.host.plugins["XEP-0095"].registerSIProfile(PROFILE_NAME, self.transferRequest)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
61 host.bridge.addMethod("pipeOut", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.pipeOut)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
62
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def _kill_id(self, approval_id):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
64 """Delete a waiting_for_approval id, called after timeout
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
65 @param approval_id: id of _waiting_for_approval"""
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
66 info(_("SI Pipe Transfer: TimeOut reached for id %s") % approval_id);
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
67 try:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
68 del self._waiting_for_approval[approval_id]
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
69 except KeyError:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
70 warning(_("kill id called on a non existant approval id"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
71
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
72 def transferRequest(self, iq_id, from_jid, si_id, si_mime_type, si_el, profile):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
73 """Called when a pipe transfer is requested
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
74 @param iq_id: id of the iq request
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @param from_jid: jid of the sender
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @param si_id: Stream Initiation session id
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
77 @param si_mime_type: Mime type of the pipe (or default "application/octet-stream" if unknown)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
78 @param si_el: domish.Element of the request
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
79 @param profile: %(doc_profile)s"""
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
80 info (_("EXP-PIPE file transfer requested"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
81 debug(si_el.toXml())
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
82 pipe_elts = filter(lambda elt: elt.name == 'pipe', si_el.elements())
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
83 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_el)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
84
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if not pipe_elts:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
86 warning(_("No pipe element found"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
88 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
89
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
90 if feature_elts:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
91 feature_el = feature_elts[0]
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
92 form = data_form.Form.fromElement(feature_el.firstChildElement())
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
93 try:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
94 stream_method = self.host.plugins["XEP-0020"].negociate(feature_el, 'stream-method',self.managed_stream_m)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
95 except KeyError:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
96 warning(_("No stream method found"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
97 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
98 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if not stream_method:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
100 warning(_("Can't find a valid stream method"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
101 self.host.plugins["XEP-0095"].sendFailedError(iq_id, from_jid, profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
102 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
103 else:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
104 warning(_("No feature element found"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
105 self.host.plugins["XEP-0095"].sendBadRequestError(iq_id, from_jid, profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
106 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
107
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
108 #if we are here, the transfer can start, we just need user's agreement
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
109 data={ "id": iq_id, "from":from_jid }
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
110 self._waiting_for_approval[si_id] = [data, reactor.callLater(300, self._kill_id, si_id), stream_method, [], profile]
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
111
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
112 self.host.askConfirmation(si_id, "PIPE_TRANSFER", data, self.confirmationCB)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
113
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
114
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
115 def confirmationCB(self, sid, accepted, frontend_data):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
116 """Called on confirmation answer
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
117 @param sid: file transfer session id
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
118 @param accepted: True if file transfer is accepted
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
119 @param frontend_data: data sent by frontend"""
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
120 data, timeout, stream_method, failed_methods, profile = self._waiting_for_approval[sid]
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
121 if accepted:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
122 if timeout.active():
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
123 timeout.cancel()
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
124 try:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
125 dest_path = frontend_data['dest_path']
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
126 except KeyError:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
127 error(_('dest path not found in frontend_data'))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
128 del(self._waiting_for_approval[sid])
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
130 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
131 file_obj = open(dest_path, 'w+')
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
132 self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
133 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
134 file_obj = open(dest_path, 'w+')
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
135 self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
136 else:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
137 error(_("Unknown stream method, this should not happen at this stage, cancelling transfer"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
138 del(self._waiting_for_approval[sid])
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
139 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
140
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
141 #we can send the iq result
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
142 feature_elt = self.host.plugins["XEP-0020"].chooseOption({'stream-method':stream_method})
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
143 misc_elts = []
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
144 misc_elts.append(domish.Element((PROFILE, "file")))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
145 self.host.plugins["XEP-0095"].acceptStream(data["id"], data['from'], feature_elt, misc_elts, profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
146 else:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
147 debug (_("Transfer [%s] refused"), sid)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
148 self.host.plugins["XEP-0095"].sendRejectedError (data["id"], data['from'], profile=profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
149 del(self._waiting_for_approval[sid])
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
150
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
151 def _transferSucceeded(self, sid, file_obj, stream_method):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
152 """Called by the stream method when transfer successfuly finished
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
153 @param id: stream id"""
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
154 file_obj.close()
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
155 info(_('Transfer %s successfuly finished') % sid)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
156 del(self._waiting_for_approval[sid])
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
157
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
158 def _transferFailed(self, sid, file_obj, stream_method, reason):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
159 """Called when something went wrong with the transfer
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
160 @param id: stream id
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
161 @param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR"""
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
162 data, timeout, stream_method, failed_methods, profile = self._waiting_for_approval[sid]
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
163 warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid,
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
164 's_method': stream_method })
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
165 filepath = file_obj.name
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
166 file_obj.close()
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
167 #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
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
168 warning(_("All stream methods failed, can't transfer the file"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
169 del(self._waiting_for_approval[sid])
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
170
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
171 def pipeCb(self, profile, filepath, sid, IQ):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
172 if IQ['type'] == "error":
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
173 stanza_err = jab_error.exceptionFromStanza(IQ)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if stanza_err.code == '403' and stanza_err.condition == 'forbidden':
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
175 debug(_("Pipe transfer refused by %s") % IQ['from'])
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
176 self.host.bridge.newAlert(_("The contact %s refused your pipe stream") % IQ['from'], _("Pipe stream refused"), "INFO", profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
177 else:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
178 warning(_("Error during pipe stream transfer with %s") % IQ['from'])
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
179 self.host.bridge.newAlert(_("Something went wrong during the pipe stream session intialisation with %s") % IQ['from'], _("Pipe stream error"), "ERROR", profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
180 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
181
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
182 si_elt = IQ.firstChildElement()
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
183
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
184 if IQ['type'] != "result" or not si_elt or si_elt.name != "si":
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
185 error(_("Protocol error during file transfer"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
186 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
187
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
188 feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_elt)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
189 if not feature_elts:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
190 warning(_("No feature element"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
191 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
192
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
193 choosed_options = self.host.plugins["XEP-0020"].getChoosedOptions(feature_elts[0])
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
194 try:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
195 stream_method = choosed_options["stream-method"]
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
196 except KeyError:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
197 warning(_("No stream method choosed"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
198 return
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
199
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
200 if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
201 #fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
202 #file_obj = os.fdopen(fd, 'r')
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
203 file_obj = open(filepath, 'r') #XXX: we have to be sure that filepath is well opened, as reading can block it
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
204 self.host.plugins["XEP-0065"].startStream(file_obj, jid.JID(IQ['from']), sid, None, self.sendSuccessCb, self.sendFailureCb, None, profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
205 elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
206 #fd = os.open(filepath, os.O_RDONLY | os.O_NONBLOCK) #XXX: non blocking openingl cause issues with XEP-0065's FileSender
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
207 #file_obj = os.fdopen(fd, 'r')
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
208 file_obj = open(filepath, 'r') #XXX: we have to be sure that filepath is well opened, as reading can block it
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
209 self.host.plugins["XEP-0047"].startStream(file_obj, jid.JID(IQ['from']), sid, None, self.sendSuccessCb, self.sendFailureCb, None, profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
210 else:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
211 warning(_("Invalid stream method received"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
212
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
213 def pipeOut(self, to_jid, filepath, data={}, profile_key='@DEFAULT@'):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
214 """send a file using EXP-PIPE
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
215 @to_jid: recipient
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
216 @filepath: absolute path to the named pipe to send
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
217 @data: dictionnary with the optional data
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
218 @param profile_key: %(doc_profile_key)s
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
219 @return: an unique id to identify the transfer
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
220 """
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
221 profile = self.host.memory.getProfileName(profile_key)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
222 if not profile:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
223 warning(_("Trying to send a file from an unknown profile"))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
224 return ""
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
225 feature_elt = self.host.plugins["XEP-0020"].proposeFeatures({'stream-method': self.managed_stream_m})
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
226
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
227 pipe_transfer_elts = []
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
228
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
229 pipe_elt = domish.Element((PROFILE, 'pipe'))
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
230 pipe_transfer_elts.append(pipe_elt)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
231
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
232 sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, pipe_transfer_elts, profile_key = profile)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
233 offer.addCallback(self.pipeCb, profile, filepath, sid)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
234 return sid
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
235
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
236 def sendSuccessCb(self, sid, file_obj, stream_method):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
237 info(_('Transfer %s successfuly finished') % sid)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
238 file_obj.close()
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
239
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
240 def sendFailureCb(self, sid, file_obj, stream_method, reason):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
241 file_obj.close()
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
diff changeset
242 warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid, "s_method": stream_method })