Debian Codegraph @

bin/xx

raw
1 #!/usr/bin/perl -w
2
3 # CLI client for the F*EX service (send, list, delete)
4 #
5 # see also: fexget
6 #
7 # Author: Ulli Horlacher <[email protected]>
8 #
9 # Perl Artistic Licence
10
11 # BEGIN { $SIG{__WARN__} = sub { die @_ } }
12
13 use 5.010;
14 use strict qw(vars subs);
15 use Encode;
16 use Config;
17 use Socket;
18 use IO::Handle;
19 use IO::Socket::INET;
20 use Getopt::Std;
21 use File::Basename;
22 use Term::ReadLine;
23 use Sys::Hostname;
24 use Cwd qw(abs_path getcwd);
25 use Fcntl qw(:flock :mode);
26 use Digest::MD5 qw(md5_hex);
27 use Time::HiRes qw(time);
28 # use Smart::Comments;
29 use constant kB => 2**10;
30 use constant MB => 2**20;
31
32 our $CTYPE = 'ISO-8859-1';
33
34 $ENV{LANGUAGE} = 'en';
35 $ENV{LC_MESSAGES} = 'C';
36
37 eval 'use Net::INET6Glue::INET_is_INET6';
38 eval {
39 local $^W = 0;
40 require I18N::Langinfo;
41 I18N::Langinfo->import(qw'langinfo CODESET');
42 $CTYPE = langinfo(CODESET());
43 };
44
45 $| = 1;
46
47 our ($SH,$fexhome,$idf,$fextmp,$useragent,$editor,$nomail);
48 our ($windoof,$cygwin,$macos);
49 our ($transferfile,$share_);
50 our ($anonymous,$public);
51 our ($frecipient);
52 our ($FEXID,$FEXXX,$HOME);
53 our (%alias);
54 our $version = 20200429;
55 our $_0 = $0;
56 our $prg = $0;
57 our $FEXOPT = $ENV{FEXOPT}||'';
58 our $nmtime = 0;
59 our $command = '';
60 our ($fexsend,$fexget);
61
62 our %SSL = (SSL_version => 'TLSv1_2');
63
64 my $sigpipe;
65 my $chunksize = 0;
66 my $sleepwait = 1;
67 my $xn = '\d{8}_\d{6}';
68 my ($xlist,$xp);
69
70 unless ($version) {
71 my @d = localtime((stat $0)[9]);
72 $version = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3])
73 }
74 $version .= '_sw' if abs_path($0) =~ m:/sw/:;
75
76 &update if "@ARGV" eq 'UPDATE';
77
78 if ($Config{osname} =~ /^mswin/i) {
79 # http://slu.livejournal.com/17395.html
80 $windoof = $Config{osname};
81 $HOME = $ENV{USERPROFILE};
82 $fexhome = $ENV{FEXHOME} || $HOME.'\fex';
83 $fextmp = $ENV{FEXTMP} || "$fexhome\\tmp";
84 $idf = "$fexhome\\id";
85 $editor = $ENV{EDITOR} || 'notepad.exe';
86 $useragent = sprintf("fexsend-$version (%s %s)",
87 $Config{osname},$Config{archname});
88 $SSL{SSL_verify_mode} = 0;
89 } else {
90 $prg =~ s:(.*/):: or die "$0: cannot find path\n";
91 $fexsend = $1.'fexsend';
92 $fexget = $1.'fexget';
93 $0 = "$prg @ARGV";
94 if ($Config{osname} =~ /^darwin/i or $ENV{MACOS}) {
95 # https://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
96 $ENV{COPYFILE_DISABLE} = 1;
97 # http://stackoverflow.com/questions/989349/running-a-command-in-a-new-mac-os-x-terminal-window
98 $macos = $Config{osname};
99 $HOME = (getpwuid($<))[7]||$ENV{HOME};
100 $fexhome = $HOME.'/.fex';
101 $fextmp = $ENV{FEXTMP} || "$fexhome/tmp";
102 $fextmp =~ s:/$::;
103 $idf = "$fexhome/id";
104 chmod 0600,$idf;
105 $editor = $ENV{EDITOR} || 'open -W -n -e';
106 $_ = `sw_vers -productVersion 2>/dev/null`||'';
107 chomp;
108 $useragent = "fexsend-$version (MacOS $_)";
109 } else {
110 $HOME = (getpwuid($<))[7]||$ENV{HOME};
111 $fexhome = $HOME.'/.fex';
112 $fextmp = $ENV{FEXTMP} || "$fexhome/tmp";
113 $idf = "$fexhome/id";
114 chmod 0600,$idf;
115 $editor = $ENV{EDITOR} || 'vi';
116 my ($os,$osp,$osn,$osv);
117 if (open my $osr,'/etc/os-release') {
118 while (<$osr>) {
119 $osp = $1 if /^PRETTY_NAME="(.+)"/;
120 $osn = $1 if /^NAME="(.+)"/;
121 $osv = $1 if /^VERSION="(.+)"/;
122 }
123 close $osr;
124 }
125 if ($osp and $osp =~ /\d/) {
126 $os = $osp;
127 } elsif ($osn and $osv) {
128 $os = "$osn $osv";
129 } else {
130 $os = `(lsb_release -d||uname -a)2>/dev/null`||'';
131 chomp $os;
132 $os =~ s/^Description:\s+//;
133 }
134 $useragent = "fexsend-$version ($os)";
135 }
136 }
137 if ($Config{osname} eq 'cygwin' or $ENV{CYGWIN}) {
138 # https://cygwin.com/faq/faq.html#faq.using.fixing-fork-failures
139 $cygwin = 1;
140 $SSL{SSL_verify_mode} = 0;
141 }
142
143 if (-f ($_ = '/etc/fex/config.pl')) {
144 eval { require } or warn $@;
145 }
146
147 if (my $fua = $ENV{FUA}) {
148 if ($useragent =~ /(\S+)/ and $fua !~ /\Q$1/) {
149 $useragent = "$fua/$useragent";
150 } else {
151 $useragent = $fua;
152 }
153 }
154 $ENV{FUA} = $useragent;
155 $ENV{FUA} =~ s/ .*//;
156
157 # force tar format (SUSE uses pax!)
158 $ENV{TAR_OPTIONS} .= ' --format=gnu';
159
160 my $from = '';
161 my $to = '';
162 my $id = '';
163 my $skey = '';
164 my $gkey = '';
165 my $okey = '';
166 my $pkey = '';
167 my $share = '';
168 my $atype = ''; # archive type
169 my $fexcgi; # F*EX CGI URL
170 my @files; # files to send
171 my @ifiles; # files to send from index file
172 my %AB = (); # server based address book
173 my ($server,$port,$sid,$https);
174 my $proxy = '';
175 my $proxy_prefix = '';
176 my $proxy_authorization = '';
177 my $features = '';
178 my $timeout = 30; # server timeout
179 my $fexlist = "$fextmp/fexlist";
180 my ($usage,$hints);
181 my $xx = $prg =~ /\bxx$/;
182 my $xxx = $prg =~ /\bxxx$/;
183
184 if ($xx) {
185 $usage = "
186 usage:
187 send file(s): xx FILE...
188 send all files including .*: xx ./DIRECTORY
189 send STDIN: xx -
190 send pipe: ... | xx
191 get file(s) or STDIN: xx
192 get file(s) no-questions: xx --
193
194 options:
195 -v verbose mode
196 -m KBS limit throughput (KBS kB/s)
197 -p SIZE:WAIT send file in chunks (SIZE MB, WAIT seconds)
198
199 examples:
200 dmesg | xx
201 xx project
202 xx --
203 ";
204 } elsif ($xxx) {
205 $usage = "
206 $prg: internet clipboard with versioning
207 file upload: $prg [-m LIMIT] [-k DAYS] [-w] [-q] [-p] [-z] [-Z] FILE...
208 pipe upload: ... | $prg [-m LIMIT] [-k DAYS] [-w] [-q] [-t] [COMMENT]
209 download: $prg [-m LIMIT] [-o] [DATE_TIME|URL|-]
210 delete: $prg -d [DATE_TIME|URL|REGEXP|-]
211 other: $prg [-l] [-w] [-u] [-x] [-X] [-k DAYS DATE_TIME|URL]
212 options:
213 -m limit throughput (kB/s)
214 -k keep DAYS (default: 1 day)
215 -w show wget download command
216 -q quiet mode: no transfer status info
217 -p add path for single file argument
218 -z force compression
219 -0 force no compression
220 -Z use zip
221 -T use tar (default for UNIX)
222 -t text mode (URL content displayble by webbrowser)
223 -o overwrite existing files
224 -d delete
225 -l list
226 -u generate upload function to be used by other users
227 -x show xxx propagation shell code (with your F*EX ID!)
228 -X same as -x, but generate URL shell command
229 upload arguments:
230 .* files are default excluded, to include use .FILE or ./DIRECTORY
231 download or delete argument:
232 - last upload (in overwrite mode for download)
233 examples:
234 $prg *jpg # upload all jpg files
235 $prg -m 1000 -k 5 . # upload current directory with 1000 kB/s and keep 5 days
236 ls -l | $prg -t # upload directory listing in text format
237 $prg -d - # delete last upload
238 $prg # interactive usage: download, list, delete
239 see also: fexsend, fexget, fexpack, fexstore, fexsync
240 ";
241
242 $hints = '
243 xxx is the extension of the F*EX clipboard xx with versioning.
244
245 With xxx you can store files or directories or pipe data of any size.
246
247 Files or directories beginning with a "." will not be transfered unless you use
248 an argument with such a name.
249 ".del" and ".snapshot*" are always excluded!
250
251 The default keep time is 1 day, then the upload will expire.
252 If you need a longer keep time, then use option -k
253 For verbose mode you can use option -v
254
255 xxx autodetermines whether compression is suggestive by using container taz,
256 see: fexsend -H
257
258 You can use different F*EX accounts with fexsend and xxx:
259 xxx uses the [xx] F*EX id (env $FEXXX) if it is there, otherwise the regular
260 F*EX id (env $FEXID).
261 ';
262
263 } else {
264 $usage = "
265 usage:
266 $prg [options] FILE[...] [@] RECIPIENT[,...]
267 $prg [special option]
268 $prg -l ['RECIPIENT-REGEXP']
269 $prg -f NUMBER RECIPIENT[,...]
270 $prg -x NUMBER [-C -k -D -K]
271 options:
272 -v verbose mode
273 -a ARCHIVE put files in archive (.zip .7z .tar .tgz) without .* files
274 -A ARCHIVE put files in archive (.zip .7z .tar .tgz) with .* files
275 -# FILELIST exclude files (# is list separator) from archive
276 -0 do not compress files for .zip and .7z archives
277 -d delete file on fex server
278 -o overwrite mode, do not try to resume
279 -m KBS limit throughput (KBS kB/s)
280 -p SIZE:WAIT send file in chunks (SIZE MB, WAIT seconds)
281 -i ACCOUNT use ID data [ACCOUNT] from ID file
282 -C 'COMMENT' add COMMENT to notification email
283 -k MAX keep file MAX days on fex server
284 -D delay auto-delete after download
285 -K no auto-delete after download
286 -M MIME-file (to be displayed in recipient's webbrowser)
287 -s STREAM read data from pipe and upload it with STREAM name
288 -r ADDRESS use Reply-To ADDRESS in notification email
289 -n send no notification email, just show the download URL
290 -/ do not upload the file, but tell the server to link it
291 -= ALIAS use ALIAS as filename
292 -^ add date_time to filename
293 -q quiet mode
294 -+ undocumented feature - test it :-)
295 special options:
296 -I initialize ID file or show ID
297 -I ACCOUNT add alternate ID data (secondary logins) to ID file
298 -l list sent files (numbers needed for -f -x -d -N)
299 -u list download URLs of sent files (show NUMBERs)
300 -f NUMBER forward already uploaded file to another recipient
301 -x NUMBER use -C -k -D -K for already uploaded file
302 -d NUMBER delete file on fex server
303 -N NUMBER resend notification email
304 -Q check quotas
305 -U generate upload URL with bash function (-U+ for archives)
306 -T UP:DOWN test internet speed with upload and download MBs
307 -@ edit server address book (aliases)
308 -S show server/user settings and auth-ID
309 -V show version and installation/upgrade hint
310 -H show hints and examples and more options
311 examples:
312 $prg visualization.mp4 framstag\@rus.uni-stuttgart.de
313 $prg -a images.zip *.jpg webmaster\@belwue.de,metoo
314 lshw | $prg -s hardware.list admin\@belwue.de
315 ";
316 # usage: $prg -R FEX-URL email
317 # -R FEX mail self-register your email address at FEX server
318
319 $hints = '
320 fexsend hints and more options:
321
322 usage: fexsend [options] file recipient(s)
323
324 Recipient can be a comma separated address list. Example:
325 fexsend big.file [email protected],[email protected]
326
327 Recipient can be an alias from your server address book
328 (use "fexsend -@" to edit it). Example:
329 fexsend big.file framstag
330
331 Recipient can be a SKEY URL, which you have received from a regular F*EX user.
332 When using this URL you are a subuser of this full user and the file will be
333 sent to him. Example:
334 fexsend big.file http://fex.rus.uni-stuttgart.de/fup?skey=4285f8cdd881626524fba686d5f0a83a
335
336 Recipient can be a GKEY URL, which you have received from a regular F*EX user.
337 Using this URL you are a member of his group and the file will be sent to all
338 members of this group. Example:
339 fexsend big.file http://fex.rus.uni-stuttgart.de/fup?gkey=50d26547b1e8c1110beb8748fc1d9444
340
341 Recipient can be a OKEY URL, which you have received from a regular F*EX user.
342 Using this URL you can send exactly one file. Example:
343 fexsend big.file http://fex.rus.uni-stuttgart.de/[email protected]?okey=HZ6UEkCQ
344
345 When you use "FEX-URL/anonymous" as recipient and your F*EX administrator has
346 allowed anonymous upload for your IP address then no auth-ID is needed.
347
348 "." as recipient means fex to yourself and show the download URL after upload
349 without sending a notification email. Example:
350 fexsend software.tar .
351
352 "+" as recipient means fex to yourself and show the download URL immediatelly.
353 With this URL the file can be downloaded while the upload is still in progress!
354 Example:
355 fexsend big_VM.tar +
356
357 "//" as recipient means fex to yourself and create extra short download URL.
358 Example:
359 fexsend software.tar //
360
361 If you want a Bcc of the notification email then add \'!bcc!\' to the comment:
362 fexsend -C \'!bcc! for me and you\' ...
363
364 To manage your subuser and groups or forward or redirect files, use a
365 webbrowser with the URL from "fexsend -U", e.g.: firefox $(fexsend -U)
366
367 If you want to copy-forward an already uploaded file to another recipient,
368 then you first have to query the file number with:
369 fexsend -l
370 and then copy-forward it with:
371 fexsend -b # other@address
372 Where # is the file number.
373
374 You can list an uploaded file in more detail with
375 fexsend -l #
376 Where # is the file number.
377
378 If you want to modify the keep time, comment or auto-delete behaviour of an
379 already uploaded file then you first have to query the file number with:
380 fexsend -l
381 and then for example set the keep time to 30 days with:
382 fexsend -x # -k 30
383 Where # is the file number.
384
385 To list files sent only to yourself, use:
386 fexsend -l .
387
388 To list files sent to others, but yourself, use:
389 fexsend -l -
390
391 To show the content of your id file, use:
392 fexsend -I -
393
394 With option -. the server sends a short instead of a detailed notification
395 email to the recipient.
396
397 With option -a (excludes .* files) or -A (includes .* files) you can send
398 several files or whole directories within a single archive file.
399 ".del" and ".snapshot*" are always excluded!
400 To keep the full path inside the archive, add a trailing / to the directory
401 name, example: fexsend -a example.tar keep/this/directory/path/ .
402
403 With option -O USER you can generate an onetime upload URL
404 (USER must be an email address).
405
406 Option -# uses shell pattern to exclude files.
407
408 The archive types tar and tgz are build on-the-fly (streaming) whereas archive
409 types zip and 7z need a temporary archive file in $HOME/.fex/tmp/ (disk space!)
410
411 When you use the pseudo archive type taz, then fexsend will determine whether
412 compression is suggestive by inspecting all file names and then uses either
413 tar or tgz archive. Suggestive means: total size must be less than 2 GB and at
414 least 20% must be compressable.
415
416 With option -s you can send any data coming from a pipe (STDIN) as a file
417 without wasting local disc space.
418
419 With option -p SIZE:WAIT you can send your file in chunks.
420 This may be necessary if you have a firewall/gateway with a tcp transfer
421 limitation (e.g. Arbor). The option parameter SIZE:WAIT specifies the chunk
422 size in MB and the time to wait between the chunks in seconds. Examples:
423 fexsend -p 100 data.tar [email protected] # 100 MB chunks
424 fexsend -p 10:3 data.tar [email protected] # 10 MB chunks with 3 s wait time
425
426 With option -X you can specify any URL parameter, e.g.:
427 fexsend -X autodelete=yes ...
428 fexsend -X \'autodelete=no&locale=german\' ...
429
430 With option -F you activate female mode.
431
432 For HTTPS you can set the environment variables:
433 SSLVERIFY=1 # activate server identity verification
434 SSLVERSION=TLSv1_2 # this is the default
435 SSLCAPATH=/etc/ssl/certs # path to trusted (root) certificates
436 SSLCAFILE=/etc/ssl/cert.pem # file with trusted (root) certificates
437 SSLCIPHERLIST=HIGH:!3DES # see "man ciphers" from openssl
438
439 Partner programs:
440 fexget is for downloading. See: fexget -h
441 fexpush is for archive sharing. See: fexpush -h
442 fexpull is for archive sharing. See: fexpull -h
443 xx is an internet clipboard. See: xx -h
444 xxx is an internet clipboard with versioning. See: xxx -h
445 fexpack is a named internet clipboard. See: fexpack -h
446
447 fexsend stores the login data (server, user and auth-ID) in the file
448 $HOME/.fex/id
449 The format of this file is ([data] is optional):
450
451 server-URL[!proxy[:port[:chunk-size]]
452 email-address
453 auth-ID
454
455 For temporary usage of a HTTP proxy use:
456 fexsend -P your_proxy:port:chunksize_in_MB file recipient
457 Example:
458 fexsend -P wwwproxy.uni-stuttgart.de.de:8080:1024 4GB.tar .
459
460 For temporary usage of an alternative F*EX server or user use:
461 FEXID="FEXSERVER USER AUTHID" fexsend file recipient
462 Example:
463 FEXID="fex.belwue.de [email protected] blubb" fexsend big.file [email protected]
464
465 When using http (and not https), then your auth-ID will be sent encrypted with
466 a one-time session ID, offered by the F*EX server. This will no happen, when
467 you use an http proxy, because most proxys do not support connection keep alive.
468
469 You can define aliases (and optional fexsend options) in $HOME/.fex/config.pl:
470 %alias = (
471 \'alias1\' => \'[email protected]\',
472 \'alias2\' => \'[email protected]\',
473 \'both\' => \'[email protected],[email protected]\',
474 \'extra\' => \'[email protected]:-i other -K -k 30\',
475 );
476
477 fexsend also respects aliases in $HOME/.mutt/aliases
478 The alias priority is (descending):
479 $HOME/.fex/config.pl
480 $HOME/.mutt/aliases
481 fexserver address book
482
483 In $HOME/.fex/config.pl you can also set the SSL* environment variables and the
484 $opt_* variables, e.g.:
485
486 $ENV{SSLVERSION} = \'TLSv1_2\';
487 ${\'opt_+\'} = 1; # equivalent to option -+
488 $opt_0 = 1; # equivalent to option -0
489 $opt_m = 200; # equivalent to option -m 200
490 ';
491 }
492
493 $usage =~ s/^\n//;
494
495 my @rcamel = (
496 '
497 _ _ c*_)
498 / \/ \//
499 *=( __ /
500 \\\\/\\\\/
501 ',
502 " \\\\/\\\\/ \n",
503 " //\\\\//\\\\\n"
504 );
505
506 my @rrcamel = (
507 '
508 (_*p _ _
509 \\\\/ \/ \\
510 \ __ )=*
511 //\\\\//\\\\
512 ',
513 " \\\\/\\\\/ \n",
514 " //\\\\//\\\\\n"
515 );
516
517 autoflush STDOUT;
518 autoflush STDERR;
519
520 if ($windoof and not @ARGV and not $ENV{PROMPT}) {
521 # restart with cmd.exe to have mouse cut+paste
522 exec qw'cmd /k',$prg,'-W';
523 exit;
524 }
525
526 unless (-d $fexhome) {
527 mkdir $fexhome or die "$prg: cannot create $fexhome - $!\n";
528 chmod 0700,$fexhome;
529 }
530
531 unless (-d $fextmp) {
532 mkdir $fextmp or die "$prg: cannot create $fextmp - $!\n";
533 chmod 0700,$fextmp;
534 }
535
536 if (-d $fextmp and my @s = stat($fextmp)) {
537 if ($< ne $s[4]) {
538 die "$prg: not owner of $fextmp\n";
539 }
540 my $yt = time-24*60*60;
541 foreach my $tmp (glob "$fextmp/*") {
542 if (@s = stat $tmp) {
543 unlink $tmp if $yt > $s[9];
544 }
545 }
546 } else {
547 die "$prg: $fextmp is not a directory\n";
548 }
549
550
551 my @_ARGV = @ARGV; # save arguments
552
553 our ($opt_q,$opt_h,$opt_H,$opt_v,$opt_m,$opt_c,$opt_k,$opt_d,$opt_l,$opt_I,
554 $opt_K,$opt_D,$opt_u,$opt_f,$opt_a,$opt_C,$opt_R,$opt_M,$opt_L,$opt_Q,
555 $opt_A,$opt_i,$opt_z,$opt_Z,$opt_b,$opt_P,$opt_x,$opt_X,$opt_V,$opt_U,
556 $opt_s,$opt_o,$opt_g,$opt_F,$opt_n,$opt_r,$opt_S,$opt_N,$opt_T,$opt_O,
557 $opt_p,$opt_t,$opt_w,$opt_0);
558
559 if ($xx) {
560 $opt_q = 1 if @ARGV and $ARGV[-1] eq '--' and pop @ARGV or not -t STDOUT;
561 $opt_h = $opt_v = $opt_I = 0;
562 $opt_X = $opt_p = $opt_m = $opt_i = '';
563 $_ = "$fexhome/config.pl"; require if -f;
564 getopts('hvI.m:p:i:') or die $usage;
565 } elsif ($xxx) {
566 $opt_h = $opt_H = $opt_v = $opt_g = $opt_d = $opt_I = $opt_u = 0;
567 $opt_l = $opt_L = $opt_x = $opt_X = $opt_o = $opt_t = $opt_w = 0;
568 $opt_k = $opt_z = $opt_Z = $opt_T = $opt_q = $opt_p = $opt_0 = 0;
569 $opt_m = '';
570 getopts('hHvlLdIuxXopzZTtwq.0m:k:') or die $usage;
571 if ($opt_g+$opt_d+$opt_u+$opt_l+$opt_L+$opt_x+$opt_X > 1) {
572 die "$prg: you cannot mix options -d -g -u -l -x -X\n";
573 }
574 $opt_x ||= $opt_X;
575 @ARGV = ('-') unless -t STDOUT or @ARGV;
576 $opt_g = 1 if -t STDIN and not @ARGV and
577 not $opt_l||$opt_L||$opt_u||$opt_x||$opt_d||$opt_I;
578 if ($opt_w) {
579 $ENV{opt_w} = $opt_w;
580 $opt_l = $opt_w if -t STDIN and not @ARGV;
581 }
582 } else {
583 if ($macos and not @ARGV) {
584 &ask_file;
585 }
586 $opt_h = $opt_v = $opt_m = $opt_c = $opt_d = $opt_l = $opt_I = 0;
587 $opt_H = $opt_K = $opt_D = $opt_R = $opt_M = $opt_L = $opt_Q = $opt_0 = 0;
588 $opt_x = $opt_o = $opt_g = $opt_V = $opt_U = $opt_F = $opt_n = $opt_q = 0;
589 $opt_S = $opt_N = $opt_u = 0;
590 ${'opt_@'} = ${'opt_!'} = ${'opt_+'} = ${'opt_.'} = ${'opt_/'} = 0;
591 ${'opt_^'} = 0;
592 ${'opt_='} = ${'opt_~'} = ${'opt_#'} = ${'opt_?'} = '';
593 $opt_k = $opt_f = $opt_C = $opt_i = $opt_b = $opt_P = $opt_X = '';
594 $opt_a = $opt_A = $opt_s = $opt_r = $opt_T = $opt_p = $opt_O = '';
595 $_ = "$fexhome/config.pl"; require if -f;
596 unshift @ARGV,split if $_ = $ENV{FEXSEND_OPT};
597 getopts('hHvdonVDKlILuURWMFzZqQS@0!+./^?~:r:m:k:f:a:A:s:C:i:b:O:p:P:x:X:N:T:=:#:')
598 or die $usage;
599
600 if ($opt_V) {
601 $useragent =~ /fexsend-(.+)/;
602 print "Version: $1\n";
603 if ("@ARGV" ne '.' and abs_path($_0) !~ m:/sw/:) {
604 print "Upgrade: wget fex.belwue.de/download/fex.pl && perl fex.pl\n";
605 }
606 exit;
607 if (not @ARGV and -w $_0) {
608 print "Update fexsend? "; # ex "upgrade fexsend"
609 $_ = <STDIN>||'';
610 if (/^y/i) {
611 my $url = 'http://fex.belwue.de/download/fexsend';
612 warn "$prg: downloading $url\n";
613 my $new = `wget -qO- $url`;
614 if ($new !~ /update fexsend/) { die "fexsend: bad update\n" }
615 if ($new =~ /version = (\d+)/ and $1 le $version) {
616 warn "$prg: version $version is uptodate\n";
617 exit;
618 }
619 $_0 = abs_path($_0);
620 system "vv -vs '$_0' 2>/dev/null || rsync -av '$_0' '$_0~'";
621 exit $? if $?;
622 open $_0,'>',$_0 or die "$prg: cannot write $_0 - $!\n";
623 print {$_0} $new;
624 close $_0;
625 exec $_0,qw'-V .';
626 }
627 }
628 exit;
629 }
630
631 if ($opt_K and $opt_D) {
632 die "$prg: you cannot use both options -D and -K\n";
633 }
634
635 if ($opt_a and $opt_A) {
636 die "$prg: you cannot use both options -a and -A\n";
637 }
638
639 $opt_a = $opt_A if $opt_A;
640 $opt_l = $opt_u if $opt_u;
641
642 if ($opt_a and $opt_c) {
643 $opt_a =~ s/\.tar$/.tgz/;
644 }
645
646 if ($opt_a and $opt_s) {
647 die "$prg: you cannot use both options -a and -s\n";
648 }
649
650 if ($opt_g and $opt_c) {
651 $opt_c = 0;
652 }
653
654 if ($opt_A) {
655 $xlist = ${'opt_#'} . '#.del#.snapshot*#.fex/tmp';
656 } else {
657 $xlist = ${'opt_#'} . '#.*';
658 }
659 $xlist =~ s/^#//;
660
661 $xp = $xlist;
662 $xp =~ s/\./\\./g;
663 $xp =~ s/\+/\\+/g;
664 $xp =~ s/\?/./g;
665 $xp =~ s/\*/.*/g;
666 $xp =~ s/\#/|/g;
667 # $xp = "#|$xp";
668
669 $opt_f ||= $opt_b;
670 if ($opt_f and $opt_f !~ /^\d+$/) {
671 die "$prg: option -f needs a number, see $prg -l\n";
672 }
673
674 if ($opt_I and $opt_R) {
675 die "$prg: you cannot use both options -I and -R\n";
676 }
677
678 $command =
679 ($opt_d) ? 'DELETE':
680 ($opt_l or $opt_L) ? 'LIST':
681 ($opt_Q) ? 'CHECKQUOTA':
682 ($opt_S) ? 'LISTSETTINGS':
683 ($opt_Z) ? 'RECEIVEDLOG':
684 ($opt_z) ? 'SENDLOG':
685 (${'opt_!'}) ? 'FOPLOG':
686 '';
687
688 $opt_D =
689 ($opt_D) ? 'DELAY':
690 ($opt_K) ? 'NO':
691 $opt_D;
692 }
693
694 die $usage if $opt_m and $opt_m !~ /^\d+/;
695
696 if ($opt_h) {
697 female_mode("show help?") if $opt_F;
698 print $usage;
699 exit;
700 }
701
702 if ($opt_H) {
703 if (0 and -t STDOUT) {
704 exec "$prg -H |".($ENV{PAGER}||'less');
705 } else {
706 print $hints;
707 }
708 exit;
709 }
710
711 if ($opt_R) {
712 &register;
713 exit;
714 }
715
716 if ($opt_I) {
717 if ($xx or $xxx) { &show_id }
718 else { &init_id }
719 exit;
720 }
721
722 if ($xx or $xxx) {
723
724
725 # alternativ ID?
726 if ($opt_i) {
727 $proxy = $proxy_prefix = '';
728 open $idf,$idf or die "$prg: cannot open $idf - $!\n";
729 while (<$idf>) {
730 if (/^\[$opt_i\]/) {
731 get_id($idf);
732 last;
733 }
734 }
735 close $idf;
736 die "$prg: no [$opt_i] in $idf\n" unless $_;
737 } elsif ($FEXXX = $ENV{FEXXX}) {
738 $FEXXX = decode_b64($FEXXX) if $FEXXX !~ /\s/;
739 ($fexcgi,$from,$id) = split(/\s+/,$FEXXX);
740 warn "using ID from \$FEXXX\n" if $opt_v;
741 } elsif (open $idf,$idf) {
742 while (<$idf>) {
743 if (/^\[xx\]/) {
744 $proxy = $proxy_prefix = '';
745 get_id($idf);
746 warn "using ID from $idf [xx]\n" if $opt_v;
747 last;
748 }
749 }
750 close $idf;
751 }
752
753 unless ($id) {
754 if ($FEXID = $ENV{FEXID}) {
755 $FEXID = decode_b64($FEXID) if $FEXID !~ /\s/;
756 ($fexcgi,$from,$id) = split(/\s+/,$FEXID);
757 warn "using ID from \$FEXID\n" if $opt_v;
758 } else {
759 if (open $idf,$idf) {
760 get_id($idf);
761 close $idf;
762 warn "using ID from $idf\n" if $opt_v;
763 }
764 }
765 }
766
767 unless ($id) {
768 die "$prg: no $idf or \$FEXID or \$FEXXX available\n";
769 }
770
771 } else {
772
773 # alternativ ID?
774 if ($opt_i) {
775 $proxy = $proxy_prefix = '';
776 open $idf,$idf or die "$prg: cannot open $idf - $!\n";
777 while (<$idf>) {
778 if (/^\[$opt_i\]/) {
779 get_id($idf);
780 last;
781 }
782 }
783 close $idf;
784 die "$prg: no [$opt_i] in $idf\n" unless $_;
785 } else {
786 if ($ENV{FEXXX} and not $ENV{FEXID} and not -f $idf) {
787 $ENV{FEXID} = $ENV{FEXXX};
788 }
789 if ($FEXID = $ENV{FEXID}) {
790 $FEXID = decode_b64($FEXID) if $FEXID !~ /\s/;
791 ($fexcgi,$from,$id) = split(/\s+/,$FEXID);
792 die "$prg: no id in \$FEXID\n" unless $id;
793 $id =~ s/^PKEY:// and $pkey = $id;
794 warn "using ID from \$FEXID\n" if $opt_v;
795 } else {
796 if ($windoof and not -f $idf) { &init_id }
797 if (open $idf,$idf) {
798 get_id($idf);
799 close $idf;
800 warn "using ID from $idf\n" if $opt_v;
801 }
802 }
803 }
804 }
805
806 if (0 and not $proxy and $proxy = $ENV{http_proxy}) {
807 if ($proxy =~ s{(http://)(\S+:\S+)@}{$1}) {
808 $proxy_authorization = encode_b64($2);
809 }
810 if ($proxy =~ m{(http://\S+)(:(\d+))?}) {
811 $proxy_prefix = $1.($2||'');
812 }
813 }
814
815 if ($xxx) {
816 &xxx;
817 exit;
818 }
819
820 if ($opt_P) {
821 if ($opt_P =~ /^([\w.-]+:\d+)(:(\d+))?/) {
822 $proxy = $1;
823 $chunksize = $3 || 0;
824 } else {
825 die "$prg: proxy must be: SERVER:PORT\n";
826 }
827 } elsif ($opt_p =~ /^(\d+)$/) {
828 $chunksize = $1;
829 $sleepwait = 1;
830 } elsif ($opt_p =~ /^(\d+):(\d+)$/) {
831 $chunksize = $1;
832 $sleepwait = $2;
833 }
834 $chunksize *= MB;
835
836 if ($opt_T) {
837 my ($up,$down);
838
839 $usage = "usage: $prg -T MB_up[:MB_down] [fexserver]\n";
840 if ($opt_T =~ /^(\d+)$/) {
841 $up = $down = $1;
842 } elsif ($opt_T =~ /^(\d+):(\d+)$/) {
843 $up = $1;
844 $down = $2;
845 } else {
846 die $usage;
847 }
848
849 if (@ARGV) {
850 nettest($ARGV[0],$up,$down);
851 } elsif ($fexcgi) {
852 nettest($fexcgi,$up,$down);
853 } else {
854 nettest('fex.belwue.de',$up,$down);
855 }
856 exit;
857 }
858
859 if (@ARGV > 1 and $ARGV[-1] =~ /(^|\/)anonymous/) {
860 $fexcgi = $1 if $ARGV[-1] =~ s:(.+)/::;
861 die "usage: $prg [options] file FEXSERVER/anonymous\n" unless $fexcgi;
862 $anonymous = $from = 'anonymous';
863 $sid = $id = 'ANONYMOUS';
864 } elsif (@ARGV > 1 and $id eq 'PUBLIC') {
865 $public = $sid = $id;
866 } elsif (@ARGV > 1 and $ARGV[-1] =~ m{^(https?://[\w.-]+(:\d+)?/fup.*\?[sgo]key=\w+)}) {
867 $fexcgi = $1;
868 if ($fexcgi =~ /skey=(\w+)/) {
869 $skey = $1;
870 if ($command) {
871 die "$prg: command $command not allowed for subuser\n";
872 }
873 }
874 if ($fexcgi =~ /gkey=(\w+)/) {
875 $gkey = $1;
876 if ($command) {
877 die "$prg: command $command not allowed for groupuser\n";
878 }
879 }
880 if ($fexcgi =~ /okey=(\w+)/) {
881 $okey = $1;
882 if ($command) {
883 die "$prg: command $command not allowed for onetime user\n";
884 }
885 }
886 } else {
887
888 if ($ENV{FEXSERVER} and not $ENV{GATEWAY_INTERFACE} and not $opt_i) {
889 $fexcgi = $ENV{FEXSERVER};
890 }
891
892 if (not -e $idf and not ($fexcgi and $from and $id)) {
893 die "$prg: no ID file $idf found, use \"fexsend -I\" to create it\n";
894 }
895
896 unless ($fexcgi) {
897 die "$prg: no FEX URL found, use \"$prg -u URL\" or \"$prg -I\"\n";
898 }
899
900 unless ($from and $id) {
901 die "$prg: no sender found, use \"$prg -f FROM:ID\" or \"$prg -I\"\n";
902 }
903
904 if ($fexcgi !~ /^http/) {
905 if ($fexcgi =~ /:443/) { $fexcgi = "https://$fexcgi" }
906 else { $fexcgi = "http://$fexcgi" }
907 }
908
909 }
910
911 $server = $fexcgi;
912
913 $port = 80;
914 $port = 443 if $server =~ s{https://}{};
915 $port = $1 if $server =~ s/:(\d+)//;
916
917 if ($port == 443) {
918 # $opt_s and die "$prg: cannot use -s with https due to stunnel bug\n";
919 # $opt_g and die "$prg: cannot use -g with https due to stunnel bug\n";
920 $https = $port;
921 }
922
923 $server =~ s{http://}{};
924 $server =~ s{/.*}{};
925
926 if ($proxy) {
927 if ($port == 80) { $proxy_prefix = "http://$server" }
928 elsif ($port != 443) { $proxy_prefix = "http://$server:$port" }
929 }
930
931 # xx: special file exchange between own accounts
932 if ($xx) {
933 if ($opt_v) {
934 print "server: $server:$port\n";
935 print "user: $from\n";
936 # printf "FEXXX=%s\n",encode_b64($FEXXX);
937 }
938 $transferfile = "$fextmp/STDFEX";
939 open my $lock,'>>',$transferfile
940 or die "$prg: cannot write $transferfile - $!\n";
941 flock($lock,LOCK_EX|LOCK_NB)
942 or die "$prg: $transferfile is locked by another process\n";
943 truncate $transferfile,0;
944 if (not @ARGV and -t) {
945 &get_xx($transferfile);
946 } else {
947 &send_xx($transferfile);
948 }
949 exit;
950 }
951
952
953 # regular fexsend
954
955 if (${'opt_?'} or
956 ($windoof or $cygwin) and not @ARGV and $useragent !~ /^\w+\// and not
957 ($opt_l or $opt_L or $opt_Q or $opt_A or $opt_U or $opt_I or
958 $opt_f or $opt_x or $opt_N or ${'opt_~'}))
959 { &inquire }
960
961 unless (length $opt_C) {
962 $opt_C = $ENV{FEXCOMMENT}||'';
963 }
964
965 if (${'opt_.'}) {
966 $opt_C = "!SHORTMAIL! $opt_C";
967 }
968
969 if ($opt_n or $opt_C =~ /^(NOMAIL|!#!)/) {
970 $opt_C = $nomail = 'NOMAIL';
971 }
972
973 if ((not ($opt_q||$skey||$gkey||$okey||$anonymous) and ($opt_i||${'opt_/'}
974 or $ENV{FEXID}) and $useragent !~ m:/:))
975 { print "server/user: $fexcgi/$from\n" }
976
977 my $A = "@ARGV";
978 if ($opt_f) { &forward }
979 elsif ($opt_x) { &modify }
980 elsif ($opt_N) { &renotify }
981 elsif ($opt_Q) { &query_quotas }
982 elsif ($opt_S) { &query_settings }
983 elsif ($opt_l or $opt_L) { &list }
984 elsif ($opt_U) { command('GENUKEY',@ARGV) }
985 elsif ($opt_O) { command("GENOKEY:$opt_O") }
986 elsif ($opt_z or $opt_Z or ${'opt_!'}) { &get_log }
987 elsif (${'opt_@'}) { edit_address_book($from) }
988 elsif (${'opt_~'}) { command(${'opt_~'}) }
989 elsif ($opt_d and $anonymous) { &purge }
990 elsif ($opt_d and $A =~ /^[\d ]+$/) { &delete_file_number }
991 elsif ($opt_d and $A =~ m:^http.*/fop/:) { &delete_file_url }
992 elsif ($opt_V||$opt_v and not @ARGV) { exit }
993 else { &send_fex }
994 exit;
995
996
997 # initialize ID file or show ID
998 sub init_id {
999 my $tag;
1000 my $proxy = '';
1001
1002 if ($opt_I) {
1003 $tag = shift @ARGV;
1004 die $usage if @ARGV;
1005 }
1006
1007 $fexcgi = $from = $id = '';
1008
1009 unless (-d $fexhome) {
1010 mkdir $fexhome or die "$prg: cannot create FEXHOME $fexhome - $!\n";
1011 chmod 0700,$fexhome;
1012 }
1013
1014 # show ID
1015 if (not $tag and open $idf,$idf) {
1016 if ($opt_i) {
1017 while (<$idf>) {
1018 last if /^\[$opt_i\]/;
1019 }
1020 }
1021 $fexcgi = <$idf>;
1022 $from = <$idf>;
1023 $id = <$idf>;
1024 close $idf;
1025 if ($id) {
1026 chomp($fexcgi,$from,$id);
1027 $FEXID = encode_b64("$fexcgi $from $id");
1028 if (-t STDIN) {
1029 print "# server: $fexcgi\n";
1030 print "# user: $from\n";
1031 print "# hint: to edit the ID file \$HOME/.fex/id use \"$prg -I .\" #\n";
1032 print "export FEXID=$FEXID;history -d \$((HISTCMD-1))\n";
1033 } else {
1034 print "FEXID=$FEXID\n";
1035 }
1036 exit;
1037 } else {
1038 die "$prg: no ID data found\n";
1039 }
1040 }
1041
1042 if ($tag) {
1043 if ($tag eq '.') {
1044 exec $ENV{EDITOR}||'vi',$idf;
1045 }
1046 if ($tag eq '-') {
1047 open $idf,$idf or die "$prg: cannot open $idf - $!\n";
1048 print while <$idf>;
1049 exit;
1050 }
1051 if ($tag =~ /^FEXID=(\S+)$/) {
1052 $FEXID = $1;
1053 if (-f $idf) {
1054 die "$prg: $idf does already exist\n".
1055 "$prg: use \"$prg -I .\" to edit it\n";
1056 }
1057 ($fexcgi,$from,$id) = split(/\s+/,decode_b64($FEXID));
1058 unless ($id) {
1059 die "$prg: $FEXID is not a valid FEXID\n";
1060 }
1061 if (open $idf,'>',$idf) {
1062 print {$idf} "$fexcgi\n",
1063 "$from\n",
1064 "$id\n";
1065 close $idf;
1066 print "data written to $idf\n";
1067 exit;
1068 } else {
1069 die "$prg: cannot write to $idf - $!\n";
1070 }
1071 }
1072 }
1073
1074 if ($tag) { print "F*EX server URL for [$tag]: " }
1075 else { print "F*EX server URL: " }
1076 $fexcgi = <STDIN>;
1077 $fexcgi =~ s/[\s\n]//g;
1078 die "you MUST provide a FEX-URL!\n" unless $fexcgi;
1079 if ($fexcgi =~ /\?/) {
1080 $from = $1 if $fexcgi =~ /\bfrom=(.+?)(&|$)/i;
1081 $id = $1 if $fexcgi =~ /\bid=(.+?)(&|$)/i;
1082 # $skey = $1 if $fexcgi =~ /\bskey=(.+?)(&|$)/i;
1083 # $gkey = $1 if $fexcgi =~ /\bgkey=(.+?)(&|$)/i;
1084 die "$prg: cannot use GKEY URL in ID file\n" if $fexcgi =~ /gkey=/i;
1085 die "$prg: cannot use SKEY URL in ID file\n" if $fexcgi =~ /skey=/i;
1086 $fexcgi =~ s/\?.*//;
1087 }
1088 unless ($fexcgi =~ /^[_:=\w\-\.\/\@\%]+$/) {
1089 die "\"$fexcgi\" is not a legal FEX-URL!\n";
1090 }
1091 $fexcgi =~ s:/fup/*$::;
1092 print "proxy address (hostname:port or empty if none): ";
1093 $proxy = <STDIN>;
1094 $proxy =~ s/[\s\n]//g;
1095 if ($proxy =~ /^[\w.-]+:\d+$/) {
1096 $proxy = "!$proxy";
1097 } elsif ($proxy =~ /\S/) {
1098 die "wrong proxy address format\n";
1099 } else {
1100 $proxy = "";
1101 }
1102 if ($proxy) {
1103 print "proxy POST limit in MB (use 2048 if unknown): ";
1104 $_ = <STDIN>;
1105 if (/(\d+)/) {
1106 $proxy .= "[$1]";
1107 }
1108 }
1109 if ($skey) {
1110 $from = 'SUBUSER';
1111 $id = $skey;
1112 } elsif ($gkey) {
1113 $from = 'GROUPMEMBER';
1114 $id = $gkey;
1115 } elsif ($okey) {
1116 $from = 'ONETIMEUSER';
1117 $id = $okey;
1118 } else {
1119 unless ($from) {
1120 print "Your email address as registered at $fexcgi: ";
1121 $from = <STDIN>;
1122 $from =~ s/[\s\n]//g;
1123 die "you MUST provide your email address!\n" unless $from;
1124 }
1125 unless ($from =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
1126 die "\"$from\" is not a legal email address!\n";
1127 }
1128 unless ($id) {
1129 print "Your auth-ID for $from at $fexcgi: ";
1130 $id = <STDIN>;
1131 $id =~ s/[\s\n]//g;
1132 die "you MUST provide your ID!\n" unless $id;
1133 }
1134 }
1135 if (open $idf,'>>',$idf) {
1136 print {$idf} "\n[$tag]\n" if $tag and -s $idf;
1137 print {$idf} "$fexcgi$proxy\n",
1138 "$from\n",
1139 "$id\n";
1140 close $idf;
1141 print "data written to $idf\n";
1142 } else {
1143 die "$prg: cannot write to $idf - $!\n";
1144 }
1145 }
1146
1147
1148 sub show_id {
1149 my ($fexcgi,$from,$id);
1150 if (open $idf,$idf) {
1151 $fexcgi = <$idf>;
1152 # $fexcgi = <$idf> if $fexcgi =~ /^\[.+\]/;
1153 $from = <$idf>;
1154 $id = <$idf>;
1155 while (<$idf>) {
1156 if (/^\[xx\]/) {
1157 $fexcgi = <$idf>;
1158 $from = <$idf>;
1159 $id = <$idf>;
1160 }
1161 }
1162 close $idf;
1163 die "$prg: too few data in $idf" unless defined $id;
1164 chomp($fexcgi);
1165 chomp($from);
1166 chomp($id);
1167 $FEXXX = encode_b64("$fexcgi $from $id");
1168 if (-t STDIN) {
1169 print "# server: $fexcgi\n";
1170 print "# user: $from\n";
1171 print "export FEXXX=$FEXXX;history -d \$((HISTCMD-1))\n";
1172 } else {
1173 print "FEXXX=$FEXXX\n";
1174 }
1175 } else {
1176 die "$prg: cannot read $idf - $!\n";
1177 }
1178 }
1179
1180
1181 sub register {
1182 my $fs = shift @ARGV or die $usage;
1183 my $mail = shift @ARGV or die $usage;
1184 my $port;
1185 my ($server,$user,$id);
1186
1187 die "$prg: $idf does already exist\n" if -e $idf;
1188
1189 if ($fs =~ /^https/) {
1190 die "$prg: cannot handle https at this time\n";
1191 }
1192
1193 $fs =~ s{^http://}{};
1194 $fs =~ s{/.*}{};
1195 if ($fs =~ s/:(\d+)//) { $port = $1 }
1196 else { $port = 80 }
1197
1198 tcpconnect($fs,$port);
1199 sendheader("$fs:$port","GET $proxy_prefix/fur?user=$mail&verify=no HTTP/1.1");
1200 http_response();
1201
1202 # header
1203 while (<$SH>) {
1204 s/\r//;
1205 warn "<-- $_" if $opt_v;
1206 last if /^\s*$/;
1207 }
1208
1209 while (<$SH>) {
1210 s/\r//;
1211 warn "<-- $_" if $opt_v;
1212 if (m{http://(.*)/fup\?from=(.+)&ID=(.+)}) {
1213 $server = $1;
1214 $user = $2;
1215 $id = $3;
1216
1217 if (open F,">$idf") {
1218 print F "$server\n",
1219 "$user\n",
1220 "$id\n";
1221 close F;
1222 chmod 0600,$idf;
1223 print "user data written to $idf\n";
1224 print "you can now fex!\n";
1225 exit;
1226 } else {
1227 die "$prg: cannot write to $idf - $!\n";
1228 }
1229 }
1230 }
1231
1232 die "$prg: no account data received from F*EX server\n";
1233
1234 }
1235
1236
1237 # menu for MacOS users
1238 sub menu {
1239 my $key;
1240 my $new;
1241 local $_;
1242
1243 system 'clear';
1244 print "\n";
1245 print "fexsend-$version\n";
1246
1247 for (;;) {
1248 if (open $idf,$idf) {
1249 $fexcgi = getline($idf) and
1250 $from = getline($idf) and
1251 $id = getline($idf);
1252 close $idf;
1253 last if $id;
1254 }
1255 &set_ID;
1256 }
1257
1258 print "\n";
1259 print "$from on $fexcgi\n";
1260 print "\n";
1261
1262 for (;;) {
1263 print "\n";
1264 print "[s] send a file or directory\n";
1265 # print "[u] update fexsend\n";
1266 print "[l] change login data (user, server, auth-ID)\n";
1267 print "[h] help\n";
1268 print "[q] quit\n";
1269 print "\n";
1270 print "your choice: ";
1271 $key = ReadKey(0);
1272 if ($key eq 'q') {
1273 print "$key\n";
1274 print "\n";
1275 print "Type [Cmd]W to close this window.\n";
1276 exit;
1277 }
1278 if ($key eq 'h') {
1279 print "$key\n";
1280 print
1281 "\n".
1282 "With fexsend you can send files of any size to any email address.\n".
1283 "\n".
1284 "At the recipient or file prompt [ENTER] brings you to this option menu.\n".
1285 "\n".
1286 "To send more than one file:\n".
1287 "When you enter * at the file prompt, you will be first asked for an archive name\n".
1288 "and then you can drag+drop multiple files.\n".
1289 "\n".
1290 "Do not forget to terminate each input line with [ENTER].\n".
1291 "\n".
1292 "See http://fex.rus.uni-stuttgart.de/ for more information.\n";
1293 next;
1294 }
1295 if (0 and $key eq 'u') {
1296 print "$key\n";
1297 if ($prg =~ m:(^/client/|/sw/):) {
1298 print "\n";
1299 print "use swupdate to update fexsend!\n";
1300 next;
1301 }
1302 $new = $prg.'.new';
1303 system qw'wget -nv -O',$new,'http://fex.belwue.de/download/fexsend';
1304 chmod 0755,$new;
1305 system qw'perl -c',$new;
1306 if ($? == 0) {
1307 rename $new,$prg;
1308 exec $prg;
1309 } else {
1310 print "\n";
1311 print "cannot install new fexsend\n";
1312 }
1313 next;
1314 }
1315 if ($key eq 'l') {
1316 print "$key\n";
1317 system 'clear';
1318 &set_ID;
1319 next;
1320 }
1321 if ($key eq 's' or $key eq "\n") {
1322 print "s\n";
1323 &ask_file;
1324 next;
1325 }
1326 }
1327 exit;
1328 }
1329
1330
1331 # for MacOS
1332 sub ask_file {
1333 my ($file,$comment,$recipient,$archive,$size,$cmd,$key);
1334 my @files;
1335 my $qfiles;
1336 local $_;
1337
1338 system 'clear';
1339
1340 &set_ID unless -s $idf;
1341
1342 print "\n";
1343 print "Enter [ENTER] after each input line.\n";
1344 print "\n";
1345
1346 for (;;) {
1347 print "Recipient(s): ";
1348 $recipient = <STDIN>;
1349 chomp $recipient;
1350 $recipient =~ s/^\s+//;
1351 $recipient =~ s/\s+$//;
1352 $recipient =~ s/[\s;,]+/,/g;
1353 &menu unless $recipient;
1354 last if $recipient =~ /\w/ or $recipient eq '.';
1355 }
1356
1357 for (;;) {
1358 print "\n";
1359 print "Drag a file into this window or hit [ENTER] ";
1360 print $archive ? "to continue.\n" : "for menu options.\n";
1361 print "File to send: ";
1362 $file = <STDIN>||'';
1363 chomp $file;
1364 $file =~ s/^\s+//;
1365 $file =~ s/ $// if $file !~ /\\ $/;
1366 &menu unless $file or $archive;
1367 if ($file eq '*') {
1368 print "Archive name: ";
1369 $archive = <STDIN>||'';
1370 chomp $archive;
1371 next unless $archive;
1372 $archive =~ s/^\s+//g;
1373 $archive =~ s/\s+$//g;
1374 next;
1375 }
1376 if ($file) {
1377 unless (-e $file) {
1378 $file =~ s/\\\\/\000/g;
1379 $file =~ s/\\//g;
1380 $file =~ s/\000/\\/g;
1381 }
1382 unless (-r $file) {
1383 print "\"$file\" is not readable\n";
1384 next;
1385 }
1386 my $qf = shellquote($file);
1387 if (`du -ms $qf` =~ /^(\d+)/) {
1388 $size += $1;
1389 printf "%d MB\n",$1;
1390 }
1391 if ($archive) {
1392 push @files,$file;
1393 next;
1394 }
1395 }
1396 if ($archive) {
1397 $archive =~ s/[^\w=.+-]/_/g;
1398 $archive =~ s/^\./_./g;
1399 next unless @files;
1400 $qfiles = join(' ',map(shellquote($_),@files));
1401 if ($size < 2048) {
1402 $archive .= '.zip';
1403 } else {
1404 $archive .= '.tar';
1405 }
1406 }
1407 print "\n";
1408 print "Comment: ";
1409 $comment = <STDIN>||'';
1410 chomp $comment;
1411 print "\n";
1412 if ($comment =~ s/^:\s*-/-/) {
1413 $cmd = shellquote($_0)." $comment ";
1414 if ($archive) {
1415 $cmd .= '-A '.shellquote($archive).' '.$qfiles;
1416 } else {
1417 $cmd .= shellquote($file);
1418 }
1419 $cmd .= ' '.shellquote($recipient);
1420 print $cmd,"\n";
1421 system $cmd;
1422 } else {
1423 print shellquote($prg)." -C '$comment' ";
1424 if ($archive) {
1425 printf "-A %s %s %s\n",shellquote($archive),$qfiles,$recipient;
1426 system $prg,'-C',$comment,'-A',$archive,@files,$recipient;
1427 } else {
1428 printf "%s %s\n",shellquote($file),$recipient;
1429 system $prg,'-C',$comment,$file,$recipient;
1430 }
1431 }
1432 print "\n";
1433 print "[s] send another file to $recipient\n";
1434 print "[n] send another file to another recipient\n";
1435 print "[q] quit\n";
1436 print "\n";
1437 print "your choice: ";
1438 for (;;) {
1439 $key = ReadKey(0);
1440 &ask_file if $key eq 'n';
1441 if ($key eq 's' or $key eq "\n") {
1442 print "s\n";
1443 last;
1444 }
1445 if ($key eq 'q') {
1446 print "$key\n";
1447 exit;
1448 }
1449 }
1450 $file = $comment = $archive = '';
1451 @files = ();
1452 }
1453 }
1454
1455
1456 sub set_ID {
1457 my ($server,$port,$user,$logo);
1458 local $_;
1459
1460 print "\n";
1461 for (;;) {
1462 print "F*EX server URL: ";
1463 $server = <STDIN>;
1464 $server =~ s/[\s\n]//g;
1465 if ($server =~ s:/fup/(\w+)$::) {
1466 $_ = decode_b64($1);
1467 if (/(from|user)=(.+)&id=(.+)/) {
1468 $user = $2;
1469 $id = $3;
1470 }
1471 }
1472 $server =~ s:/fup.*::;
1473 $server =~ s:/+$::;
1474 next if $server !~ /\w/;
1475 if ($server =~ s/^https:..// or $server =~ /:443/) {
1476 $server =~ s/:.*//;
1477 $port = 443;
1478 eval "use IO::Socket::SSL";
1479 if ($@) {
1480 print "\nno perl SSL modules installed - cannot use https\n\n";
1481 next;
1482 }
1483 $SH = IO::Socket::SSL->new(
1484 PeerAddr => $server,
1485 PeerPort => $port,
1486 Proto => 'tcp',
1487 %SSL
1488 );
1489 } else {
1490 $server =~ s:^http.//::;
1491 if ($server =~ s/:(\d+)//) {
1492 $port = $1;
1493 } else {
1494 $port = 80;
1495 }
1496 $SH = IO::Socket::INET->new(
1497 PeerAddr => $server,
1498 PeerPort => $port,
1499 Proto => 'tcp',
1500 );
1501 }
1502 unless ($SH) {
1503 print "\ncannot connect to $server:$port - $!\n\n";
1504 next;
1505 }
1506 sendheader(
1507 "$server:$port",
1508 "GET /logo.jpg HTTP/1.0",
1509 "Connection: close",
1510 );
1511 $_ = <$SH>||'';
1512 unless (/HTTP.1.1 200/) {
1513 print "\nbad server reply: $_\n";
1514 next;
1515 }
1516 while (<$SH>) { last if /^\s*$/ }
1517 local $/;
1518 $logo = <$SH>||'';
1519 close $SH;
1520 if (length $logo < 9999) {
1521 print "\n$server is not a F*EX server!\n\n";
1522 next;
1523 }
1524 open $logo,">$fextmp/fex.jpg";
1525 print {$logo} $logo;
1526 close $logo;
1527 last;
1528 }
1529
1530 for (;;) {
1531 last if $user;
1532 print "Your login (email address): ";
1533 $user = <STDIN>;
1534 $user =~ s/[\s\n]//g;
1535 if ($user !~ /.@[\w.-]+$/) {
1536 print "\"$user\" is not a valid email address!\n";
1537 next;
1538 }
1539 }
1540
1541 for (;;) {
1542 last if $id;
1543 print "Your auth-ID for this account: ";
1544 $id = <STDIN>;
1545 $id =~ s/[\s\n]//g;
1546 }
1547
1548 open $idf,'>',$idf or die "$prg: cannot write to $idf - $!\n";
1549 print {$idf} "$server\n",
1550 "$user\n",
1551 "$id\n";
1552 close $idf;
1553 print "\n";
1554 print "Login data written to $idf\n\n";
1555 print "fexing test file to $user:\n\n";
1556 system "$prg -o -M -C test $fextmp/fex.jpg $user";
1557 print "\n";
1558 if ($? != 0) {
1559 print "fexsend failed, login data is invalid, try again\n";
1560 &set_ID;
1561 } else {
1562 print "fexsend test succeeded!\n";
1563 sleep 3;
1564 }
1565 }
1566
1567
1568
1569 sub nettest {
1570 my $url = shift;
1571 my $up = shift;
1572 my $down = shift;
1573 my $bs = 2**16;
1574 my ($length,$t0,$t1,$t2,$tt,$tb,$tc,$B,$kBs,$bt);
1575
1576 my $nettest = $sid = 'nettest';
1577
1578 $port ||= 80;
1579 if ($url =~ s:^https.//::) {
1580 $https = $port = 443;
1581 } else {
1582 $url =~ s:^http.//::;
1583 $port = $1 if $url =~ s/:(\d+)//;
1584 }
1585 $url =~ s/[\/:].*//;
1586 $server = $url;
1587
1588 if ($up) {
1589 serverconnect($server,$port);
1590 checkrecipient($nettest,$nettest);
1591 warn "$prg: send to $server:$port\n";
1592 formdatapost(
1593 from => $nettest,
1594 to => $nettest,
1595 id => $nettest,
1596 file => $nettest,
1597 size => $up*MB,
1598 comment => 'NOSTORE',
1599 );
1600 }
1601
1602 if ($down) {
1603 serverconnect($server,$port);
1604 warn "$prg: receive from $server:$port\n";
1605 sendheader("$server:$port","GET $proxy_prefix/ddd/$down HTTP/1.0");
1606 $_ = <$SH>;
1607 die "$prg: no response from fex server $server\n" unless $_;
1608 s/\r//;
1609
1610 if (/^HTTP\/[\d.]+ 2/) {
1611 warn "<-- $_" if $opt_v;
1612 while (<$SH>) {
1613 s/\r//;
1614 warn "<-- $_" if $opt_v;
1615 last if /^$/;
1616 $length = $1 if /^Content-Length:\s*(\d+)/i;
1617 }
1618 } else {
1619 s/HTTP\/[\d.]+ \d+ //;
1620 die "$prg: bad server reply: $_";
1621 }
1622
1623 unless ($length) {
1624 die "$prg: no Content-Length header in server reply\n";
1625 }
1626
1627
1628 if (${'opt_+'}) {
1629 print $rrcamel[0];
1630 $tc = 0;
1631 }
1632
1633 $t0 = $t1 = $t2 = int(time);
1634 $B = 0;
1635 while ($B < $length) {
1636 $b = read $SH,$_,$bs or die "$prg: cannot read after $B bytes - $!\n";
1637 # defined($_ = <$SH>) or die "$prg: cannot read after $B bytes - $!\n";
1638 # $b = length;
1639 $B += $b;
1640 $bt += $b;
1641 $t2 = time;
1642 if (${'opt_+'} and int($t2*10)>$tc) {
1643 print $rrcamel[$tc%2+1];
1644 $tc = int($t2*10);
1645 }
1646 if (int($t2) > $t1) {
1647 $kBs = int($bt/kB/($t2-$t1));
1648 $t1 = $t2;
1649 $bt = 0;
1650 printf STDERR "nettest: %d MB (%d%%) %d kB/s \r",
1651 int($B/MB),int(100*$B/$length),$kBs;
1652 }
1653 }
1654 close $SH;
1655
1656 $tt = $t2-$t0;
1657 $kBs = int($B/kB/($tt||1));
1658 if (${'opt_+'}) {
1659 print $rrcamel[1];
1660 print $rrcamel[2];
1661 }
1662 printf STDERR "nettest: %d MB in %d s = %d kB/s \n",
1663 int($B/MB),$tt,$kBs;
1664 }
1665 }
1666
1667
1668 # read one key from terminal in raw mode
1669 sub ReadKey {
1670 my $key;
1671 local $SIG{INT} = sub { stty('reset'); exit };
1672
1673 stty('raw');
1674 # loop necessary for ESXi support
1675 $key = getc(STDIN) while not defined $key;
1676 stty('reset');
1677 return $key;
1678 }
1679
1680
1681 sub stty {
1682 if (shift eq 'raw') {
1683 system qw'stty -echo -icanon eol',"\001";
1684 } else {
1685 system qw'stty echo icanon eol',"\000";
1686 }
1687 }
1688
1689
1690 sub send_xx {
1691 my $transferfile = shift;
1692 my $file = '';
1693 my (@r,@tar,$dir);
1694 local $_;
1695
1696 $SIG{PIPE} = $SIG{INT} = sub {
1697 unlink $transferfile;
1698 exit 3;
1699 };
1700
1701 @tar = qw'tar -cvz';
1702 push @tar,qw'--exclude=.snapshot* --exclude=.fex/tmp';
1703
1704 if (-t) {
1705 if ("@ARGV" eq '-') {
1706 # store STDIN to transfer file
1707 shelldo("gzip >> $transferfile");
1708 } elsif (@ARGV) {
1709 # local $opt_v = 1; # show tar command
1710 # print "making tar transfer file $transferfile :\n";
1711 # single file? then add this directly
1712 if (scalar @ARGV == 1) {
1713 # strip path if not ending with /
1714 if ($ARGV[0] =~ m:(.+)/(.+): and $2 !~ m:/$:) {
1715 ($dir,$file) = ($1,$2);
1716 chdir $dir or die "$prg: $dir - $!\n";
1717 } else {
1718 $file = $ARGV[0];
1719 }
1720 if (-l $file) {
1721 shelldo(@tar,qw'--dereference -f',$transferfile,$file);
1722 } else {
1723 shelldo(@tar,'-f',$transferfile,$file);
1724 }
1725 } else {
1726 # remove common path
1727 my $dir = dirname($ARGV[0]);
1728 foreach (@ARGV) {
1729 unless (/^\Q$dir\E\/./) {
1730 undef $dir;
1731 last;
1732 }
1733 }
1734 if (defined($dir) and chdir $dir) {
1735 @ARGV = grep { s:^\Q$dir\E\/::; } @ARGV;
1736 }
1737 shelldo(@tar,'-f',$transferfile,@ARGV);
1738 }
1739 if ($?) {
1740 unlink $transferfile;
1741 if ($? == 2) {
1742 die "$prg: interrupted making tar transfer file\n";
1743 } else {
1744 die "$prg: error while making tar transfer file\n";
1745 }
1746 }
1747 }
1748 } else {
1749 # write input from pipe to transfer file
1750 shelldo("gzip >> $transferfile");
1751 }
1752
1753 die "$prg: no transfer file\n" unless -s $transferfile;
1754
1755 serverconnect($server,$port);
1756 query_sid($server,$port);
1757
1758 @r = formdatapost(
1759 from => $from,
1760 to => $from,
1761 id => $sid,
1762 file => $transferfile,
1763 comment => 'NOMAIL',
1764 autodelete => $transferfile =~ /STDFEX/ ? 'NO' : 'DELAY',
1765 );
1766
1767 # open P,'|w3m -T text/html -dump' or die "$prg: w3m - $!\n";
1768 # print P @r;
1769 http_response(@r);
1770 if ($transferfile =~ /:/ and $prg ne 'xxx') {
1771 if ("@r" =~ /\s(X-)?Location: (http.*?)\s/) {
1772 print "wget -qO - $2 | tar xvzf -\n";
1773 }
1774 }
1775
1776 unlink $transferfile;
1777 }
1778
1779
1780 sub query_quotas {
1781 my (@r,$r);
1782 local $_;
1783
1784 female_mode("query quotas?") if $opt_F;
1785
1786 @r = formdatapost(
1787 from => $from,
1788 to => $from,
1789 command => $command,
1790 );
1791 die "$prg: no response from fex server $server\n" unless @r;
1792 $_ = shift @r;
1793 unless (/^HTTP.* 2/) {
1794 s:HTTP/[\d\. ]+::;
1795 die "$prg: server response: $_\n";
1796 }
1797 if (($_) = grep(/^X-Sender-Quota/,@r) and /(\d+)\s+(\d+)/) {
1798 print "sender quota (used): $1 ($2) MB\n";
1799 } else {
1800 print "sender quota: unlimited\n";
1801 }
1802 if (($_) = grep(/^X-Recipient-Quota/,@r) and /(\d+)\s+(\d+)/) {
1803 print "recipient quota (used): $1 ($2) MB\n";
1804 } else {
1805 print "recipient quota: unlimited\n";
1806 }
1807 }
1808
1809
1810 sub command {
1811 my (@r,$r);
1812 my ($psp,$owner,$share,$archive,$avt,$dkey);
1813 my $command = shift;
1814 local $_;
1815
1816 if ($port == 80) { $psp = "http://$server" }
1817 elsif ($port == 443) { $psp = "https://$server" }
1818 else { $psp = "http://$server:$port" }
1819
1820 if ($command eq 'FUPWATCH') {
1821 warn "$prg: querying $psp/$from\n";
1822 @r = formdatapost(
1823 from => $from,
1824 to => $from,
1825 command => $command,
1826 );
1827 die "$prg: no response from fex server $server\n" unless @r;
1828 die "$prg: server does not support FUPWATCH\n" if $features !~ /FUPWATCH/;
1829 $_ = shift @r;
1830 unless (/^HTTP.* 2/) {
1831 s:HTTP/[\d\. ]+::;
1832 die "$prg: server response: $_\n";
1833 }
1834 while (<$SH>) {
1835 warn "<-- $_" if $opt_v;
1836 if (/^(\S+) (\S+) (\S+) (\w+) (\d+) (".*")$/) {
1837 printf "%s %d %s/fop/%s/%s %s\n",$1,$5,$psp,$4,$3,$6;
1838 }
1839 }
1840 die "$prg: server has closed the connection\n";
1841 }
1842
1843 if ($command eq 'GENUKEY') {
1844 @r = formdatapost(
1845 from => $from,
1846 to => $from,
1847 command => $command,
1848 );
1849 die "$prg: no response from fex server $server\n" unless @r;
1850 $_ = shift @r;
1851 unless (/^HTTP.* 2/) {
1852 s:HTTP/[\d\. ]+::;
1853 die "$prg: server response: $_\n";
1854 }
1855 if (grep /^Content-Type: text\/html/,@r) {
1856 die "$prg: no $command support on $server:$port\n";
1857 }
1858 my $ukey = '';
1859 while (shift @r) {
1860 $ukey = $1 if /^X-UKEY: (\w+)/;
1861 }
1862 $ukey ||= shift @r;
1863 if ($ukey) {
1864 if ($ukey =~ /_$/) {
1865 # xxx / xup
1866 print "FUP=$psp/fup?ukey=$ukey\n";
1867 exit;
1868 }
1869 if ($ENV{FUA} =~ /fexpack/) {
1870 print "# upload URL and bash function for copy&paste\n";
1871 print "# upload key is valid for one day!\n";
1872 print "FUP=$psp/fup?ukey=$ukey\n";
1873 print
1874 'fup(){ ',
1875 'local a t;',
1876 'a=$(basename "$1");',
1877 '[ -r "$1" ]||{ echo "usage: fup FILE...";return; };',
1878 '[ -z "$2" ]||{ echo -n "archive name: ";read a; };',
1879 't="${TMPDIR:-/tmp}/fup_$a.tar";',
1880 'echo $t:;',
1881 'tar cvf "$t" "$@"&&curl',
1882 ' -F "keep=1"',
1883 ' -F "comment=[$(logname)@$(hostname)]"',
1884 ' -F "file=@$t"',
1885 ' $FUP|cat;',
1886 'rm "$t";',
1887 } else {
1888 print "# upload URL and bash function for others to send you files\n";
1889 print "# upload key is valid for one day!\n";
1890 print "FUP=$psp/fup?ukey=$ukey#$from\n";
1891 print 'fup(){ ';
1892 if ("@ARGV" eq '+' or ${'opt_+'}) {
1893 print
1894 'local a t c;',
1895 'a=$(basename "$1");',
1896 '[ -r "$1" ]||{ echo "usage: fup FILE...";return; };',
1897 '[ -z "$2" ]||{ echo -n "archive name: ";read a; };',
1898 'echo -n "comment: ";read -e c;',
1899 't="${TMPDIR:-/tmp}/fup_$a.tar";',
1900 'echo $t:;',
1901 'tar cvf "$t" "$@"&&curl',
1902 ' -F "comment=[$(logname)@$(hostname)] $c"',
1903 ' -F "file=@$t"',
1904 ' $FUP|cat;',
1905 'rm "$t";',
1906 } else {
1907 print
1908 '[ -f "$1" ]&&{ printf "comment: ";read -e c;',
1909 'curl -F "file=@$1" "$FUP?comment=$c"|cat;}||',
1910 '{ echo usage: fup FILE;};',
1911 }
1912 }
1913 print "};fup\n";
1914 } else {
1915 die "$prg: no UKEY reply from $server\n";
1916 }
1917 exit;
1918 }
1919
1920 if ($command =~ /^GENOKEY:\S/) {
1921 @r = formdatapost(
1922 from => $from,
1923 to => $from,
1924 command => $command,
1925 );
1926 die "$prg: no response from fex server $server\n" unless @r;
1927 $_ = shift @r;
1928 unless (/^HTTP.* 2/) {
1929 s:HTTP/[\d\. ]+::;
1930 die "$prg: server response: $_\n";
1931 }
1932 unless (grep /^Content-Type: text\/plain/,@r) {
1933 die "$prg: no $command support on $server:$port\n[@r]";
1934 }
1935 foreach (@r) {
1936 if (/okey=/) {
1937 print "$_\n";
1938 exit;
1939 }
1940 }
1941 die "$prg: no okey in server reply\n";
1942 }
1943
1944 if ($command eq 'LISTSHARE') {
1945 if ("@ARGV" =~ /^(\S+):share=(\S+)$/) {
1946 $owner = $1;
1947 $share = $2;
1948 } else {
1949 die "usage: $prg -~ LISTSHARE OWNER:share=SHARE\n"
1950 }
1951 @r = formdatapost(
1952 from => $from,
1953 to => "@ARGV",
1954 command => $command,
1955 );
1956 die "$prg: no response from fex server $server\n" unless @r;
1957 $_ = shift @r;
1958 unless (/^HTTP.* 2/) {
1959 s:HTTP/[\d\. ]+::;
1960 die "$prg: server response: $_\n";
1961 }
1962 if (grep /^Content-Type: text\/html/,@r) {
1963 die "$prg: no archive sharing support on $server:$port\n";
1964 }
1965 # skip HTTP header
1966 while (shift @r) { }
1967 my $sx = '';
1968 if ($pkey) {
1969 $sx = md5_hex("$1:$pkey") if $id =~ /^MD5H:(\w+)/;
1970 } else {
1971 $sx = md5_hex("$1:$id") if $sid =~ /^MD5H:(\w+)/;
1972 }
1973 foreach my $line (@r) {
1974 if ($line =~ /^(\S+):owner:-$/) {
1975 $owner = $1;
1976 next;
1977 } elsif ($line =~ /^(\S+\@\S+):([a-z]+):(\w+)$/) {
1978 my $suser = $1;
1979 my $access = $2;
1980 my $pkey = $3;
1981 $pkey = pack('H*',$pkey) ^ $sx if $sx;
1982 $line = "$psp/fas/$owner/$share/$suser/$pkey (access=$access)";
1983 } elsif ($line =~ s/^((\S+) (\S+)(.*\" ))(\w+)$/$1/) {
1984 $archive = $2;
1985 $avt = $3;
1986 $dkey = $5;
1987 $dkey = pack('H*',$dkey) ^ substr($sx,0,8) if $sx;
1988 $line .= sprintf "%s/fop/%s/%s_%s",$psp,$dkey,$archive,$avt;
1989 }
1990 print $line,"\n";
1991 }
1992 exit;
1993 }
1994
1995 if ($command eq 'LISTSHARES') {
1996 @r = formdatapost(
1997 from => $from,
1998 command => $command,
1999 );
2000 die "$prg: no response from fex server $server\n" unless @r;
2001 $_ = shift @r;
2002 unless (/^HTTP.* 2/) {
2003 s:HTTP/[\d\. ]+::;
2004 die "$prg: $server response: $_\n";
2005 }
2006 if (grep /^Content-Type: text\/html/,@r) {
2007 die "$prg: no archive sharing support on $server:$port\n";
2008 }
2009 # skip HTTP header
2010 while (shift @r) { }
2011 foreach (@r) { print "$_\n" }
2012 exit;
2013 }
2014
2015 if ($command =~ /^SHAREUSER:(\S+\@[\w.-]+):[a-z]+$/) {
2016 my $suser = $1;
2017 if ("@ARGV" =~ /^(\S+):share=(\S+)$/) {
2018 $owner = $1;
2019 $share = $2;
2020 } else {
2021 die "usage: $prg -~ SHAREUSER:USER:ACCESS OWNER:share=SHARE\n"
2022 }
2023 @r = formdatapost(
2024 from => $from,
2025 to => "@ARGV",
2026 command => $command,
2027 );
2028 die "$prg: no response from fex server $server\n" unless @r;
2029 $_ = shift @r;
2030 unless (/^HTTP.* 2/) {
2031 s:HTTP/[\d\. ]+::;
2032 die "$prg: $server response: $_\n";
2033 }
2034 if (grep /^Content-Type: text\/html/,@r) {
2035 die "$prg: no archive sharing support on $server:$port\n";
2036 }
2037 my $sx = '';
2038 if ($pkey) {
2039 $sx = md5_hex("$1:$pkey") if $id =~ /^MD5H:(\w+)/;
2040 } else {
2041 $sx = md5_hex("$1:$id") if $sid =~ /^MD5H:(\w+)/;
2042 }
2043 # skip HTTP header
2044 while (shift @r) { }
2045 $_ = shift @r;
2046 if (s/^pkey=//) {
2047 if (/MD5H:(\w+)/) {
2048 $pkey = pack('H*',$1) ^ $sx;
2049 } else {
2050 $pkey = $_;
2051 }
2052 print "$psp/fas/$owner/$share/$suser/$pkey\n";
2053 } else {
2054 print "$_\n";
2055 }
2056 exit;
2057 }
2058
2059 if ($command =~ /^COPYARCHIVE:\S+:\S+:\S+:\S+:\S+/) {
2060 if ("@ARGV" =~ /^(\S+)$/) {
2061 $from = $1;
2062 } else {
2063 die "usage: $prg -~ COPYARCHIVE:SHARE:ARCHIVE:VERSION.CONTAINER:NEWSHARE:NEWARCHIVE OWNER\n";
2064 }
2065 @r = formdatapost(
2066 from => $from,
2067 to => $from,
2068 command => $command,
2069 );
2070 die "$prg: no response from fex server $server\n" unless @r;
2071 $_ = shift @r;
2072 unless (/^HTTP.* 2/) {
2073 s:HTTP/[\d\. ]+::;
2074 die "$prg: $server response: $_\n";
2075 }
2076 if (grep /^Content-Type: text\/html/,@r) {
2077 die "$prg: no archive sharing support on $server:$port\n";
2078 }
2079 while (@r) {
2080 $_ = shift @r;
2081 last if /^$/;
2082 }
2083 print "$_\n" foreach @r;
2084 exit;
2085 }
2086
2087 if ($command =~ /^DOX(SYNC|DEL|LIST):./ or $command eq 'DOXLIST') {
2088 # die "$prg: fexserver does not support DOX\n" if $features !~ /DOX/;
2089 @r = formdatapost(
2090 from => $from,
2091 command => $command,
2092 );
2093 die "$prg: no response from fex server $server\n" unless @r;
2094 $_ = shift @r;
2095 unless (/^HTTP.* 2/) {
2096 s:HTTP/[\d\. ]+::;
2097 die "$prg: $server response: $_\n";
2098 }
2099 unless (/^HTTP.* 222/) {
2100 die "$prg: no DOX support on $server\n";
2101 }
2102 while (<$SH>) {
2103 warn "<-- $_" if $opt_v;
2104 if ($command =~ /DOXSYNC/) {
2105 s/^\s+//;
2106 } elsif ($command eq 'DOXLIST') {
2107 if (/^(\d+) (\d+) (.+)/) {
2108 my @d = localtime($1);
2109 $_ = sprintf("%d-%02d-%02d %02d:%02d:%02d %7d MB %s\n",
2110 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0],$2,$3);
2111 }
2112 }
2113 print;
2114 }
2115 exit;
2116 }
2117
2118 die "$prg: unknown command $command\n";
2119 }
2120
2121
2122 sub query_settings {
2123 my (@r,$r);
2124 local $_;
2125
2126 female_mode("query settings?") if $opt_F;
2127
2128 if ($FEXID) {
2129 print "ID data from \$FEXID\n";
2130 } elsif (-f $idf) {
2131 print "ID data from $idf\n";
2132 } else {
2133 die "$prg: found no ID\n";
2134 }
2135 print "server: $fexcgi\n";
2136 print "user: $from\n";
2137 print "auth-ID: $id\n";
2138 print "login URL: ";
2139 &show_URL;
2140
2141 @r = formdatapost(
2142 from => $from,
2143 to => $from,
2144 id => $sid,
2145 command => $command,
2146 );
2147 die "$prg: no response from fex server $server\n" unless @r;
2148 $_ = shift @r;
2149 unless (/^HTTP.* 2/) {
2150 s:HTTP/[\d\. ]+::;
2151 die "$prg: server response: $_\n";
2152 }
2153 if (($_) = grep(/^X-Autodelete/,@r) and /:\s+(\w+)/) {
2154 print "autodelete: $1\n";
2155 }
2156 if (($_) = grep(/^X-Default-Keep/,@r) and /(\d+)/) {
2157 print "default keep: $1 days\n";
2158 }
2159 if (($_) = grep(/^X-Default-Keep-Archives/,@r) and /(\d+)/) {
2160 print "default archive keep: $1 days\n";
2161 }
2162 if (($_) = grep(/^X-Default-Locale/,@r) and /:\s+(\w+)/) {
2163 print "default locale: $1\n";
2164 }
2165 if (($_) = grep(/^X-MIME/,@r) and /:\s+(\w+)/) {
2166 print "display file with browser: $1\n";
2167 }
2168 if (($_) = grep(/^X-Sender-Quota/,@r) and /(\d+)\s+(\d+)/) {
2169 print "sender quota (used): $1 ($2) MB\n";
2170 } else {
2171 print "sender quota: unlimited\n";
2172 }
2173 if (($_) = grep(/^X-Recipient-Quota/,@r) and /(\d+)\s+(\d+)/) {
2174 print "recipient quota (used): $1 ($2) MB\n";
2175 } else {
2176 print "recipient quota: unlimited\n";
2177 }
2178 }
2179
2180
2181 # list spool
2182 sub list {
2183 my (@r,$r);
2184 my ($data,$dkey,$to);
2185 my $n = 0;
2186 my $s = 1;
2187 my $a = shift @ARGV || '@';
2188 local $_;
2189
2190 female_mode("list spooled files?") if $opt_F;
2191
2192 if ($opt_l) {
2193 if ($a =~ /^\d+$/) {
2194 open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
2195 while (<$fexlist>) {
2196 if (/#(\d+) (\w+) (.+)/ and $1 eq $a) {
2197 serverconnect($server,$port) unless $SH;
2198 sendheader(
2199 "$server:$port",
2200 "GET $proxy_prefix/fop/$2/$2?LIST HTTP/1.1",
2201 );
2202 $_ = <$SH>||'';
2203 s/\r//;
2204 warn "<-- $_" if $opt_v;
2205 if (/^HTTP.* 200/) {
2206 warn "<-- $_" if $opt_v;
2207 while (<$SH>) {
2208 s/\r//;
2209 if (/^\n/) {
2210 print;
2211 print while <$SH>;
2212 }
2213 }
2214 } elsif (s:HTTP/[\d\. ]+::) {
2215 die "$prg: server response: $_";
2216 } else {
2217 die "$prg: no response from fex server $server\n";
2218 }
2219 exit;
2220 }
2221 }
2222 die "$prg: file \#$a not found in fexlist\n";
2223 }
2224 }
2225
2226 @r = formdatapost(
2227 from => $from,
2228 to => $opt_l ? '*' : $from,
2229 command => $command,
2230 );
2231 die "$prg: no response from fex server $server\n" unless @r;
2232 $_ = shift @r;
2233 unless (/^HTTP.* 200/) {
2234 s:HTTP/[\d\. ]+::;
2235 die "$prg: server response: $_\n";
2236 }
2237
2238 # list sent files
2239 if ($opt_l) {
2240 open $fexlist,'>',$fexlist or die "$prg: cannot write $fexlist - $!\n";
2241 foreach (@r) {
2242 last if m:</pre>:;
2243 next unless /<pre>/ or $data;
2244 $data = 1;
2245 if (/<a href=".*dkey=(\w+).*?">/) { $dkey = $1 }
2246 else { $dkey = '' }
2247 # $_ = decode_utf8($_);
2248 # $_ = locale($_);
2249 s/<.*?>//g;
2250 s/&quot;/\"/g;
2251 s/&lt;/</g;
2252 s/&amp;/&/g;
2253 if (/^(to (.+) :)/) {
2254 $to = $2;
2255 if ($a eq '.') {
2256 $s = $to eq $from ? 1 : 0;
2257 } elsif ($a eq '-') {
2258 $s = $to eq $from ? 0 : 1;
2259 } else {
2260 $s = $to =~ /$a/;
2261 }
2262 if ($opt_u) {
2263 s/to/for/;
2264 print "\n$_\n" if $s;
2265 } else {
2266 print "\n$_\n" if $s;
2267 print {$fexlist} "\n$_\n";
2268 }
2269 } elsif (s/.* (\d+) MB //) {
2270 my $size = $1;
2271 $n++;
2272 if ($s) {
2273 if ($opt_u and /( [+-] )(\S+)/) {
2274 my $file = $2;
2275 $file =~ s/[^\w.,@~^=+-]/_/g;
2276 printf " %s/fop/%s/%s\n",$fexcgi,$dkey,$file;
2277 } else {
2278 printf "%4s %8d MB %s\n","#$n",$size,$_;
2279 }
2280 }
2281 printf {$fexlist} "%4s %s %s\n","#$n",$dkey,$_ unless $opt_u;
2282 }
2283 }
2284 close $fexlist;
2285 }
2286
2287 # list received files
2288 if ($opt_L) {
2289 my $show = 1;
2290 my $user;
2291 foreach (@r) {
2292 s/&quot;/\"/g;
2293 s/&apos;/\'/g;
2294 s/&lt;/</g;
2295 s/&amp;/&/g;
2296 # locale must be english!
2297 if ($a eq '.' and /Files for (\S+)/) {
2298 # only files from = to = user
2299 $user = $1;
2300 }
2301 next unless /<pre>/ or $data;
2302 $data = 1;
2303 last if m:</pre>:;
2304 if (/(from (\S+) :)/) {
2305 if ($user) {
2306 $show = $2 eq $user ? 1 : 0;
2307 }
2308 print "\n$1\n" if $show;
2309 }
2310 if ($show and
2311 m{(\d+) (MB.*)<a href="(https?://.*/fop/\w+/.+)">(.+)</a>( ".*")?}) {
2312 printf "%8d %s%s%s\n",$1,$2,$3,($5||'');
2313 }
2314 }
2315 }
2316 }
2317
2318
2319 sub show_URL {
2320 printf "%s/fup/%s\n",$fexcgi,encode_b64("from=$from&id=$id");
2321 }
2322
2323
2324 sub get_log {
2325 my (@r);
2326 local $_;
2327
2328 @r = formdatapost(
2329 from => $from,
2330 to => $from,
2331 id => $sid,
2332 command => $command,
2333 );
2334 die "$prg: no response from fex server $server\n" unless @r;
2335 $_ = shift @r;
2336 unless (/^HTTP.* 200/) {
2337 s:HTTP/[\d\. ]+::;
2338 die "$prg: server response: $_\n";
2339 }
2340 while (shift @r) {}
2341 foreach (@r) { print "$_\n" }
2342 exit;
2343 }
2344
2345
2346 sub show_address_book {
2347 my (%AB,@r);
2348 my $alias;
2349 local $_;
2350
2351 %AB = query_address_book($server,$port,$from);
2352 foreach $alias (sort keys %AB) {
2353 next if $alias eq 'ADDRESS_BOOK';
2354 $_ = sprintf "%s = %s (%s) # %s\n",
2355 $alias,
2356 $AB{$alias},
2357 $AB{$alias}->{options},
2358 $AB{$alias}->{comment};
2359 s/ \(\)//;
2360 s/ \# $//;
2361 print;
2362 }
2363 }
2364
2365
2366 sub purge {
2367 die "$prg: not yet implemented\n";
2368 }
2369
2370
2371 sub delete_file_number {
2372 my ($to);
2373
2374 while (@ARGV) {
2375 $opt_d = shift @ARGV;
2376 die "usage: $prg -d #\n" if $opt_d !~ /^\d+$/;
2377
2378 open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
2379 while (<$fexlist>) {
2380 if (/^to (.+\@.+) :/) {
2381 $to = $1;
2382 } elsif (/#(\d+) (\w+) .*? [+-] (\S+)/ and $1 eq $opt_d) {
2383 delete_file_dkey($2,$3);
2384 last;
2385 }
2386 }
2387 close $fexlist;
2388 sleep 1; # do not overrun server
2389 }
2390
2391 exit;
2392 }
2393
2394
2395 sub delete_file_url {
2396 if (scalar(@ARGV) != 1) {
2397 die "usage: $prg -d FEXURL\n";
2398 }
2399 if ("@ARGV" =~ m:^http.*/fop/(\w+)/(.+):) {
2400 delete_file_dkey($1,$2);
2401 } else {
2402 die "$prg: @ARGV is not a FEXURL\n";
2403 }
2404 exit;
2405 }
2406
2407
2408 sub delete_file_dkey {
2409 my ($dkey,$file) = @_;
2410 local $_;
2411
2412 serverconnect($server,$port) unless $SH;
2413 query_sid($server,$port);
2414 sendheader(
2415 "$server:$port",
2416 "GET $proxy_prefix/fop/$dkey/$file?DELETE&id=$sid HTTP/1.1",
2417 );
2418 $_ = <$SH>||'';
2419 s/\r//;
2420 warn "<-- $_" if $opt_v;
2421 if (/^HTTP.* 200/) {
2422 while (<$SH>) {
2423 s/\r//;
2424 last if /^\n/; # ignore HTML output
2425 warn "<-- $_" if $opt_v;
2426 if (/^X-File: (.+)\/(.+)\/(.+)/) {
2427 if ($1 eq $2) {
2428 printf "%s deleted\n",decode_utf8(urldecode($3));
2429 } else {
2430 printf "%s for %s deleted\n",decode_utf8(urldecode($3)),$1;
2431 }
2432 }
2433 }
2434 undef $SH;
2435 } elsif (s:HTTP/[\d\. ]+::) {
2436 die "$prg: server response: $_";
2437 } else {
2438 die "$prg: no response from fex server $server\n";
2439 }
2440 }
2441
2442
2443 sub delete_file {
2444 my ($from,$to,$file) = @_;
2445 local $_;
2446
2447 unless ($SH) {
2448 serverconnect($server,$port);
2449 query_sid($server,$port) unless $anonymous;
2450 }
2451
2452 $to = $from if $to eq '.';
2453
2454 $file = urlencode($file);
2455 sendheader(
2456 "$server:$port",
2457 "GET $proxy_prefix/fop/$to/$from/$file?id=$sid&DELETE HTTP/1.1",
2458 );
2459
2460 while (<$SH>) {
2461 s/\r//;
2462 warn "<-- $_" if $opt_v;
2463 last if /^\s*$/;
2464 }
2465 }
2466
2467
2468 sub urlencode {
2469 local $_ = shift;
2470 # $_ = encode_utf8($_) if utf8::is_utf8($_);
2471 # s/([^~^_:,;+*.!$#<>(){}\[\]\@\w\-])/'%'.uc(unpack("H2",$1))/ge;
2472 s/([^~^_:,;+*.!#\@\w\-])/'%'.uc(unpack("H2",$1))/ge;
2473 return $_;
2474 }
2475
2476
2477 sub send_fex {
2478 my @to;
2479 my $file = '';
2480 my @files = ();
2481 my ($data,$aname,$alias);
2482 my (@r,$r);
2483 my $t0 = time;
2484 my @transferfiles;
2485 my $vp = '\d{8}_\d{6}'; # fexpush version pattern
2486 local $_;
2487
2488 if ($from =~ /^SUBUSER|GROUPMEMBER$/) {
2489 $to = '_';
2490 } else {
2491 # look for single @ in arguments
2492 for (my $i=1; $i<$#ARGV; $i++) {
2493 if ($ARGV[$i] eq '@') {
2494 $ARGV[$i] = join(',',@ARGV[$i+1 .. $#ARGV]);
2495 $#ARGV = $i;
2496 last;
2497 }
2498 }
2499 $to = pop @ARGV or die $usage;
2500 if ($to eq '.' or $to eq '+') {
2501 $nomail ||= $to;
2502 $opt_M ||= $to;
2503 }
2504 if ($to eq ':') {
2505 $to = $from;
2506 $nomail = $opt_C ||= 'NOMAIL';
2507 }
2508 if ($opt_g and $to =~ /,/) {
2509 die "$prg: encryption is supported to only one recipient\n";
2510 }
2511 if ($to =~ m{^https?://.*/fup\?skey=(\w+)}) {
2512 $from = 'SUBUSER';
2513 $to = '_';
2514 $id = $1;
2515 }
2516 if ($to =~ m{^https?://.*/fup\?gkey=(\w+)}) {
2517 $from = 'GROUPMEMBER';
2518 $to = '_';
2519 $id = $1;
2520 }
2521 if ($to =~ m{^https?://.*/fup\?to=(\S+)\?okey=(\w+)}) {
2522 $from = 'ONETIMEUSER';
2523 $to = $1;
2524 $id = $2;
2525 }
2526 }
2527
2528 @to = split(',',$to);
2529
2530 die $usage unless @ARGV or $opt_a or $opt_s;
2531 die $usage if $opt_s and @ARGV;
2532
2533 if ($cygwin and scalar(@ARGV) == 1 and not $opt_a) {
2534 my $a = $ARGV[0];
2535 $opt_a = basename($a).'.zip' if -d $a;
2536 }
2537
2538 if (not $opt_a and not $opt_d and scalar(@ARGV) == 1) {
2539 $_ = $ARGV[0];
2540 if (not -e and /(.+)\.(tar|tgz|zip|7z)$/ and -e $1) {
2541 @ARGV = ($1);
2542 s:.*/::;
2543 s/[^\w_.+-]/_/g;
2544 $opt_a = $_;
2545 }
2546 }
2547
2548 if ($opt_a and scalar(@ARGV) == 1) {
2549 my $a = $ARGV[0];
2550 if (-d $a and $a !~ m:/$:) {
2551 my $dir = abs_path($a);
2552 @ARGV = (basename($dir));
2553 $dir = dirname($dir);
2554 chdir $dir or die "$prg: cannot cd $dir - $!\n";
2555 warn "\$ cd $dir\n" if $opt_v;
2556 }
2557 }
2558
2559 if ($opt_a and not $opt_A) {
2560 my @A = ();
2561 foreach my $a (@ARGV) {
2562 if ($a eq '.') {
2563 push @A,glob('*');
2564 } elsif ($a =~ m:^\.|/\.:) {
2565 die "$prg: cannot send $a with option -a, use -A instead\n";
2566 push @A,glob('*');
2567 } else {
2568 push @A,$a;
2569 }
2570 }
2571 @ARGV = @A;
2572 }
2573
2574 # early serverconnect necessary for X-Features info
2575 serverconnect($server,$port);
2576
2577 if ($anonymous) {
2578 my $aok;
2579 sendheader("$server:$port","GET /SID HTTP/1.1");
2580 $_ = <$SH>||'';
2581 s/\r//;
2582 die "$prg: no response from fex server $server\n" unless $_;
2583 warn "<-- $_" if $opt_v;
2584 if (/^HTTP.* [25]01/) {
2585 while (<$SH>) {
2586 s/\r//;
2587 warn "<-- $_" if $opt_v;
2588 last unless /\w/;
2589 $aok = $_ if /X-Features:.*ANONYMOUS/;
2590 }
2591 die "$prg: no anonymous support on server $server\n" unless $aok;
2592 } else {
2593 die "$prg: bad response from server $server : $_\n";
2594 }
2595 } elsif ($public) {
2596 } else {
2597
2598 query_sid($server,$port);
2599
2600 if ($from eq 'SUBUSER') {
2601 $skey = $sid;
2602 # die "skey=$skey\nid=$id\nsid=$sid\n";
2603 }
2604
2605 if ($from eq 'GROUPMEMBER') {
2606 $gkey = $sid;
2607 }
2608
2609 if ($to eq '.' or $to eq '+') {
2610 # @to = ($from);
2611 } elsif ($to =~ /(.+):share=(.+)/) {
2612 $frecipient = $1;
2613 $share = $2;
2614 } elsif ($to =~ m:^(//.*):) {
2615 my $xkey = $1;
2616 if ($features =~ /XKEY/) {
2617 @to = ($from);
2618 $opt_C = $xkey;
2619 } else {
2620 die "$prg: server does not support XKEY\n";
2621 }
2622 } elsif (grep /^[^@]*$/,@to and not ($skey or $gkey or $okey or $share)) {
2623 %AB = query_address_book($server,$port,$from);
2624 if ($proxy) {
2625 serverconnect($server,$port);
2626 query_sid($server,$port);
2627 }
2628 foreach $to (@to) {
2629 # alias in local config?
2630 if ($alias{$to}) {
2631 if ($alias{$to} =~ /(.+?):(.+)/) {
2632 my $ato = $1;
2633 my $opt = $2;
2634 my @argv = @_ARGV;
2635 pop @argv;
2636 # special extra upload
2637 system $prg,split(/\s/,$opt),@argv,$ato;
2638 $to = '';
2639 } else {
2640 $to = $alias{$to};
2641 }
2642 }
2643 # alias in server address book?
2644 elsif ($AB{$to}) {
2645 # do not substitute alias with expanded addresses because then
2646 # keep and autodelete options from address book will get lost
2647 # $to = $AB{$to};
2648 }
2649 # look for mutt aliases
2650 elsif ($to !~ /@/ and $to ne $from) {
2651 $to = get_mutt_alias($to);
2652 }
2653 }
2654 }
2655
2656 $to = join(',',grep /./,@to) or exit;
2657 $share_ = $to =~ /:share=_$/;
2658
2659 if (
2660 not ($skey or $gkey or $okey or $share)
2661 and $from ne $to
2662 and $to ne '.'
2663 and $to ne '+'
2664 and $features =~ /CHECKRECIPIENT/
2665 and $command !~ /^(DELETE|LIST|RECEIVEDLOG|SENDLOG|FOPLOG)$/
2666 ) {
2667 checkrecipient($from,$to);
2668 if ($proxy) {
2669 serverconnect($server,$port);
2670 query_sid($server,$port);
2671 }
2672 }
2673 }
2674
2675 if (@ARGV > 1 and not ($opt_a or $opt_s or $opt_d)) {
2676 $opt_a = inputline("Archive name (name.tar, name.tgz or name.zip) or ".
2677 "[ENTER] to send file for file:\n");
2678 $opt_a =~ s/^\s+//;
2679 $opt_a =~ s/\s+$//;
2680 $opt_a =~ s/\//_/g;
2681 }
2682
2683 if ($macos and not $opt_a and -d "@ARGV") {
2684 my $dir = "@ARGV";
2685 my $qdir = shellquote($dir);
2686 if (`du -s $qdir` =~ /^(\d+)/ and $1 < 2**21) {
2687 $opt_a = "$dir.zip";
2688 } else {
2689 $opt_a = "$dir.tar";
2690 }
2691 }
2692
2693 if ($opt_s) {
2694 $opt_s =~ s/^=//;
2695 $opt_s =~ s:.*/::;
2696 $opt_s =~ s/[^\w_.+-]/_/g;
2697 @files = ($opt_s);
2698 } elsif ($opt_a) {
2699 $opt_a =~ s/^=//;
2700 $opt_a =~ s:/+$::;
2701 if ($opt_a =~ /(.+)\.(zip|tar|tgz|taz|7z)$/) {
2702 $aname = $1;
2703 $atype = $2;
2704 # no file argument left?
2705 unless (@ARGV) {
2706 # use filename as archive name
2707 push @ARGV,$aname;
2708 }
2709 $aname =~ s:.*/::;
2710 $aname =~ s/[^\w_.+-]/_/g;
2711 } else {
2712 die "$prg: archive name must be one of ".
2713 "$opt_a.tar $opt_a.tgz $opt_a.taz $opt_a.zip\n";
2714 }
2715 if ("@ARGV" =~ /^\[(\S+)\]$/) {
2716 die "$prg: cannot read \"$1\"\n" unless -r $1;
2717 } else {
2718 foreach my $file (@ARGV) {
2719 die "$prg: cannot read \"$file\"\n" unless -l $file or -r $file;
2720 }
2721 }
2722 $opt_a =~ s:.*/::;
2723 $opt_a =~ s/[^\w_.+-]/_/g;
2724 $opt_a .= ".$atype" if $opt_a !~ /\.$atype$/;
2725
2726 if (0 and $useragent !~ /\// and scalar(@ARGV) > 1) {
2727 # remove common path
2728 my $dir = dirname($ARGV[0]);
2729 foreach (@ARGV) {
2730 unless (/^\Q$dir\E\/./) {
2731 undef $dir;
2732 last;
2733 }
2734 }
2735 if (defined($dir) and chdir $dir) {
2736 @ARGV = grep { s:^\Q$dir\E\/::; } @ARGV;
2737 }
2738 }
2739
2740 if ($useragent =~ /fexsync/) {
2741 if ($features !~ /CCCSYNC/) {
2742 die "$prg: fexserver does not support fexsync\n";
2743 }
2744 if ($opt_C =~/^DOX/ and $features !~ /DOX/) {
2745 die "$prg: fexserver does not support fexdox\n";
2746 }
2747 }
2748
2749 if (${'opt_^'}) {
2750 nmtime(@ARGV);
2751 my @d = localtime $nmtime;
2752 my $dt .= sprintf("_%d%02d%02d_%02d%02d%02d",
2753 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
2754 $opt_a =~ s/(.+)\./$1$dt./;
2755 }
2756
2757 $transferfile = "$fextmp/$opt_a";
2758 unlink $transferfile;
2759 if ($atype eq 'zip') {
2760 print "Making fex archive ($opt_a):\n" unless $opt_q;
2761 if ($windoof) {
2762 system(qw'7z a -tzip',$transferfile,@ARGV);
2763 @files = ($transferfile);
2764 } elsif ($macos and scalar(@ARGV) == 1) {
2765 ## ditto-zip is now handled by formdatapost()
2766 system 'true';
2767 @files = ($opt_a);
2768 } else {
2769 # zip archives must be < 2 GB, so split as necessary
2770 # @files = zipsplit($transferfile,@ARGV);
2771 # if (scalar(@files) == 1) {
2772 # $transferfile = $files[0];
2773 # $transferfile =~ s/_1.zip$/.zip/;
2774 # rename $files[0],$transferfile;
2775 # @files = ($transferfile);
2776 # }
2777 @files = zip($transferfile,@ARGV);
2778 }
2779 @transferfiles = @files;
2780 } elsif ($atype eq '7z') {
2781 print "Making fex archive ($opt_a):\n" unless $opt_q;
2782 # http://www.7-zip.org/
2783 my @sz = qw'7z a -t7z';
2784 push @sz,'-mx0' if $opt_0 or not compressable(@ARGV);
2785 foreach my $x (split('#',$xlist)) { push @sz,"-xr!$x" }
2786 vsystem(@sz,$transferfile,@ARGV);
2787 @transferfiles = @files = ($transferfile);
2788 } elsif ($atype eq 'tar') {
2789 if ($windoof) {
2790 print "Making fex archive ($opt_a):\n" unless $opt_q;
2791 system(qw'7z a -ttar',$transferfile,@ARGV);
2792 @transferfiles = @files = ($transferfile);
2793 } else {
2794 ## tar is handled by formdatapost()
2795 # system(qw'tar cvf',$transferfile,@ARGV);
2796 @files = ($opt_a);
2797 }
2798 } elsif ($atype eq 'tgz') {
2799 if ($windoof) {
2800 die "$prg: archive type tgz not available, use tar, zip or 7z\n";
2801 } else {
2802 ## tgz is handled by formdatapost()
2803 # system(qw'tar cvzf',$transferfile,@ARGV);
2804 @files = ($opt_a);
2805 }
2806 } elsif ($atype eq 'taz') {
2807 if ($windoof) {
2808 die "$prg: archive type taz not available, use tar, zip or 7z\n";
2809 } else {
2810 ## taz is handled by formdatapost()
2811 @files = ($opt_a);
2812 }
2813 } else {
2814 die "$prg: unknown archive format \"$atype\"\n";
2815 }
2816
2817 if (@transferfiles) {
2818
2819 # error in making transfer archive?
2820 if ($?) {
2821 unlink @transferfiles;
2822 die "$prg: $! - aborting upload\n";
2823 }
2824
2825 # maybe timeout, so make new connect
2826 if (time-$t0 >= $timeout) {
2827 serverconnect($server,$port);
2828 query_sid($server,$port) unless $anonymous;
2829 }
2830
2831 }
2832
2833 } else {
2834
2835 unless (@ARGV) {
2836 if ($windoof) {
2837 &inquire;
2838 } else {
2839 die $usage;
2840 }
2841 }
2842
2843 foreach (@ARGV) {
2844 my $file = $_;
2845 unless ($opt_d) {
2846 unless (-f $file) {
2847 if (-e $file) {
2848 die "$prg: \"$file\" is not a regular file, try option -a\n"
2849 } else {
2850 die "$prg: \"$file\" does not exist\n";
2851 }
2852 }
2853 die "$prg: cannot read \"$file\"\n" unless -r $file;
2854 }
2855 push @files,$file;
2856 }
2857 }
2858
2859 if (${'opt_/'}) {
2860 foreach my $file (@files) {
2861 my @s = stat($file);
2862 unless (@s and ($s[2] & S_IROTH) and -r $file) {
2863 die "$prg: \"$file\" is not world readable\n";
2864 }
2865 }
2866 }
2867
2868 my $nextfile = 0;
2869 foreach my $file (@files) {
2870
2871 if ($nextfile) {
2872 warn "\n";
2873 } else {
2874 sleep 1; # do not overrun server!
2875 $nextfile++
2876 }
2877
2878 unless (-s $file or $opt_d or $opt_a or $opt_s) {
2879 die "$prg: cannot send empty file \"$file\"\n";
2880 }
2881 female_mode("send file $file?") if $opt_F;
2882
2883 if (basename($file) =~ /^ARCHIVE_SHARES?$/ and $features !~ /\bFAS\b/) {
2884 die "$prg: server $server:$port has no archive sharing support\n";
2885 }
2886
2887 @r = formdatapost(
2888 from => $from,
2889 to => $to,
2890 replyto => $opt_r,
2891 id => $sid,
2892 file => $file,
2893 keep => $opt_k,
2894 comment => $opt_C,
2895 command => $command,
2896 autodelete => $opt_D,
2897 );
2898
2899 if ($opt_s) {
2900 if (@r) {
2901 if (($from eq $to or $to eq '.') and not $opt_q) {
2902 my $xxx = $useragent =~ /xxx/;
2903 foreach (@r) {
2904 if ($xxx and /Location: (http\S+)/) {
2905 my $url = $1;
2906 warn $_;
2907 if ($url =~ /xxx.bash/) {
2908 print "# copy this bash command to your other account to temporary\n";
2909 print "# install the F*EX clients (including xxx) with your F*EX ID\n";
2910 print "eval \$(wget -qO - $url)\n";
2911 exit;
2912 }
2913 if ($url =~ /($xn)/) {
2914 print "xxx $1\n";
2915 if ($ENV{opt_w}) {
2916 if (/Location: .*\.txt$/) {
2917 print "wget -qO- $url\n";
2918 } elsif (/Location: .*\.gz$/) {
2919 print "wget -qO- $url | gunzip\n";
2920 }
2921 }
2922 }
2923 }
2924 if (/Location: (http.*\.gz$)/) {
2925 my $url = $1;
2926 if ($share_ and $url =~ /\/__$vp/) {
2927 print "wget -qO- $url | gunzip\n";
2928 }
2929 } elsif (/Location:/) {
2930 print;
2931 }
2932 }
2933 }
2934 exit;
2935 } else {
2936 die "$prg: no server response\n";
2937 }
2938 }
2939
2940 if (not @r or not grep /\w/,@r) {
2941 die "$prg: no response from server\n";
2942 }
2943
2944 next if "@r" eq '0';
2945 # already transfered
2946 if ("@r" =~ /^Location: (http.*)/) {
2947 foreach (@r) {
2948 print "Location: $1\n" if /^Location: (http.*)/;
2949 }
2950 # my $url = $1;
2951 # if ($useragent =~ /xxx/) {
2952 # if ($cygwin) {
2953 # print "Location: $url\n";
2954 # }
2955 # print "fexget $1\n";
2956 # } elsif ($useragent =~ /zip|autofex/ or $cygwin) {
2957 # print "Location: $url\n";
2958 # } else {
2959 # print "FOP=$url\n";
2960 # if ($url =~ /\.tar$/) {
2961 # print "wget -qO - \$FOP | tar -xvf -\n";
2962 # } elsif ($url =~ /\.(tgz|tar\.gz)$/) {
2963 # print "wget -qO - \$FOP | tar -xvzf -\n";
2964 # } elsif ($url =~ /\.tar\.bz2$/) {
2965 # print "FOP=$url\n";
2966 # print "wget -qO - \$FOP | bunzip2 | tar -xvf -\n";
2967 # } elsif ($url =~ m:.*/(.+):) {
2968 # print "rm -f $1; ";
2969 # print "wget -c \$FOP\n";
2970 # }
2971 # }
2972 next;
2973 }
2974
2975 if ("@r" =~ /INTERNAL ERROR.*<pre>(.+)<\/pre>/s) {
2976 die "$prg: server error: $1\n";
2977 }
2978
2979 if ($r[0] =~ /^HTTP.* 201 (.+)/) {
2980 print "$1\n";
2981 exit;
2982 }
2983
2984 if (($r) = grep /^ERROR:/,@r) {
2985 if ($anonymous and $r =~ /purge it/) {
2986 die "$prg: file is already on server for $to - use another anonymous recipent\n";
2987 } elsif ($r =~ /timeout/i) {
2988 close $SH;
2989 retry("timed out");
2990 } else {
2991 $r =~ s/.*?:\s*//;
2992 $r =~ s/<.+?>//g;
2993 $r =~ s/^HTTP\/1.. \d+ //;
2994 die "$prg: server error: $r\n";
2995 }
2996 }
2997 unless ($opt_d) {
2998 if (scalar(@r) == 1) {
2999 die "$prg: server error: @r\n";
3000 } else {
3001 if ($r[0] !~ /HTTP.1.. 2/) {
3002 if ($r[0] =~ /HTTP.[\s\d.]+(.+)/) {
3003 die "$prg: server error: $1\n";
3004 } else {
3005 die "$prg: server error:\n".join("\n",@r)."\n";
3006 }
3007 }
3008 }
3009 }
3010 if ("@r" =~ /<h3>(.*?)</) {
3011 print "$1\n";
3012 }
3013
3014 if ($useragent =~ /fexsync/) {
3015 if ($opt_C =~ /^DOX/) {
3016 foreach (@r) {
3017 print "$1\n" if /^Location: (.+)/;
3018 }
3019 }
3020 exit;
3021 }
3022
3023 if ($opt_a !~ /^afex_\d+\.tar$/ and $file !~ /afex_\d+\.tar$/) {
3024 # print grep({s/^(X-Recipient:.*\((.+)\))/Parameters: $2\n/i} @r);
3025 my $nonot = 0;
3026 my $recipient = '';
3027 my @location = ();
3028 foreach (@r) {
3029 if ($useragent =~ /dox/) {
3030 if (/^(X-)?(Recipient: \S+)/i) {
3031 # $recipient = $2."\n";
3032 }
3033 if (m:^(X-)?(Location.*/dox/.*):i) {
3034 push @location,$2."\n";
3035 }
3036 } else {
3037 if (/^(X-)?(Recipient.*)/i) {
3038 $recipient = $2."\n";
3039 if (/notification=no/i) { $nonot = 1 }
3040 else { $nonot = 0 }
3041 }
3042 if (/^(X-)?(Location.*)/i) {
3043 push @location,$2."\n";
3044 }
3045 }
3046 }
3047 if ($from eq $to or $from =~ /^\Q$to\E@/i
3048 or $nomail or $anonymous or $nonot)
3049 {
3050 if ($id =~ /^PKEY:/) {
3051 $recipient =~ s/ \(.*//;;
3052 print $recipient;
3053 } else {
3054 print $recipient if $useragent !~ /xxx/;
3055 print @location if $to ne '.' and $to ne '+';
3056 }
3057 }
3058 if (grep /\/__$vp/,@location) {
3059 } elsif ($to eq '.') {
3060 if ($useragent =~ /autofex|zip/) {
3061 print @location;
3062 exit;
3063 } elsif ($useragent =~ /xxx/) {
3064 if ("@location" =~ /(http\S+xxx_($xn)\S+)/) {
3065 print "xxx -o $2\n";
3066 }
3067 if ($ENV{opt_w}) {
3068 my $cmd;
3069 foreach (@location) {
3070 if (/(http\S+xxx_($xn)\S+)/) {
3071 my $url = $1;
3072 if ($url =~ /\.tar$/) {
3073 print "FOP=$url\n";
3074 $cmd = "wget -qO - \$FOP | tar -xvf -\n";
3075 } elsif ($url =~ /\.(tar\.gz|tgz)$/) {
3076 print "FOP=$url\n";
3077 $cmd = "wget -qO - \$FOP | tar -xvzf -\n";
3078 } elsif ($url =~ /.*\/(.+\.zip)$/) {
3079 print "FOP=$url\n";
3080 $cmd = "wget -c $url && unzip $1 && rm $1\n";
3081 } elsif ($url =~ /\.txt$/) {
3082 $cmd = "wget -qO - $url\n";
3083 } else {
3084 $cmd = "wget -c $url\n";
3085 }
3086 }
3087 }
3088 print $cmd;
3089 }
3090 exit;
3091 } elsif (@location) {
3092 print @location;
3093 # my $cmd;
3094 # foreach (@location) {
3095 # if (/(http.*\.(tar|tg(z))$)/) {
3096 # my $z = $3||'';
3097 # print "FOP=$1\n";
3098 # $cmd = "wget -qO - \$FOP | tar -xv$z -f -\n";
3099 # } elsif (m:(http.*/(.+)):) {
3100 # print "FOP=$1\n";
3101 # $cmd = "rm -f $2; wget \$FOP\n";
3102 # }
3103 # }
3104 # print $cmd;
3105 }
3106 } elsif ($share_) {
3107 print @location;
3108 }
3109 foreach (@r) {
3110 if (/<!-- %W: (.+) -->/) {
3111 $_ = $1;
3112 s/&lt;/</g;
3113 s/&gt;/>/g;
3114 warn "Warning: $_\n";
3115 }
3116 }
3117 print @location if $to eq '+';
3118 }
3119 }
3120
3121 if (@r and $from =~ /^fexmaster@/ and $from ne $to and $to ne '.' and
3122 $useragent !~ m:/:)
3123 {
3124 foreach (@r) {
3125 print "$1\n" if /^X-(Recipient: .+)/;
3126 print "$1\n" if /^X-(Location: http.+)/;
3127 }
3128 }
3129
3130 # delete transfer tmp file
3131 unlink $transferfile if $transferfile;
3132 }
3133
3134
3135 sub forward {
3136 my (@r);
3137 my ($to,$n,$dkey,$file,$req);
3138 my ($status,$fp);
3139 local $_;
3140
3141 # look for single @ in arguments
3142 for (my $i=1; $i<$#ARGV; $i++) {
3143 if ($ARGV[$i] eq '@') {
3144 $ARGV[$i] = join(',',@ARGV[$i+1 .. $#ARGV]);
3145 $#ARGV = $i;
3146 last;
3147 }
3148 }
3149
3150 # if ($windoof and not @ARGV) { &inquire }
3151 $to = pop @ARGV or die $usage;
3152 $to = $from if $to eq '.';
3153 if ($to !~ /@/ and $to ne $from) {
3154 $to = get_mutt_alias($to);
3155 }
3156
3157 open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
3158 while (<$fexlist>) {
3159 if (/#(\d+) (\w+) .\s*\d+ d. ([+-] )?(.+)/ and $1 eq $opt_f) {
3160 $n = $1;
3161 $dkey = $2;
3162 $file = $4;
3163 if ($file =~ s/ "(.*)"$//) {
3164 $opt_C ||= $1 if $1 ne 'NOMAIL';
3165 }
3166 last;
3167 }
3168 }
3169 close $fexlist;
3170
3171 unless ($n) {
3172 die "$prg: file #$opt_f not found in fexlist\n";
3173 }
3174
3175 female_mode("forward file #$opt_f?") if $opt_F;
3176
3177 serverconnect($server,$port);
3178 query_sid($server,$port);
3179
3180 $req = "GET $proxy_prefix/fup?"
3181 ."from=$from&ID=$sid&to=$to&dkey=$dkey&command=FORWARD";
3182 $req .= "&comment=$opt_C" if $opt_C;
3183 $req .= "&keep=$opt_k" if $opt_k;
3184 $req .= "&autodelete=$opt_D" if $opt_D;
3185 $req .= "&$opt_X" if $opt_X;
3186 $req .= " HTTP/1.1";
3187 sendheader("$server:$port",$req);
3188 http_response();
3189 $fp = $file;
3190 $fp =~ s/[^\w_.-]/.+/g; # because of UTF8 filename
3191 $status = 1;
3192 while (<$SH>) {
3193 warn "<-- $_" if $opt_v;
3194 if (/copy-forwarded to/) {
3195 $status = 0;
3196 print;
3197 }
3198 }
3199
3200 if ($status) {
3201 die "$prg: server failed, rerun command with option -v\n";
3202 }
3203 exit;
3204 }
3205
3206
3207 sub renotify {
3208 my (@r);
3209 my ($to,$n,$dkey,$file,$req,$recipient);
3210 local $_;
3211
3212 die $usage if @ARGV;
3213
3214 open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
3215 while (<$fexlist>) {
3216 if (/#(\d+) (\w+) .\s*\d+ d. (.+)/ and $1 eq $opt_N) {
3217 $n = $1;
3218 $dkey = $2;
3219 last;
3220 }
3221 }
3222 close $fexlist;
3223
3224 unless ($n) {
3225 die "$prg: file #$opt_N not found in fexlist\n";
3226 }
3227
3228 female_mode("resend notification for file #$opt_N?") if $opt_F;
3229
3230 serverconnect($server,$port);
3231 query_sid($server,$port);
3232
3233 $req = "GET $proxy_prefix/fup?"
3234 ."from=$from&ID=$sid&dkey=$dkey&command=RENOTIFY"
3235 ." HTTP/1.1";
3236 sendheader("$server:$port",$req);
3237 http_response();
3238 while (<$SH>) {
3239 s/\r//;
3240 warn "<-- $_" if $opt_v;
3241 last if /^\s*$/;
3242 if (/^X-Notify: (.+)\/(.+)\/(.+)/) {
3243 $recipient = $1;
3244 $file = $3;
3245 }
3246 }
3247
3248 if ($file) {
3249 print "notification email for $file has been resent to $recipient\n";
3250 } else {
3251 if ($opt_v) {
3252 die "$prg: server failed\n";
3253 } else {
3254 die "$prg: server failed, rerun command with option -v\n";
3255 }
3256 }
3257
3258 exit;
3259 }
3260
3261
3262 sub modify {
3263 my (@r);
3264 my ($n,$dkey,$file,$req);
3265 local $_;
3266
3267 die $usage if @ARGV;
3268 die $usage unless $opt_C or $opt_k or $opt_D;
3269
3270 open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
3271 while (<$fexlist>) {
3272 if ($opt_x =~ /^\d+$/) {
3273 if (/#(\d+) (\w+) .\s*\d+ d. . (.+)/ and $1 eq $opt_x) {
3274 $n = $1;
3275 $dkey = $2;
3276 $file = $3;
3277 $file =~ s/ "(.*)"$//;
3278 last;
3279 }
3280 } else {
3281 if (/#(\d+) (\w+) .\s*\d+ d. . (.+)/ and $2 eq $opt_x) {
3282 $n = $1;
3283 $dkey = $2;
3284 $file = $3;
3285 $file =~ s/ "(.*)"$//;
3286 last;
3287 }
3288 }
3289 }
3290 close $fexlist;
3291
3292 unless ($n) {
3293 die "$prg: file #$opt_x not found in fexlist\n";
3294 }
3295
3296 female_mode("modify file #$opt_x?") if $opt_F;
3297
3298 serverconnect($server,$port);
3299 query_sid($server,$port);
3300
3301 $req = "GET $proxy_prefix/fup?"
3302 ."from=$from&ID=$sid&dkey=$dkey&command=MODIFY";
3303 $req .= "&comment=$opt_C" if $opt_C;
3304 $req .= "&keep=$opt_k" if $opt_k;
3305 $req .= "&autodelete=$opt_D" if $opt_D;
3306 $req .= " HTTP/1.1";
3307 sendheader("$server:$port",$req);
3308 http_response();
3309 while (<$SH>) {
3310 s/\n*$/\n/;
3311 warn "<-- $_" if $opt_v;
3312 print if /\Q$file/;
3313 }
3314
3315 exit;
3316 }
3317
3318
3319 sub get_xx {
3320 my $transferfile = shift;
3321 my $ft = '';
3322 local $_;
3323
3324 # get transfer file from FEX server
3325 unless ($SH) {
3326 serverconnect($server,$port);
3327 query_sid($server,$port);
3328 }
3329
3330 xxget($from,$sid,$transferfile);
3331
3332 # empty file?
3333 unless (-s $transferfile) {
3334 unlink $transferfile;
3335 exit;
3336 }
3337
3338 if ($ft = `file $transferfile 2>/dev/null`) {
3339 if ($ft =~ /compressed/) {
3340 rename $transferfile,"$transferfile.gz";
3341 shelldo(ws("gunzip $transferfile.gz"));
3342 }
3343 $ft = `file $transferfile`;
3344 }
3345 # file command failed, so we look ourself into the file...
3346 elsif (open $transferfile,$transferfile) {
3347 read $transferfile,$_,4;
3348 close $transferfile;
3349 # gzip magic?
3350 if (/\x1F\x8B\x08\x00/) {
3351 rename $transferfile,"$transferfile.gz";
3352 shelldo(ws("gunzip $transferfile.gz"));
3353 # assuming tar
3354 $ft = 'tar archive';
3355 }
3356 }
3357
3358 if ($ft =~ /tar archive/ and -t STDOUT) {
3359 rename $transferfile,"$transferfile.tar";
3360 $transferfile .= '.tar';
3361 if ($opt_q) {
3362 $_ = 'y';
3363 } else {
3364 print "Files in transfer-container:\n\n";
3365 shelldo(ws("tar tvf $transferfile"));
3366 print "\nExtract these files? [Yn] ";
3367 $_ = <STDIN>;
3368 }
3369 if (/^n/i) {
3370 print "keeping $transferfile\n";
3371 } else {
3372 my $untar = "tar xvf";
3373 # if ($> == 0 and `tar --help 2>&1` =~ /gnu/) {
3374 # $untar = "tar --no-same-owner -xvf";
3375 # }
3376 vsystem("$untar $transferfile && rm $transferfile");
3377 die "$prg: error while untaring, see $transferfile\n" if -f $transferfile;
3378 }
3379 } else {
3380 exec 'cat',$transferfile;
3381 }
3382 exit;
3383 }
3384
3385
3386 sub formdatapost {
3387 my %P = @_;
3388 my ($boundary,$filename,$length,$buf,$file,$fpsize,$resume,$seek,$nettest);
3389 my ($flink);
3390 my (@hh,@hb,@r,@pv,$to);
3391 my ($bytes,$tbytes,$b,$t,$bt);
3392 my ($t0,$t1,$t2,$tt,$tc);
3393 my $readahead; # flag if pipe is read ahead
3394 my $bs = 2**16; # blocksize for reading and sending file
3395 my $fileid = int(time);
3396 my $chunk = 0;
3397 my $filesize = 0;
3398 my $connection = '';
3399 my $pct = '';
3400 my $dittodir = '.';
3401 my ($tar,$ditto,$aname,$atype,$list,$error,$location,$checkrecipient);
3402 local $_;
3403
3404 $_ = $P{command}||'';
3405 $checkrecipient = $_ if /CHECKRECIPIENT/i;
3406
3407 if (defined($file = $P{file})) {
3408
3409 $to = $AB{$P{to}} || $P{to}; # for gpg
3410
3411 if ($_ = $P{comment}) {
3412 $_ = encode_utf8($_);
3413 s/%/%25/g;
3414 s/([^ -~])/urlencode($1)/eg;
3415 $P{comment} = $_;
3416 }
3417
3418 # special file: stream from STDIN
3419 if ($opt_s) {
3420 $filename = encode_utf8($file);
3421 $filesize = -1;
3422 } elsif (not $opt_a) {
3423 # $opt_c and $opt_g are no longer active, because cannot resume
3424 if ($opt_c) {
3425 my ($if,$of);
3426 $if = shellquote($file);
3427 $transferfile = $fextmp . '/' . basename($file) . '.gz';
3428 $of = shellquote($transferfile);
3429 shelldo("gzip <$if>$of");
3430 $filesize = -s $transferfile;
3431 die "$prg: cannot gzip \"$file\"\n" unless $filesize;
3432 $file = $transferfile;
3433 } elsif ($opt_g) {
3434 my ($if,$of);
3435 $if = shellquote($file);
3436 $transferfile = $fextmp . '/' . basename($file) . '.gpg';
3437 $of = shellquote($transferfile);
3438 shelldo("gpg -e -r $to <$if>$of");
3439 $filesize = -s $transferfile;
3440 die "$prg: cannot gpg \"$file\"\n" unless $filesize;
3441 $file = $transferfile;
3442 }
3443 }
3444
3445 # special file: tar-on-the-fly
3446 if (not $windoof and $opt_a and $file =~ /(.+)\.(tar|tgz|taz)$/) {
3447 $aname = $1;
3448 $atype = $2;
3449 $list = "$fextmp/$aname.list";
3450 $error = "$fextmp/$aname.error";
3451 $tar = 'tar -c';
3452 $tar .= 'v' unless $opt_q;
3453 if (-t STDOUT and not $opt_q and
3454 `tar --help 2>/dev/null` =~ /--index-file/)
3455 { $tar .= " --index-file=$list" }
3456 foreach my $x (split('#',$xlist)) {
3457 if ($x eq '.') {
3458 $tar .= " --exclude='.*'";
3459 } else {
3460 $x =~ s/\'/\\\'/g;
3461 $tar .= " --exclude='$x'";
3462 }
3463 }
3464 $tar .= " -f-";
3465 if ("@ARGV" =~ /^\[(\S+)\]$/) {
3466 # [fileslist]
3467 $tar .= ' -T '.shellquote($1);
3468 } else {
3469 foreach (@ARGV) {
3470 s:/+$::;
3471 $_ = '/' if /^$/;
3472 $tar .= ' '.shellquote($_);
3473 }
3474 }
3475 if ($atype eq 'taz') {
3476 if (compressable(@ARGV)) {
3477 $atype = 'tgz';
3478 } else {
3479 $atype = 'tar';
3480 }
3481 $opt_a =~ s/taz$/$atype/;
3482 }
3483 $tar =~ s/-cv/-cvz/ if $atype eq 'tgz';
3484
3485 # print "calculating archive size... ";
3486 print "Making fex archive ($opt_a):\n" unless $opt_q;
3487 warn "\$ $tar\n" if $opt_v;
3488 open $tar,"$tar 2>$error|" or die "$prg: cannot run tar - $!\n";
3489 $t0 = int(time) if -t STDOUT;
3490 while ($b = read $tar,$_,$bs) {
3491 $filesize += $b;
3492 if ($t0) {
3493 $t1 = int(time);
3494 if (not $opt_q and -t STDOUT and $t1>$t0) {
3495 printf "Archive size: %d MB\r",int($filesize/MB);
3496 $t0 = $t1;
3497 }
3498 }
3499 }
3500 printf "Archive size: %d MB\n",int($filesize/MB) if -t STDOUT and not $opt_q;
3501 unless (close $tar) {
3502 $_ = '';
3503 if (open $error,$error) {
3504 local $/;
3505 $_ = <$error>;
3506 close $error;
3507 }
3508 unlink $list,$error;
3509 die "$prg: tar error:\n$_";
3510 }
3511 unlink $error;
3512 $file = "$aname.$atype";
3513 $filename = encode_utf8($file);
3514 undef $SH; # force reconnect (timeout!)
3515 }
3516
3517 # special file: ditto-zip-on-the-fly
3518 # ditto: Can't archive multiple sources
3519 elsif ($macos and $opt_a and $file =~ /(.+)\.(zip)$/ and scalar(@ARGV) == 1)
3520 {
3521 $aname = $1;
3522 $atype = $2;
3523 $list = "$fextmp/$aname.list";
3524 $error = "$fextmp/$aname.error";
3525 $ditto = 'ditto -c -k --sequesterRsrc --keepParent';
3526 if (-d "@ARGV" and "@ARGV" =~ m:^(.+)/(.+):) {
3527 $dittodir = $1;
3528 $file = $2;
3529 $file =~ s/([^\w\-\@\#%,.=+_:])/\\$1/g;
3530 $ditto .= ' '.$file;
3531 } else {
3532 foreach (@ARGV) {
3533 $file = $_;
3534 $file =~ s/([^\w\-\@\#%,.=+_:])/\\$1/g;
3535 $ditto .= ' '.$file;
3536 }
3537 }
3538 # print "calculating archive size... ";
3539 debug("cd $dittodir;$ditto -");
3540 open $ditto,"cd $dittodir;$ditto - 2>$error|"
3541 or die "$prg: cannot run ditto - $!\n";
3542 $t0 = int(time) if -t STDOUT;
3543 while ($b = read $ditto,$_,$bs) {
3544 $filesize += $b;
3545 if ($t0) {
3546 $t1 = int(time);
3547 if (not $opt_q and -t STDOUT and $t1>$t0) {
3548 printf "Archive size: %d MB\r",int($filesize/MB);
3549 $t0 = $t1;
3550 }
3551 }
3552 }
3553 printf "Archive size: %d MB\n",int($filesize/MB) if -t STDOUT and not $opt_q;
3554 unless (close $ditto) {
3555 $_ = '';
3556 if (-s $error and open $error,$error) {
3557 local $/;
3558 $_ = <$error>;
3559 close $error;
3560 }
3561 unlink $list,$error;
3562 die "$prg: ditto-zip error:\n$_";
3563 }
3564 unlink $error;
3565 $file = "$aname.$atype";
3566 $filename = encode_utf8($file);
3567 undef $SH; # force reconnect (timeout!)
3568 }
3569
3570 elsif ($P{to} eq 'nettest') {
3571 $filename = $nettest = 'nettest';
3572 $filesize = $P{size};
3573 $fileid = 0;
3574 }
3575
3576 # single file
3577 elsif (not $opt_s) {
3578 if (${'opt_^'} and my @s = stat $file) {
3579 my @d = localtime($s[9]);
3580 my $dt = sprintf('_%d%02d%02d_%02d%02d%02d',
3581 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
3582 ${'opt_='} = $file;
3583 ${'opt_='} =~ s/(.+?)\./$1$dt./ or ${'opt_='} .= $dt;
3584 }
3585 $filename = encode_utf8(${'opt_='} || $file);
3586 # $filename = urlencode($filename);
3587
3588 if ($windoof) {
3589 $filename =~ s/^[a-z]://;
3590 $filename =~ s/.*\\//;
3591 }
3592 $filename =~ s:.*/::;
3593 $filename =~ s:[\r\n]+: :g;
3594 if ($opt_d) {
3595 $filesize = 0;
3596 } elsif (not $opt_s) {
3597 $filesize = -s $file or die "$prg: \"$file\" is empty or not readable\n";
3598 }
3599 }
3600
3601 unless ($opt_d or $nettest) {
3602 if (0 and $opt_g) {
3603 $filesize = -1;
3604 $fileid = int(time);
3605 } else {
3606 if ($opt_a) {
3607 # index file?
3608 if ($tar and "@ARGV" =~ /^\[(\S+)\]$/) {
3609 my $if = $1;
3610 open $if,$if or die "$prg: cannot open $if - $!\n";
3611 while (<$if>) {
3612 chomp;
3613 push @ifiles,$_;
3614 }
3615 close $if;
3616 if (@ifiles) {
3617 $fileid = md5_hex(fmd(@ifiles));
3618 } else {
3619 $fileid = int(time);
3620 }
3621 } else {
3622 $fileid = md5_hex(fmd(@ARGV));
3623 }
3624 } elsif ($opt_s) {
3625 $fileid = randstring(32);
3626 } else {
3627 $fileid = fileid($file);
3628 }
3629 }
3630 }
3631
3632 } else {
3633 $file = $filename = '';
3634 $filesize = 0;
3635 }
3636
3637 FORMDATAPOST:
3638
3639 @hh = (); # HTTP header
3640 @hb = (); # HTTP body
3641 @r = (); # HTTP reply
3642 $seek = 0;
3643 $resume = '';
3644 $chunk++;
3645
3646 if (not $SH or $transferfile) {
3647 serverconnect($server,$port);
3648 query_sid($server,$port) unless $anonymous or $nettest;
3649 }
3650
3651 $P{id} = $sid; # ugly hack!
3652
3653 $filename =~ s/\\/_/g; # \ is a illegal character for fexsrv
3654
3655 if ($file and not $xx and not $nettest) {
3656 if ($opt_o and not $opt_d and $P{to} ne '+') {
3657 # delete before overwrite
3658 delete_file($from,$to,$filename);
3659 serverconnect($server,$port);
3660 query_sid($server,$port) unless $anonymous;
3661 $P{id} = $sid; # ugly hack!
3662 } elsif (not($opt_s or $opt_d or $opt_l or $opt_L or ${'opt_/'})) {
3663 # ask server if this file has been already sent
3664 ($seek,$location) = query_file($server,$port,
3665 $frecipient||$P{to},$P{from},$P{id},$filename,$fileid);
3666 $seek = 0 if $P{to} eq '+' and $opt_o;
3667 if ($filesize == $seek) {
3668 unlink $list if $list;
3669 if ($share) {
3670 warn "$prg: archive $file is already in share $share\n";
3671 exit;
3672 } else {
3673 warn "$prg: $file has been already transferred\n";
3674 }
3675 if ($location) {
3676 if ($nomail and not $pkey or $share_ or $P{from} eq $P{to}) {
3677 return "Location: $location\n";
3678 }
3679 if ($from =~ /^fexmaster@/) {
3680 print "Location: $location\n";
3681 }
3682 return 0;
3683 }
3684 } elsif ($seek and $seek < $filesize) {
3685 $resume = " (resuming at byte $seek)";
3686 } elsif ($filesize <= $seek) {
3687 $seek = 0;
3688 }
3689 if ($location and $P{to} eq '+') {
3690 print "Location: $location\n";
3691 }
3692 }
3693 if ($proxy) {
3694 sleep 1; # do not overrun proxy
3695 serverconnect($server,$port);
3696 }
3697 }
3698
3699 # file part size
3700 if ($chunksize and ($proxy and $port != 443 or not $proxy)
3701 and $filesize - $seek > $chunksize - $bs) {
3702 if ($features !~ /MULTIPOST/) {
3703 die "$prg: server does not support chunked multi-POST\n";
3704 }
3705 $opt_o = 0; # no overwriting mode for next chunks
3706 $fpsize = $chunksize - $bs;
3707 } else {
3708 $fpsize = $filesize - $seek;
3709 }
3710
3711 $boundary = randstring(48);
3712
3713 $P{seek} = $seek;
3714 $P{filesize} = $filesize;
3715 $P{keep} = '00' if defined $P{keep} and $P{keep} eq '0';
3716
3717 # send HTTP POST variables
3718 if ($skey) {
3719 $P{skey} = $skey;
3720 @pv = qw'from to skey keep autodelete comment seek filesize';
3721 } elsif ($gkey) {
3722 $P{gkey} = $gkey;
3723 @pv = qw'from to gkey keep autodelete comment seek filesize';
3724 } elsif ($okey) {
3725 $P{okey} = $okey;
3726 @pv = qw'from to okey keep autodelete comment seek filesize';
3727 } elsif ($pkey) {
3728 $P{pkey} = $sid;
3729 @pv = qw'from to pkey comment command seek filesize';
3730 } else {
3731 @pv = qw'from to id replyto keep autodelete comment command seek filesize';
3732 }
3733 foreach my $v (@pv) {
3734 if ($P{$v}) {
3735 my $name = uc($v);
3736 push @hb,"--$boundary";
3737 push @hb,"Content-Disposition: form-data; name=\"$name\"";
3738 push @hb,"";
3739 # push @hb,encode_utf8($P{$v});
3740 push @hb,$P{$v};
3741 }
3742 }
3743
3744 # at last, POST the file
3745 if ($file) {
3746 push @hb,"--$boundary";
3747 push @hb,"Content-Disposition: form-data; ".
3748 qq'name="FILE"; filename="$filename"';
3749 unless ($opt_d) {
3750 if ($opt_M) { push @hb,"Content-Type: application/x-mime" }
3751 else { push @hb,"Content-Type: application/octet-stream" }
3752 if (${'opt_/'}) {
3753 $flink = abs_path($file);
3754 push @hb,"Content-Location: $flink";
3755 } else {
3756 push @hb,"Content-Length: $fpsize"; # optional header! NOT filesize!
3757 push @hb,"X-File-ID: $fileid";
3758 }
3759 push @hb,"";
3760 }
3761 push @hb,"";
3762 # prevent proxy chunked mode reply
3763 $connection = "close";
3764 }
3765
3766 push @hb,"--$boundary--";
3767
3768 if ($fpsize < 0) {
3769 $length = $fpsize;
3770 } else {
3771 $length = length(join('',@hb)) + scalar(@hb)*2 + $fpsize;
3772 }
3773
3774 if ($file and not $opt_d) {
3775 if ($flink) { $hb[-2] = $flink }
3776 else { $hb[-2] = '(file content)' }
3777 }
3778 # any other extra URL arguments
3779 my $opt_X = '';
3780 $opt_X = "?LOCALE=english" if $P{command};
3781 $opt_X = "?$::opt_X" if $::opt_X and ($file or $checkrecipient);
3782
3783 # HTTP header
3784 push @hh,"POST $proxy_prefix/fup$opt_X HTTP/1.1";
3785 push @hh,"Host: $server:$port";
3786 push @hh,"User-Agent: $useragent";
3787 push @hh,"Content-Length: $length";
3788 push @hh,"Content-Type: multipart/form-data; boundary=$boundary";
3789 push @hh,"Connection: $connection" if $connection;
3790 push @hh,'';
3791
3792 $SIG{PIPE} = \&sigpipehandler;
3793 # foreach $sig (keys %SIG) {
3794 # eval '$SIG{$sig} = sub { print "\n!!! SIGNAL '.$sig.' !!!\n"; exit; }';
3795 # }
3796
3797 if ($file) {
3798 pop @hb;
3799 pop @hb unless $flink;
3800 nvtsend(@hh,@hb) or do {
3801 warn "$prg: server has closed the connection, reconnecting...\n";
3802 sleep 3;
3803 undef $SH;
3804 goto FORMDATAPOST; # necessary: new $sid ==> new @hh
3805 };
3806
3807 if ($opt_v and not $opt_d) {
3808 if ($flink) { warn "--> $flink\n" }
3809 else { warn "--> (file content)\n" }
3810 }
3811
3812 unless ($opt_d or $flink) {
3813
3814 $t0 = $t2 = int(time);
3815 $tt = $t0-1;
3816 $t1 = 0;
3817 $tc = 0;
3818
3819 if ($opt_s) {
3820 if ($opt_g) {
3821 open $file,"gpg -e -r $to|" or die "$prg: cannot run gpg - $!\n";
3822 } else {
3823 open $file,'>&=STDIN' or die "$prg: cannot open STDIN - $!\n";
3824 }
3825 } elsif ($tar) {
3826 unless ($readahead) {
3827 if ($opt_g) {
3828 warn "\$ $tar|gpg -e -r $to|\n" if $opt_v;
3829 open $file,"$tar|gpg -e -r $to|" or die "$prg: cannot run tar&gpg - $!\n";
3830 } else {
3831 warn "\$ $tar|\n" if $opt_v;
3832 open $file,"$tar|" or die "$prg: cannot run tar - $!\n";
3833 }
3834 if ($seek) {
3835 print "Fast forward to byte $seek (resuming)\n";
3836 readahead($file,$seek);
3837 $readahead = 1;
3838 # reconnect because of possible server timeout
3839 shutdown($SH,2);
3840 undef $SH;
3841 sleep 1;
3842 goto FORMDATAPOST; # necessary: new $sid ==> new @hh
3843 }
3844 }
3845 } elsif ($ditto) {
3846 unless ($readahead) {
3847 $ditto =~ s/ditto/ditto -V/;
3848 open $file,"cd $dittodir;$ditto -|" or die "$prg: cannot run ditto - $!\n";
3849 if ($seek and not $readahead) {
3850 print "Fast forward to byte $seek (resuming)\n";
3851 readahead($file,$seek);
3852 $readahead = 1;
3853 # reconnect because of possible server timeout
3854 shutdown($SH,2);
3855 undef $SH;
3856 sleep 1;
3857 goto FORMDATAPOST; # necessary: new $sid ==> new @hh
3858 }
3859 }
3860 } elsif ($nettest) {
3861 #
3862 } else {
3863 if ($opt_g and not $tar) {
3864 # my $fileq = shellquote($file);
3865 # open $file,"gpg -e -r $to <$fileq|" or die "$prg: cannot run gpg - $!\n";
3866 open $file,$file or die "$prg: cannot read \"$file\" - $!\n";
3867 seek $file,$seek,0;
3868 } else {
3869 open $file,$file or die "$prg: cannot read \"$file\" - $!\n";
3870 seek $file,$seek,0;
3871 }
3872 binmode $file;
3873 }
3874
3875 $readahead = 0;
3876 $bytes = 0;
3877 $tbytes = $seek;
3878
3879 autoflush $SH 0;
3880
3881 print $rcamel[0] if ${'opt_+'};
3882
3883 $buf = '#' x $bs if $nettest;
3884
3885 $SIG{ALRM} = sub { retry("timed out") };
3886
3887 while ($bytes < $fpsize or $opt_s) {
3888 if ($nettest) {
3889 $b = $bs;
3890 } else {
3891 $b = read($file,$buf,$bs)||0;
3892 last if $b == 0;
3893 }
3894 alarm($timeout*2);
3895 if ($https) {
3896 print {$SH} $buf or &sigpipehandler;
3897 } else {
3898 syswrite $SH,$buf or &sigpipehandler;
3899 }
3900 alarm(0);
3901 $bytes += $b;
3902 $tbytes += $b;
3903 if (not $nettest and $filesize > 0 and $tbytes > $filesize) {
3904 die "$prg: \"$file\" filesize has grown while uploading\n";
3905 }
3906 $bt += $b;
3907 $t2 = time;
3908 if (${'opt_+'} and int($t2*10)>$tc) {
3909 print $rcamel[$tc%2+1];
3910 $tc = int($t2*10);
3911 }
3912 if (int($t2)>$t1) {
3913 &sigpipehandler unless $SH->connected;
3914
3915 show_tarindex($list,$pct);
3916
3917 if (not $opt_q and -t STDOUT) {
3918 # smaller block size is better on slow links
3919 $bs = 4096 if $t1 and $bs>4096 and $bytes/($t2-$t0)<65536;
3920 if ($filesize > 0) {
3921 $pct = sprintf "(%d%%)",int($tbytes/$filesize*100);
3922 }
3923 if ($tbytes>2*MB and $bs>4096) {
3924 printf STDERR "%s: %d MB of %d MB %s %d kB/s \r",
3925 $opt_s||$opt_a||$file,
3926 int($tbytes/MB),
3927 int($filesize/MB),
3928 $pct,
3929 int($bt/kB/($t2-$tt));
3930 } else {
3931 printf STDERR "%s: %d kB of %d MB %s %d kB/s \r",
3932 $opt_s||$opt_a||$file,
3933 int($tbytes/kB),
3934 int($filesize/MB),
3935 $pct,
3936 int($bt/kB/($t2-$tt));
3937 }
3938 }
3939
3940 $t1 = $t2;
3941 # time window for transfer rate calculation
3942 if ($t2-$tt>10) {
3943 $bt = 0;
3944 $tt = $t2;
3945 }
3946 }
3947 last if $filesize > 0 and $bytes >= $fpsize;
3948 sleep 1 while ($opt_m and $bytes/kB/(time-$t0||1) > $opt_m);
3949 }
3950
3951 show_tarindex($list,$pct);
3952
3953 warn "\n--> --$boundary--\n" if $opt_v;
3954
3955 $tt = ($t2-$t0)||1;
3956
3957 print $rcamel[2] if ${'opt_+'};
3958
3959 if ($useragent !~ /fexsync/ and not $opt_s and $fileid =~ /[a-z]/) {
3960 if ($opt_a) {
3961 my @files = @ARGV;
3962 @files = @ifiles if @ifiles;
3963 if ($fileid ne md5_hex(fmd(@files))) {
3964 print "\n" unless $opt_q;
3965 die "$prg: files have been modified while uploading\n";
3966 }
3967 } else {
3968 if ($fileid ne fileid($file)) {
3969 print "\n" unless $opt_q;
3970 die "$prg: file has been modified while uploading\n";
3971 }
3972 }
3973 }
3974
3975 if (not $chunksize and $tbytes < $filesize and $useragent !~ /fexsync/) {
3976 die "$prg: \"$file\" filesize has shrunk while uploading\n";
3977 }
3978
3979 unless ($opt_q) {
3980 if ($seek or $chunksize and $chunksize < $filesize) {
3981 if ($fpsize>2*MB) {
3982 printf STDERR "%s: %d MB in %d s = %d kB/s",
3983 $opt_s||$opt_a||$file,
3984 int($bytes/MB),
3985 int($tt)||1,
3986 int($bytes/kB/$tt)||1;
3987 if ($tbytes == $filesize) {
3988 printf STDERR ", total %d MB\n",int($filesize/MB);
3989 } else {
3990 printf STDERR ", chunk #%d : %d MB\n",
3991 $chunk,int($tbytes/MB);
3992 }
3993 } else {
3994 printf STDERR "%s: %d kB in %d s = %d kB/s",
3995 $opt_s||$opt_a||$file,
3996 int($bytes/kB)||1,
3997 int($tt)||1,
3998 int($bytes/kB/$tt)||1;
3999 if ($tbytes == $filesize) {
4000 printf STDERR ", total %d kB\n",int($filesize/kB)||1;
4001 } else {
4002 printf STDERR ", chunk #%d : %d kB\n",
4003 $chunk,int($tbytes/kB)||1;
4004 }
4005 }
4006 } else {
4007 if ($tbytes>2*MB) {
4008 printf STDERR "%s: %d MB in %d s = %d kB/s \n",
4009 $opt_s||$opt_a||$file,
4010 int($bytes/MB),
4011 int($tt)||1,
4012 int($bytes/kB/$tt)||1;
4013 } else {
4014 printf STDERR "%s: %d kB in %d s = %d kB/s \n",
4015 $opt_s||$opt_a||$file,
4016 int($bytes/kB)||1,
4017 int($tt)||1,
4018 int($bytes/kB/$tt)||1;
4019 }
4020 }
4021 }
4022 }
4023
4024 autoflush $SH 1;
4025 print {$SH} "\r\n--$boundary--\r\n";
4026 # return if $nettest;
4027
4028 # special handling of streaming file because of stunnel tcp shutdown bug
4029 if ($opt_s or 0 and $opt_g) {
4030 print "waiting for server response" if -t STDOUT and not $opt_q;
4031 for (1..3) {
4032 shutdown($SH,2);
4033 close $SH;
4034 sleep 2;
4035 print "." if -t STDOUT and not $opt_q;
4036 serverconnect($server,$port);
4037 query_sid($server,$port) unless $anonymous;
4038 ($seek,$location) =
4039 query_file($server,$port,$P{to},$P{from},$sid,$filename,$fileid);
4040 if ($seek == $bytes) {
4041 print "\r \r" if -t STDOUT and not $opt_q;
4042 return "Location: $location\n";
4043 }
4044 }
4045 if ($location) {
4046 die "$prg: streamed $bytes bytes but server received $seek bytes\n";
4047 }
4048 print "\r \r" if -t STDOUT and not $opt_q;
4049 return '';
4050 }
4051
4052 if ($flink) {
4053 $bytes = -s $flink;
4054 if ($bytes>2*MB) {
4055 printf STDERR "%s: %d MB\n",$flink,int($bytes/MB);
4056 } else {
4057 printf STDERR "%s: %d kB\n",$flink,int($bytes/kB);
4058 }
4059 }
4060 } else {
4061 autoflush $SH 1;
4062 nvtsend(@hh,@hb) or die "$prg: server has closed the connection\n";
4063 }
4064
4065 # SuSe: Can't locate object method "BINMODE" via package "IO::Socket::SSL::SSL_HANDLE"
4066 # binmode $SH,':utf8';
4067
4068 # if (not $opt_q and $file and -t STDOUT) {
4069 # print STDERR "\r \r";
4070 # }
4071
4072
4073 my $cl = '';
4074 if (-t STDOUT and not ($opt_s or $nettest) or $useragent =~ /fexsync/) {
4075 unless ($opt_q) {
4076 print STDERR "waiting for server ok...";
4077 $cl = "\r \r";
4078 }
4079 }
4080 while (<$SH>) {
4081 s/[\r\n]+//g;
4082 print STDERR $cl; $cl = '';
4083 warn "<-- $_\n" if $opt_v;
4084 if (@r) {
4085 last if $r[0] =~ / (204|222) / and /^$/ or /<\/html>/i;
4086 last if /^$/ and grep /^Connection: close/,@r and grep /^X-Location/,@r;
4087 }
4088 push @r,decode_utf8($_);
4089 }
4090 print STDERR $cl; $cl = '';
4091
4092 if ($file) {
4093 close $SH;
4094 undef $SH;
4095 if ($chunksize and $tbytes < $filesize) {
4096 if ($sleepwait) {
4097 my $wait = $sleepwait;
4098 while ($wait) {
4099 print STDERR "next connect in $wait s \r" if -t STDOUT;
4100 sleep 1;
4101 $wait--;
4102 }
4103 }
4104 $readahead = 1;
4105 goto FORMDATAPOST;
4106 }
4107 close $file if fileno $file; # unless $nettest or $opt_d or $flink;
4108
4109 }
4110
4111 return @r;
4112 }
4113
4114
4115 sub show_tarindex {
4116 my $list = shift;
4117 my $pct = shift;
4118 my $nl = '';
4119 local $_;
4120
4121 if ($list) {
4122 unless (fileno $list) {
4123 open $list,$list and unlink $list;
4124 }
4125 if (fileno $list) {
4126 $nl = "\n" if $pct;
4127 while (<$list>) {
4128 print $nl,$_;
4129 $nl = '';
4130 }
4131 }
4132 }
4133 }
4134
4135
4136 sub randstring {
4137 my $n = shift;
4138 my @rc = ('A'..'Z','a'..'z',0..9 );
4139 my $rn = @rc;
4140 my $rs;
4141
4142 for (1..$n) { $rs .= $rc[int(rand($rn))] };
4143 return $rs;
4144 }
4145
4146
4147 sub zip {
4148 no strict 'refs';
4149 my $zip = shift;
4150 my $du = 0;
4151 my ($cmd);
4152 my @q;
4153 local $_;
4154
4155 warn "\$ find -type f @_\n" if $opt_v;
4156 if (not $opt_q and open my $F,'-|','find',@_,qw'-type f') {
4157 my $xp = '';
4158 foreach my $x (split('#',$xlist)) {
4159 if ($x eq '.') {
4160 $xp .= '|.*/\.';
4161 } else {
4162 $x = quotemeta $x;
4163 $x =~ s/\\\?/./g;
4164 $x =~ s/\\\*/.*/g;
4165 $xp .= "|$x|.*/$x";
4166 }
4167 }
4168 $xp =~ s/.//;
4169 while (<$F>) {
4170 chomp;
4171 unless ($xp and /^$xp$/) {
4172 $du += -s;
4173 }
4174 }
4175 close $F;
4176 $du = int($du/1024/1024);
4177 print "$du MB\n";
4178 if ($du >= 2048) {
4179 print "zip cannot handle more than 2 GB correctly, better use 7z\n";
4180 print "continue anyway? ";
4181 $_ = <STDIN>||'';
4182 exit 1 if /^n/i;
4183 }
4184 }
4185
4186 unlink $zip;
4187 # if ($opt_c) { $cmd = "zip -@ $zip" }
4188 # else { $cmd = "zip -0 -@ $zip" }
4189 $cmd = "zip -r -y -@ $zip -x";
4190 $cmd =~ s/ / -0 / if $opt_0 or not compressable(@_);
4191 foreach my $x (split('#',$xlist)) {
4192 if ($x eq '.') {
4193 $cmd .= " '*/.*' '*/.*/*'";
4194 } else {
4195 $x =~ s/\'/'"\'"'/g;
4196 $cmd .= " '$x' '*/$x' '*/$x/*'";
4197 }
4198 }
4199 warn "\$ $cmd\n" if $opt_v;
4200 open $cmd,"|$cmd" or die "$prg: cannot create $zip - $!\n";
4201 foreach (@_) {
4202 print {$cmd} $_."\n";
4203 warn " $_\n" if $opt_v;
4204 }
4205 close $cmd or warn "$prg: zip failed - $!\n";
4206
4207 return $zip;
4208 }
4209
4210
4211 sub getline {
4212 my $file = shift;
4213 local $_;
4214
4215 while (<$file>) {
4216 chomp;
4217 s/^#.*//;
4218 s/\s+#.*//;
4219 s/^\s+//;
4220 s/\s+$//;
4221 return $_ if length($_);
4222 }
4223 return '';
4224 }
4225
4226
4227 sub query_file {
4228 my ($server,$port,$to,$from,$qid,$filename,$fileid) = @_;
4229 my ($head,$response,$fexsrv,$cc);
4230 my $seek = 0;
4231 my $qfileid = '';
4232 my $location = '';
4233 local $_;
4234
4235 $to =~ s/[,:].*//;
4236 $to = $AB{$to} if $AB{$to};
4237 $to = $from if $to eq '.';
4238 $filename = urlencode($filename);
4239 if ($skey) {
4240 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??SKEY=$qid HTTP/1.1";
4241 } elsif ($gkey) {
4242 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??GKEY=$qid HTTP/1.1";
4243 } elsif ($okey) {
4244 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??OKEY=$qid HTTP/1.1";
4245 } elsif ($share) {
4246 my $qid = $pkey || $id;
4247 $head = sprintf "HEAD $proxy_prefix/fop/$to/$share/$from/%s/%s HTTP/1.1",
4248 md5_hex("$filename:$qid"),$filename;
4249 # die "$filename:$qid\n$head";
4250 } else {
4251 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??ID=$qid HTTP/1.1";
4252 }
4253 sendheader("$server:$port",$head);
4254 $_ = <$SH>;
4255 unless (defined $_ and /\w/) {
4256 die "$prg: no response from server\n";
4257 }
4258 s/\r//;
4259 warn "<-- $_" if $opt_v;
4260 unless (/^HTTP.* 200/) {
4261 s:HTTP/[\d\. ]+::;
4262 $response = $_;
4263 while (<$SH>) {
4264 s/\r//;
4265 warn "<-- $_" if $opt_v;
4266 $fexsrv = $_ if /^(Server: fexsrv|X-Features:)/;
4267 last if /^\s*$/;
4268 }
4269 die "$prg: no fexserver at $server:$port\n" unless $fexsrv;
4270 chomp $response;
4271 if ($share) {
4272 die "$prg: no share support on $server:$port ($response)\n";
4273 } else {
4274 die "$prg: response from $server:$port : $response\n";
4275 }
4276 }
4277 while (<$SH>) {
4278 s/\r//;
4279 warn "<-- $_" if $opt_v;
4280 last if /^$/;
4281 if (/^Content-Length:\s+(\d+)/) { $seek = $1 }
4282 if (/^X-File-ID:\s+(.+)/) { $qfileid = $1 }
4283 if (/^X-Features:\s+(.+)/) { $features = $1 }
4284 if (/^X-Location:\s+(.+)/) { $location = $1 }
4285 if (/^Connection: close/) { $cc = $_ }
4286 }
4287
4288 # return true seek only if file is identified
4289 $seek = 0 if $qfileid and $qfileid ne $fileid;
4290
4291 if ($cc) {
4292 serverconnect($server,$port);
4293 $sid = $id;
4294 }
4295
4296 return ($seek,$location);
4297 }
4298
4299
4300 sub edit_address_book {
4301 my ($user) = @_;
4302 my $alias;
4303 my $ab = "$fexhome/ADDRESS_BOOK";
4304 my (%AB,@r);
4305 local $_;
4306
4307 die "$prg: address book not available for subusers\n" if $skey;
4308 die "$prg: address book not available for onetime users\n" if $okey;
4309 die "$prg: address book not available for group members\n" if $gkey;
4310 die "$prg: address book not available for share users\n" if $pkey;
4311
4312 unless (-t STDOUT) {
4313 &show_address_book;
4314 exit;
4315 }
4316
4317 female_mode("edit your address book?") if $opt_F;
4318
4319 %AB = query_address_book($server,$port,$user);
4320 if ($AB{ADDRESS_BOOK} !~ /\w/) {
4321 $AB{ADDRESS_BOOK} =
4322 "# Format: alias email-address # Comment\n".
4323 "# Example:\n".
4324 "framstag framstag\@rus.uni-stuttgart.de\n";
4325 }
4326 open $ab,">$ab" or die "$prg: cannot write to $ab - $!\n";
4327 print {$ab} $AB{ADDRESS_BOOK};
4328 close $ab;
4329
4330 system "$editor $ab";
4331 exit unless -s $ab;
4332
4333 $opt_o = $opt_A;
4334
4335 serverconnect($server,$port);
4336 query_sid($server,$port);
4337
4338 @r = formdatapost(
4339 from => $user,
4340 to => $user,
4341 id => $sid,
4342 file => $ab,
4343 );
4344
4345 unlink $ab,$ab.'~';
4346 }
4347
4348
4349 sub query_address_book {
4350 my ($server,$port,$user) = @_;
4351 my ($req,$alias,$address,$options,$comment,$cl,$ab,$b);
4352 my %AB;
4353 local $_;
4354
4355 unless ($SH) {
4356 serverconnect($server,$port);
4357 query_sid($server,$port);
4358 }
4359
4360 $req = "GET $proxy_prefix/fop/$user/$user/ADDRESS_BOOK?ID=$sid HTTP/1.1";
4361 sendheader("$server:$port",$req);
4362 $_ = <$SH>;
4363 unless (defined $_ and /\w/) {
4364 die "$prg: no response from server\n";
4365 }
4366 s/\r//;
4367 warn "<-- $_" if $opt_v;
4368 unless (/^HTTP.* 200/) {
4369 if (/^HTTP.* 404/) {
4370 while (<$SH>) { last if /^\r?\n/ }
4371 return;
4372 } else {
4373 # s:HTTP/[\d\. ]+::;
4374 # die "$prg: server response: $_";
4375 close $SH;
4376 undef $SH;
4377 return ();
4378 }
4379 }
4380 while (<$SH>) {
4381 s/\r//;
4382 warn "<-- $_" if $opt_v;
4383 last if /^$/;
4384 $cl = $1 if /^Content-Length: (\d+)/;
4385 }
4386
4387 if ($cl) {
4388 while (<$SH>) {
4389 $b += length;
4390 $ab .= $_;
4391 s/[\r\n]//g;
4392 s/^\s+//;
4393 s/\s+$//;
4394 warn "<-- $_\n" if $opt_v;
4395 s/\s*#\s*(.*)//;
4396 if ($_) {
4397 $comment = $1||'';
4398 ($alias,$address,$options) = split;
4399 if ($address) {
4400 if ($options) { $options =~ s/[()]//g }
4401 else { $options = '' }
4402 $AB{$alias} = $address;
4403 $AB{$alias}->{options} = $options||'';
4404 $AB{$alias}->{comment} = $comment||'';
4405 if ($options and $options =~ /keep=(\d+)/i) {
4406 $AB{$alias}->{keep} = $1;
4407 }
4408 if ($options and $options =~ /autodelete=(\w+)/i) {
4409 $AB{$alias}->{autodelete} = $1;
4410 }
4411 }
4412 }
4413 last if $b >= $cl;
4414 }
4415 }
4416
4417 $AB{ADDRESS_BOOK} = $ab;
4418
4419 return %AB;
4420 }
4421
4422
4423 # sets global $sid $features $timeout # ugly hack! :-}
4424 sub query_sid {
4425 my ($server,$port) = @_;
4426 my ($req,$fexsrv,$cc);
4427 local $_;
4428
4429 $sid = $id;
4430
4431 if ($port eq 443 or $proxy) {
4432 return if $opt_d;
4433 return if $features; # early return if we know enough
4434 $req = "OPTIONS /FEX HTTP/1.1"; # does not work with (some) proxies
4435 $req = "GET /SID HTTP/1.1"; # needed as FEATURES query
4436 } else {
4437 $req = "GET /SID HTTP/1.1";
4438 }
4439
4440 sendheader("$server:$port",$req,"X-User: $from");
4441 $_ = <$SH>;
4442 unless (defined $_ and /\w/) {
4443 warn "\n" if $opt_v;
4444 die "$prg: no response from server\n";
4445 }
4446 s/\r//;
4447 warn "<-- $_" if $opt_v;
4448
4449 if (/^HTTP.* [25]0[01] /) {
4450 if ($port ne 443 and not $okey and $FEXOPT !~ /\bNOSID\b/i) {
4451 if (/^HTTP.* 201 (.+)/ and not $proxy) {
4452 $sid = 'MD5H:'.md5_hex($id.$1);
4453 } else {
4454 warn "$prg: no SID available, sending auth-ID unencrypted\n";
4455 }
4456 }
4457 while (<$SH>) {
4458 s/\r//;
4459 warn "<-- $_" if $opt_v;
4460 $features = $1 if /^X-Features: (.+)/;
4461 $timeout = $1 if /^X-Timeout: (\d+)/;
4462 $cc = $_ if /^Connection: close/;
4463 last if /^\n/;
4464 }
4465 if ($cc) {
4466 serverconnect($server,$port);
4467 $sid = $id;
4468 }
4469 } elsif (/^HTTP.* 301 /) {
4470 while (<$SH>) { last if /Location/ }
4471 die "$prg: cannot use $server:$port because server has a redirection to\n".$_;
4472 } else {
4473 # no SID support - perhaps transparent web proxy?
4474 while (<$SH>) {
4475 s/\r//;
4476 warn "<-- $_" if $opt_v;
4477 $fexsrv = $_ if /^(Server: fexsrv|X-Features:)/;
4478 last if /^\s*$/;
4479 }
4480 die "$prg: no fexserver at $server:$port\n" unless $fexsrv;
4481 serverconnect($server,$port);
4482 $sid = $id;
4483 }
4484
4485 # warn "proxy: $proxy\n";
4486 if ($proxy) {
4487 serverconnect($server,$port);
4488 $sid = $id;
4489 }
4490
4491 }
4492
4493
4494 sub xxget {
4495 my ($from,$id,$save) = @_;
4496 my $bs = 4096;
4497 my $xx = $save;
4498 my ($url,$B,$b,$t0,$t1,$cl);
4499 my ($ts,$tso);
4500 local $_;
4501
4502 $xx =~ s:.*/::;
4503 $url = "$proxy_prefix/fop/$from/$from/$xx?ID=$id";
4504
4505 sendheader("$server:$port","GET $url HTTP/1.0");
4506 http_response();
4507 while (<$SH>) {
4508 s/\r//;
4509 warn "<-- $_" if $opt_v;
4510 $cl = $1 if /^Content-Length:\s(\d+)/;
4511 # $ft = $1 if /^X-File-Type:\s(.+)/;
4512 last if /^$/;
4513 }
4514
4515 die "$prg: no Content-Length in server-reply\n" unless $cl;
4516
4517 open $save,">$save" or die "$prg: cannot write to $save - $!\n";
4518 binmode $save;
4519
4520 $t0 = $t1 = int(time);
4521 $tso = '';
4522
4523 while ($b = read($SH,$_,$bs)) {
4524 $B += $b;
4525 print {$save} $_;
4526 if (int(time) > $t1) {
4527 $t1 = int(time);
4528 $ts = ts($B,$cl);
4529 if ($ts ne $tso) {
4530 print STDERR $ts,"\r";
4531 $tso = $ts;
4532 }
4533 }
4534 sleep 1 while ($opt_m and $B/kB/(time-$t0||1) > $opt_m);
4535 }
4536
4537 print STDERR ts($B,$cl),"\n";
4538 close $save;
4539 }
4540
4541
4542 # transfer status
4543 sub ts {
4544 my ($b,$tb) = @_;
4545 return sprintf("transferred: %d MB (%d%%)",int($b/MB),int($b/$tb*100));
4546 }
4547
4548
4549 sub sigpipehandler {
4550 retry("died");
4551 }
4552
4553 sub retry {
4554 my $reason = shift;
4555 local $SIG{ALRM} = sub { };
4556
4557 if (fileno $SH) {
4558 alarm(1);
4559 my @r = <$SH>;
4560 alarm(0);
4561 if (@r and $opt_v) {
4562 die "\n$prg: ($$) server error: @r\n";
4563 }
4564 if (@r and $r[0] =~ /^HTTP.* \d+ (.*)/) {
4565 die "\n$prg: server error: $1\n";
4566 }
4567 }
4568 $timeout *= 2;
4569 warn "\n$prg: connection to $server $reason\n";
4570 if ($opt_s) { exit 3 }
4571 warn "retrying after $timeout seconds...\n";
4572 sleep $timeout;
4573 if ($windoof) { exec $^X,$prg,@_ARGV }
4574 else { exec $_0,@_ARGV }
4575 die $!;
4576 }
4577
4578
4579 sub checkrecipient {
4580 my ($from,$to) = @_;
4581 my @r;
4582 local $_;
4583
4584 @r = formdatapost(
4585 from => $from,
4586 to => $to,
4587 id => $sid,
4588 autodelete => $opt_D,
4589 keep => $opt_k,
4590 command => 'CHECKRECIPIENT',
4591 );
4592
4593 $_ = shift @r or die "$prg: no reply from server\n";
4594
4595 if (/ 2\d\d /) {
4596 return if $to eq 'nettest';
4597 foreach (@r) {
4598 last if /^$/;
4599 if (s/X-(Recipient: .+)/$1\n/) {
4600 s/autodelete=\w+/autodelete=$opt_D/ if $opt_D;
4601 s/keep=\d+/keep=$opt_k/ if $opt_k;
4602 print unless $opt_q;
4603 $frecipient ||= (split)[1];
4604 }
4605 }
4606 } else {
4607 http_response($_,@r);
4608 }
4609 }
4610
4611
4612 # get ID data from ID file
4613 sub get_id {
4614 my $idf = shift;
4615
4616 $fexcgi = getline($idf) || die "$prg: no FEX-URL in $idf\n";
4617 $from = getline($idf) || die "$prg: no FROM in $idf\n";
4618 $id = getline($idf) || die "$prg: no ID in $idf\n";
4619 if ($fexcgi =~ s/!([\w.-]+:\d+)(:(\d+))?//) {
4620 $proxy = $1;
4621 $chunksize = $3 || 0;
4622 }
4623 unless ($fexcgi =~ /^[_:=\w\-\.\/\@\%]+$/) {
4624 die "$prg: illegal FEX-URL \"$fexcgi\" in $idf\n";
4625 }
4626 unless ($from =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
4627 die "$prg: illegal FROM \"$from\" in $idf\n";
4628 }
4629 $fexcgi =~ s:/+$::;
4630 }
4631
4632
4633 # for windows
4634 sub inquire {
4635 my ($file,$to);
4636 unless (@ARGV) {
4637 for (;;) {
4638 $file = inputline("file or directory to send: ");
4639 exit unless length $file;
4640 $file =~ s/^'(.+)'$/$1/;
4641 last if -e $file;
4642 warn "$file does not exist\n";
4643 }
4644 if (-d $file) {
4645 my $dir = dirname($file);
4646 $file = basename($file);
4647 chdir $dir or die "$prg: $dir - $!\n";
4648 $opt_a ||= "$file.zip";
4649 }
4650 @ARGV = ($file);
4651 }
4652 $to = inputline("recipient (email address): ");
4653 unless (length $to) {
4654 $opt_n = $to = '.';
4655 }
4656 $to =~ s/\s+/,/g;
4657 $to =~ s/,,+/,/g;
4658 $to =~ s/^,//;
4659 $to =~ s/,$//;
4660 push @ARGV,$to;
4661 unless ($opt_n or $opt_C) {
4662 $opt_C = inputline("comment: ");
4663 }
4664 }
4665
4666
4667 sub shelldo {
4668 warn "\$ @_\n" if $opt_v;
4669 if (system(@_) < 0) { die "failed: @_\n" }
4670 }
4671
4672
4673 # emulate seek on a pipe
4674 sub readahead {
4675 my $fh = shift; # filehandle
4676 my $ba = shift; # bytes ahead
4677 my $bs = 2**16;
4678 my $s = 0;
4679 my $n;
4680 local $_;
4681
4682 while ($s < $ba) {
4683 $n = $ba-$s;
4684 $n = $bs if $n > $bs;
4685 $s += read $fh,$_,$n;
4686 }
4687 }
4688
4689
4690 sub fileid {
4691 my $file = shift;
4692 my $dirmode = shift;
4693 my @s = $dirmode ? lstat($file) : stat($file);
4694
4695 if (@s) {
4696 return md5_hex($file.$s[0].$s[1].$s[7].$s[9]);
4697 } else {
4698 warn "$prg: $file - $!\n";
4699 return int(time);
4700 }
4701 }
4702
4703
4704 sub get_mutt_alias {
4705 my $to = shift;
4706 my $ma = $HOME.'/.mutt/aliases';
4707 my ($alias,$options);
4708 local $_;
4709
4710 $to =~ s/(:.+)// and $options = $1;
4711 open $ma,$ma or return $to;
4712 while (<$ma>) {
4713 if (/^alias \Q$to\E\s/i) {
4714 chomp;
4715 s/\s*#.*//;
4716 s/\(.*?\)//;
4717 s/\s+$//;
4718 s/.*\s+//;
4719 s/[<>]//g;
4720 if (/,/) {
4721 warn "$prg: ignoring mutt multi-alias $to = $_\n";
4722 last;
4723 }
4724 if (/@/) {
4725 $alias = $_;
4726 warn "$prg: found mutt alias $to = $alias\n";
4727 $alias .= $options if $options;
4728 last;
4729 }
4730 }
4731 }
4732 close $ma;
4733 $to = "$to:$options" if $options;
4734 return ($alias||$to);
4735 }
4736
4737
4738 # collect (hashed) file meta data
4739 sub fmd {
4740 my @files = @_;
4741 my ($file,$dir);
4742 my $fmd = '';
4743
4744 foreach $file (@files) {
4745 if (not -l $file and -d $file) {
4746 $dir = $file;
4747 next if $dir =~ m:/\.fex/tmp$:;
4748 if (opendir $dir,$dir) {
4749 while (defined($file = readdir($dir))) {
4750 next if $file eq '..';
4751 next if $xp and $file =~ /^($xp)$/;
4752 if ($file eq '.') {
4753 $fmd .= fileid($dir);
4754 } elsif (-l "$dir/$file") {
4755 # hack for dangling symlinks: do not raise an error
4756 $fmd .= fileid("$dir/$file",'dirmode');
4757 } else {
4758 $fmd .= fmd("$dir/$file");
4759 }
4760 }
4761 closedir $dir;
4762 }
4763 } else {
4764 $fmd .= fileid($file,'dirmode');
4765 }
4766 }
4767
4768 return $fmd;
4769 }
4770
4771
4772 sub female_mode {
4773 local $_;
4774 if (open my $tty,'/dev/tty') {
4775 print "@_\n";
4776 print " [y] yes\n",
4777 " [n] no\n",
4778 " [p] perhaps - don't know\n",
4779 "your choice: ";
4780 $_ = <$tty> || '';
4781 close $tty;
4782 if (/^y/i) { return }
4783 if (/^n/i) { exit }
4784 if (/^p/i) { int(rand(2)) ? return : exit }
4785 female_mode(@_);
4786 }
4787 }
4788
4789
4790 sub http_response {
4791 local $_ = shift || <$SH>;
4792 my @r = @_;
4793 my $error;
4794
4795 $_ = <$SH> unless $_;
4796 unless (defined $_ and /\w/) {
4797 die "$prg: no response from server\n";
4798 }
4799 s/\r?\n//;
4800 # warn "<-- $_\n" if $opt_v;
4801 # CGI fatalsToBrowser
4802 if (/^HTTP.* 500/) {
4803 @r = <$SH> unless @r;
4804 @r = () unless @r;
4805 die "$prg: server error: $_\n@r\n";
4806 }
4807 unless (/^HTTP.* 200/) {
4808 $error = $_;
4809 $error =~ s/HTTP.[\s\d.]+//;
4810 if (defined($SH)) {
4811 @r = <$SH> unless @r;
4812 @r = () unless @r;
4813 }
4814 foreach (@r) {
4815 chomp;
4816 $error .= "\n".$_ if /^Location/;
4817 warn "<-- $_\n" if $opt_v;
4818 }
4819 die "$prg: server error: $error\n";
4820 }
4821
4822 if ("@r" =~ /INTERNAL ERROR.*<pre>(.+)<\/pre>/s) {
4823 die "$prg: server error: $1\n";
4824 }
4825
4826 return $_;
4827 }
4828
4829
4830 # check (heuristcally) if files should be compressed
4831 sub compressable {
4832 my $uf = 1;
4833 my $cf = 1;
4834 my $cfx = 'gif|jpg|png|avi|mp\d|flv|m4v|ogg|tar|t?gz|zip|7z|bz2|rar|iso';
4835
4836 local $_;
4837
4838 if ("@_" =~ /^\[(.+)\]$/) {
4839 my $flist = $1;
4840 if (open $flist,$flist) {
4841 while (<$flist>) {
4842 chomp;
4843 if (-f) {
4844 if (/\.($cfx)$/i) {
4845 $cf += -s;
4846 } else {
4847 $uf += -s;
4848 }
4849 }
4850 }
4851 close $flist;
4852 }
4853 } else {
4854 warn "\$ find @_ -type f|\n" if $opt_v;
4855 my @find = ('find',@_,qw'-type f');
4856 foreach my $x (split('#',$xlist)) {
4857 $x = '.*' if $x eq '.';
4858 $_ = "( ! -path */$x -prune )";
4859 push @find,split;
4860 }
4861 if (open my $find,'-|',@find) {
4862 while (<$find>) {
4863 chomp;
4864 if (/\.($cfx)$/i) {
4865 $cf += -s;
4866 } else {
4867 $uf += -s;
4868 }
4869 }
4870 close $find;
4871 }
4872 }
4873
4874 if ($uf+$cf < 2**31 and $uf/$cf > 0.2) {
4875 return 1;
4876 } else {
4877 return 0;
4878 }
4879 }
4880
4881
4882 sub ws {
4883 local $_ = shift;
4884 return split;
4885 }
4886
4887
4888 sub update {
4889 my $cfb = '### common functions ###';
4890 my $cfc;
4891
4892 local $/;
4893
4894 open $prg,$prg or die "cannot read $prg - $!\n";
4895 $cfc = <$prg>;
4896 close $prg;
4897 $cfc =~ s/.*\n$cfb\n//s;
4898
4899 my @p = qw(fexget sexsend);
4900 foreach my $p (@p) {
4901 if (open $p,$p) {
4902 $_ = <$p>;
4903 close $p;
4904 s/\n$cfb.*/\n$cfb\n$cfc/s;
4905 system "vv -s $p";
4906 open $p,'>',$p or die "cannot write $p - $!\n";
4907 print {$p} $_;
4908 close $p;
4909 } else {
4910 warn "cannot read $p - $!\n";
4911 }
4912 }
4913
4914 exec "l fexsend @p";
4915 exit;
4916 }
4917
4918
4919 # newest modification time
4920 sub nmtime {
4921 my ($file,$dir);
4922 my @files;
4923 my @s;
4924 local $_;
4925
4926 if (scalar(@_) == 1 and not -d $_[0]) {
4927 if (@s = stat $_[0]) {
4928 $nmtime = $s[9] if $s[9] > $nmtime;
4929 }
4930 } else {
4931 foreach $file (@_) {
4932 @files = ();
4933 if (-d $file and opendir $dir,$file) {
4934 while (defined($_ = readdir $dir)) {
4935 next if /^\.\.?$/;
4936 next if $xp and /^($xp)$/;
4937 push @files, "$file/$_";
4938 }
4939 closedir $dir;
4940 nmtime(@files);
4941 } else {
4942 if (@s = lstat $file) {
4943 $nmtime = $s[9] if $s[9] > $nmtime;
4944 }
4945 }
4946 }
4947 }
4948 }
4949
4950
4951 sub xxx {
4952 my (%fp,%list);
4953 my $clear = `clear`;
4954 my $first = 1;
4955
4956 $ENV{FUA} = 'xxx';
4957
4958 my @fexget = ($fexget);
4959 push @fexget,'-o' if $opt_o;
4960 push @fexget,'-m',$opt_m if $opt_m;
4961
4962 unless ($FEXID = $ENV{FEXXX}||$ENV{FEXID}) {
4963 if (open $idf,$idf) {
4964 get_id($idf);
4965 while (<$idf>) {
4966 if (/^\[xx\]/) {
4967 get_id($idf);
4968 last;
4969 }
4970 }
4971 close $idf;
4972 $FEXID = encode_b64("$fexcgi $from $id");
4973 }
4974 # $FEXID = $1 if `xx -I</dev/null` =~ /FEXXX=(.+)/;
4975 }
4976
4977 if ($opt_I) {
4978 if ($FEXID) {
4979 print "export FEXXX='$FEXID';history -d \$((HISTCMD-1))\n";
4980 } else {
4981 die "$prg: no FEXID\n";
4982 }
4983 exit;
4984 }
4985
4986 if ($FEXID) {
4987 if (($ENV{FEXID}||'') ne $FEXID) {
4988 warn "\$ export FEXID='$FEXID'\n" if $opt_v;
4989 $ENV{FEXID} = $FEXID;
4990 }
4991 } else {
4992 die "$prg: no FEXID\n";
4993 }
4994
4995 if ($opt_x) {
4996 my $xi;
4997 # "USER=\$(id|sed 's/).*//;s/.*(//')",
4998 $xi .= "$_\n" foreach (
4999 'FEXBIN=/tmp/$USER/.fex/tmp;',
5000 'mkdir -p $FEXBIN && cd $FEXBIN &&',
5001 'wget -qO - fex.belwue.de/download/xxx.tgz | tar xzf -;',
5002 'test "$UID" = 0 && chown -h root:root $FEXBIN/*;',
5003 'PATH="$FEXBIN:$PATH";',
5004 "export FEXID='$FEXID';",
5005 'ls -l'
5006 );
5007 my $fx = "| $fexsend -k 1 -s xxx.bash .";
5008 warn "$fx\n" if $opt_v;
5009 if ($opt_X and open $fx,$fx) {
5010 warn $xi if $opt_v;
5011 print {$fx} $xi;
5012 close $fx;
5013 } else {
5014 print "# copy these bash commands to your other account to temporary\n";
5015 print "# install the F*EX clients (including xxx) with your F*EX ID\n";
5016 $xi =~ s/;\n/\n/g;
5017 $xi =~ s/(FEXID=.*)/$1;history -d \$((HISTCMD-1))/;
5018 print $xi;
5019 }
5020 exit;
5021 }
5022
5023 if ($opt_u) {
5024 $_ = vsystem("$fexsend -~ GENUKEY|");
5025 if ($? == 0 and /FUP=(http.+)/) {
5026 my $fup = $1;
5027 print "# to get (one day valid) upload function, copy&paste:\n";
5028 print "eval \$(curl -s $fup)\n";
5029 print if $opt_v and /\n/;
5030 }
5031 exit $?;
5032 }
5033
5034 if ($opt_l or $opt_L) {
5035 my @xxx = qw'
5036 which xxx || xxx() {
5037 case $1 in
5038 *.tgz) wget -qO - $1|tar xvzf -;;
5039 *.tar) wget -qO - $1|tar xvf -;;
5040 *.gz) wget -qO - $1|gunzip;;
5041 *.txt) wget -qO - $1;;
5042 esac;
5043 }
5044 ';
5045 warn "\$ $fexsend -L .|\n" if $opt_v;
5046 open my $p,"$fexsend -L .|" or exit 1;
5047 while (<$p>) {
5048 if (my ($size,$keep,$url,$n,$container,$comment) =
5049 m:(\d+ MB) \(\s*(\d+ d)\) (http.*/xxx_($xn)\.(\S+)) "(.+)":)
5050 {
5051 if (not $opt_w and @xxx) {
5052 print "@xxx\n";
5053 @xxx = ();
5054 }
5055 print "\n$comment ($size, $keep)\n";
5056 if ($opt_w) {
5057 # print "xxx $n\n";
5058 print "xxx=$url\n";
5059 if ($container eq 'txt') {
5060 print "wget -qO- \$xxx\n";
5061 } elsif ($container eq 'gz') {
5062 print "wget -qO- \$xxx | gunzip\n";
5063 } elsif ($container eq 'tar') {
5064 print "wget -qO- \$xxx | tar xvf -\n";
5065 } elsif ($container eq 'tgz' or $container eq 'tar.gz') {
5066 print "wget -qO- \$xxx | tar xvzf -\n";
5067 } elsif ($container eq 'zip') {
5068 my $zip = "xxx_$n.zip";
5069 print "wget -c \$xxx && unzip $zip && rm $zip\n";
5070 } else {
5071 print "wget -c \$xxx\n";
5072 }
5073 } else {
5074 print "xxx $url\n";
5075 }
5076 }
5077 }
5078 exit;
5079 }
5080
5081 if ($opt_k and "@ARGV" =~ /($xn)/) {
5082 my $x = $1;
5083 my $n = 0;
5084 warn "\$ $fexsend -l|\n" if $opt_v;
5085 open my $p,"$fexsend -l|" or exit 1;
5086 while (<$p>) {
5087 if (/#(\d+).* xxx_($x)/) {
5088 $n = $1;
5089 }
5090 }
5091 close $p;
5092 if ($n) {
5093 vexec($fexsend,'-x',$n,'-k',$opt_k);
5094 exit $?;
5095 } else {
5096 die "$prg: archive not found\n";
5097 }
5098 }
5099
5100 # get or delete named archive
5101 if ("@ARGV" =~ /^($xn|-)$|^(http.*xxx_$xn\S+)/) {
5102 my $n = $1||'';
5103 my $url = $2;
5104 my $fpl;
5105 unless ($url) {
5106 warn "\$ $fexsend -L .|\n" if $opt_v;
5107 open my $p,"$fexsend -L .|" or exit 1;
5108 while (<$p>) {
5109 $url = $1 if m:(http\S+/xxx_$n\.\S+):;
5110 $fpl = $_ if m:(http\S+/xxx_$xn\.\S+):;
5111 }
5112 close $p;
5113
5114 if ($n eq '-' and $fpl =~ m:(\d+ MB).*?(http\S+/xxx_$xn\.\S+) (".+"):) {
5115 $url = $2;
5116 }
5117 }
5118
5119 if ($url) {
5120 if ($opt_d) {
5121 vexec($fexget[0],'-d',$url);
5122 } else {
5123 if ($url =~ /xxx_$xn\.txt$/) {
5124 vexec("@fexget -s- $url");
5125 } elsif ($url =~ /xxx_$xn\.gz$/) {
5126 vexec("@fexget -s- $url | gunzip");
5127 } else {
5128 if (-t STDOUT) {
5129 push @fexget,'-o' if $n eq '-';
5130 } else {
5131 push @fexget,'-s-';
5132 }
5133 vexec(@fexget,$url);
5134 }
5135 }
5136 exit $?;
5137 } else {
5138 die "$prg: no such archive\n";
5139 }
5140 }
5141
5142 # delete archive with regexp
5143 if ($opt_d and "@ARGV" =~ /(.+)/) {
5144 my $x = $1;
5145 my (%url,%comment);
5146 warn "\$ $fexsend -L .|\n" if $opt_v;
5147 open my $p,"$fexsend -L .|" or exit 1;
5148 while (<$p>) {
5149 if (/(http\S+\/xxx_($xn)\.\w+) "(.*$x.*)"/) {
5150 $url{$2} = $1;
5151 $comment{$2} = $3;
5152 }
5153 }
5154 close $p;
5155 if (%url) {
5156 foreach (sort keys %url) {
5157 if (/(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)/) {
5158 print "$1-$2-$3 $4:$5:$6 $comment{$_}\n";
5159 }
5160 }
5161 print "delete? ";
5162 for (;;) {
5163 my $k = ReadKey(0);
5164 if ($k eq 'n') {
5165 print "no\n";
5166 exit;
5167 }
5168 if ($k eq 'y') {
5169 print "yes\n";
5170 last;
5171 }
5172 }
5173 foreach my $url (sort keys %url) {
5174 print "\n";
5175 vsystem($fexget[0],'-d',$url{$url});
5176 sleep 1;
5177 }
5178 }
5179 exit;
5180 }
5181
5182 if ($opt_g or $opt_d) {
5183 die $usage if @ARGV or not -t STDIN;
5184 my $long = 0;
5185 my $showurl = 0;
5186 my $dl = "\r".(" "x60)."\r";
5187
5188 for (;;) {
5189 my $n = 0;
5190 my $i = 0;
5191 my $k = '';
5192 my $last = '';
5193 my $input = '';
5194 my %fp = ();
5195 my %dkey = ();
5196 my %list = ();
5197 my @xxx = ();
5198
5199 warn "\$ $fexsend -L .|\n" if $opt_v;
5200 open my $p,"$fexsend -L .|" or exit 1;
5201 while (<$p>) {
5202 chomp;
5203 if (m:http\S+/xxx_$xn:) {
5204 s/ //;
5205 s/\(//;
5206 s/\)//;
5207 s/\"//g;
5208 push @xxx,$_;
5209 }
5210 }
5211 close $p;
5212
5213 $i = $n = scalar(@xxx) or die "$prg: no stored archives found\n";
5214
5215 print "\n";
5216 foreach (@xxx) {
5217 if (s:((http\S+)/fop/(\w+)/xxx_(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)\.(\w+)) ::) {
5218 $fp{$i} = $1;
5219 $dkey{$i} = $3;
5220 $list{$i} = $_ = sprintf "%3s $4-$5-$6 $7:$8:$8 %s\n","#$i",$_;
5221 s/\d\d\d\d-.*?\[/\[/ unless $long;
5222 print;
5223 printf " %s\n",$fp{$i} if $showurl;
5224 $i--;
5225 }
5226 }
5227
5228 for (;;) {
5229 print $dl;
5230 print "get" if $opt_g;
5231 print "delete" if $opt_d;
5232 print "list" if $opt_l;
5233 print "keep" if $opt_k;
5234 if ($input) {
5235 print " #$input";
5236 } else {
5237 print " # (or enter h for help): ";
5238 }
5239 last if $last;
5240 $k = ReadKey(0);
5241 if ($k eq "\n") {
5242 $input ||= 'q' if $first;
5243 last if $input;
5244 redo;
5245 }
5246 $first = 0;
5247 unless ($input) {
5248 # first input character
5249 if ($k eq ' ') {
5250 $input = $k;
5251 last;
5252 }
5253 if ($k eq 'q') {
5254 print "q\n";
5255 exit;
5256 }
5257 if ($k eq "h" or $k eq "?") {
5258 print $dl;
5259 if ($opt_g) {
5260 print "#- get archive # and overwrite existing files\n";
5261 print "[v] toggle view format\n";
5262 print "[u] toggle show URL\n";
5263 print "[d] delete\n";
5264 print "[k] keep\n";
5265 print "[l] list content\n";
5266 }
5267 if ($opt_d) {
5268 print "#-# delete archives from # to #\n";
5269 print "[v] toggle view format\n";
5270 print "[u] toggle show URL\n";
5271 print "[g] get\n";
5272 print "[k] keep\n";
5273 print "[l] list content\n";
5274 }
5275 if ($opt_k) {
5276 print "# set new keep time for #\n";
5277 print "[v] toggle view format\n";
5278 print "[u] toggle show URL\n";
5279 print "[g] get\n";
5280 print "[d] delete\n";
5281 print "[l] list content\n";
5282 }
5283 if ($opt_l) {
5284 print "[v] toggle view format\n";
5285 print "[u] toggle show URL\n";
5286 print "[g] get\n";
5287 print "[d] delete\n";
5288 print "[k] keep\n";
5289 }
5290 print "[q] quit\n";
5291 print "[SPACE] reload overview\n";
5292 redo;
5293 }
5294 if ($opt_g and $k eq "-") {
5295 $last = $input = 1;
5296 push @fexget,'-o';
5297 redo;
5298 }
5299 if ($k eq "v" or $k eq "u") {
5300 # print "\r",' 'x40,"\n";
5301 print $clear;
5302 $long = not $long if $k eq "v";
5303 $showurl = not $showurl if $k eq "u";
5304 for (my $i=$n;$i;$i--) {
5305 $_ = $list{$i};
5306 s/\d\d\d\d-.*?\[/\[/ unless $long;
5307 print;
5308 printf " %s\n",$fp{$i} if $showurl;
5309 }
5310 redo;
5311 }
5312 if ($k eq "d") {
5313 $opt_d = 1;
5314 $opt_g = 0;
5315 $opt_l = 0;
5316 $opt_k = 0;
5317 redo;
5318 }
5319 if ($k eq "g") {
5320 $opt_g = 1;
5321 $opt_d = 0;
5322 $opt_l = 0;
5323 $opt_k = 0;
5324 redo;
5325 }
5326 if ($k eq "l") {
5327 $opt_l = 1;
5328 $opt_d = 0;
5329 $opt_g = 0;
5330 $opt_k = 0;
5331 redo;
5332 }
5333 if ($k eq "k") {
5334 $opt_k = 1;
5335 $opt_d = 0;
5336 $opt_g = 0;
5337 $opt_l = 0;
5338 $long = 1;
5339 $input = ' ';
5340 last;
5341 }
5342 }
5343 if ($opt_g and $k eq "-") {
5344 push @fexget,'-o';
5345 $input ||= 1;
5346 $last = $input;
5347 redo;
5348 }
5349 if (ord($k) == 4 or ord($k) == 127) {
5350 $input =~ s/.$//;
5351 } elsif ($k =~ /\d/) {
5352 $input .= $k;
5353 } elsif ($opt_d and $input =~ /^\d+$/) {
5354 $input .= $k if $k eq ' ';
5355 $input .= $k if $k eq '-';
5356 }
5357 }
5358 if ($input eq ' ') {
5359 print $clear;
5360 redo;
5361 }
5362 if ($input eq '') {
5363 print "\r",' 'x72,"\r";
5364 exit;
5365 }
5366
5367 print "\n";
5368 exit if $input !~ /\d/;
5369
5370 $input =~ s/-$//;
5371 if ($input =~ s/^(\d+)-(\d+)$//) {
5372 for ($1..$2) {
5373 $input .= $_.' ';
5374 }
5375 }
5376
5377 foreach my $i (split(' ',$input)) {
5378 print "\n";
5379 if ($i =~ /^\d+$/) {
5380 if (my $durl = $fp{$i}) {
5381 $_ = $list{$i};
5382 s/ *#\d+ //;
5383 s/ +( \d+ MB)/$1/;
5384 print;
5385 if ($opt_g) {
5386 my $pp;
5387 if ($durl =~ /\d+\.gz$/) {
5388 my $pipe = inputline("pipe data to: ");
5389 $pipe = '| '.$pipe if $pipe and $pipe !~ /^>/;
5390 vexec("@fexget -s- $durl | gunzip $pipe");
5391 } elsif ($durl =~ /\d+\.txt$/) {
5392 vexec("@fexget -s- $durl");
5393 } else {
5394 close STDIN;
5395 if (($windoof or $cygwin) and getcwd() =~ /Downloads$/) {
5396 system(@fexget,$durl);
5397 system qw'explorer .';
5398 exit;
5399 } else {
5400 vexec(@fexget,$durl);
5401 }
5402 }
5403 exit 1;
5404 }
5405 if ($opt_d) {
5406 vsystem($fexsend,'-d',basename($durl),'.');
5407 sleep 1;
5408 }
5409 if ($opt_k) {
5410 my $keep = inputline("keep days: ");
5411 # update fexlist
5412 vsystem("$fexsend -l >/dev/null");
5413 vsystem($fexsend,'-x',$dkey{$i},'-k',$keep);
5414 sleep 1;
5415 }
5416 if ($opt_l) {
5417 if ($durl =~ /\.(gz|txt)$/) {
5418 print "\nis not an archive, but pipe data\n";
5419 } else {
5420 vsystem($fexget[0],'-t',$durl);
5421 sleep 1;
5422 }
5423 }
5424 } else {
5425 print "#$i does not exist\n" if $i;
5426 }
5427 }
5428 }
5429 print "\nhit [SPACE] to continue, [q] to quit";
5430 $k = ReadKey(0);
5431 if ($k eq "q") {
5432 print "\r",' 'x72,"\r";
5433 exit;
5434 }
5435 print $clear;
5436 }
5437 }
5438
5439 # upload
5440 my @d = localtime time;
5441 my $fp = sprintf('xxx_%d%02d%02d_%02d%02d%02d',
5442 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
5443 my $comment = sprintf('[%s@%s]',scalar(getpwuid($<)),hostname());
5444
5445 $opt_k ||= 1;
5446 $fexsend .= " -q" if $opt_q;
5447
5448 unless (-t STDIN) {
5449 $fexsend .= " -k $opt_k";
5450 $comment .= ' -';
5451 if (@ARGV) {
5452 $comment .= " # @ARGV";
5453 } elsif (0 and my $pipe = readlink '/proc/self/fd/0') {
5454 # search for pipe STDIN process
5455 # does not work reliable, because STDIN process may has already terminated
5456 foreach my $fd1 (glob '/proc/*/fd/1') {
5457 if ((readlink($fd1)||'') eq $pipe and $fd1 =~ m:(/proc/\d+):) {
5458 my $cmdline = "$1/cmdline";
5459 if (open $cmdline,$cmdline) {
5460 local $/;
5461 $_ = <$cmdline>;
5462 close $cmdline;
5463 s/\000$//;
5464 s/\000/ /g;
5465 $comment .= " # $_";
5466 }
5467 }
5468 }
5469 }
5470 $comment = shellquote($comment);
5471 if ($opt_t) {
5472 vsystem("cat|$fexsend -M -C $comment -s $fp.txt .");
5473 } else {
5474 vsystem("gzip|$fexsend -C $comment -s $fp.gz .");
5475 }
5476 $fp =~ s/_/ /;
5477 # print "$fp\n" if $? == 0;
5478 exit $?;
5479 }
5480
5481 my @fexsend = ($fexsend);
5482 push @fexsend,'-0' if $opt_0;
5483 push @fexsend,('-k',$opt_k) if $opt_k;
5484 push @fexsend,('-m',$opt_m) if $opt_m;
5485
5486 my $ao = '-a';
5487 grep { /^\..|\/\./ and $ao = '-A' } @ARGV;
5488
5489 map { s:/+$:/: } @ARGV;
5490 my @files = @ARGV;
5491
5492 if ("@ARGV" eq '.') {
5493 die "$prg: . is empty\n" unless glob '*';
5494 $comment .= ' '.abs_path("@ARGV").'/';
5495 } else {
5496 foreach (@ARGV) {
5497 $comment .= " $_";
5498 $comment =~ s:/*$:/: if -d and not -l;
5499 }
5500 if (scalar(@ARGV) == 1 and not $opt_p) {
5501 my $a = $ARGV[0];
5502 die "$prg: cannot read $a\n" unless -r $a;
5503 if ($a !~ /\/$/) {
5504 $a = abs_path($a);
5505 die "$prg: cannot send /\n" if $a eq '/';
5506 if ($a =~ m:(.+)/(.+):) {
5507 chdir $1 or die "$prg: cannot cd $1 - $!\n";
5508 warn "\$ cd $1\n" if $opt_v;
5509 @files = ($2);
5510 } else {
5511 chdir '/';
5512 warn "\$ cd /\n" if $opt_v;
5513 @files = ($a);
5514 }
5515 $comment =~ s/ .*/ @files/;
5516 }
5517 }
5518 }
5519
5520 my $container = 'taz';
5521 if ($opt_Z or ($windoof or $cygwin) and not $opt_T) {
5522 $container = 'zip';
5523 } elsif ($opt_z) {
5524 $container = 'tgz';
5525 } elsif ($opt_0) {
5526 $container = 'tar';
5527 }
5528 vsystem(@fexsend,'-C',$comment,$ao,"$fp.$container",@files,'.');
5529 if ($ENV{RUNCMD}) {
5530 exec 'xxx';
5531 }
5532 exit $?;
5533
5534 }
5535
5536 ### common functions ###
5537
5538 sub mtime {
5539 my @d = localtime((stat shift)[9]);
5540 return sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]);
5541 }
5542
5543
5544 sub urldecode {
5545 local $_ = shift;
5546 s/\%([a-f\d]{2})/chr(hex($1))/ige;
5547 return $_;
5548 }
5549
5550
5551 sub serverconnect {
5552 my ($server,$port) = @_;
5553 my $connect = "CONNECT $server:$port HTTP/1.1";
5554 local $_;
5555
5556 if ($proxy) {
5557 tcpconnect(split(':',$proxy));
5558 if ($port == 443) {
5559 nvtsend($connect,"");
5560 $_ = <$SH>;
5561 s/\r//;
5562 warn "<-- $_"if $opt_v;
5563 unless (/^HTTP.1.. 200/) {
5564 die "$prg: proxy error : $_";
5565 }
5566 &enable_ssl;
5567 $SH = IO::Socket::SSL->start_SSL($SH,%SSL);
5568 }
5569 } else {
5570 tcpconnect($server,$port);
5571 }
5572 }
5573
5574
5575 # set up tcp/ip connection
5576 sub tcpconnect {
5577 my ($server,$port) = @_;
5578
5579 if ($SH) {
5580 close $SH;
5581 undef $SH;
5582 }
5583
5584 if ($port == 443) {
5585 # eval "use IO::Socket::SSL qw(debug3)";
5586 &enable_ssl;
5587 $SH = IO::Socket::SSL->new(
5588 PeerAddr => $server,
5589 PeerPort => $port,
5590 Proto => 'tcp',
5591 %SSL
5592 );
5593 } else {
5594 $SH = IO::Socket::INET->new(
5595 PeerAddr => $server,
5596 PeerPort => $port,
5597 Proto => 'tcp',
5598 );
5599 }
5600
5601 die "$prg: $@\n" if $@;
5602
5603 if ($SH) {
5604 autoflush $SH 1;
5605 binmode $SH;
5606 } else {
5607 die "$prg: cannot connect $server:$port - $@\n";
5608 }
5609
5610 warn "TCPCONNECT to $server:$port\n" if $opt_v;
5611 }
5612
5613
5614 sub sendheader {
5615 my $sp = shift;
5616 my @head = @_;
5617 my $head;
5618
5619 push @head,"Host: $sp";
5620 push @head,"User-Agent: $useragent";
5621
5622 foreach $head (@head) {
5623 chomp $head;
5624 warn "--> $head\n" if $opt_v;
5625 print {$SH} $head,"\r\n";
5626 }
5627 warn "-->\n" if $opt_v;
5628 print {$SH} "\r\n";
5629 }
5630
5631
5632 sub nvtsend {
5633 local $SIG{PIPE} = sub { $sigpipe = "@_" };
5634
5635 $sigpipe = '';
5636
5637 die "$prg: internal error: no active network handle\n" unless $SH;
5638 die "$prg: remote host has closed the link\n" unless $SH->connected;
5639
5640 foreach my $line (@_) {
5641 warn "--> $line\n" if $opt_v;
5642 print {$SH} $line,"\r\n";
5643 if ($sigpipe) {
5644 undef $SH;
5645 return 0;
5646 }
5647 }
5648
5649 return 1;
5650 }
5651
5652
5653 sub shellquote {
5654 local $_ = shift;
5655 s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
5656 return $_;
5657 }
5658
5659
5660 sub debug {
5661 print "## DEBUG: @_\n" if $FEXOPT =~ /\bDEBUG\b/i;
5662 }
5663
5664
5665 sub locale {
5666 my $string = shift;
5667
5668 # my @x = Encode->encodings(':all'); die "$CTYPE - @x";
5669 if ($CTYPE) {
5670 if ($CTYPE =~ /UTF-?8/i) {
5671 return $string;
5672 } elsif (grep { $CTYPE =~ /^$_$/i } Encode->encodings(':all')) {
5673 Encode::from_to($string,'UTF8',$CTYPE,Encode::FB_WARN);
5674 # return encode($CTYPE,decode('UTF8',$string,Encode::FB_WARN),Encode::FB_WARN);
5675 } else {
5676 Encode::from_to($string,'UTF8','ISO-8859-1',Encode::FB_WARN);
5677 # return encode('ISO-8859-1',decode('UTF8',$string));
5678 }
5679 return $string;
5680 }
5681
5682 return $string;
5683 }
5684
5685
5686 sub enable_ssl {
5687 local $_;
5688 eval "use IO::Socket::SSL";
5689 die "$prg: cannot load IO::Socket::SSL\n" if $@;
5690 # needed for CentOS (Redhat, too?)
5691 foreach (qw'/etc/ssl/cert.pem /etc/ssl/certs/ca-bundle.crt') {
5692 $SSL{SSL_ca_file} = $_ if -f;
5693 }
5694 &get_ssl_env;
5695 eval '$SSL{SSL_verify_mode} = 0 if Net::SSLeay::SSLeay() <= 9470143';
5696 if ($opt_v) {
5697 foreach my $v (keys %SSL) {
5698 printf STDERR "%s => %s\n",$v,$SSL{$v};
5699 }
5700 }
5701 }
5702
5703
5704 sub get_ssl_env {
5705 # set SSL/TLS options
5706 if (defined($ENV{SSLVERIFY})) {
5707 $SSL{SSL_verify_mode} = $ENV{SSLVERIFY};
5708 }
5709 foreach my $opt (qw(
5710 SSL_version
5711 SSL_cipher_list
5712 SSL_verify_mode
5713 SSL_ca_path
5714 SSL_ca_file)
5715 ) {
5716 my $env = uc($opt);
5717 $env =~ s/_//g;
5718 $SSL{$opt} = $ENV{$env} if defined($ENV{$env});
5719 }
5720
5721 if ($SSL{SSL_verify_mode} and $SSL{SSL_verify_mode} ne 'SSL_VERIFY_NONE') {
5722 &search_ca;
5723 unless ($SSL{SSL_ca_path} or $SSL{SSL_ca_file}) {
5724 die "$prg: \$SSLVERIFYMODE, but not valid \$SSLCAPATH or \$SSLCAFILE\n";
5725 }
5726 } elsif (defined($SSL{SSL_verify_mode})) {
5727 # user has set SSLVERIFY=0 !
5728 } else {
5729 &search_ca;
5730 $SSL{SSL_verify_mode} = 1 if $SSL{SSL_ca_path} or $SSL{SSL_ca_file};
5731 }
5732 }
5733
5734
5735 sub search_ca {
5736 local $_;
5737
5738 return if $SSL{SSL_ca_file} or $SSL{SSL_ca_path};
5739
5740 foreach (qw(/etc/ssl/certs/ca-certificates.crt)) {
5741 if (-f) {
5742 $SSL{SSL_ca_file} = $_;
5743 return;
5744 }
5745 }
5746
5747 foreach (qw(/etc/ssl/certs /etc/pki/tls/certs)) {
5748 if (-d) {
5749 $SSL{SSL_ca_path} = $_;
5750 return;
5751 }
5752 }
5753 }
5754
5755
5756 # from MIME::Base64::Perl
5757 sub encode_b64 {
5758 my $res = "";
5759 my $eol = "\n";
5760 my $padding;
5761
5762 pos($_[0]) = 0;
5763 $res = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
5764 $res =~ tr|\` -_|AA-Za-z0-9+/|;
5765 $padding = (3-length($_[0])%3)%3;
5766 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
5767 return $res;
5768 }
5769
5770
5771 # from MIME::Base64::Perl
5772 sub decode_b64 {
5773 local $_ = shift;
5774 my $uu = '';
5775 my ($i,$l);
5776
5777 tr|A-Za-z0-9+=/||cd;
5778 s/=+$//;
5779 tr|A-Za-z0-9+/| -_|;
5780 return "" unless length;
5781
5782 $l = (length)-60;
5783 for ($i = 0; $i <= $l; $i += 60) {
5784 $uu .= "M" . substr($_,$i,60);
5785 }
5786 $_ = substr($_,$i);
5787 if (length) {
5788 $uu .= chr(32+(length)*3/4) . $_;
5789 }
5790 return unpack("u",$uu);
5791 }
5792
5793
5794 sub inputline {
5795 my $prompt = shift;
5796 my $default = shift||'';
5797 my $term = new Term::ReadLine $prg;
5798
5799 $term->ornaments(0) unless $ENV{PERL_RL};
5800 return $term->readline($prompt,$default)||'';
5801 }
5802
5803
5804 sub vexec {
5805 vsystem(@_);
5806 exit $?;
5807 }
5808
5809
5810 sub vsystem {
5811 my @cmd = @_;
5812 my $cmd;
5813 my $shellmeta = '[\\\'"`*?&|<>(){};]';
5814 local $_;
5815
5816 if ($opt_v) {
5817 if (-t STDIN) { print STDERR '$ ' }
5818 else { print STDERR '| ' }
5819 if (scalar(@cmd) == 1) {
5820 warn "@cmd\n";
5821 } else {
5822 my @w;
5823 foreach (@cmd) {
5824 if (/\'/) {
5825 push @w,shellquote($_);
5826 } elsif (/[^\w\/:=~^%@,.+-]/) {
5827 push @w,"'$_'";
5828 } else {
5829 push @w,$_;
5830 }
5831 }
5832 warn "@w\n";
5833 }
5834 }
5835
5836 $_ = "@cmd";
5837 if (scalar(@cmd) == 1 and /\s/ and not (/^\w+=/ or /$shellmeta/)) {
5838 @cmd = split;
5839 }
5840
5841 if (scalar(@cmd) == 1) {
5842 $cmd = "@cmd";
5843 $cmd =~ s/\|&$/ 2>&1|/;
5844 if ($cmd =~ s/\|$//) {
5845 my @a = `$cmd`;
5846 if (wantarray) {
5847 map { chomp } @a;
5848 return @a;
5849 } else {
5850 chomp $a[0] if scalar(@a) == 1;
5851 return join('',@a);
5852 }
5853 } else {
5854 system $cmd;
5855 }
5856 } else {
5857 system @cmd;
5858 }
5859 }