Mercurial > libervia-backend
annotate frontends/src/jp/jp @ 401:b2caa2615c4c
jp roster name manegement + Pipe transfer
- added experimental pipe transfer protocol (based on xep-0096)
- jp now manage roster name to jid conversion (if an argument correspond to a roster name, it replace it with the corresponding jid)
- jp now add last known resource of when a resource is needed but we have a bare jid (e.g. for file or pipe transfer)
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 07 Oct 2011 00:25:15 +0200 |
parents | 393b35aa86d2 |
children | f03688bdb858 |
rev | line source |
---|---|
0 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 jp: a SAT command line tool | |
228 | 6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) |
0 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 #consts | |
191 | 23 name = u"jp" |
228 | 24 about = name+u""" v%s (c) Jérôme Poisson (aka Goffi) 2009, 2010, 2011 |
0 | 25 |
26 --- | |
228 | 27 """+name+u""" Copyright (C) 2009, 2010, 2011 Jérôme Poisson (aka Goffi) |
0 | 28 This program comes with ABSOLUTELY NO WARRANTY; |
29 This is free software, and you are welcome to redistribute it | |
30 under certain conditions. | |
31 --- | |
32 | |
33 This software is a command line tool for jabber | |
34 Get the latest version at http://www.goffi.org | |
35 """ | |
36 | |
37 global pbar_available | |
38 pbar_available = True #checked before using ProgressBar | |
39 | |
40 ### logging ### | |
41 import logging | |
42 from logging import debug, info, error, warning | |
43 logging.basicConfig(level=logging.DEBUG, | |
44 format='%(message)s') | |
45 ### | |
46 | |
70 | 47 import gettext |
48 gettext.install('jp', "i18n", unicode=True) | |
49 | |
0 | 50 import sys |
51 import os | |
52 from os.path import abspath, basename, dirname | |
53 from optparse import OptionParser | |
54 import pdb | |
236 | 55 from sat.tools.jid import JID |
0 | 56 import gobject |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
57 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService |
0 | 58 import tarfile |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
59 import tempfile |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
60 import shutil |
0 | 61 try: |
62 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed | |
63 except ImportError, e: | |
70 | 64 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) |
65 info (_('Progress bar deactivated\n--\n')) | |
0 | 66 pbar_available=False |
67 | |
68 | |
69 | |
70 | |
71 class JP(): | |
72 def __init__(self): | |
165
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
73 try: |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
74 self.bridge=DBusBridgeFrontend() |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
75 except BridgeExceptionNoService: |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
76 print(_(u"Can't connect to SàT backend, are you sure it's launched ?")) |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
77 import sys |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
78 sys.exit(1) |
391 | 79 self.transfer_id = None |
0 | 80 |
81 def check_options(self): | |
82 """Check command line options""" | |
70 | 83 usage=_(""" |
0 | 84 %prog [options] [FILE1 FILE2 ...] JID |
85 %prog -w [options] [JID1 JID2 ...] | |
86 | |
87 %prog --help for options list | |
70 | 88 """) |
191 | 89 version = unicode(self.bridge.getVersion()) |
90 parser = OptionParser(usage=usage,version=about % version) | |
0 | 91 |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
92 parser.add_option("-p", "--profile", action="store", type="string", default='@DEFAULT@', |
156
a216dfbb0d50
jp: added default value in --profile option's help
Goffi <goffi@goffi.org>
parents:
110
diff
changeset
|
93 help=_("Use PROFILE profile key (default: %default)")) |
0 | 94 parser.add_option("-b", "--bz2", action="store_true", default=False, |
70 | 95 help=_("Make a bzip2 tarball")) |
0 | 96 parser.add_option("-w", "--wait-file", action="store_true", default=False, |
70 | 97 help=_("Wait for a file to be sent by a contact")) |
0 | 98 parser.add_option("-m", "--multiple", action="store_true", default=False, |
70 | 99 help=_("Accept multiple files (you'll have to stop manually)")) |
0 | 100 parser.add_option("-f", "--force", action="store_true", default=False, |
70 | 101 help=_("Force overwritting of existing files")) |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
102 parser.add_option("-g", "--progress", action="store_true", default=False, |
70 | 103 help=_("Show progress bar")) |
0 | 104 parser.add_option("-s", "--separate", action="store_true", default=False, |
70 | 105 help=_("Separate xmpp messages: send one message per line instead of one message alone.")) |
0 | 106 parser.add_option("-n", "--new-line", action="store_true", default=False, |
70 | 107 help=_("Add a new line at the beginning of the input (usefull for ascii art ;))")) |
393 | 108 parser.add_option("--connect", action="store_true", default=False, |
109 help=_("Connect the profile before doing anything else")) | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
110 parser.add_option("--pipe-in", action="store_true", default=False, |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
111 help=_("Wait for the reception of a pipe stream")) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
112 parser.add_option("--pipe-out", action="store_true", default=False, |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
113 help=_("Pipe a stream out ")) |
0 | 114 |
115 (self.options, args) = parser.parse_args() | |
116 | |
117 if len(args) < 1 and not self.options.wait_file: | |
70 | 118 parser.error(_("You must specify the destination JID (Jabber ID)").encode('utf-8')) |
0 | 119 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
120 if self.options.wait_file or self.options.pipe_in: |
0 | 121 #several jid |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
122 self.dest_jids = [arg.decode('utf-8') for arg in args] |
0 | 123 else: |
124 #one dest_jid, other args are files | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
125 self.dest_jid = JID(args[-1].decode('utf-8')) |
0 | 126 self.files = args[:-1] |
127 | |
128 if not pbar_available and self.options.progress: | |
129 self.options.progress = False | |
70 | 130 error (_("Option progress is not available, deactivated.")) |
0 | 131 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
132 if self.options.progress or self.options.wait_file or self.options.connect or self.options.pipe_in: |
0 | 133 self.start_loop = True #We have to use loop for these options |
134 else: | |
135 self.start_loop = False | |
136 | |
137 | |
138 return args | |
139 | |
140 def check_jabber_status(self): | |
141 """Check that jabber status is allright""" | |
393 | 142 def cantConnect(): |
143 error(_(u"Can't connect profile")) | |
144 exit(1) | |
145 | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
146 |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
147 self.profile = self.bridge.getProfileName(self.options.profile) |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
148 if not self.profile: |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
149 error(_("The profile asked doesn't exist")) |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
150 exit(1) |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
151 |
393 | 152 if self.options.connect: #if connection is asked, we connect the profile |
153 self.bridge.asyncConnect(self.profile, self.connected, cantConnect) | |
154 return | |
155 elif not self.bridge.isConnected(self.profile): | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
156 error(_(u"SàT is not conneted, please connect before using jp")) |
0 | 157 exit(1) |
158 | |
393 | 159 self.connected() |
160 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
161 def check_jids(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
162 """Check jids validity, transform roster name to corresponding jids""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
163 names2jid = {} |
393 | 164 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
165 for contact in self.bridge.getContacts(self.options.profile): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
166 _jid, attr, groups = contact |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
167 if attr.has_key("name"): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
168 names2jid[attr["name"].lower()] = _jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
169 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
170 def expandJid(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
171 _jid = jid.lower() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
172 return unicode(names2jid[_jid] if _jid in names2jid else jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
173 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
174 def check(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
175 if not jid.is_valid: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
176 error (_("%s is not a valid JID !"), self.dest_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
177 exit(1) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
178 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
179 try: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
180 self.dest_jid = expandJid(self.dest_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
181 check(self.dest_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
182 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
183 pass |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
184 try: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
185 for i in range(len(self.dest_jids)): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
186 self.dest_jids[i] = expandJid(self.dest_jids[i]) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
187 check(self.dest_jids[i]) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
188 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
189 pass |
0 | 190 |
191 def send_stdin(self): | |
192 """Send incomming data on stdin to jabber contact""" | |
193 header = "\n" if self.options.new_line else "" | |
194 | |
195 if self.options.separate: #we send stdin in several messages | |
196 if header: | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
197 self.bridge.sendMessage(self.dest_jid, header, profile_key=self.profile) |
0 | 198 while (True): |
199 line = sys.stdin.readline() | |
200 if not line: | |
201 break | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
202 self.bridge.sendMessage(self.dest_jid, line.replace("\n",""), profile_key=self.profile) |
0 | 203 else: |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
204 self.bridge.sendMessage(self.dest_jid, header + "".join(sys.stdin.readlines()), profile_key=self.profile) |
0 | 205 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
206 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
207 def pipe_out(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
208 """Create named pipe, and send stdin to it""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
209 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
210 fifopath = os.path.join(tmp_dir,"pipe_out") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
211 os.mkfifo(fifopath) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
212 self.bridge.pipeOut(self._getFullJid(self.dest_jid), fifopath, {}, profile_key=self.profile) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
213 f = open(fifopath, 'w+') |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
214 shutil.copyfileobj(sys.stdin, f) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
215 f.close() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
216 shutil.rmtree(tmp_dir) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
217 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
218 |
0 | 219 def send_files(self): |
220 """Send files to jabber contact""" | |
221 | |
222 for file in self.files: | |
223 if not os.path.exists(file): | |
70 | 224 error (_("File [%s] doesn't exist !") % file) |
0 | 225 exit(1) |
226 if not self.options.bz2 and os.path.isdir(file): | |
70 | 227 error (_("[%s] is a dir ! Please send files inside or use compression") % file) |
0 | 228 exit(1) |
229 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
230 full_dest_jid = self._getFullJid(self.dest_jid) |
0 | 231 if self.options.bz2: |
232 tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path | |
233 if os.path.exists(tmpfile): | |
70 | 234 error (_("tmp file (%s) already exists ! Please remove it"), tmpfile) |
0 | 235 exit(1) |
70 | 236 warning(_("bz2 is an experimental option at an early dev stage, use with caution")) |
0 | 237 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) |
70 | 238 info(_("Starting compression, please wait...")) |
0 | 239 sys.stdout.flush() |
240 bz2=tarfile.open(tmpfile, "w:bz2") | |
241 for file in self.files: | |
70 | 242 info(_("Adding %s"), file) |
0 | 243 bz2.add(file) |
244 bz2.close() | |
70 | 245 info(_("OK !")) |
0 | 246 path = abspath(tmpfile) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
247 self.transfer_id = self.bridge.sendFile(full_dest_jid, path, {}, profile_key=self.profile) |
0 | 248 else: |
249 for file in self.files: | |
250 path = abspath(file) | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
251 self.transfer_id = self.bridge.sendFile(full_dest_jid, path, {}, profile_key=self.profile) #FIXME: show progress only for last transfer_id |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
252 |
0 | 253 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
254 def _getFullJid(self, param_jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
255 """Return the full jid if possible (add last resource when find a bare jid""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
256 _jid = JID(param_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
257 if not _jid.resource: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
258 #if the resource is not given, we try to add the last known resource |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
259 last_resource = self.bridge.getLastResource(param_jid, self.options.profile) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
260 if last_resource: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
261 return "%s/%s" % (_jid.short, last_resource) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
262 return param_jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
263 |
0 | 264 |
265 def askConfirmation(self, type, id, data): | |
391 | 266 """CB used for file transfer, accept files depending on parameters""" |
0 | 267 answer_data={} |
391 | 268 if type == "FILE_TRANSFER": |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
269 if not self.options.wait_file: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
270 return |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
271 if self.dest_jids and not JID(data['from']).short in [JID(_jid).short for _jid in self.dest_jids]: |
0 | 272 return #file is not sent by a filtered jid |
273 | |
274 answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] | |
275 | |
276 if self.options.force or not os.path.exists(answer_data["dest_path"]): | |
277 self.bridge.confirmationAnswer(id, True, answer_data) | |
70 | 278 info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']}) |
391 | 279 self.transfer_id = id |
0 | 280 else: |
281 self.bridge.confirmationAnswer(id, False, answer_data) | |
70 | 282 warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']}) |
0 | 283 |
284 | |
285 if not self.options.multiple and not self.options.progress: | |
286 #we just accept one file | |
287 self.loop.quit() | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
288 elif type == "PIPE_TRANSFER": |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
289 if not self.options.pipe_in: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
290 return |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
291 if self.dest_jids and not JID(data['from']).short in [JID(_jid).short for _jid in self.dest_jids]: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
292 return #pipe stream is not sent by a filtered jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
293 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
294 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
295 fifopath = os.path.join(tmp_dir,"pipe_in") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
296 answer_data["dest_path"] = fifopath |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
297 os.mkfifo(fifopath) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
298 self.bridge.confirmationAnswer(id, True, answer_data) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
299 f = open(fifopath, 'r') |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
300 shutil.copyfileobj(f, sys.stdout) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
301 f.close() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
302 shutil.rmtree(tmp_dir) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
303 self.loop.quit() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
304 |
0 | 305 |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
306 def actionResult(self, type, id, data): |
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
307 #FIXME |
70 | 308 info (_("FIXME: actionResult not implemented")) |
0 | 309 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
310 def confirmation_reply(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
311 """Auto reply to confirmations requests""" |
390 | 312 self.bridge.register("askConfirmation", self.askConfirmation) |
0 | 313 |
314 def progressCB(self): | |
391 | 315 if self.transfer_id: |
316 data = self.bridge.getProgress(self.transfer_id) | |
0 | 317 if data: |
318 if not data['position']: | |
319 data['position'] = '0' | |
320 if not self.pbar: | |
321 #first answer, we must construct the bar | |
70 | 322 self.pbar = ProgressBar(int(data['size']),[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()]) |
0 | 323 self.pbar.start() |
324 | |
325 self.pbar.update(int(data['position'])) | |
326 elif self.pbar: | |
327 self.pbar.finish() | |
328 if not self.options.multiple: | |
329 self.loop.quit() | |
330 return False | |
331 | |
332 return True | |
333 | |
334 def go(self): | |
335 self.check_options() | |
336 self.check_jabber_status() | |
393 | 337 if self.start_loop: |
338 self.loop = gobject.MainLoop() | |
339 try: | |
340 self.loop.run() | |
341 except KeyboardInterrupt: | |
342 info(_("User interruption: good bye")) | |
343 | |
344 def connected(self): | |
345 """This is called when the profile is connected""" | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
346 self.check_jids() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
347 if self.options.wait_file or self.options.pipe_in: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
348 self.confirmation_reply() |
0 | 349 else: |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
350 if self.files: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
351 self.send_files() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
352 elif self.options.pipe_out: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
353 self.pipe_out() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
354 else: |
0 | 355 self.send_stdin() |
356 | |
393 | 357 if self.options.progress: |
358 self.pbar = None | |
359 gobject.timeout_add(10, self.progressCB) | |
360 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
361 if self.start_loop and not self.options.progress and not self.options.wait_file and not self.options.pipe_in: |
393 | 362 self.loop.quit() |
0 | 363 |
364 | |
365 if __name__ == "__main__": | |
366 jp = JP() | |
367 jp.go() |