Mercurial > libervia-backend
annotate frontends/src/jp/jp @ 538:2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
- added profile argument to askConfirmation, actionResult, actionResultExt, entityDataUpdated, confirmationAnswer, getProgress
- core, frontends: fixed calls/signals according to new bridge API
- user of proper profile namespace for progression indicators and dialogs
- memory: getParam* now return bool when param type is bool
- memory: added getStringParam* to return string instead of typed value
- core, memory, storage, quick_frontend: getHistory now manage properly multi-profiles
- plugins XEP-0047, XEP-0054, XEP-0065, XEP-0077, XEP-0096; multi-profiles proper handling
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 10 Nov 2012 16:38:16 +0100 |
parents | b7c4bb2c0668 |
children | 3eeb6c865e4d |
rev | line source |
---|---|
0 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 jp: a SAT command line tool | |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
0 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
0 | 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 | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
0 | 17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
0 | 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" |
459 | 26 about = name+u""" v%s (c) Jérôme Poisson (aka Goffi) 2009, 2010, 2011, 2012 |
0 | 27 |
28 --- | |
459 | 29 """+name+u""" Copyright (C) 2009, 2010, 2011, 2012 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 | |
236 | 56 from sat.tools.jid import JID |
0 | 57 import gobject |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
58 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService |
0 | 59 import tarfile |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
60 import tempfile |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
61 import shutil |
493
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
62 import unicodedata |
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) |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
81 self.transfer_data = 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): | |
493
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
158 error(_(u"Profile [%(profile)s] is not connected, please connect it before using jp, or use --connect option") % { "profile": self.profile }) |
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 = {} |
493
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
166 nodes2jid = {} |
393 | 167 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
168 for contact in self.bridge.getContacts(self.options.profile): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
169 _jid, attr, groups = contact |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
170 if attr.has_key("name"): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
171 names2jid[attr["name"].lower()] = _jid |
493
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
172 nodes2jid[JID(_jid).node.lower()] = _jid |
401
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 expandJid(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
175 _jid = jid.lower() |
493
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
176 if _jid in names2jid: |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
177 expanded = names2jid[_jid] |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
178 elif _jid in nodes2jid: |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
179 expanded = nodes2jid[_jid] |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
180 else: |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
181 expanded = jid |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
182 return unicode(expanded) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
183 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
184 def check(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
185 if not jid.is_valid: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
186 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
|
187 exit(1) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
188 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
189 try: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
190 self.dest_jid = expandJid(self.dest_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
191 check(self.dest_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
192 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
193 pass |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
194 try: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
195 for i in range(len(self.dest_jids)): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
196 self.dest_jids[i] = expandJid(self.dest_jids[i]) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
197 check(self.dest_jids[i]) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
198 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
199 pass |
0 | 200 |
493
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
201 def clean_ustr(self, ustr): |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
202 """Clean unicode string |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
203 remove special characters from unicode string""" |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
204 def valid_chars(unicode_source): |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
205 for char in unicode_source: |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
206 if unicodedata.category(char) == 'Cc' and char!='\n': |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
207 continue |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
208 yield char |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
209 return ''.join(valid_chars(ustr)) |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
210 |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
211 |
0 | 212 def send_stdin(self): |
213 """Send incomming data on stdin to jabber contact""" | |
214 header = "\n" if self.options.new_line else "" | |
215 | |
216 if self.options.separate: #we send stdin in several messages | |
217 if header: | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
218 self.bridge.sendMessage(self.dest_jid, header, profile_key=self.profile) |
0 | 219 while (True): |
493
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
220 line = self.clean_ustr(sys.stdin.readline().decode('utf-8','ignore')) |
0 | 221 if not line: |
222 break | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
223 self.bridge.sendMessage(self.dest_jid, line.replace("\n",""), profile_key=self.profile) |
0 | 224 else: |
493
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
225 self.bridge.sendMessage(self.dest_jid, header + self.clean_ustr(u"".join([stream.decode('utf-8','ignore') for stream in sys.stdin.readlines()])), profile_key=self.profile) |
0 | 226 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
227 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
228 def pipe_out(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
229 """Create named pipe, and send stdin to it""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
230 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
231 fifopath = os.path.join(tmp_dir,"pipe_out") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
232 os.mkfifo(fifopath) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
233 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
|
234 with open(fifopath, 'w') as f: |
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
235 shutil.copyfileobj(sys.stdin, f) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
236 shutil.rmtree(tmp_dir) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
237 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
238 |
0 | 239 def send_files(self): |
240 """Send files to jabber contact""" | |
241 | |
242 for file in self.files: | |
243 if not os.path.exists(file): | |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
244 error (_(u"File [%s] doesn't exist !") % file) |
0 | 245 exit(1) |
246 if not self.options.bz2 and os.path.isdir(file): | |
70 | 247 error (_("[%s] is a dir ! Please send files inside or use compression") % file) |
0 | 248 exit(1) |
249 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
250 full_dest_jid = self._getFullJid(self.dest_jid) |
0 | 251 if self.options.bz2: |
252 tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path | |
253 if os.path.exists(tmpfile): | |
70 | 254 error (_("tmp file (%s) already exists ! Please remove it"), tmpfile) |
0 | 255 exit(1) |
70 | 256 warning(_("bz2 is an experimental option at an early dev stage, use with caution")) |
0 | 257 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) |
70 | 258 info(_("Starting compression, please wait...")) |
0 | 259 sys.stdout.flush() |
260 bz2=tarfile.open(tmpfile, "w:bz2") | |
261 for file in self.files: | |
70 | 262 info(_("Adding %s"), file) |
0 | 263 bz2.add(file) |
264 bz2.close() | |
70 | 265 info(_("OK !")) |
0 | 266 path = abspath(tmpfile) |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
267 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, profile_key=self.profile) |
0 | 268 else: |
269 for file in self.files: | |
270 path = abspath(file) | |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
271 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, profile_key=self.profile) #FIXME: show progress only for last transfer_id |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
272 |
0 | 273 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
274 def _getFullJid(self, param_jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
275 """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
|
276 _jid = JID(param_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
277 if not _jid.resource: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
278 #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
|
279 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
|
280 if last_resource: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
281 return "%s/%s" % (_jid.short, last_resource) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
282 return param_jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
283 |
0 | 284 |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
285 def askConfirmation(self, type, confirm_id, data, profile): |
391 | 286 """CB used for file transfer, accept files depending on parameters""" |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
287 if profile != self.profile: |
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
288 debug("Ask confirmation ignored: not our profile") |
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
289 return |
0 | 290 answer_data={} |
391 | 291 if type == "FILE_TRANSFER": |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
292 if not self.options.wait_file: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
293 return |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
294 if self.dest_jids and not JID(data['from']).short in [JID(_jid).short for _jid in self.dest_jids]: |
0 | 295 return #file is not sent by a filtered jid |
296 | |
297 answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] | |
298 | |
299 if self.options.force or not os.path.exists(answer_data["dest_path"]): | |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
300 self.bridge.confirmationAnswer(confirm_id, True, answer_data, profile) |
70 | 301 info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']}) |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
302 self.transfer_data = confirm_id |
0 | 303 else: |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
304 self.bridge.confirmationAnswer(confirm_id, False, answer_data, profile) |
70 | 305 warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']}) |
0 | 306 |
307 | |
308 if not self.options.multiple and not self.options.progress: | |
309 #we just accept one file | |
310 self.loop.quit() | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
311 elif type == "PIPE_TRANSFER": |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
312 if not self.options.pipe_in: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
313 return |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
314 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
|
315 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
|
316 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
317 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
318 fifopath = os.path.join(tmp_dir,"pipe_in") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
319 answer_data["dest_path"] = fifopath |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
320 os.mkfifo(fifopath) |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
321 self.bridge.confirmationAnswer(confirm_id, True, answer_data, profile) |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
322 with open(fifopath, 'r') as f: |
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
323 shutil.copyfileobj(f, sys.stdout) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
324 shutil.rmtree(tmp_dir) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
325 self.loop.quit() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
326 |
0 | 327 |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
328 def actionResult(self, action_type, action_id, data, profile): |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
329 #FIXME |
70 | 330 info (_("FIXME: actionResult not implemented")) |
0 | 331 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
332 def confirmation_reply(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
333 """Auto reply to confirmations requests""" |
390 | 334 self.bridge.register("askConfirmation", self.askConfirmation) |
0 | 335 |
336 def progressCB(self): | |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
337 if self.transfer_data: |
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
338 transfer_id = self.transfer_data |
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
339 data = self.bridge.getProgress(transfer_id, self.profile) |
0 | 340 if data: |
341 if not data['position']: | |
342 data['position'] = '0' | |
343 if not self.pbar: | |
344 #first answer, we must construct the bar | |
70 | 345 self.pbar = ProgressBar(int(data['size']),[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()]) |
0 | 346 self.pbar.start() |
347 | |
348 self.pbar.update(int(data['position'])) | |
349 elif self.pbar: | |
350 self.pbar.finish() | |
351 if not self.options.multiple: | |
352 self.loop.quit() | |
353 return False | |
354 | |
355 return True | |
356 | |
357 def go(self): | |
358 self.check_options() | |
359 self.check_jabber_status() | |
393 | 360 if self.start_loop: |
361 self.loop = gobject.MainLoop() | |
362 try: | |
363 self.loop.run() | |
364 except KeyboardInterrupt: | |
365 info(_("User interruption: good bye")) | |
366 | |
367 def connected(self): | |
368 """This is called when the profile is connected""" | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
369 self.check_jids() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
370 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
|
371 self.confirmation_reply() |
0 | 372 else: |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
373 if self.files: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
374 self.send_files() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
375 elif self.options.pipe_out: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
376 self.pipe_out() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
377 else: |
0 | 378 self.send_stdin() |
379 | |
393 | 380 if self.options.progress: |
381 self.pbar = None | |
382 gobject.timeout_add(10, self.progressCB) | |
383 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
384 if self.start_loop and not self.options.progress and not self.options.wait_file and not self.options.pipe_in: |
393 | 385 self.loop.quit() |
0 | 386 |
387 | |
388 if __name__ == "__main__": | |
389 jp = JP() | |
390 jp.go() |