3 # CLI client for the F*EX service (send, list, delete)
7 # Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
9 # Perl Artistic Licence
12 use strict qw'vars subs';
21 use Fcntl qw':flock :mode';
22 use Digest::MD5 qw'md5_hex'; # encrypted ID / SID
23 use Time::HiRes qw'time';
24 # use Smart::Comments;
25 use constant k => 2**10;
26 use constant M => 2**20;
28 eval 'use Net::INET6Glue::INET_is_INET6';
30 &update if "@ARGV" eq 'UPDATE';
34 our ($SH,$fexhome,$idf,$tmpdir,$windoof,$macos,$useragent,$editor,$nomail);
35 our ($anonymous,$public);
36 our ($tpid,$frecipient);
37 our ($FEXID,$FEXXX,$HOME);
40 our $version = 20160104;
42 our $DEBUG = $ENV{DEBUG};
44 my %SSL = (SSL_version => 'TLSv1');
47 if ($Config{osname} =~ /^mswin/i) {
48 $windoof = $Config{osname};
49 $HOME = $ENV{USERPROFILE};
50 $fexhome = $ENV{FEXHOME} || $HOME.'\fex';
51 $tmpdir = $ENV{FEXTMP} || $ENV{TEMP} || "$fexhome\\tmp";
52 $idf = "$fexhome\\id";
53 $editor = $ENV{EDITOR} || 'notepad.exe';
54 $useragent = sprintf("fexsend-$version (%s %s)",
55 $Config{osname},$Config{archname});
56 $SSL{SSL_verify_mode} = 0;
57 } elsif ($Config{osname} =~ /^darwin/i or $ENV{MACOS}) {
58 $macos = $Config{osname};
59 # http://stackoverflow.com/questions/989349/running-a-command-in-a-new-mac-os-x-terminal-window
60 $HOME = (getpwuid($<))[7]||$ENV{HOME};
61 $fexhome = $HOME.'/.fex';
62 $tmpdir = $ENV{FEXTMP} || $ENV{TMPDIR} || "$fexhome/tmp";
66 $editor = $ENV{EDITOR} || 'open -W -n -e';
67 $_ = `sw_vers -productVersion 2>/dev/null`||'';
69 $useragent = "fexsend-$version (MacOS $_)";
72 $HOME = (getpwuid($<))[7]||$ENV{HOME};
73 $fexhome = $HOME.'/.fex';
74 $tmpdir = $ENV{FEXTMP} || "$fexhome/tmp";
77 $editor = $ENV{EDITOR} || 'vi';
78 $_ = `(lsb_release -d||uname -a)2>/dev/null`||'';
81 $useragent = "fexsend-$version ($_)";
84 if (-f ($_ = '/etc/fex/config.pl')) {
85 eval { require } or warn $@;
93 my $atype = ''; # archive type
94 my $fexcgi; # F*EX CGI URL
95 my @files; # files to send
96 my %AB = (); # server based address book
97 my ($server,$port,$sid,$https);
99 my $proxy_prefix = '';
101 my $timeout = 30; # server timeout
102 my $fexlist = "$tmpdir/fexlist";
104 my $xx = $0 =~ /\bxx$/;
107 $usage = "usage: send file(s): xx [:slot] file...\n".
108 " or: send STDIN: xx [:slot] -\n".
109 " or: send pipe: ... | xx [:slot] \n".
110 " or: get file(s) or STDIN: xx [:slot] \n".
111 " or: get file(s) no-questions: xx [:slot] --\n".
112 "examples: dmesg | xx\n".
115 " xx :conf /etc /boot\n";
118 usage: $0 [options] file(s) [@] recipient(s)
119 or: $0 [special options]
120 or: $0 -l [recipient-regexp]
121 or: $0 -f \# recipient(s)
122 or: $0 -x \# [-C -k -D -K -S]
123 options: -v verbose mode
124 -d delete file on fex server
125 -c compress file with gzip
126 -g encrypt file with gpg
127 -m limit limit throughput (kB/s)
128 -i account use ID data [account] from ID file
129 -C comment add comment to notification e-mail
130 -k max keep file max days on fex server
131 -D delay auto-delete after download
132 -K no auto-delete after download
133 -M MIME-file (to be displayed in recipient\'s webbrowser)
134 -o overwrite mode, do not resume
135 -a archive put files in archive (.zip .7z .tar .tgz)
136 -s stream read data from pipe and upload it with stream name
137 special options: -I initialize ID file or show ID
138 -I account add alternate ID data (secondary logins) to ID file
139 -l list sent files numbers (# needed for -f -x -d -N)
140 -f \# forward already uploaded file to another recipient
141 -x \# use -C -k -D -K for already uploaded file
142 -d \# delete file on fex server
143 -N \# resend notification e-mail
145 -A edit server address book (aliases)
146 -S show server/user settings and auth-ID
147 -H show hints, examples and more options
149 (# is a file number, see output from $0 -l)
150 examples: $0 visualization.mpg framstag\@rus.uni-stuttgart.de
151 $0 -a images.zip *.jpg webmaster\@flupp.org,metoo
152 lshw | $0 -s hardware.list admin\@flupp.org
154 # or: $0 -R FEX-URL e-mail
155 # -R FEX mail self-register your e-mail address at FEX server
158 $0 hints and more options:
160 usage: $0 [options] file recipient(s)
162 Recipient can be a comma separated address list. Example:
163 $0 big.file framstag\@rus.uni-stuttgart.de,webmaster\@flupp.org
165 Recipient can be an alias from your server address book
166 (use "$0 -A" to edit it). Example:
169 Recipient can be a SKEY URL, which you have received from a regular F*EX user.
170 When using this URL you are a subuser of this full user and the file will be
171 sent to him. Example:
172 $0 big.file http://fex.rus.uni-stuttgart.de/fup?skey=4285f8cdd881626524fba686d5f0a83a
174 Recipient can be a GKEY URL, which you have received from a regular F*EX user.
175 Using this URL you are a member of his group and the file will be sent to all
176 members of this group. Example:
177 $0 big.file http://fex.rus.uni-stuttgart.de/fup?gkey=50d26547b1e8c1110beb8748fc1d9444
179 When you use "FEX-URL/anonymous" as recipient and your F*EX administrator has
180 allowed anonymous upload for your IP address then no auth-ID is needed.
182 "." as recipient means fex to yourself and show immediately the download URL
183 (no notification e-mail will be sent). Example:
186 "//" as recipient means fex to yourself and create extra short download URL.
190 If you want a Bcc of the notification e-mail then add '!bcc!' to the comment:
191 fexsend -C '!bcc! for me and you' ...
193 Additional special options:
195 -. sends a short instead of a detailed notification e-mail
196 -/ does not upload the file, but tells the server to link it
197 -= uses an alias name as file name
198 -# excludes files (# is list separator) from archive -a
199 -n sends no notification e-mail, but shows the download URL immediately
201 -r ADDRESS sets e-mail Reply-To ADDRESS
202 -F activates female mode
203 -U show authorized URL
204 -+ is an undocumented feature - test it :-)
206 To manage your subuser and groups or forward or redirect files, use a
207 webbrowser with the URL from "$0 -U", e.g.: firefox \$($0 -U)
209 If you want to copy-forward an already uploaded file to another recipient,
210 then you first have to query the file number with:
212 and then copy-forward it with:
213 $0 -b # other\@address
214 Where # is the file number.
216 You can list an uploaded file in more detail with
218 Where # is the file number.
220 If you want to modify the keep time, comment or auto-delete behaviour of an
221 already uploaded file then you first have to query the file number with:
223 and then for example set the keep time to 30 days with:
225 Where # is the file number.
227 With option -a you can send several files or whole directories within a single
228 archive file. The archive types tar and tgz are build on-the-fly (streaming)
229 whereas archive types zip and 7z need a temporary archive file on local disk.
231 With option -s you can send any data coming from a pipe (STDIN) as a file
232 without wasting local disc space.
234 With option -X you can specify any URL parameter, e.g.:
235 fexsend -X autodelete=yes ...
236 fexsend -X 'autodelete=no&locale=german' ...
238 For HTTPS you can set the environment variables:
239 SSLVERIFY=1 # activate server identity verification
240 SSLVERSION=TLSv1 # this is the default
241 SSLCAPATH=/etc/ssl/certs # path to trusted (root) certificates
242 SSLCAFILE=/etc/ssl/cert.pem # file with trusted (root) certificates
243 SSLCIPHERLIST=HIGH:!3DES # see http://www.openssl.org/docs/apps/ciphers.html
245 Partner program xx is an internet clipboard. See: xx -h
247 Partner program fexget is for downloading. See: fexget -h
249 For temporary usage of a HTTP proxy use:
250 $0 -P your_proxy:port:chunksize_in_MB file recipient
252 $0 -P wwwproxy.uni-stuttgart.de.de:8080:1024 4GB.tar .
254 For temporary usage of an alternative F*EX server or user use:
255 FEXID="FEXSERVER USER AUTHID" $0 file recipient
257 FEXID="fex.flupp.org gaga\@flupp.org blubb" $0 big.file framstag\@rus.uni-stuttgart.de
259 You can define aliases (and optional fexsend options) in \$HOME/.fex/config.pl:
261 'alias1' => 'user1\@domain1.org',
262 'alias2' => 'user2\@domain2.org',
263 'both' => 'user1\@domain1.org,user2\@domain2.org',
264 'extra' => 'extra\@special.net:-i other -K -k 30',
267 fexsend also respects aliases in $HOME/.mutt/aliases
268 The alias priority is (descending):
269 \$HOME/.fex/config.pl
271 fexserver address book
273 In \$HOME/.fex/config.pl you can also set the SSL* environment variables and the
274 \$opt_* variables, e.g.:
276 \$ENV{SSLVERSION} = 'TLSv1';
296 if ($windoof and not @ARGV and not $ENV{PROMPT}) {
297 # restart with cmd.exe to have mouse cut+paste
298 exec qw'cmd /k',$0,'-W';
302 unless (-d $fexhome) {
303 mkdir $fexhome,0700 or die "$0: cannot create FEXHOME $fexhome - $!\n";
306 unless (-d $tmpdir) {
307 mkdir $tmpdir,0700 or die "$0: cannot create tmpdir $tmpdir - $!\n";
310 my @_ARGV = @ARGV; # save arguments
312 our ($opt_q,$opt_h,$opt_H,$opt_v,$opt_m,$opt_c,$opt_k,$opt_d,$opt_l,$opt_I,
313 $opt_K,$opt_D,$opt_u,$opt_f,$opt_a,$opt_C,$opt_R,$opt_M,$opt_L,$opt_Q,
314 $opt_A,$opt_i,$opt_z,$opt_Z,$opt_b,$opt_P,$opt_x,$opt_X,$opt_V,$opt_U,
315 $opt_s,$opt_o,$opt_g,$opt_F,$opt_n,$opt_r,$opt_S,$opt_N);
318 $opt_q = 1 if @ARGV and $ARGV[-1] eq '--' and pop @ARGV or not -t STDOUT;
319 $opt_h = $opt_v = $opt_m = $opt_I = 0;
321 $_ = "$fexhome/config.pl"; require if -f;
322 getopts('hvIm:') or die $usage;
324 if ($macos and not @ARGV) {
327 $opt_h = $opt_v = $opt_m = $opt_c = $opt_k = $opt_d = $opt_l = $opt_I = 0;
328 $opt_H = $opt_K = $opt_D = $opt_R = $opt_M = $opt_L = $opt_Q = $opt_A = 0;
329 $opt_x = $opt_o = $opt_g = $opt_V = $opt_U = $opt_F = $opt_n = $opt_q = 0;
331 ${'opt_@'} = ${'opt_!'} = ${'opt_+'} = ${'opt_.'} = ${'opt_/'} = 0;
332 ${'opt_='} = ${'opt_#'} = '';
333 $opt_u = $opt_f = $opt_a = $opt_C = $opt_i = $opt_b = $opt_P = $opt_X = '';
334 $opt_s = $opt_r = '';
335 $_ = "$fexhome/config.pl"; require if -f;
336 getopts('hHvcdognVDKlILUARWMFzZqQS@!+./r:m:k:u:f:a:s:C:i:b:P:x:X:N:=:#:')
345 print "Version: $version\n";
348 if ($opt_K and $opt_D) {
349 die "$0: you cannot use both options -D and -K\n";
352 if ($opt_a and $opt_c) {
353 die "$0: you cannot use both options -a and -c\n";
356 if ($opt_a and $opt_s) {
357 die "$0: you cannot use both options -a and -s\n";
360 if ($opt_g and $opt_c) {
365 if ($opt_f and $opt_f !~ /^\d+$/) {
366 die "$0: option -f needs a number, see $0 -l\n";
369 if ($opt_I and $opt_R) {
370 die "$0: you cannot use both options -I and -R\n";
373 # $opt_C is COMMENT command in F*EX protocol
376 ($opt_l or $opt_L) ? 'LIST':
377 ($opt_Q) ? 'CHECKQUOTA':
378 ($opt_S) ? 'LISTSETTINGS':
379 ($opt_Z) ? 'RECEIVEDLOG':
380 ($opt_z) ? 'SENDLOG':
381 (${'opt_!'}) ? 'FOPLOG':
393 female_mode("show help?") if $opt_F;
405 die $usage if $opt_m and $opt_m !~ /^\d+/;
408 if ($opt_P =~ /^([\w.-]+:\d+)(:(\d+))?/) {
410 $chunksize = $3 || 0;
412 die "$0: proxy must be: SERVER:PORT\n";
416 if ($FEXID = $ENV{FEXID}) {
417 $FEXID = decode_b64($FEXID) if $FEXID !~ /\s/;
418 ($fexcgi,$from,$id) = split(/\s+/,$FEXID);
420 if ($windoof and not -f $idf) { &init_id }
421 if (open $idf,$idf) {
428 # convert old idxx file
429 if ($idf and open $idf,$idf.'xx') {
432 if (open $idf,'>>',$idf) {
433 print {$idf} "\n[xx]\n",
443 if ($FEXXX = $ENV{FEXXX}) {
444 $FEXXX = decode_b64($FEXXX) if $FEXXX !~ /\s/;
445 ($fexcgi,$from,$id) = split(/\s+/,$FEXXX);
446 } elsif (open $idf,$idf) {
449 $proxy = $proxy_prefix = '';
461 $proxy = $proxy_prefix = '';
462 open $idf,$idf or die "$0: cannot open $idf - $!\n";
470 die "$0: no [$opt_i] in $idf\n" unless $_;
475 if ($xx) { &show_id }
480 if (@ARGV > 1 and $ARGV[-1] =~ /(^|\/)anonymous/) {
481 $fexcgi = $1 if $ARGV[-1] =~ s:(.+)/::;
482 die "usage: $0 [options] file FEXSERVER/anonymous\n" unless $fexcgi;
483 $anonymous = $from = 'anonymous';
484 $sid = $id = 'ANONYMOUS';
485 } elsif (@ARGV > 1 and $id eq 'PUBLIC') {
486 $public = $sid = $id;
487 } elsif (@ARGV > 1 and $ARGV[-1] =~ m{^(https?://[\w.-]+(:\d+)?/fup\?[sg]key=\w+)}) {
489 $skey = $1 if $fexcgi =~ /skey=(\w+)/;
490 $gkey = $1 if $fexcgi =~ /gkey=(\w+)/;
493 $fexcgi = $opt_u if $opt_u;
495 if (not -e $idf and not ($fexcgi and $from and $id)) {
496 die "$0: no ID file $idf found, use \"fexsend -I\" to create it\n";
500 die "$0: no FEX URL found, use \"$0 -u URL\" or \"$0 -I\"\n";
503 unless ($from and $id) {
504 die "$0: no sender found, use \"$0 -f FROM:ID\" or \"$0 -I\"\n";
507 if ($fexcgi !~ /^http/) {
508 if ($fexcgi =~ /:443/) { $fexcgi = "https://$fexcgi" }
509 else { $fexcgi = "http://$fexcgi" }
517 $port = 443 if $server =~ s{https://}{};
518 $port = $1 if $server =~ s/:(\d+)//;
521 # $opt_s and die "$0: cannot use -s with https due to stunnel bug\n";
522 # $opt_g and die "$0: cannot use -g with https due to stunnel bug\n";
526 $server =~ s{http://}{};
529 # $chunksize = 4*k unless $chunksize;
533 if ($port == 80) { $proxy_prefix = "http://$server" }
534 elsif ($port != 443) { $proxy_prefix = "http://$server:$port" }
537 # xx: special file exchange between own accounts
539 my $transferfile = "$tmpdir/STDFEX";
542 $transferfile = "$tmpdir/xx:xxx";
543 } elsif (@ARGV and $ARGV[0] =~ /^:([\w.=+-]+)$/) {
544 $transferfile = "$tmpdir/xx:$1";
547 open my $lock,'>>',$transferfile
548 or die "$0: cannot write $transferfile - $!\n";
549 flock($lock,LOCK_EX|LOCK_NB)
550 or die "$0: $transferfile is locked by another process\n";
551 truncate $transferfile,0;
552 if (not @ARGV and -t) {
553 &get_xx($transferfile);
555 &send_xx($transferfile);
562 &inquire if $windoof and not @ARGV and not
563 ($opt_l or $opt_L or $opt_Q or $opt_A or $opt_U or $opt_I or
564 $opt_f or $opt_x or $opt_N);
567 $opt_C = "!SHORTMAIL! $opt_C";
570 if ($opt_n or $opt_C =~ /NOMAIL|!#!/) {
574 unless ($skey or $gkey or $anonymous) {
576 $opt_f||$opt_x||$opt_Q||$opt_l||$opt_L||$opt_U||$opt_z||$opt_Z||$opt_A
577 ||$opt_d||${'opt_!'}||${'opt_@'})
578 ) { warn "Server/User: $fexcgi/$from\n" }
581 if ($opt_V and not @ARGV) { exit }
582 if ($opt_f) { &forward }
583 elsif ($opt_x) { &modify }
584 elsif ($opt_N) { &renotify }
585 elsif ($opt_Q) { &query_quotas }
586 elsif ($opt_S) { &query_settings }
587 elsif ($opt_l or $opt_L) { &list }
588 elsif ($opt_U) { &show_URL }
589 elsif ($opt_z or $opt_Z or ${'opt_!'}) { &get_log }
590 elsif ($opt_A) { edit_address_book($from) }
591 elsif (${'opt_@'}) { &show_address_book }
592 elsif ($opt_d and $anonymous) { &purge }
593 elsif ($opt_d and $ARGV[-1] =~ /^\d+$/) { &delete_file_number }
599 # initialize ID file or show ID
609 $fexcgi = $from = $id = '';
611 unless (-d $fexhome) {
612 mkdir $fexhome,0700 or die "$0: cannot create FEXHOME $fexhome - $!\n";
616 if (not $tag and open $idf,$idf) {
619 last if /^\[$opt_i\]/;
627 chomp($fexcgi,$from,$id);
628 $FEXID = encode_b64("$fexcgi $from $id");
630 print "# hint: to edit the ID file $idf use \"$0 -I .\" #\n";
631 print "export FEXID=$FEXID\n";
632 print "history -d \$((HISTCMD-1));history -d \$((HISTCMD-1))\n";
634 print "FEXID=$FEXID\n";
638 die "$0: no ID data found\n";
642 if ($tag and $tag eq '.') { exec $ENV{EDITOR}||'vi',$idf }
644 if ($tag) { print "F*EX server URL for [$tag]: " }
645 else { print "F*EX server URL: " }
647 $fexcgi =~ s/[\s\n]//g;
648 die "you MUST provide a FEX-URL!\n" unless $fexcgi;
649 if ($fexcgi =~ /\?/) {
650 $from = $1 if $fexcgi =~ /\bfrom=(.+?)(&|$)/i;
651 $id = $1 if $fexcgi =~ /\bid=(.+?)(&|$)/i;
652 # $skey = $1 if $fexcgi =~ /\bskey=(.+?)(&|$)/i;
653 # $gkey = $1 if $fexcgi =~ /\bgkey=(.+?)(&|$)/i;
654 die "$0: cannot use GKEY URL in ID file\n" if $fexcgi =~ /gkey=/i;
655 die "$0: cannot use SKEY URL in ID file\n" if $fexcgi =~ /skey=/i;
658 unless ($fexcgi =~ /^[_:=\w\-\.\/\@\%]+$/) {
659 die "\"$fexcgi\" is not a legal FEX-URL!\n";
661 $fexcgi =~ s:/fup/*$::;
662 print "proxy address (hostname:port or empty if none): ";
664 $proxy =~ s/[\s\n]//g;
665 if ($proxy =~ /^[\w.-]+:\d+$/) {
667 } elsif ($proxy =~ /\S/) {
668 die "wrong proxy address format\n";
673 print "proxy POST limit in MB (use 2048 if unknown): ";
683 $from = 'GROUPMEMBER';
687 print "Your e-mail address as registered at $fexcgi: ";
689 $from =~ s/[\s\n]//g;
690 die "you MUST provide your e-mail address!\n" unless $from;
692 unless ($from =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
693 die "\"$from\" is not a legal e-mail address!\n";
696 print "Your auth-ID for $from at $fexcgi: ";
699 die "you MUST provide your ID!\n" unless $id;
702 if (open $idf,'>>',$idf) {
703 print {$idf} "\n[$tag]\n" if $tag and -s $idf;
704 print {$idf} "$fexcgi$proxy\n",
708 print "data written to $idf\n";
710 die "$0: cannot write to $idf - $!\n";
716 my ($fexcgi,$from,$id);
717 if (open $idf,$idf) {
719 # $fexcgi = <$idf> if $fexcgi =~ /^\[.+\]/;
730 die "$0: too few data in $idf" unless defined $id;
734 $FEXXX = encode_b64("$fexcgi $from $id");
736 print "export FEXXX=$FEXXX\n";
737 print "history -d \$((HISTCMD-1));history -d \$((HISTCMD-1))\n";
739 print "FEXXX=$FEXXX\n";
742 die "$0: cannot read $idf - $!\n";
748 my $fs = shift @ARGV or die $usage;
749 my $mail = shift @ARGV or die $usage;
751 my ($server,$user,$id);
753 die "$0: $idf does already exist\n" if -e $idf;
755 if ($fs =~ /^https/) {
756 die "$0: cannot handle https at this time\n";
759 $fs =~ s{^http://}{};
761 if ($fs =~ s/:(\d+)//) { $port = $1 }
764 tcpconnect($fs,$port);
765 sendheader("$fs:$port","GET $proxy_prefix/fur?user=$mail&verify=no HTTP/1.1");
771 printf "<-- $_"if $opt_v;
777 printf "<-- $_"if $opt_v;
778 if (m{http://(.*)/fup\?from=(.+)&ID=(.+)}) {
783 if (open F,">$idf") {
789 print "user data written to $idf\n";
790 print "you can now fex!\n";
793 die "$0: cannot write to $idf - $!\n";
798 die "$0: no account data received from F*EX server\n";
803 # menu for MacOS users
811 print "fexsend-$version\n";
814 if (open $idf,$idf) {
815 $fexcgi = getline($idf) and
816 $from = getline($idf) and
825 print "$from on $fexcgi\n";
830 print "[s] send a file or directory\n";
831 print "[u] update fexsend\n";
832 print "[l] change login data (user, server, auth-ID)\n";
836 print "your choice: ";
841 print "Type [Cmd]W to close this window.\n";
848 "With fexsend you can send files of any size to any e-mail address.\n".
850 "At the recipient or file prompt [RETURN] brings you to this option menu.\n".
852 "To send more than one file:\n".
853 "When you enter * at the file prompt, you will be first asked for an archive name\n".
854 "and then you can drag+drop multiple files.\n".
856 "Do not forget to terminate each input line with [RETURN].\n".
858 "See http://fex.rus.uni-stuttgart.de/ for more informations.\n";
863 if ($0 =~ m:(^/client/|/sw/):) {
865 print "use swupdate to update fexsend!\n";
869 system "curl http://fex.belwue.de/download/fexsend>".quote($new);
871 system qw'perl -c',$new;
877 print "cannot install new fexsend\n";
887 if ($key eq 's' or $key eq "\n") {
899 my ($file,$comment,$recipient,$archive,$size,$cmd,$key);
906 &set_ID unless -s $idf;
909 print "Enter [RETURN] after each input line.\n";
913 print "Recipient(s): ";
914 $recipient = <STDIN>;
916 $recipient =~ s/^\s+//;
917 $recipient =~ s/\s+$//;
918 $recipient =~ s/[\s;,]+/,/g;
919 &menu unless $recipient;
920 last if $recipient =~ /\w/ or $recipient eq '.';
925 print "Drag a file into this window or hit [RETURN] ";
926 print $archive ? "to continue.\n" : "for menu options.\n";
927 print "File to send: ";
931 $file =~ s/ $// if $file !~ /\\ $/;
932 &menu unless $file or $archive;
934 print "Archive name: ";
935 $archive = <STDIN>||'';
937 next unless $archive;
938 $archive =~ s/^\s+//g;
939 $archive =~ s/\s+$//g;
940 $archive =~ s/[^\w=.+-]/_/g;
945 $file =~ s/\\\\/\000/g;
947 $file =~ s/\000/\\/g;
950 print "\"$file\" is not readable\n";
953 my $qf = quote($file);
954 if (`du -ms $qf` =~ /^(\d+)/) {
965 $qfiles = join(' ',map(quote($_),@files));
974 $comment = <STDIN>||'';
977 if ($comment =~ s/^:\s*-/-/) {
978 $cmd = quote($0)." $comment ";
980 $cmd .= '-a '.quote($archive).' '.$qfiles;
982 $cmd .= quote($file);
984 $cmd .= ' '.quote($recipient);
988 print quote($0)." -C '$comment' ";
990 printf "-a %s %s %s\n",quote($archive),$qfiles,$recipient;
991 system $0,'-C',$comment,'-a',$archive,@files,$recipient;
993 printf "%s %s\n",quote($file),$recipient;
994 system $0,'-C',$comment,$file,$recipient;
998 print "[s] send another file to $recipient\n";
999 print "[n] send another file to another recipient\n";
1002 print "your choice: ";
1005 &ask_file if $key eq 'n';
1006 if ($key eq 's' or $key eq "\n") {
1015 $file = $comment = $archive = '';
1022 my ($server,$port,$user,$logo);
1027 print "F*EX server URL: ";
1029 $server =~ s/[\s\n]//g;
1030 if ($server =~ s:/fup/(\w+)$::) {
1031 $_ = decode_b64($1);
1032 if (/(from|user)=(.+)&id=(.+)/) {
1037 $server =~ s:/fup.*::;
1039 next if $server !~ /\w/;
1040 if ($server =~ s/^https:..// or $server =~ /:443/) {
1043 eval "use IO::Socket::SSL";
1045 print "\nno perl SSL modules installed - cannot use https\n\n";
1048 $SH = IO::Socket::SSL->new(
1049 PeerAddr => $server,
1055 $server =~ s:^http.//::;
1056 if ($server =~ s/:(\d+)//) {
1061 $SH = IO::Socket::INET->new(
1062 PeerAddr => $server,
1068 print "\ncannot connect to $server:$port - $!\n\n";
1073 "GET /logo.jpg HTTP/1.0",
1074 "User-Agent: $useragent",
1075 "Connection: close",
1078 unless (/HTTP.1.1 200/) {
1079 print "\nbad server reply: $_\n";
1082 while (<$SH>) { last if /^\s*$/ }
1086 if (length $logo < 9999) {
1087 print "\n$server is not a F*EX server!\n\n";
1090 open $logo,">$tmpdir/fex.jpg";
1091 print {$logo} $logo;
1098 print "Your login (e-mail address): ";
1100 $user =~ s/[\s\n]//g;
1101 if ($user !~ /.@[\w.-]+$/) {
1102 print "\"$user\" is not a valid e-mail address!\n";
1109 print "Your auth-ID for this account: ";
1114 open $idf,'>',$idf or die "$0: cannot write to $idf - $!\n";
1115 print {$idf} "$server\n",
1120 print "Login data written to $idf\n\n";
1121 print "fexing test file to $user:\n\n";
1122 system "$0 -o -M -C test $tmpdir/fex.jpg $user";
1125 print "fexsend failed, login data is invalid, try again\n";
1128 print "fexsend test succeeded!\n";
1134 # read one key from terminal in raw mode
1137 local $SIG{INT} = sub { stty('reset'); exit };
1140 # loop necessary for ESXi support
1141 while (not defined $key) {
1150 if (shift eq 'raw') {
1151 system qw'stty -echo -icanon eol',"\001";
1153 system qw'stty echo icanon eol',"\000";
1159 my $transferfile = shift;
1163 $SIG{PIPE} = $SIG{INT} = sub {
1164 unlink $transferfile;
1168 if ($0 eq 'xxx') { @tar = qw'tar -cv' }
1169 else { @tar = qw'tar -cvz' }
1172 if ("@ARGV" eq '-') {
1173 # store STDIN to transfer file
1174 shelldo("cat >> $transferfile");
1176 print "making tar transfer file $transferfile :\n";
1177 # single file? then add this directly
1178 if (scalar @ARGV == 1) {
1179 # strip path if not ending with /
1180 if ($ARGV[0] =~ m:(.+)/(.+): and $2 !~ m:/$:) {
1181 ($dir,$file) = ($1,$2);
1182 chdir $dir or die "$0: $dir - $!\n";
1187 shelldo(@tar,qw'--dereference -f',$transferfile,$file);
1189 shelldo(@tar,'-f',$transferfile,$file);
1192 shelldo(@tar,'-f',$transferfile,@ARGV);
1195 unlink $transferfile;
1197 die "$0: interrupted making tar transfer file\n";
1199 die "$0: error while making tar transfer file\n";
1204 # write input from pipe to transfer file
1205 shelldo("cat >> $transferfile");
1208 die "$0: no transfer file\n" unless -s $transferfile;
1210 serverconnect($server,$port);
1211 query_sid($server,$port);
1217 file => $transferfile,
1218 comment => 'NOMAIL',
1219 autodelete => $transferfile =~ /STDFEX/ ? 'NO' : 'DELAY',
1222 # open P,'|w3m -T text/html -dump' or die "$0: w3m - $!\n";
1225 if ($transferfile =~ /:/ and $0 ne 'xxx') {
1226 if ("@r" =~ /\s(X-)?Location: (http.*)\s/) {
1227 print "wget -O- $2 | tar xvzf -\n";
1231 unlink $transferfile;
1239 female_mode("query quotas?") if $opt_F;
1247 die "$0: no response from fex server $server\n" unless @r;
1249 unless (/^HTTP.* 2/) {
1251 die "$0: server response: $_\n";
1253 if (($_) = grep(/^X-Sender-Quota/,@r) and /(\d+)\s+(\d+)/) {
1254 print "sender quota (used): $1 ($2) MB\n";
1256 print "sender quota: unlimited\n";
1258 if (($_) = grep(/^X-Recipient-Quota/,@r) and /(\d+)\s+(\d+)/) {
1259 print "recipient quota (used): $1 ($2) MB\n";
1261 print "recipient quota: unlimited\n";
1266 sub query_settings {
1270 female_mode("query settings?") if $opt_F;
1273 print "ID data from \$FEXID\n";
1275 print "ID data from $idf\n";
1277 die "$0: found no ID\n";
1279 print "server: $fexcgi\n";
1280 print "user: $from\n";
1281 print "auth-ID: $id\n";
1282 print "login URL: ";
1291 die "$0: no response from fex server $server\n" unless @r;
1293 unless (/^HTTP.* 2/) {
1295 die "$0: server response: $_\n";
1297 if (($_) = grep(/^X-Autodelete/,@r) and /:\s+(\w+)/) {
1298 print "autodelete: $1\n";
1300 if (($_) = grep(/^X-Default-Keep/,@r) and /(\d+)/) {
1301 print "default keep: $1 days\n";
1303 if (($_) = grep(/^X-Default-Locale/,@r) and /:\s+(\w+)/) {
1304 print "default locale: $1\n";
1306 if (($_) = grep(/^X-MIME/,@r) and /:\s+(\w+)/) {
1307 print "display file with browser: $1\n";
1309 if (($_) = grep(/^X-Sender-Quota/,@r) and /(\d+)\s+(\d+)/) {
1310 print "sender quota (used): $1 ($2) MB\n";
1312 print "sender quota: unlimited\n";
1314 if (($_) = grep(/^X-Recipient-Quota/,@r) and /(\d+)\s+(\d+)/) {
1315 print "recipient quota (used): $1 ($2) MB\n";
1317 print "recipient quota: unlimited\n";
1328 my $a = shift @ARGV || '.';
1331 female_mode("list spooled files?") if $opt_F;
1334 if ($a =~ /^\d+$/) {
1335 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
1336 while (<$fexlist>) {
1337 if (/^\s*(\d+)\) (\w+) (.+)/ and $1 eq $a) {
1338 serverconnect($server,$port) unless $SH;
1341 "GET $proxy_prefix/fop/$2/$2?LIST HTTP/1.1",
1342 "User-Agent: $useragent",
1346 print "<-- $_" if $opt_v;
1347 if (/^HTTP.* 200/) {
1348 print "<-- $_" if $opt_v;
1356 } elsif (s:HTTP/[\d\. ]+::) {
1357 die "$0: server response: $_";
1359 die "$0: no response from fex server $server\n";
1364 die "$0: file \#$a not found in fexlist\n";
1370 to => $opt_l ? '*' : $from,
1373 die "$0: no response from fex server $server\n" unless @r;
1375 unless (/^HTTP.* 200/) {
1377 die "$0: server response: $_\n";
1382 open $fexlist,">$fexlist" or die "$0: cannot write $fexlist - $!\n";
1384 next unless /<pre>/ or $data;
1387 if (/<a href=".*dkey=(\w+).*?">/) { $dkey = $1 }
1389 # $_ = encode_utf8($_);
1394 if (/^(to (.+) :)/) {
1396 print "\n$_\n" if $s;
1397 print {$fexlist} "\n$_\n";
1398 } elsif (m/(\d+) MB (.+)/) {
1400 printf "%4s) %8d MB %s\n","#$n",$1,$2 if $s;
1401 printf {$fexlist} "%3d) %s %s\n",$n,$dkey,$2;
1407 # list received files
1410 next unless /<pre>/ or $data;
1414 if (/(from .* :)/) {
1417 if (m{(\d+) (MB.*)<a href="(https?://.*/fop/\w+/.+)">(.+)</a>( ".*")?}) {
1418 printf "%8d %s%s%s\n",$1,$2,$3,($5||'');
1426 printf "%s/fup/%s\n",$fexcgi,encode_b64("from=$from&id=$id");
1440 die "$0: no response from fex server $server\n" unless @r;
1442 unless (/^HTTP.* 200/) {
1444 die "$0: server response: $_\n";
1447 foreach (@r) { print "$_\n" }
1451 sub show_address_book {
1456 %AB = query_address_book($server,$port,$from);
1457 foreach $alias (sort keys %AB) {
1458 next if $alias eq 'ADDRESS_BOOK';
1459 $_ = sprintf "%s = %s (%s) # %s\n",
1462 $AB{$alias}->{options},
1463 $AB{$alias}->{comment};
1472 die "$0: not yet implemented\n";
1476 sub delete_file_number {
1480 $opt_d = shift @ARGV;
1481 die "usage: $0 -d #\n" if $opt_d !~ /^\d+$/;
1483 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
1484 while (<$fexlist>) {
1485 if (/^to (.+\@.+) :/) {
1487 } elsif (/^\s*(\d+)\) (\w+) (.+)/ and $1 eq $opt_d) {
1488 serverconnect($server,$port) unless $SH;
1491 "GET $proxy_prefix/fop/$2/$2?DELETE HTTP/1.1",
1492 "User-Agent: $useragent",
1496 print "<-- $_" if $opt_v;
1497 if (/^HTTP.* 200/) {
1500 last if /^\n/; # ignore HTML output
1501 print "<-- $_" if $opt_v;
1502 if (/^X-File:.*\/(.+)/) {
1503 printf "%s deleted\n",decode_utf8(urldecode($1));
1507 } elsif (s:HTTP/[\d\. ]+::) {
1508 die "$0: server response: $_";
1510 die "$0: no response from fex server $server\n";
1516 sleep 1; # do not overrun server
1524 my ($from,$to,$file) = @_;
1528 serverconnect($server,$port);
1529 query_sid($server,$port) unless $anonymous;
1532 $file = urlencode($file);
1535 "GET $proxy_prefix/fop/$to/$from/$file?id=$sid&DELETE HTTP/1.1",
1536 "User-Agent: $useragent",
1541 printf "<-- $_"if $opt_v;
1549 s/([^_=:,;<>()+.\w\-])/'%'.uc(unpack("H2",$1))/ge;
1558 my ($data,$aname,$alias);
1565 if ($from =~ /^SUBUSER|GROUPMEMBER$/) {
1568 # look for single @ in arguments
1569 for (my $i=1; $i<$#ARGV; $i++) {
1570 if ($ARGV[$i] eq '@') {
1571 $ARGV[$i] = join(',',@ARGV[$i+1 .. $#ARGV]);
1576 $to = pop @ARGV or die $usage;
1579 $nomail = $opt_C ||= 'NOMAIL';
1583 $nomail = $opt_C ||= 'NOMAIL';
1585 if ($opt_g and $to =~ /,/) {
1586 die "$0: encryption is supported to only one recipient\n";
1588 if ($to =~ m{^https?://.*/fup\?skey=(\w+)}) {
1593 if ($to =~ m{^https?://.*/fup\?gkey=(\w+)}) {
1594 $from = 'GROUPMEMBER';
1599 @to = split(',',lc($to));
1601 die $usage unless @ARGV or $opt_a or $opt_s;
1602 die $usage if $opt_s and @ARGV;
1604 # early serverconnect necessary for X-Features info
1605 serverconnect($server,$port);
1609 sendheader("$server:$port","OPTIONS /FEX HTTP/1.1");
1612 die "$0: no response from fex server $server\n" unless $_;
1613 print "<-- $_" if $opt_v;
1614 if (/^HTTP.* 201/) {
1617 print "<-- $_" if $opt_v;
1619 $aok = $_ if /X-Features:.*ANONYMOUS/;
1621 die "$0: no anonymous support on server $server\n" unless $aok;
1623 die "$0: bad response from server $server : $_\n";
1628 query_sid($server,$port);
1630 if ($from eq 'SUBUSER') {
1632 # die "skey=$skey\nid=$id\nsid=$sid\n";
1635 if ($from eq 'GROUPMEMBER') {
1641 $opt_C ||= 'NOMAIL';
1642 } elsif ($to =~ m:^(//.*):) {
1644 if ($features =~ /XKEY/) {
1648 die "$0: server does not support XKEY\n";
1650 } elsif (grep /^[^@]*$/,@to and not $skey and not $gkey) {
1651 %AB = query_address_book($server,$port,$from);
1653 serverconnect($server,$port);
1654 query_sid($server,$port);
1657 # alias in local config?
1659 if ($alias{$to} =~ /(.+?):(.+)/) {
1664 # special extra upload
1665 system $0,split(/\s/,$opt),@argv,$ato;
1671 # alias in server address book?
1673 # do not substitute alias with expanded addresses because then
1674 # keep and autodelete options from address book will get lost
1677 # look for mutt aliases
1678 elsif ($to !~ /@/ and $to ne $from) {
1679 $to = get_mutt_alias($to);
1684 $to = join(',',grep /./,@to) or exit;
1685 # warn "Server/User: $fexcgi/$from\n" unless $opt_q;
1688 not $skey and not $gkey
1690 and $features =~ /CHECKRECIPIENT/
1691 and $opt_C !~ /^(DELETE|LIST|RECEIVEDLOG|SENDLOG|FOPLOG)$/
1693 checkrecipient($from,$to);
1695 serverconnect($server,$port);
1696 query_sid($server,$port);
1701 if (@ARGV > 1 and not ($opt_a or $opt_s or $opt_d)) {
1702 print "Archive name (name.tar, name.tgz or name.zip) or [RETURN] to send file for file:\n";
1709 if ($macos and not $opt_a and -d "@ARGV") {
1711 my $qdir = quote($dir);
1712 if (`du -s $qdir` =~ /^(\d+)/ and $1 < 2**21) {
1713 $opt_a = "$dir.zip";
1715 $opt_a = "$dir.tar";
1722 $opt_s =~ s/[^\w_.+-]/_/g;
1727 $opt_a =~ s/[^\w_.+-]/_/g;
1728 if ($opt_a =~ /(.+)\.(zip|tar|tgz|7z)$/) {
1732 die "$0: archive name must be one of ".
1733 "$opt_a.tar $opt_a.tgz $opt_a.zip\n";
1735 # no file argument left?
1737 # use file name as archive name
1742 foreach my $file (@ARGV) {
1743 die "$0: cannot read \"$file\"\n" unless -l $file or -r $file;
1745 $opt_a .= ".$atype" if $opt_a !~ /\.$atype$/;
1746 $transferfile = "$tmpdir/$opt_a";
1747 unlink $transferfile;
1748 print "Making fex archive ($opt_a):\n";
1749 if ($atype eq 'zip') {
1751 # if ($opt_c) { system(qw'7z a -tzip',$transferfile,@ARGV) }
1752 # else { system(qw'7z a -tzip -mm=copy',$transferfile,@ARGV) }
1753 system(qw'7z a -tzip',$transferfile,@ARGV);
1754 @files = ($transferfile);
1755 } elsif ($macos and scalar(@ARGV) == 1) {
1756 ## ditto-zip is now handled by formdatapost()
1760 # zip archives must be < 2 GB, so split as necessary
1761 @files = zipsplit($transferfile,@ARGV);
1762 if (scalar(@files) == 1) {
1763 $transferfile = $files[0];
1764 $transferfile =~ s/_1.zip$/.zip/;
1765 rename $files[0],$transferfile;
1766 @files = ($transferfile);
1769 @transferfiles = @files;
1770 } elsif ($atype eq '7z') {
1771 # http://www.7-zip.org/
1772 my @X = (); # exclude list
1774 foreach my $x (split('#',${'opt_#'})) {
1778 if ($opt_c) { system(qw'7z a',@X,$transferfile,@ARGV) }
1779 else { system(qw'7z a -t7z -mx0',@X,$transferfile,@ARGV) }
1780 @transferfiles = @files = ($transferfile);
1781 } elsif ($atype eq 'tar') {
1783 system(qw'7z a -ttar',$transferfile,@ARGV);
1784 @transferfiles = @files = ($transferfile);
1786 ## tar is now handled by formdatapost()
1787 # system(qw'tar cvf',$transferfile,@ARGV);
1791 } elsif ($atype eq 'tgz') {
1793 die "$0: archive type tgz not available, use tar, zip or 7z\n";
1795 ## tgz is now handled by formdatapost()
1796 # system(qw'tar cvzf',$transferfile,@ARGV);
1800 die "$0: unknown archive format \"$atype\"\n";
1803 if (@transferfiles) {
1805 # error in making transfer archive?
1807 unlink @transferfiles;
1808 die "$0: $! - aborting upload\n";
1811 # maybe timeout, so make new connect
1812 if (time-$t0 >= $timeout) {
1813 serverconnect($server,$port);
1814 query_sid($server,$port) unless $anonymous;
1834 die "$0: \"$file\" is not a regular file, try option -a\n"
1836 die "$0: \"$file\" does not exist\n";
1839 die "$0: cannot read \"$file\"\n" unless -r $file;
1846 foreach my $file (@files) {
1847 my @s = stat($file);
1848 unless (@s and ($s[2] & S_IROTH) and -r $file) {
1849 die "$0: \"$file\" is not world readable\n";
1854 foreach my $file (@files) {
1855 sleep 1; # do not overrun server!
1856 unless (-s $file or $opt_d or $opt_a or $opt_s) {
1857 die "$0: cannot send empty file \"$file\"\n";
1859 female_mode("send file $file?") if $opt_F;
1868 autodelete => $opt_D,
1871 if (not @r or not grep /\w/,@r) {
1872 die "$0: no response from server\n";
1874 next if "@r" eq '0'; # already transfered
1875 if (($r) = grep /^ERROR:/,@r) {
1876 if ($anonymous and $r =~ /purge it/) {
1877 die "$0: file is already on server for $to - use another anonymous recipent\n";
1878 } elsif ($r =~ /timeout/i) {
1884 die "$0: server error: $r\n";
1888 if (scalar(@r) == 1) {
1889 die "$0: server error: @r\n";
1891 if ($r[0] !~ /HTTP.1.. 2/) {
1892 if ($r[0] =~ /HTTP.[\s\d.]+(.+)/) {
1893 die "$0: server error: $1\n";
1895 die "$0: server error:\n".join("\n",@r)."\n";
1900 if (($r) = grep /<h3>\Q$file/,@r) {
1904 if ($opt_a !~ /^afex_\d+\.tar$/ and $file !~ /afex_\d+\.tar$/) {
1905 # print grep({s/^(X-Recipient:.*\((.+)\))/Parameters: $2\n/i} @r);
1910 if (/^(X-)?(Recipient.*)/i) {
1912 if (/notification=no/i) { $nonot = 1 }
1915 if (/^(X-)?(Location.*)/i) {
1919 if ($from eq $to or $from =~ /^\Q$to\E@/i
1920 or $nomail or $anonymous or $nonot)
1922 print "$recipient\n" if $recipient;
1923 print "$location\n" if $location;
1928 # delete transfer tmp file
1929 unlink $transferfile if $transferfile;
1935 my ($to,$n,$dkey,$file,$req);
1939 # look for single @ in arguments
1940 for (my $i=1; $i<$#ARGV; $i++) {
1941 if ($ARGV[$i] eq '@') {
1942 $ARGV[$i] = join(',',@ARGV[$i+1 .. $#ARGV]);
1948 # if ($windoof and not @ARGV) { &inquire }
1949 $to = pop @ARGV or die $usage;
1950 $to = $from if $to eq '.';
1951 if ($to !~ /@/ and $to ne $from) {
1952 $to = get_mutt_alias($to);
1955 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
1956 while (<$fexlist>) {
1957 if (/^\s*(\d+)\) (\w+) .\s*\d+ d. ([+-] )?(.+)/ and $1 eq $opt_f) {
1961 if ($file =~ s/ "(.*)"$//) {
1962 $opt_C ||= $1 if $1 ne 'NOMAIL';
1970 die "$0: file #$opt_f not found in fexlist\n";
1973 female_mode("forward file #$opt_f?") if $opt_F;
1975 serverconnect($server,$port);
1976 query_sid($server,$port);
1978 $req = "GET $proxy_prefix/fup?"
1979 ."from=$from&ID=$sid&to=$to&dkey=$dkey&command=FORWARD";
1980 $req .= "&comment=$opt_C" if $opt_C;
1981 $req .= "&keep=$opt_k" if $opt_k;
1982 $req .= "&autodelete=$opt_D" if $opt_D;
1983 $req .= "&$opt_X" if $opt_X;
1984 $req .= " HTTP/1.1";
1985 sendheader("$server:$port",$req);
1988 $fp =~ s/[^\w_.-]/.+/g; # because of UTF8 filename
1991 $status = 0 if /"$fp"/;
1992 print if $opt_v or /"$fp"/;
1996 die "$0: server failed, rerun command with option -v\n";
2004 my ($to,$n,$dkey,$file,$req,$recipient);
2007 die $usage if @ARGV;
2009 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
2010 while (<$fexlist>) {
2011 if (/^\s*(\d+)\) (\w+) .\s*\d+ d. (.+)/ and $1 eq $opt_N) {
2020 die "$0: file #$opt_N not found in fexlist\n";
2023 female_mode("resend notification for file #$opt_N?") if $opt_F;
2025 serverconnect($server,$port);
2026 query_sid($server,$port);
2028 $req = "GET $proxy_prefix/fup?"
2029 ."from=$from&ID=$sid&dkey=$dkey&command=RENOTIFY"
2031 sendheader("$server:$port",$req);
2035 print "<-- $_" if $opt_v;
2037 if (/^X-Notify: (.+)\/(.+)\/(.+)/) {
2044 print "notification e-mail for $file has been resent to $recipient\n";
2047 die "$0: server failed\n";
2049 die "$0: server failed, rerun command with option -v\n";
2059 my ($n,$dkey,$file,$req);
2062 die $usage if @ARGV;
2063 die $usage unless $opt_C or $opt_k or $opt_D;
2065 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
2066 while (<$fexlist>) {
2067 if (/^\s*(\d+)\) (\w+) .\s*\d+ d. (.+)/ and $1 eq $opt_x) {
2071 $file =~ s/ "(.*)"$//;
2078 die "$0: file #$opt_x not found in fexlist\n";
2081 female_mode("modify file #$opt_x?") if $opt_F;
2083 serverconnect($server,$port);
2084 query_sid($server,$port);
2086 $req = "GET $proxy_prefix/fup?"
2087 ."from=$from&ID=$sid&dkey=$dkey&command=MODIFY";
2088 $req .= "&comment=$opt_C" if $opt_C;
2089 $req .= "&keep=$opt_k" if $opt_k;
2090 $req .= "&autodelete=$opt_D" if $opt_D;
2091 $req .= " HTTP/1.1";
2092 sendheader("$server:$port",$req);
2107 my $transferfile = shift;
2111 # get transfer file from FEX server
2113 serverconnect($server,$port);
2114 query_sid($server,$port);
2117 xxget($from,$sid,$transferfile);
2120 unless (-s $transferfile) {
2121 unlink $transferfile;
2125 # no further processing if delivering to pipe
2126 exec 'cat',$transferfile unless -t STDOUT;
2128 if ($ft = `file $transferfile 2>/dev/null`) {
2129 if ($ft =~ /compressed/) {
2130 rename $transferfile,"$transferfile.gz";
2131 shelldo(ws("gunzip $transferfile.gz"));
2133 $ft = `file $transferfile`;
2135 # file command failed, so we look ourself into the file...
2136 elsif (open $transferfile,$transferfile) {
2137 read $transferfile,$_,4;
2138 close $transferfile;
2140 if (/\x1F\x8B\x08\x00/) {
2141 rename $transferfile,"$transferfile.gz";
2142 shelldo(ws("gunzip $transferfile.gz"));
2144 $ft = 'tar archive';
2147 if ($ft =~ /tar archive/) {
2148 rename $transferfile,"$transferfile.tar";
2149 $transferfile .= '.tar';
2153 print "Files in transfer-container:\n\n";
2154 shelldo(ws("tar tvf $transferfile"));
2155 print "\nExtract these files? [Yn] ";
2159 print "keeping $transferfile\n";
2161 my $untar = "tar xvf";
2162 # if ($> == 0 and `tar --help 2>&1` =~ /gnu/) {
2163 # $untar = "tar --no-same-owner -xvf";
2165 system("$untar $transferfile && rm $transferfile");
2166 die "$0: error while untaring, see $transferfile\n" if -f $transferfile;
2169 exec 'cat',$transferfile;
2177 my ($boundary,$filename,$length,$buf,$file,$fpsize,$resume,$seek);
2179 my (@hh,@hb,@r,@pv,$to);
2181 my ($t0,$t1,$t2,$tt,$tc);
2182 my $bs = 2**16; # blocksize for reading and sending file
2183 my $fileid = int(time);
2186 my $connection = '';
2189 my ($tar,$ditto,$aname,$atype,$list,$error,$location,$transferfile);
2192 if (defined($file = $P{file})) {
2194 $to = $AB{$P{to}} || $P{to}; # for gpg
2196 # special file: stream from STDIN
2198 $filename = encode_utf8($file);
2206 $if =~ s/([^_\w\.\-])/\\$1/g;
2207 $transferfile = $tmpdir . '/' . basename($file) . '.gz';
2208 $of = $transferfile;
2209 $of =~ s/([^_\w\.\-])/\\$1/g;
2210 shelldo("gzip <$if>$of");
2211 $filesize = -s $transferfile;
2212 die "$0: cannot gzip \"$file\"\n" unless $filesize;
2213 $file = $transferfile;
2216 # special file: tar-on-the-fly
2217 if (not $windoof and $opt_a and $file =~ /(.+)\.(tar|tgz)$/) {
2220 $list = "$tmpdir/$aname.list";
2221 $error = "$tmpdir/$aname.error";
2223 $tar .= 'z' if $atype eq 'tgz';
2224 if (`tar --help 2>/dev/null` =~ /--index-file/) {
2225 $tar .= " --index-file=$list -f-";
2230 foreach my $x (split('#',${'opt_#'})) {
2231 $tar .= " --exclude=$x";
2235 $tar .= ' '.quote($_);
2237 # print "calculating archive size... ";
2238 open $tar,"$tar 2>$error|" or die "$0: cannot run tar - $!\n";
2239 $t0 = int(time) if -t STDOUT;
2240 while ($b = read $tar,$_,$bs) {
2245 printf "Archive size: %d MB\r",int($filesize/M);
2250 printf "Archive size: %d MB\n",int($filesize/M) if -t STDOUT;
2251 unless (close $tar) {
2253 if (open $error,$error) {
2258 unlink $list,$error;
2259 die "$0: tar error:\n$_";
2261 $file = "$aname.$atype";
2262 $filename = encode_utf8($file);
2263 undef $SH; # force reconnect (timeout!)
2266 # special file: ditto-zip-on-the-fly
2267 # ditto: Can't archive multiple sources
2268 elsif ($macos and $opt_a and $file =~ /(.+)\.(zip)$/ and scalar(@ARGV) == 1) {
2271 $list = "$tmpdir/$aname.list";
2272 $error = "$tmpdir/$aname.error";
2273 $ditto = 'ditto -c -k --sequesterRsrc --keepParent';
2274 if (-d "@ARGV" and "@ARGV" =~ m:^(.+)/(.+):) {
2277 $file =~ s/([^\w\-\@\#%,.=+_:])/\\$1/g;
2278 $ditto .= ' '.$file;
2282 $file =~ s/([^\w\-\@\#%,.=+_:])/\\$1/g;
2283 $ditto .= ' '.$file;
2286 # print "calculating archive size... ";
2287 debug("cd $dittodir;$ditto -");
2288 open $ditto,"cd $dittodir;$ditto - 2>$error|"
2289 or die "$0: cannot run ditto - $!\n";
2290 $t0 = int(time) if -t STDOUT;
2291 while ($b = read $ditto,$_,$bs) {
2296 printf "Archive size: %d MB\r",int($filesize/M);
2301 printf "Archive size: %d MB\n",int($filesize/M) if -t STDOUT;
2302 unless (close $ditto) {
2304 if (-s $error and open $error,$error) {
2309 unlink $list,$error;
2310 die "$0: ditto-zip error:\n$_";
2312 unlink $list,$error;
2313 $file = "$aname.$atype";
2314 $filename = encode_utf8($file);
2315 undef $SH; # force reconnect (timeout!)
2320 $filename = encode_utf8(${'opt_='} || $file);
2323 $filename =~ s/^[a-z]://;
2324 $filename =~ s/.*\\//;
2326 $filename =~ s:.*/::;
2327 $filename =~ s:[\r\n]+: :g;
2330 } elsif (not $opt_g and not $opt_s) {
2331 $filesize = -s $file or die "$0: \"$file\" is empty or not readable\n";
2335 $filename .= '.gpg' if $opt_g;
2340 $fileid = int(time);
2343 $fileid = md5_hex(fmd(@ARGV));
2345 $fileid = fileid($file);
2351 $file = $filename = '';
2357 @hh = (); # HTTP header
2358 @hb = (); # HTTP body
2365 serverconnect($server,$port);
2366 query_sid($server,$port) unless $anonymous;
2369 $P{id} = $sid; # ugly hack!
2371 $filename =~ s/\\/_/g; # \ is a illegal character for fexsrv
2373 # ask server if this file has been already sent
2374 if ($file and not $xx) {
2375 if (not $opt_d and $opt_o) {
2376 # delete before overwrite
2377 delete_file($from,$to,$filename);
2378 serverconnect($server,$port);
2379 query_sid($server,$port) unless $anonymous;
2380 $P{id} = $sid; # ugly hack!
2381 } elsif (not($opt_s or $opt_g or $opt_d or $opt_l or $opt_L or ${'opt_/'})) {
2382 ($seek,$location) = query_file($server,$port,
2383 $frecipient||$P{to},$P{from},$P{id},$filename,$fileid);
2384 if ($filesize == $seek) {
2385 print "Location: $location\n" if $location and $nomail;
2386 warn "$0: $file has been already transferred\n";
2388 } elsif ($seek and $seek < $filesize) {
2389 $resume = " (resuming at byte $seek)";
2390 } elsif ($filesize <= $seek) {
2395 sleep 1; # do not overrun proxy
2396 serverconnect($server,$port);
2401 if ($chunksize and $proxy and $port != 443
2402 and $filesize - $seek > $chunksize - $bs) {
2403 if ($features !~ /MULTIPOST/) {
2404 die sprintf("$0: server does not support chunked multi-POST needed for"
2405 ." files > %d MB via proxy\n",$chunksize/M);
2407 $opt_o = 0; # no overwriting mode for next chunks
2408 $fpsize = $chunksize - $bs;
2410 $fpsize = $filesize - $seek;
2413 $boundary = randstring(48);
2416 $P{filesize} = $filesize;
2418 # send HTTP POST variables
2421 @pv = qw'from to skey keep autodelete comment seek filesize';
2424 @pv = qw'from to gkey keep autodelete comment seek filesize';
2426 @pv = qw'from to id replyto keep autodelete comment command seek filesize';
2428 foreach my $v (@pv) {
2431 push @hb,"--$boundary";
2432 push @hb,"Content-Disposition: form-data; name=\"$name\"";
2434 # push @hb,encode_utf8($P{$v});
2439 # at last, POST the file
2441 push @hb,"--$boundary";
2442 push @hb,"Content-Disposition: form-data; name=\"FILE\"; filename=\"$filename\"";
2444 if ($opt_M) { push @hb,"Content-Type: application/x-mime" }
2445 else { push @hb,"Content-Type: application/octet-stream" }
2447 $flink = abs_path($file);
2448 push @hb,"Content-Location: $flink";
2450 # push @hb,"Content-Length: " . ((-s $file||0) - $seek); # optional header!
2451 push @hb,"Content-Length: $fpsize"; # optional header! NOT filesize!
2452 push @hb,"X-File-ID: $fileid";
2457 # prevent proxy chunked mode reply
2458 $connection = "close";
2461 push @hb,"--$boundary--";
2466 $length = length(join('',@hb)) + scalar(@hb)*2 + $fpsize;
2469 if ($file and not $opt_d) {
2470 if ($flink) { $hb[-2] = $flink }
2471 else { $hb[-2] = '(file content)' }
2473 # any other extra URL arguments
2475 $opt_X = "?$::opt_X" if $::opt_X and $file;
2478 push @hh,"POST $proxy_prefix/fup$opt_X HTTP/1.1";
2479 push @hh,"Host: $server:$port";
2480 push @hh,"User-Agent: $useragent";
2481 push @hh,"Content-Length: $length";
2482 push @hh,"Content-Type: multipart/form-data; boundary=$boundary";
2483 push @hh,"Connection: $connection" if $connection;
2487 print "--> $_\n" foreach (@hh,@hb);
2490 $SIG{PIPE} = \&sigpipehandler;
2491 # foreach $sig (keys %SIG) {
2492 # eval '$SIG{$sig} = sub { print "\n!!! SIGNAL '.$sig.' !!!\n"; exit; }';
2497 pop @hb unless $flink;
2498 nvtsend(@hh,@hb) or do {
2499 warn "$0: server has closed the connection, reconnecting...\n";
2501 goto FORMDATAPOST; # necessary: new $sid ==> new @hh
2504 unless ($opt_d or $flink) {
2506 $t0 = $t2 = int(time);
2513 open $file,"gpg -e -r $to|" or die "$0: cannot run gpg - $!\n";
2515 open $file,'>&=STDIN' or die "$0: cannot open STDIN - $!\n";
2519 open $file,"$tar|gpg -e -r $to|" or die "$0: cannot run tar&gpg - $!\n";
2521 open $file,"$tar|" or die "$0: cannot run tar - $!\n";
2525 if (defined $tpid and $tpid == 0) {
2527 if (open $list,$list) {
2528 # print "\n$tar|\n"; system "ls -l $list";
2531 print ' 'x(length($file)+40),"\r",$_;
2538 $SIG{CHLD} = 'IGNORE';
2541 print "Fast forward to byte $seek (resuming)\n";
2542 readahead($file,$seek);
2545 $ditto =~ s/ditto/ditto -V/;
2546 open $file,"cd $dittodir;$ditto -|" or die "$0: cannot run ditto - $!\n";
2548 print "Fast forward to byte $seek (resuming)\n";
2549 readahead($file,$seek);
2553 my $fileq = quote($file);
2554 open $file,"gpg -e -r $to <$fileq|" or die "$0: cannot run gpg - $!\n";
2556 open $file,$file or die "$0: cannot read \"$file\" - $!\n";
2565 print $rcamel[0] if ${'opt_+'};
2567 $SIG{ALRM} = sub { retry("timed out") };
2568 while (my $b = read $file,$buf,$bs) {
2571 print {$SH} $buf or &sigpipehandler;
2573 syswrite $SH,$buf or &sigpipehandler;
2577 if ($filesize > 0 and $bytes+$seek > $filesize) {
2582 die "$0: \"$file\" filesize has grown while uploading\n";
2586 if (${'opt_+'} and int($t2*10)>$tc) {
2587 print $rcamel[$tc%2+1];
2590 if (not $opt_q and -t STDOUT and int($t2)>$t1) {
2591 &sigpipehandler unless $SH->connected;
2592 # smaller block size is better on slow links
2593 $bs = 4096 if $t1 and $bs>4096 and $bytes/($t2-$t0)<65536;
2594 if ($filesize > 0) {
2595 $pct = sprintf "(%d%%)",int(($bytes+$seek)/$filesize*100);
2597 if ($bytes>2*M and $bs>4096) {
2598 printf STDERR "%s: %d MB of %d MB %s %d kB/s \r",
2599 $opt_s||$opt_a||$file,
2600 int(($bytes+$seek)/M),
2603 int($bt/k/($t2-$tt));
2605 printf STDERR "%s: %d kB of %d MB %s %d kB/s \r",
2606 $opt_s||$opt_a||$file,
2607 int(($bytes+$seek)/k),
2610 int($bt/k/($t2-$tt));
2613 # time window for transfer rate calculation
2619 last if $filesize > 0 and $bytes >= $fpsize;
2620 sleep 1 while ($opt_m and $bytes/k/(time-$t0||1) > $opt_m);
2622 close $file; # or die "$0: error while reading $file - $!\n";
2625 print $rcamel[2] if ${'opt_+'};
2627 # terminate tar verbose output job
2634 if ($fileid =~ /[a-z]/ and not ($opt_s or $opt_g)) {
2636 if ($fileid ne md5_hex(fmd(@ARGV))) {
2637 print "\n" unless $opt_q;
2638 die "$0: files have been modified while uploading\n";
2641 if ($fileid ne fileid($file)) {
2642 print "\n" unless $opt_q;
2643 die "$0: file has been modified while uploading\n";
2649 if (not $chunksize and $bytes+$seek < $filesize) {
2650 die "$0: \"$file\" filesize has shrunk while uploading\n";
2653 if ($seek or $chunksize and $chunksize < $filesize) {
2655 printf STDERR "%s: %d MB in %d s (%d kB/s)",
2656 $opt_s||$opt_a||$file,
2660 if ($bytes+$seek == $filesize) {
2661 printf STDERR ", total %d MB\n",int($filesize/M);
2663 printf STDERR ", chunk #%d : %d MB\n",
2664 $chunk,int(($bytes+$seek)/M);
2667 printf STDERR "%s: %d kB in %d s (%d kB/s)",
2668 $opt_s||$opt_a||$file,
2672 if ($bytes+$seek == $filesize) {
2673 printf STDERR ", total %d kB\n",int($filesize/k);
2675 printf STDERR ", chunk #%d : %d kB\n",
2676 $chunk,int(($bytes+$seek)/k);
2681 printf STDERR "%s: %d MB in %d s (%d kB/s) \n",
2682 $opt_s||$opt_a||$file,
2687 printf STDERR "%s: %d kB in %d s (%d kB/s) \n",
2688 $opt_s||$opt_a||$file,
2695 if (-t STDOUT and not ($opt_s or $opt_g)) {
2696 print STDERR "waiting for server ok..."
2702 print {$SH} "\r\n--$boundary--\r\n";
2704 # special handling of streaming file because of stunnel tcp shutdown bug
2705 if ($opt_s or $opt_g) {
2708 serverconnect($server,$port);
2709 query_sid($server,$port) unless $anonymous;
2710 ($seek,$location) = query_file($server,$port,$P{to},$P{from},$sid,
2712 if ($seek != $bytes) {
2713 die "$0: streamed $bytes bytes but server received $seek bytes\n";
2715 return "X-Location: $location\n";
2721 printf STDERR "%s: %d MB\n",$flink,int($bytes/M);
2723 printf STDERR "%s: %d kB\n",$flink,int($bytes/k);
2731 # SuSe: Can't locate object method "BINMODE" via package "IO::Socket::SSL::SSL_HANDLE"
2732 # binmode $SH,':utf8';
2734 if (not $opt_q and $file and -t STDOUT) {
2735 print STDERR "\r \r";
2739 print "<-- $_\n" if $opt_v;
2740 last if @r and $r[0] =~ / 204 / and /^$/ or /<\/html>/i;
2741 push @r,decode_utf8($_);
2747 if ($proxy and $fpsize+$seek < $filesize) {
2758 my @rc = ('A'..'Z','a'..'z',0..9 );
2762 for (1..$n) { $rs .= $rc[int(rand($rn))] };
2768 my $zipbase = shift;
2772 my ($zsize,$size,$n);
2774 $zipbase =~ s/\.zip$//;
2775 map { s/([^_\w\+\-\.])/\\$1/g } @files;
2777 open my $ff,"find @files|" or die "$0: cannot search for @_ - $!\n";
2784 die "$0: too many zip-archives\n";
2787 while ($file = <$ff>) {
2789 # next if -l $file or not -f $file;
2790 next unless -f $file;
2792 if ($size > 2147480000) {
2794 die "$0: \"$file\" too big for zip\n";
2796 if ($zsize + $size > 2147000000) {
2797 push @zipfiles,zip($zipbase.'_'.$n.'.zip',@files);
2808 push @zipfiles,zip($zipbase.'_'.$n.'.zip',@files);
2820 # if ($opt_c) { $cmd = "zip -@ $zip" }
2821 # else { $cmd = "zip -0 -@ $zip" }
2822 $cmd = "zip -@ $zip";
2824 ${'opt_#'} =~ s/#/ /g;
2825 $cmd .= " -x ".${'opt_#'};
2827 print $cmd,"\n" if $opt_v;
2828 open $cmd,"|$cmd" or die "$0: cannot create $zip - $!\n";
2830 print {$cmd} $_."\n";
2831 print " $_\n" if $opt_v;
2833 close $cmd or die "$0: zip failed - $!\n";
2849 return $_ if length($_);
2856 my ($server,$port,$to,$from,$id,$filename,$fileid) = @_;
2859 my ($head,$location);
2860 my ($response,$fexsrv,$cc);
2865 $to = $AB{$to} if $AB{$to};
2866 $filename =~ s/([^_=:,;<>()+.\w\-])/'%'.uc(unpack("H2",$1))/ge; # urlencode
2868 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??SKEY=$id HTTP/1.1";
2870 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??GKEY=$id HTTP/1.1";
2872 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??ID=$id HTTP/1.1";
2874 sendheader("$server:$port",$head);
2876 unless (defined $_ and /\w/) {
2877 die "$0: no response from server\n";
2880 print "<-- $_" if $opt_v;
2881 unless (/^HTTP.* 200/) {
2886 print "<-- $_" if $opt_v;
2887 $fexsrv = $_ if /^(Server: fexsrv|X-Features:)/;
2890 die "$0: no fexserver at $server:$port\n" unless $fexsrv;
2891 die "$0: server response: $response";
2895 print "<-- $_" if $opt_v;
2897 if (/^Content-Length:\s+(\d+)/) { $seek = $1 }
2898 if (/^X-File-ID:\s+(.+)/) { $qfileid = $1 }
2899 if (/^X-Features:\s+(.+)/) { $features = $1 }
2900 if (/^X-Location:\s+(.+)/) { $location = $1 }
2901 if (/^Connection: close/) { $cc = $_ }
2904 # return true seek only if file is identified
2905 $seek = 0 if $qfileid and $qfileid ne $fileid;
2908 serverconnect($server,$port);
2912 return ($seek,$location);
2916 sub edit_address_book {
2919 my $ab = "$fexhome/ADDRESS_BOOK";
2923 die "$0: address book not available for subusers\n" if $skey;
2924 die "$0: address book not available for group members\n" if $gkey;
2926 female_mode("edit your address book?") if $opt_F;
2928 %AB = query_address_book($server,$port,$user);
2929 if ($AB{ADDRESS_BOOK} !~ /\w/) {
2931 "# Format: alias e-mail-address # Comment\n".
2933 "framstag framstag\@rus.uni-stuttgart.de\n";
2935 open $ab,">$ab" or die "$0: cannot write to $ab - $!\n";
2936 print {$ab} $AB{ADDRESS_BOOK};
2939 system "$editor $ab";
2944 serverconnect($server,$port);
2945 query_sid($server,$port);
2958 sub query_address_book {
2959 my ($server,$port,$user) = @_;
2960 my ($req,$alias,$address,$options,$comment,$cl,$ab,$b);
2965 serverconnect($server,$port);
2966 query_sid($server,$port);
2969 $req = "GET $proxy_prefix/fop/$user/$user/ADDRESS_BOOK?ID=$sid HTTP/1.1";
2970 sendheader("$server:$port",$req);
2972 unless (defined $_ and /\w/) {
2973 die "$0: no response from server\n";
2976 print "<-- $_" if $opt_v;
2977 unless (/^HTTP.* 200/) {
2978 if (/^HTTP.* 404/) {
2979 while (<$SH>) { last if /^\r?\n/ }
2982 # s:HTTP/[\d\. ]+::;
2983 # die "$0: server response: $_";
2991 print "<-- $_" if $opt_v;
2993 $cl = $1 if /^Content-Length: (\d+)/;
3003 print "<-- $_\n" if $opt_v;
3007 ($alias,$address,$options) = split;
3009 if ($options) { $options =~ s/[()]//g }
3010 else { $options = '' }
3011 $AB{$alias} = $address;
3012 $AB{$alias}->{options} = $options||'';
3013 $AB{$alias}->{comment} = $comment||'';
3014 if ($options and $options =~ /keep=(\d+)/i) {
3015 $AB{$alias}->{keep} = $1;
3017 if ($options and $options =~ /autodelete=(\w+)/i) {
3018 $AB{$alias}->{autodelete} = $1;
3026 $AB{ADDRESS_BOOK} = $ab;
3032 # sets global $sid $features $timeout # ugly hack! :-}
3034 my ($server,$port) = @_;
3040 if ($port eq 443 or $proxy) {
3041 return if $features; # early return if we know enough
3042 $req = "OPTIONS /FEX HTTP/1.1";
3043 $req = "HEAD / HTTP/1.1";
3045 $req = "GET /SID HTTP/1.1";
3048 sendheader("$server:$port",$req,"User-Agent: $useragent");
3050 unless (defined $_ and /\w/) {
3051 print "\n" if $opt_v;
3052 die "$0: no response from server\n";
3055 print "<-- $_" if $opt_v;
3057 if ($req =~ /OPTIONS/ and /^HTTP.* 502 /) {
3058 # (reverse) proxy error
3060 serverconnect($server,$port);
3061 $req = "GET /SID HTTP/1.0";
3062 sendheader("$server:$port",$req,"User-Agent: $useragent");
3064 unless (defined $_ and /\w/) {
3065 print "\n" if $opt_v;
3066 die "$0: no response from server\n";
3069 print "<-- $_" if $opt_v;
3072 print "<-- $_" if $opt_v;
3073 $features = $1 if /^X-Features: (.+)/;
3074 $timeout = $1 if /^X-Timeout: (\d+)/;
3078 serverconnect($server,$port);
3079 } elsif (/^HTTP.* [25]0[01] /) {
3080 if (not $proxy and $port ne 443 and /^HTTP.* 201 (.+)/) {
3081 $sid = 'MD5H:'.md5_hex($id.$1);
3086 print "<-- $_" if $opt_v;
3087 $features = $1 if /^X-Features: (.+)/;
3088 $timeout = $1 if /^X-Timeout: (\d+)/;
3089 $cc = $_ if /^Connection: close/;
3093 serverconnect($server,$port);
3096 } elsif (/^HTTP.* 301 /) {
3097 while (<$SH>) { last if /Location/ }
3098 die "$0: cannot use $server:$port because server has a redirection to\n".$_;
3100 # no SID support - perhaps transparent web proxy?
3103 print "<-- $_" if $opt_v;
3104 $fexsrv = $_ if /^(Server: fexsrv|X-Features:)/;
3107 die "$0: no fexserver at $server:$port\n" unless $fexsrv;
3108 serverconnect($server,$port);
3112 # warn "proxy: $proxy\n";
3114 serverconnect($server,$port);
3122 my ($from,$id,$save) = @_;
3125 my ($url,$B,$b,$t0,$t1,$cl);
3130 $url = "$proxy_prefix/fop/$from/$from/$xx?ID=$id";
3132 sendheader("$server:$port","GET $url HTTP/1.0","User-Agent: $useragent");
3136 print "<-- $_" if $opt_v;
3137 $cl = $1 if /^Content-Length:\s(\d+)/;
3138 # $ft = $1 if /^X-File-Type:\s(.+)/;
3142 die "$0: no Content-Length in server-reply\n" unless $cl;
3144 open $save,">$save" or die "$0: cannot write to $save - $!\n";
3147 $t0 = $t1 = int(time);
3150 while ($b = read($SH,$_,$bs)) {
3153 if (int(time) > $t1) {
3157 print STDERR $ts,"\r";
3161 sleep 1 while ($opt_m and $B/k/(time-$t0||1) > $opt_m);
3164 print STDERR ts($B,$cl),"\n";
3172 return sprintf("transferred: %d MB (%d%%)",int($b/M),int($b/$tb*100));
3176 sub sigpipehandler {
3182 local $SIG{ALRM} = sub { };
3188 kill 9,$tpid if $tpid;
3189 if (@r and $opt_v) {
3190 die "\n$0: ($$) server error: @r\n";
3192 if (@r and $r[0] =~ /^HTTP.* \d+ (.*)/) {
3193 die "\n$0: server error: $1\n";
3197 warn "\n$0: connection to $server $reason\n";
3198 warn "retrying after $timeout seconds...\n";
3200 if ($windoof) { exec $^X,$0,@_ARGV }
3201 else { exec $_0,@_ARGV }
3206 sub checkrecipient {
3207 my ($from,$to) = @_;
3215 command => 'CHECKRECIPIENT',
3218 $_ = shift @r or die "$0: no reply from server\n";
3223 if (s/X-(Recipient: .+)/$1\n/) {
3224 s/autodelete=\w+/autodelete=$opt_D/ if $opt_D;
3225 s/keep=\d+/keep=$opt_k/ if $opt_k;
3227 $frecipient ||= (split)[1];
3231 http_response($_,@r);
3236 # get ID data from ID file
3240 $fexcgi = getline($idf) || die "$0: no FEX-URL in $idf\n";
3241 $from = getline($idf) || die "$0: no FROM in $idf\n";
3242 $id = getline($idf) || die "$0: no ID in $idf\n";
3243 if ($fexcgi =~ s/!([\w.-]+:\d+)(:(\d+))?//) {
3245 $chunksize = $3 || 0;
3247 unless ($fexcgi =~ /^[_:=\w\-\.\/\@\%]+$/) {
3248 die "$0: illegal FEX-URL \"$fexcgi\" in $idf\n";
3250 unless ($from =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
3251 die "$0: illegal FROM \"$from\" in $idf\n";
3261 print "file to send: ";
3262 chomp($file = <STDIN>);
3266 warn "$file does not exist\n";
3268 print "recipient (e-mail address): ";
3269 chomp($to = <STDIN>);
3270 die $usage unless $to;
3273 chomp($opt_C = <STDIN>);
3275 @ARGV = ($file,$to);
3280 if (system(@_) < 0) { die "failed: @_\n" }
3284 # emulate seek on a pipe
3286 my $fh = shift; # filehandle
3287 my $ba = shift; # bytes ahead
3295 $n = $bs if $n > $bs;
3296 $s += read $fh,$_,$n;
3303 my @s = stat($file);
3306 return md5_hex($file.$s[0].$s[1].$s[7].$s[9]);
3308 warn "$0: $file - $!\n";
3314 sub get_mutt_alias {
3316 my $ma = $HOME.'/.mutt/aliases';
3320 open $ma,$ma or return $to;
3322 if (/^alias \Q$to\E\s/i) {
3330 warn "$0: ignoring mutt multi-alias $to = $_\n";
3335 warn "$0: found mutt alias $to = $alias\n";
3341 return ($alias||$to);
3345 # collect (hashed) file meta data
3351 foreach $file (@files) {
3352 if (not -l $file and -d $file) {
3354 if (opendir $dir,$dir) {
3355 while (defined ($file = readdir($dir))) {
3356 next if $file eq '..';
3358 $fmd .= fileid($dir);
3360 $fmd .= fmd("$dir/$file");
3366 $fmd .= fileid($file);
3374 # from MIME::Base64::Perl
3380 tr|A-Za-z0-9+=/||cd;
3382 tr|A-Za-z0-9+/| -_|;
3383 return "" unless length;
3386 for ($i = 0; $i <= $l; $i += 60) {
3387 $uu .= "M" . substr($_,$i,60);
3391 $uu .= chr(32+(length)*3/4) . $_;
3393 return unpack("u",$uu);
3399 if (open my $tty,'/dev/tty') {
3403 " [p] perhaps - don't know\n",
3407 if (/^y/i) { return }
3409 if (/^p/i) { int(rand(2)) ? return : exit }
3416 local $_ = shift || <$SH>;
3420 $_ = <$SH> unless $_;
3421 unless (defined $_ and /\w/) {
3422 die "$0: no response from server\n";
3425 print "<-- $_\n" if $opt_v;
3426 # CGI fatalsToBrowser
3427 if (/^HTTP.* 500/) {
3428 @r = <$SH> unless @r;
3430 die "$0: server error: $_\n@r\n";
3432 unless (/^HTTP.* 200/) {
3434 $error =~ s/HTTP.[\s\d.]+//;
3435 @r = <$SH> unless @r;
3439 $error .= "\n".$_ if /^Location/;
3440 print "<-- $_\n" if $opt_v;
3442 die "$0: server error: $error\n";
3456 my $cfb = '### common functions ###';
3461 open $0,$0 or die "cannot read $0 - $!\n";
3464 $cfc =~ s/.*\n$cfb\n//s;
3466 foreach my $p (qw'fexget sexsend') {
3467 open $p,$p or die "cannot read $p - $!\n";
3470 s/\n$cfb.*/\n$cfb\n$cfc/s;
3472 open $p,'>',$p or die "cannot write $p - $!\n";
3477 exec "l fexsend fexget sexsend";
3481 ### common functions ###
3485 my @d = localtime((stat shift)[9]);
3486 return sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]);
3492 s/\%([a-f\d]{2})/chr(hex($1))/ige;
3498 # set SSL/TLS options
3499 $SSL{SSL_verify_mode} = $ENV{SSLVERIFY} if defined($ENV{SSLVERIFY});
3500 foreach my $opt (qw(
3509 $SSL{$opt} = $ENV{$env} if defined($ENV{$env});
3512 if ($SSL{SSL_verify_mode}) {
3514 unless ($SSL{SSL_ca_path} or $SSL{SSL_ca_file}) {
3515 die "$0: \$SSLVERIFYMODE, but not valid \$SSLCAPATH or \$SSLCAFILE\n";
3517 } elsif (defined($SSL{SSL_verify_mode})) {
3518 # user has set SSLVERIFY=0 !
3521 $SSL{SSL_verify_mode} = 1 if $SSL{SSL_ca_path} or $SSL{SSL_ca_file};
3527 return if $SSL{SSL_ca_file} or $SSL{SSL_ca_path};
3528 foreach (qw(/etc/ssl/certs/ca-certificates.crt)) {
3530 $SSL{SSL_ca_file} = $_;
3534 foreach (qw(/etc/ssl/certs /etc/pki/tls/certs)) {
3536 $SSL{SSL_ca_path} = $_;
3544 my ($server,$port) = @_;
3545 my $connect = "CONNECT $server:$port HTTP/1.1";
3549 tcpconnect(split(':',$proxy));
3551 printf "--> %s\n",$connect if $opt_v;
3552 nvtsend($connect,"");
3555 printf "<-- $_"if $opt_v;
3556 unless (/^HTTP.1.. 200/) {
3557 die "$0: proxy error : $_";
3560 $SH = IO::Socket::SSL->start_SSL($SH,%SSL);
3563 tcpconnect($server,$port);
3565 # if ($https and $opt_v) {
3566 # printf "%s\n",$SH->get_cipher();
3571 # set up tcp/ip connection
3573 my ($server,$port) = @_;
3581 # eval "use IO::Socket::SSL qw(debug3)";
3583 $SH = IO::Socket::SSL->new(
3584 PeerAddr => $server,
3590 $SH = IO::Socket::INET->new(
3591 PeerAddr => $server,
3601 die "$0: cannot connect $server:$port - $@\n";
3604 print "TCPCONNECT to $server:$port\n" if $opt_v;
3609 eval "use IO::Socket::SSL";
3610 die "$0: cannot load IO::Socket::SSL\n" if $@;
3611 eval '$SSL{SSL_verify_mode} = 0 if Net::SSLeay::SSLeay() <= 9470143';
3613 foreach my $v (keys %SSL) {
3614 printf "%s => %s\n",$v,$SSL{$v};
3625 push @head,"Host: $sp";
3627 foreach $head (@head) {
3628 print "--> $head\n" if $opt_v;
3629 print {$SH} $head,"\r\n";
3631 print "-->\n" if $opt_v;
3637 local $SIG{PIPE} = sub { $sigpipe = "@_" };
3641 die "$0: internal error: no active network handle\n" unless $SH;
3642 die "$0: remote host has closed the link\n" unless $SH->connected;
3644 foreach my $line (@_) {
3645 print {$SH} $line,"\r\n";
3658 s/([^\w\@\/%^,.=+_:+-])/\\$1/g;
3664 print "## DEBUG: @_\n" if $DEBUG;
3668 # from MIME::Base64::Perl
3675 $res = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
3676 $res =~ tr|` -_|AA-Za-z0-9+/|;
3677 $padding = (3-length($_[0])%3)%3;
3678 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;