Mercurial > libervia-backend
annotate frontends/src/jp/jp @ 771:bfabeedbf32e
core: i18n refactoring:
- _() is no more installed in __builtin__
- instead, there is a new sat.core.i18n module
- added D_() method for deferred translation
- languageSwitch method allow to dynamically change translation language
- import gettext is tested against ImportError, and dummy methods are used when not available (mainly useful for Libervia)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 29 Dec 2013 17:06:01 +0100 |
parents | 0077912bc9ba |
children | f89173f44850 |
rev | line source |
---|---|
0 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
4 # jp: a SAT command line tool |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
0 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
10 # (at your option) any later version. |
0 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
15 # GNU Affero General Public License for more details. |
0 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 19 |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
20 from __future__ import with_statement |
771 | 21 from sat.core.i18n import _ |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
22 |
0 | 23 #consts |
191 | 24 name = u"jp" |
459 | 25 about = name+u""" v%s (c) Jérôme Poisson (aka Goffi) 2009, 2010, 2011, 2012 |
0 | 26 |
27 --- | |
572 | 28 """+name+u""" Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (aka Goffi) |
0 | 29 This program comes with ABSOLUTELY NO WARRANTY; |
30 This is free software, and you are welcome to redistribute it | |
31 under certain conditions. | |
32 --- | |
33 | |
34 This software is a command line tool for jabber | |
35 Get the latest version at http://www.goffi.org | |
36 """ | |
37 | |
38 global pbar_available | |
39 pbar_available = True #checked before using ProgressBar | |
40 | |
41 ### logging ### | |
42 import logging | |
43 from logging import debug, info, error, warning | |
44 logging.basicConfig(level=logging.DEBUG, | |
45 format='%(message)s') | |
46 ### | |
47 | |
48 import sys | |
49 import os | |
50 from os.path import abspath, basename, dirname | |
51 from optparse import OptionParser | |
236 | 52 from sat.tools.jid import JID |
0 | 53 import gobject |
627
d207c2186519
core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents:
613
diff
changeset
|
54 from sat_frontends.bridge.DBus import DBusBridgeFrontend |
d207c2186519
core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents:
613
diff
changeset
|
55 from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
56 from sat.tools.utils import clean_ustr |
0 | 57 import tarfile |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
58 import tempfile |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
59 import shutil |
0 | 60 try: |
61 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed | |
62 except ImportError, e: | |
70 | 63 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) |
64 info (_('Progress bar deactivated\n--\n')) | |
0 | 65 pbar_available=False |
66 | |
67 | |
68 | |
69 | |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
70 class JP(object): |
0 | 71 def __init__(self): |
165
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
72 try: |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
73 self.bridge=DBusBridgeFrontend() |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
74 except BridgeExceptionNoService: |
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
75 print(_(u"Can't connect to SàT backend, are you sure it's launched ?")) |
627
d207c2186519
core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents:
613
diff
changeset
|
76 sys.exit(1) |
d207c2186519
core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents:
613
diff
changeset
|
77 except BridgeInitError: |
d207c2186519
core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents:
613
diff
changeset
|
78 print(_(u"Can't init bridge")) |
165
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
79 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
|
80 self.transfer_data = None |
0 | 81 |
82 def check_options(self): | |
83 """Check command line options""" | |
70 | 84 usage=_(""" |
0 | 85 %prog [options] [FILE1 FILE2 ...] JID |
86 %prog -w [options] [JID1 JID2 ...] | |
87 | |
88 %prog --help for options list | |
70 | 89 """) |
191 | 90 version = unicode(self.bridge.getVersion()) |
91 parser = OptionParser(usage=usage,version=about % version) | |
0 | 92 |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
93 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
|
94 help=_("Use PROFILE profile key (default: %default)")) |
0 | 95 parser.add_option("-b", "--bz2", action="store_true", default=False, |
70 | 96 help=_("Make a bzip2 tarball")) |
0 | 97 parser.add_option("-w", "--wait-file", action="store_true", default=False, |
70 | 98 help=_("Wait for a file to be sent by a contact")) |
0 | 99 parser.add_option("-m", "--multiple", action="store_true", default=False, |
70 | 100 help=_("Accept multiple files (you'll have to stop manually)")) |
0 | 101 parser.add_option("-f", "--force", action="store_true", default=False, |
70 | 102 help=_("Force overwritting of existing files")) |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
103 parser.add_option("-g", "--progress", action="store_true", default=False, |
70 | 104 help=_("Show progress bar")) |
0 | 105 parser.add_option("-s", "--separate", action="store_true", default=False, |
70 | 106 help=_("Separate xmpp messages: send one message per line instead of one message alone.")) |
0 | 107 parser.add_option("-n", "--new-line", action="store_true", default=False, |
70 | 108 help=_("Add a new line at the beginning of the input (usefull for ascii art ;))")) |
657 | 109 parser.add_option("--list-profiles", action="store_true", default=False, |
110 help=_("List available profiles")) | |
111 parser.add_option("-c", "--create-profile", action="store", type="string", nargs=3, | |
112 help=_("Create a profile (args: profile_name jid password)")) | |
113 parser.add_option("--get-profile", action="store", type="string", | |
114 help=_("Get profile informations (arg: profile_name)")) | |
115 parser.add_option("--rm-profile", action="store", type="string", | |
116 help=_("Remove profile")) | |
393 | 117 parser.add_option("--connect", action="store_true", default=False, |
118 help=_("Connect the profile before doing anything else")) | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
119 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
|
120 help=_("Wait for the reception of a pipe stream")) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
121 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
|
122 help=_("Pipe a stream out ")) |
0 | 123 |
124 (self.options, args) = parser.parse_args() | |
657 | 125 if self.options.list_profiles: |
126 for p in self.bridge.getProfilesList(): | |
127 info(p) | |
128 exit(0) | |
129 if self.options.create_profile or self.options.get_profile: | |
130 self.start_loop = True | |
131 return args | |
132 if self.options.rm_profile: | |
133 self.start_loop = False | |
134 return args | |
0 | 135 |
136 if len(args) < 1 and not self.options.wait_file: | |
70 | 137 parser.error(_("You must specify the destination JID (Jabber ID)").encode('utf-8')) |
0 | 138 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
139 if self.options.wait_file or self.options.pipe_in: |
0 | 140 #several jid |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
141 self.dest_jids = [arg.decode('utf-8') for arg in args] |
0 | 142 else: |
143 #one dest_jid, other args are files | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
144 self.dest_jid = JID(args[-1].decode('utf-8')) |
0 | 145 self.files = args[:-1] |
146 | |
147 if not pbar_available and self.options.progress: | |
148 self.options.progress = False | |
70 | 149 error (_("Option progress is not available, deactivated.")) |
0 | 150 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
151 if self.options.progress or self.options.wait_file or self.options.connect or self.options.pipe_in: |
0 | 152 self.start_loop = True #We have to use loop for these options |
153 else: | |
154 self.start_loop = False | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
155 |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
156 |
0 | 157 return args |
158 | |
657 | 159 def create_profile(self): |
160 """Create a new profile""" | |
161 profile, jid, password = self.options.create_profile | |
162 if profile in self.bridge.getProfilesList(): | |
163 error("Profile %s already exists."%profile) | |
164 exit(1) | |
165 self.bridge.asyncCreateProfile(profile, lambda : self._create_profile(profile, jid, password), None) | |
166 | |
167 def get_profile(self): | |
168 def setJID(jid): | |
169 info("jid: %s"%jid) | |
170 self.bridge.asyncGetParamA("Password", "Connection", profile_key=profile_name, callback=setPassword) | |
171 def setPassword(password): | |
172 info("pwd: %s"%password) | |
173 self.loop.quit() | |
174 profile_name = self.options.get_profile | |
175 if profile_name not in self.bridge.getProfilesList(): | |
176 error("Profile %s doesn't exist."%profile_name) | |
177 exit(1) | |
178 self.bridge.asyncGetParamA("JabberID", "Connection", profile_key=profile_name, callback=setJID) | |
179 | |
180 def rm_profile(self): | |
181 profile_name = self.options.rm_profile | |
182 if profile_name not in self.bridge.getProfilesList(): | |
183 error("Profile %s doesn't exist."%profile_name) | |
184 exit(1) | |
185 self.bridge.deleteProfile(profile_name) | |
186 | |
187 def _create_profile(self, profile_name, jid, password): | |
188 self.bridge.setParam("JabberID", jid, "Connection" ,profile_key=profile_name) | |
189 self.bridge.setParam("Server", JID(jid).domain, "Connection", profile_key=profile_name) | |
190 self.bridge.setParam("Password", password, "Connection", profile_key=profile_name) | |
191 self.loop.quit() | |
192 | |
0 | 193 def check_jabber_status(self): |
194 """Check that jabber status is allright""" | |
393 | 195 def cantConnect(): |
196 error(_(u"Can't connect profile")) | |
197 exit(1) | |
198 | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
199 |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
200 self.profile = self.bridge.getProfileName(self.options.profile) |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
201 if not self.profile: |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
202 error(_("The profile asked doesn't exist")) |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
203 exit(1) |
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
204 |
393 | 205 if self.options.connect: #if connection is asked, we connect the profile |
206 self.bridge.asyncConnect(self.profile, self.connected, cantConnect) | |
207 return | |
208 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
|
209 error(_(u"Profile [%(profile)s] is not connected, please connect it before using jp, or use --connect option") % { "profile": self.profile }) |
0 | 210 exit(1) |
211 | |
393 | 212 self.connected() |
213 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
214 def check_jids(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
215 """Check jids validity, transform roster name to corresponding jids""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
216 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
|
217 nodes2jid = {} |
393 | 218 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
219 for contact in self.bridge.getContacts(self.options.profile): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
220 _jid, attr, groups = contact |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
221 if attr.has_key("name"): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
222 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
|
223 nodes2jid[JID(_jid).node.lower()] = _jid |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
224 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
225 def expandJid(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
226 _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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 else: |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
232 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
|
233 return unicode(expanded) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
234 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
235 def check(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
236 if not jid.is_valid: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
237 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
|
238 exit(1) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
239 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
240 try: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
241 self.dest_jid = expandJid(self.dest_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
242 check(self.dest_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
243 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
244 pass |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
245 try: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
246 for i in range(len(self.dest_jids)): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
247 self.dest_jids[i] = expandJid(self.dest_jids[i]) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
248 check(self.dest_jids[i]) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
249 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
250 pass |
0 | 251 |
493
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
252 |
0 | 253 def send_stdin(self): |
254 """Send incomming data on stdin to jabber contact""" | |
255 header = "\n" if self.options.new_line else "" | |
256 | |
257 if self.options.separate: #we send stdin in several messages | |
258 if header: | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
259 self.bridge.sendMessage(self.dest_jid, header, profile_key=self.profile) |
0 | 260 while (True): |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
261 line = clean_ustr(sys.stdin.readline().decode('utf-8','ignore')) |
0 | 262 if not line: |
263 break | |
110
cb904fa7de3c
jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
264 self.bridge.sendMessage(self.dest_jid, line.replace("\n",""), profile_key=self.profile) |
0 | 265 else: |
601
a4f6f78f0620
jp, core: jp's clean_ustr moved to a new general utils module
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
266 self.bridge.sendMessage(self.dest_jid, header + clean_ustr(u"".join([stream.decode('utf-8','ignore') for stream in sys.stdin.readlines()])), profile_key=self.profile) |
0 | 267 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
268 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
269 def pipe_out(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
270 """Create named pipe, and send stdin to it""" |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
271 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
272 fifopath = os.path.join(tmp_dir,"pipe_out") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
273 os.mkfifo(fifopath) |
721
0077912bc9ba
jp: removed named argument for pipeOut, as it's not managed with dynamicaly added methods
Goffi <goffi@goffi.org>
parents:
720
diff
changeset
|
274 self.bridge.pipeOut(self._getFullJid(self.dest_jid), fifopath, {}, self.profile) |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
275 with open(fifopath, 'w') as f: |
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
276 shutil.copyfileobj(sys.stdin, f) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
277 shutil.rmtree(tmp_dir) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
278 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
279 |
0 | 280 def send_files(self): |
281 """Send files to jabber contact""" | |
282 | |
283 for file in self.files: | |
284 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
|
285 error (_(u"File [%s] doesn't exist !") % file) |
0 | 286 exit(1) |
287 if not self.options.bz2 and os.path.isdir(file): | |
70 | 288 error (_("[%s] is a dir ! Please send files inside or use compression") % file) |
0 | 289 exit(1) |
290 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
291 full_dest_jid = self._getFullJid(self.dest_jid) |
0 | 292 if self.options.bz2: |
293 tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path | |
294 if os.path.exists(tmpfile): | |
70 | 295 error (_("tmp file (%s) already exists ! Please remove it"), tmpfile) |
0 | 296 exit(1) |
70 | 297 warning(_("bz2 is an experimental option at an early dev stage, use with caution")) |
0 | 298 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) |
70 | 299 info(_("Starting compression, please wait...")) |
0 | 300 sys.stdout.flush() |
301 bz2=tarfile.open(tmpfile, "w:bz2") | |
302 for file in self.files: | |
70 | 303 info(_("Adding %s"), file) |
0 | 304 bz2.add(file) |
305 bz2.close() | |
70 | 306 info(_("OK !")) |
0 | 307 path = abspath(tmpfile) |
720 | 308 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) |
0 | 309 else: |
310 for file in self.files: | |
311 path = abspath(file) | |
720 | 312 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, 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
|
313 |
0 | 314 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
315 def _getFullJid(self, param_jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
316 """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
|
317 _jid = JID(param_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
318 if not _jid.resource: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
319 #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
|
320 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
|
321 if last_resource: |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
657
diff
changeset
|
322 return "%s/%s" % (_jid.bare, last_resource) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
323 return param_jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
324 |
0 | 325 |
542
3eeb6c865e4d
frontends: incoming files transfer management:
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
326 def askConfirmation(self, confirm_id, confirm_type, data, profile): |
391 | 327 """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
|
328 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
|
329 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
|
330 return |
0 | 331 answer_data={} |
542
3eeb6c865e4d
frontends: incoming files transfer management:
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
332 if confirm_type == "FILE_TRANSFER": |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
333 if not self.options.wait_file: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
334 return |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
657
diff
changeset
|
335 if self.dest_jids and not JID(data['from']).bare in [JID(_jid).bare for _jid in self.dest_jids]: |
0 | 336 return #file is not sent by a filtered jid |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
337 |
0 | 338 answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] |
339 | |
340 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
|
341 self.bridge.confirmationAnswer(confirm_id, True, answer_data, profile) |
70 | 342 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
|
343 self.transfer_data = confirm_id |
0 | 344 else: |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
345 self.bridge.confirmationAnswer(confirm_id, False, answer_data, profile) |
70 | 346 warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']}) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
347 |
0 | 348 |
349 if not self.options.multiple and not self.options.progress: | |
350 #we just accept one file | |
351 self.loop.quit() | |
542
3eeb6c865e4d
frontends: incoming files transfer management:
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
352 elif confirm_type == "PIPE_TRANSFER": |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
353 if not self.options.pipe_in: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
354 return |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
657
diff
changeset
|
355 if self.dest_jids and not JID(data['from']).bare in [JID(_jid).bare for _jid in self.dest_jids]: |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
356 return #pipe stream is not sent by a filtered jid |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
357 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
358 tmp_dir = tempfile.mkdtemp() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
359 fifopath = os.path.join(tmp_dir,"pipe_in") |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
360 answer_data["dest_path"] = fifopath |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
361 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
|
362 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
|
363 with open(fifopath, 'r') as f: |
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
364 shutil.copyfileobj(f, sys.stdout) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
365 shutil.rmtree(tmp_dir) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
366 self.loop.quit() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
367 |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
368 |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
369 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
|
370 #FIXME |
70 | 371 info (_("FIXME: actionResult not implemented")) |
0 | 372 |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
373 def confirmation_reply(self): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
374 """Auto reply to confirmations requests""" |
542
3eeb6c865e4d
frontends: incoming files transfer management:
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
375 #we register incoming confirmation |
390 | 376 self.bridge.register("askConfirmation", self.askConfirmation) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
377 |
542
3eeb6c865e4d
frontends: incoming files transfer management:
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
378 #and we ask those we have missed |
3eeb6c865e4d
frontends: incoming files transfer management:
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
379 for confirm_id, confirm_type, data in self.bridge.getWaitingConf(self.profile): |
3eeb6c865e4d
frontends: incoming files transfer management:
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
380 self.askConfirmation(confirm_id, confirm_type, data, self.profile) |
0 | 381 |
382 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
|
383 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
|
384 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
|
385 data = self.bridge.getProgress(transfer_id, self.profile) |
0 | 386 if data: |
387 if not data['position']: | |
388 data['position'] = '0' | |
389 if not self.pbar: | |
390 #first answer, we must construct the bar | |
70 | 391 self.pbar = ProgressBar(int(data['size']),[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()]) |
0 | 392 self.pbar.start() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
393 |
0 | 394 self.pbar.update(int(data['position'])) |
395 elif self.pbar: | |
396 self.pbar.finish() | |
397 if not self.options.multiple: | |
398 self.loop.quit() | |
399 return False | |
400 | |
401 return True | |
402 | |
403 def go(self): | |
404 self.check_options() | |
657 | 405 if self.options.create_profile: |
406 self.create_profile() | |
407 elif self.options.get_profile: | |
408 self.get_profile() | |
409 elif self.options.rm_profile: | |
410 self.rm_profile() | |
411 else: | |
412 self.check_jabber_status() | |
393 | 413 if self.start_loop: |
414 self.loop = gobject.MainLoop() | |
415 try: | |
416 self.loop.run() | |
417 except KeyboardInterrupt: | |
418 info(_("User interruption: good bye")) | |
419 | |
420 def connected(self): | |
421 """This is called when the profile is connected""" | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
422 self.check_jids() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
423 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
|
424 self.confirmation_reply() |
0 | 425 else: |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
426 if self.files: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
427 self.send_files() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
428 elif self.options.pipe_out: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
429 self.pipe_out() |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
430 else: |
0 | 431 self.send_stdin() |
432 | |
393 | 433 if self.options.progress: |
434 self.pbar = None | |
435 gobject.timeout_add(10, self.progressCB) | |
436 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
437 if self.start_loop and not self.options.progress and not self.options.wait_file and not self.options.pipe_in: |
393 | 438 self.loop.quit() |
0 | 439 |
440 | |
441 if __name__ == "__main__": | |
442 jp = JP() | |
443 jp.go() |