Mercurial > libervia-backend
annotate frontends/src/jp/jp @ 405:10b4f577d0c0
MUC update to follow wokkel's MUC branch update
- bridge methods have been updated (API change)
- plugin XEP-0045 has been updated
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Oct 2011 18:43:17 +0200 |
parents | f03688bdb858 |
children | cf005701624b |
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 | |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
22 from __future__ import with_statement |
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
23 |
0 | 24 #consts |
191 | 25 name = u"jp" |
228 | 26 about = name+u""" v%s (c) Jérôme Poisson (aka Goffi) 2009, 2010, 2011 |
0 | 27 |
28 --- | |
228 | 29 """+name+u""" Copyright (C) 2009, 2010, 2011 Jérôme Poisson (aka Goffi) |
0 | 30 This program comes with ABSOLUTELY NO WARRANTY; |
31 This is free software, and you are welcome to redistribute it | |
32 under certain conditions. | |
33 --- | |
34 | |
35 This software is a command line tool for jabber | |
36 Get the latest version at http://www.goffi.org | |
37 """ | |
38 | |
39 global pbar_available | |
40 pbar_available = True #checked before using ProgressBar | |
41 | |
42 ### logging ### | |
43 import logging | |
44 from logging import debug, info, error, warning | |
45 logging.basicConfig(level=logging.DEBUG, | |
46 format='%(message)s') | |
47 ### | |
48 | |
70 | 49 import gettext |
50 gettext.install('jp', "i18n", unicode=True) | |
51 | |
0 | 52 import sys |
53 import os | |
54 from os.path import abspath, basename, dirname | |
55 from optparse import OptionParser | |
56 import pdb | |
236 | 57 from sat.tools.jid import JID |
0 | 58 import gobject |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
59 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService |
0 | 60 import tarfile |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
61 import tempfile |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
62 import shutil |
0 | 63 try: |
64 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed | |
65 except ImportError, e: | |
70 | 66 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) |
67 info (_('Progress bar deactivated\n--\n')) | |
0 | 68 pbar_available=False |
69 | |
70 | |
71 | |
72 | |
73 class JP(): | |
74 def __init__(self): | |
165
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
75 try: |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
76 self.bridge=DBusBridgeFrontend() |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
77 except BridgeExceptionNoService: |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
78 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
|
79 import sys |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
80 sys.exit(1) |
391 | 81 self.transfer_id = None |
0 | 82 |
83 def check_options(self): | |
84 """Check command line options""" | |
70 | 85 usage=_(""" |
0 | 86 %prog [options] [FILE1 FILE2 ...] JID |
87 %prog -w [options] [JID1 JID2 ...] | |
88 | |
89 %prog --help for options list | |
70 | 90 """) |
191 | 91 version = unicode(self.bridge.getVersion()) |
92 parser = OptionParser(usage=usage,version=about % version) | |
0 | 93 |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
94 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
|
95 help=_("Use PROFILE profile key (default: %default)")) |
0 | 96 parser.add_option("-b", "--bz2", action="store_true", default=False, |
70 | 97 help=_("Make a bzip2 tarball")) |
0 | 98 parser.add_option("-w", "--wait-file", action="store_true", default=False, |
70 | 99 help=_("Wait for a file to be sent by a contact")) |
0 | 100 parser.add_option("-m", "--multiple", action="store_true", default=False, |
70 | 101 help=_("Accept multiple files (you'll have to stop manually)")) |
0 | 102 parser.add_option("-f", "--force", action="store_true", default=False, |
70 | 103 help=_("Force overwritting of existing files")) |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
104 parser.add_option("-g", "--progress", action="store_true", default=False, |
70 | 105 help=_("Show progress bar")) |
0 | 106 parser.add_option("-s", "--separate", action="store_true", default=False, |
70 | 107 help=_("Separate xmpp messages: send one message per line instead of one message alone.")) |
0 | 108 parser.add_option("-n", "--new-line", action="store_true", default=False, |
70 | 109 help=_("Add a new line at the beginning of the input (usefull for ascii art ;))")) |
393 | 110 parser.add_option("--connect", action="store_true", default=False, |
111 help=_("Connect the profile before doing anything else")) | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
112 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
|
113 help=_("Wait for the reception of a pipe stream")) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
114 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
|
115 help=_("Pipe a stream out ")) |
0 | 116 |
117 (self.options, args) = parser.parse_args() | |
118 | |
119 if len(args) < 1 and not self.options.wait_file: | |
70 | 120 parser.error(_("You must specify the destination JID (Jabber ID)").encode('utf-8')) |
0 | 121 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
122 if self.options.wait_file or self.options.pipe_in: |
0 | 123 #several jid |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
124 self.dest_jids = [arg.decode('utf-8') for arg in args] |
0 | 125 else: |
126 #one dest_jid, other args are files | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
127 self.dest_jid = JID(args[-1].decode('utf-8')) |
0 | 128 self.files = args[:-1] |
129 | |
130 if not pbar_available and self.options.progress: | |
131 self.options.progress = False | |
70 | 132 error (_("Option progress is not available, deactivated.")) |
0 | 133 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
134 if self.options.progress or self.options.wait_file or self.options.connect or self.options.pipe_in: |
0 | 135 self.start_loop = True #We have to use loop for these options |
136 else: | |
137 self.start_loop = False | |
138 | |
139 | |
140 return args | |
141 | |
142 def check_jabber_status(self): | |
143 """Check that jabber status is allright""" | |
393 | 144 def cantConnect(): |
145 error(_(u"Can't connect profile")) | |
146 exit(1) | |
147 | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
148 |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
149 self.profile = self.bridge.getProfileName(self.options.profile) |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
150 if not self.profile: |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
151 error(_("The profile asked doesn't exist")) |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
152 exit(1) |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
153 |
393 | 154 if self.options.connect: #if connection is asked, we connect the profile |
155 self.bridge.asyncConnect(self.profile, self.connected, cantConnect) | |
156 return | |
157 elif not self.bridge.isConnected(self.profile): | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
158 error(_(u"SàT is not conneted, please connect before using jp")) |
0 | 159 exit(1) |
160 | |
393 | 161 self.connected() |
162 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
163 def check_jids(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
164 """Check jids validity, transform roster name to corresponding jids""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
165 names2jid = {} |
393 | 166 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
167 for contact in self.bridge.getContacts(self.options.profile): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
168 _jid, attr, groups = contact |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
169 if attr.has_key("name"): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
170 names2jid[attr["name"].lower()] = _jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
171 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
172 def expandJid(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
173 _jid = jid.lower() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
174 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
|
175 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
176 def check(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
177 if not jid.is_valid: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
178 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
|
179 exit(1) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
180 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
181 try: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
182 self.dest_jid = expandJid(self.dest_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
183 check(self.dest_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
184 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
185 pass |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
186 try: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
187 for i in range(len(self.dest_jids)): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
188 self.dest_jids[i] = expandJid(self.dest_jids[i]) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
189 check(self.dest_jids[i]) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
190 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
191 pass |
0 | 192 |
193 def send_stdin(self): | |
194 """Send incomming data on stdin to jabber contact""" | |
195 header = "\n" if self.options.new_line else "" | |
196 | |
197 if self.options.separate: #we send stdin in several messages | |
198 if header: | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
199 self.bridge.sendMessage(self.dest_jid, header, profile_key=self.profile) |
0 | 200 while (True): |
201 line = sys.stdin.readline() | |
202 if not line: | |
203 break | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
204 self.bridge.sendMessage(self.dest_jid, line.replace("\n",""), profile_key=self.profile) |
0 | 205 else: |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
206 self.bridge.sendMessage(self.dest_jid, header + "".join(sys.stdin.readlines()), profile_key=self.profile) |
0 | 207 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
208 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
209 def pipe_out(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
210 """Create named pipe, and send stdin to it""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
211 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
212 fifopath = os.path.join(tmp_dir,"pipe_out") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
213 os.mkfifo(fifopath) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
214 self.bridge.pipeOut(self._getFullJid(self.dest_jid), fifopath, {}, profile_key=self.profile) |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
215 with open(fifopath, 'w') as f: |
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
216 shutil.copyfileobj(sys.stdin, f) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
217 shutil.rmtree(tmp_dir) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
218 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
219 |
0 | 220 def send_files(self): |
221 """Send files to jabber contact""" | |
222 | |
223 for file in self.files: | |
224 if not os.path.exists(file): | |
70 | 225 error (_("File [%s] doesn't exist !") % file) |
0 | 226 exit(1) |
227 if not self.options.bz2 and os.path.isdir(file): | |
70 | 228 error (_("[%s] is a dir ! Please send files inside or use compression") % file) |
0 | 229 exit(1) |
230 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
231 full_dest_jid = self._getFullJid(self.dest_jid) |
0 | 232 if self.options.bz2: |
233 tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path | |
234 if os.path.exists(tmpfile): | |
70 | 235 error (_("tmp file (%s) already exists ! Please remove it"), tmpfile) |
0 | 236 exit(1) |
70 | 237 warning(_("bz2 is an experimental option at an early dev stage, use with caution")) |
0 | 238 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) |
70 | 239 info(_("Starting compression, please wait...")) |
0 | 240 sys.stdout.flush() |
241 bz2=tarfile.open(tmpfile, "w:bz2") | |
242 for file in self.files: | |
70 | 243 info(_("Adding %s"), file) |
0 | 244 bz2.add(file) |
245 bz2.close() | |
70 | 246 info(_("OK !")) |
0 | 247 path = abspath(tmpfile) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
248 self.transfer_id = self.bridge.sendFile(full_dest_jid, path, {}, profile_key=self.profile) |
0 | 249 else: |
250 for file in self.files: | |
251 path = abspath(file) | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
252 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
|
253 |
0 | 254 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
255 def _getFullJid(self, param_jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
256 """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
|
257 _jid = JID(param_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
258 if not _jid.resource: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
259 #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
|
260 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
|
261 if last_resource: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
262 return "%s/%s" % (_jid.short, last_resource) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
263 return param_jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
264 |
0 | 265 |
266 def askConfirmation(self, type, id, data): | |
391 | 267 """CB used for file transfer, accept files depending on parameters""" |
0 | 268 answer_data={} |
391 | 269 if type == "FILE_TRANSFER": |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
270 if not self.options.wait_file: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
271 return |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
272 if self.dest_jids and not JID(data['from']).short in [JID(_jid).short for _jid in self.dest_jids]: |
0 | 273 return #file is not sent by a filtered jid |
274 | |
275 answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] | |
276 | |
277 if self.options.force or not os.path.exists(answer_data["dest_path"]): | |
278 self.bridge.confirmationAnswer(id, True, answer_data) | |
70 | 279 info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']}) |
391 | 280 self.transfer_id = id |
0 | 281 else: |
282 self.bridge.confirmationAnswer(id, False, answer_data) | |
70 | 283 warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']}) |
0 | 284 |
285 | |
286 if not self.options.multiple and not self.options.progress: | |
287 #we just accept one file | |
288 self.loop.quit() | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
289 elif type == "PIPE_TRANSFER": |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
290 if not self.options.pipe_in: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
291 return |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
292 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
|
293 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
|
294 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
295 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
296 fifopath = os.path.join(tmp_dir,"pipe_in") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
297 answer_data["dest_path"] = fifopath |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
298 os.mkfifo(fifopath) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
299 self.bridge.confirmationAnswer(id, True, answer_data) |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
300 with open(fifopath, 'r') as f: |
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
301 shutil.copyfileobj(f, sys.stdout) |
401
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() |