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 = 20160328;
42 our $DEBUG = $ENV{DEBUG};
44 my %SSL = (SSL_version => 'TLSv1');
47 if ($Config{osname} =~ /^mswin/i) {
48 # http://slu.livejournal.com/17395.html
49 $windoof = $Config{osname};
50 $HOME = $ENV{USERPROFILE};
51 $fexhome = $ENV{FEXHOME} || $HOME.'\fex';
52 $tmpdir = $ENV{FEXTMP} || $ENV{TEMP} || "$fexhome\\tmp";
53 $idf = "$fexhome\\id";
54 $editor = $ENV{EDITOR} || 'notepad.exe';
55 $useragent = sprintf("fexsend-$version (%s %s)",
56 $Config{osname},$Config{archname});
57 $SSL{SSL_verify_mode} = 0;
58 } elsif ($Config{osname} =~ /^darwin/i or $ENV{MACOS}) {
59 # http://stackoverflow.com/questions/989349/running-a-command-in-a-new-mac-os-x-terminal-window
60 $macos = $Config{osname};
61 $HOME = (getpwuid($<))[7]||$ENV{HOME};
62 $fexhome = $HOME.'/.fex';
63 $tmpdir = $ENV{FEXTMP} || $ENV{TMPDIR} || "$fexhome/tmp";
67 $editor = $ENV{EDITOR} || 'open -W -n -e';
68 $_ = `sw_vers -productVersion 2>/dev/null`||'';
70 $useragent = "fexsend-$version (MacOS $_)";
73 $HOME = (getpwuid($<))[7]||$ENV{HOME};
74 $fexhome = $HOME.'/.fex';
75 $tmpdir = $ENV{FEXTMP} || "$fexhome/tmp";
78 $editor = $ENV{EDITOR} || 'vi';
79 $_ = `(lsb_release -d||uname -a)2>/dev/null`||'';
82 $useragent = "fexsend-$version ($_)";
85 if (-f ($_ = '/etc/fex/config.pl')) {
86 eval { require } or warn $@;
94 my $atype = ''; # archive type
95 my $fexcgi; # F*EX CGI URL
96 my @files; # files to send
97 my %AB = (); # server based address book
98 my ($server,$port,$sid,$https);
100 my $proxy_prefix = '';
102 my $timeout = 30; # server timeout
103 my $fexlist = "$tmpdir/fexlist";
105 my $xx = $0 =~ /\bxx$/;
108 $usage = "usage: send file(s): xx [:slot] file...\n".
109 " or: send STDIN: xx [:slot] -\n".
110 " or: send pipe: ... | xx [:slot] \n".
111 " or: get file(s) or STDIN: xx [:slot] \n".
112 " or: get file(s) no-questions: xx [:slot] --\n".
113 "examples: dmesg | xx\n".
116 " xx :conf /etc /boot\n";
119 usage: $0 [options] file(s) [@] recipient(s)
120 or: $0 [special options]
121 or: $0 -l [recipient-regexp]
122 or: $0 -f \# recipient(s)
123 or: $0 -x \# [-C -k -D -K -S]
124 options: -v verbose mode
125 -d delete file on fex server
126 -c compress file with gzip
127 -g encrypt file with gpg
128 -m limit limit throughput (kB/s)
129 -i account use ID data [account] from ID file
130 -C comment add comment to notification e-mail
131 -k max keep file max days on fex server
132 -D delay auto-delete after download
133 -K no auto-delete after download
134 -M MIME-file (to be displayed in recipient\'s webbrowser)
135 -o overwrite mode, do not resume
136 -a archive put files in archive (.zip .7z .tar .tgz)
137 -s stream read data from pipe and upload it with stream name
138 special options: -I initialize ID file or show ID
139 -I account add alternate ID data (secondary logins) to ID file
140 -l list sent files numbers (# needed for -f -x -d -N)
141 -f \# forward already uploaded file to another recipient
142 -x \# use -C -k -D -K for already uploaded file
143 -d \# delete file on fex server
144 -N \# resend notification e-mail
146 -T up:down test internet speed with up and down MBs
147 -A edit server address book (aliases)
148 -S show server/user settings and auth-ID
149 -H show hints, examples and more options
150 -V show version and ask for upgrade
151 (# is a file number, see output from $0 -l)
152 examples: $0 visualization.mpg framstag\@rus.uni-stuttgart.de
153 $0 -a images.zip *.jpg webmaster\@flupp.org,metoo
154 lshw | $0 -s hardware.list admin\@flupp.org
156 # or: $0 -R FEX-URL e-mail
157 # -R FEX mail self-register your e-mail address at FEX server
160 $0 hints and more options:
162 usage: $0 [options] file recipient(s)
164 Recipient can be a comma separated address list. Example:
165 $0 big.file framstag\@rus.uni-stuttgart.de,webmaster\@flupp.org
167 Recipient can be an alias from your server address book
168 (use "$0 -A" to edit it). Example:
171 Recipient can be a SKEY URL, which you have received from a regular F*EX user.
172 When using this URL you are a subuser of this full user and the file will be
173 sent to him. Example:
174 $0 big.file http://fex.rus.uni-stuttgart.de/fup?skey=4285f8cdd881626524fba686d5f0a83a
176 Recipient can be a GKEY URL, which you have received from a regular F*EX user.
177 Using this URL you are a member of his group and the file will be sent to all
178 members of this group. Example:
179 $0 big.file http://fex.rus.uni-stuttgart.de/fup?gkey=50d26547b1e8c1110beb8748fc1d9444
181 When you use "FEX-URL/anonymous" as recipient and your F*EX administrator has
182 allowed anonymous upload for your IP address then no auth-ID is needed.
184 "." as recipient means fex to yourself and show immediately the download URL
185 (no notification e-mail will be sent). Example:
188 "//" as recipient means fex to yourself and create extra short download URL.
192 If you want a Bcc of the notification e-mail then add '!bcc!' to the comment:
193 fexsend -C '!bcc! for me and you' ...
195 Additional special options:
197 -. sends a short instead of a detailed notification e-mail
198 -/ does not upload the file, but tells the server to link it
199 -= uses an alias name as file name
200 -# excludes files (# is list separator) from archive -a
201 -n sends no notification e-mail, but shows the download URL immediately
203 -r ADDRESS sets e-mail Reply-To ADDRESS
204 -F activates female mode
205 -U show authorized URL
206 -+ is an undocumented feature - test it :-)
208 To manage your subuser and groups or forward or redirect files, use a
209 webbrowser with the URL from "$0 -U", e.g.: firefox \$($0 -U)
211 If you want to copy-forward an already uploaded file to another recipient,
212 then you first have to query the file number with:
214 and then copy-forward it with:
215 $0 -b # other\@address
216 Where # is the file number.
218 You can list an uploaded file in more detail with
220 Where # is the file number.
222 If you want to modify the keep time, comment or auto-delete behaviour of an
223 already uploaded file then you first have to query the file number with:
225 and then for example set the keep time to 30 days with:
227 Where # is the file number.
229 With option -a you can send several files or whole directories within a single
230 archive file. The archive types tar and tgz are build on-the-fly (streaming)
231 whereas archive types zip and 7z need a temporary archive file on local disk.
233 With option -s you can send any data coming from a pipe (STDIN) as a file
234 without wasting local disc space.
236 With option -X you can specify any URL parameter, e.g.:
237 fexsend -X autodelete=yes ...
238 fexsend -X 'autodelete=no&locale=german' ...
240 For HTTPS you can set the environment variables:
241 SSLVERIFY=1 # activate server identity verification
242 SSLVERSION=TLSv1 # this is the default
243 SSLCAPATH=/etc/ssl/certs # path to trusted (root) certificates
244 SSLCAFILE=/etc/ssl/cert.pem # file with trusted (root) certificates
245 SSLCIPHERLIST=HIGH:!3DES # see http://www.openssl.org/docs/apps/ciphers.html
247 Partner program xx is an internet clipboard. See: xx -h
249 Partner program fexget is for downloading. See: fexget -h
251 For temporary usage of a HTTP proxy use:
252 $0 -P your_proxy:port:chunksize_in_MB file recipient
254 $0 -P wwwproxy.uni-stuttgart.de.de:8080:1024 4GB.tar .
256 For temporary usage of an alternative F*EX server or user use:
257 FEXID="FEXSERVER USER AUTHID" $0 file recipient
259 FEXID="fex.flupp.org gaga\@flupp.org blubb" $0 big.file framstag\@rus.uni-stuttgart.de
261 You can define aliases (and optional fexsend options) in \$HOME/.fex/config.pl:
263 'alias1' => 'user1\@domain1.org',
264 'alias2' => 'user2\@domain2.org',
265 'both' => 'user1\@domain1.org,user2\@domain2.org',
266 'extra' => 'extra\@special.net:-i other -K -k 30',
269 fexsend also respects aliases in $HOME/.mutt/aliases
270 The alias priority is (descending):
271 \$HOME/.fex/config.pl
273 fexserver address book
275 In \$HOME/.fex/config.pl you can also set the SSL* environment variables and the
276 \$opt_* variables, e.g.:
278 \$ENV{SSLVERSION} = 'TLSv1';
291 "
\e[A \\\\/\\\\/ \n",
292 "
\e[A //\\\\//\\\\\n"
302 "
\e[A \\\\/\\\\/ \n",
303 "
\e[A //\\\\//\\\\\n"
309 if ($windoof and not @ARGV and not $ENV{PROMPT}) {
310 # restart with cmd.exe to have mouse cut+paste
311 exec qw'cmd /k',$0,'-W';
315 unless (-d $fexhome) {
316 mkdir $fexhome,0700 or die "$0: cannot create FEXHOME $fexhome - $!\n";
319 unless (-d $tmpdir) {
320 mkdir $tmpdir,0700 or die "$0: cannot create tmpdir $tmpdir - $!\n";
323 my @_ARGV = @ARGV; # save arguments
325 our ($opt_q,$opt_h,$opt_H,$opt_v,$opt_m,$opt_c,$opt_k,$opt_d,$opt_l,$opt_I,
326 $opt_K,$opt_D,$opt_u,$opt_f,$opt_a,$opt_C,$opt_R,$opt_M,$opt_L,$opt_Q,
327 $opt_A,$opt_i,$opt_z,$opt_Z,$opt_b,$opt_P,$opt_x,$opt_X,$opt_V,$opt_U,
328 $opt_s,$opt_o,$opt_g,$opt_F,$opt_n,$opt_r,$opt_S,$opt_N,$opt_T);
331 $opt_q = 1 if @ARGV and $ARGV[-1] eq '--' and pop @ARGV or not -t STDOUT;
332 $opt_h = $opt_v = $opt_m = $opt_I = 0;
334 $_ = "$fexhome/config.pl"; require if -f;
335 getopts('hvIm:') or die $usage;
337 if ($macos and not @ARGV) {
340 $opt_h = $opt_v = $opt_m = $opt_c = $opt_k = $opt_d = $opt_l = $opt_I = 0;
341 $opt_H = $opt_K = $opt_D = $opt_R = $opt_M = $opt_L = $opt_Q = $opt_A = 0;
342 $opt_x = $opt_o = $opt_g = $opt_V = $opt_U = $opt_F = $opt_n = $opt_q = 0;
344 ${'opt_@'} = ${'opt_!'} = ${'opt_+'} = ${'opt_.'} = ${'opt_/'} = 0;
345 ${'opt_='} = ${'opt_#'} = '';
346 $opt_u = $opt_f = $opt_a = $opt_C = $opt_i = $opt_b = $opt_P = $opt_X = '';
347 $opt_s = $opt_r = $opt_T = '';
348 $_ = "$fexhome/config.pl"; require if -f;
349 getopts('hHvcdognVDKlILUARWMFzZqQS@!+./r:m:k:u:f:a:s:C:i:b:P:x:X:N:T:=:#:')
358 print "Version: $version\n";
360 print "Upgrade fexsend? ";
363 my $new = `wget -nv -O- http://fex.belwue.de/download/fexsend`;
364 if ($new !~ /upgrade fexsend/) {
365 die "$0: bad update\n";
367 system qw'cp -aL',$_0,$_0.'_old';
369 open $_0,'>',$_0 or die "$0: cannot write $_0. - $!\n";
375 exit if "@ARGV" eq '.';
378 if ($opt_K and $opt_D) {
379 die "$0: you cannot use both options -D and -K\n";
382 if ($opt_a and $opt_c) {
383 die "$0: you cannot use both options -a and -c\n";
386 if ($opt_a and $opt_s) {
387 die "$0: you cannot use both options -a and -s\n";
390 if ($opt_g and $opt_c) {
395 if ($opt_f and $opt_f !~ /^\d+$/) {
396 die "$0: option -f needs a number, see $0 -l\n";
399 if ($opt_I and $opt_R) {
400 die "$0: you cannot use both options -I and -R\n";
403 # $opt_C is COMMENT command in F*EX protocol
406 ($opt_l or $opt_L) ? 'LIST':
407 ($opt_Q) ? 'CHECKQUOTA':
408 ($opt_S) ? 'LISTSETTINGS':
409 ($opt_Z) ? 'RECEIVEDLOG':
410 ($opt_z) ? 'SENDLOG':
411 (${'opt_!'}) ? 'FOPLOG':
423 female_mode("show help?") if $opt_F;
435 die $usage if $opt_m and $opt_m !~ /^\d+/;
438 if ($opt_P =~ /^([\w.-]+:\d+)(:(\d+))?/) {
440 $chunksize = $3 || 0;
442 die "$0: proxy must be: SERVER:PORT\n";
446 if ($FEXID = $ENV{FEXID}) {
447 $FEXID = decode_b64($FEXID) if $FEXID !~ /\s/;
448 ($fexcgi,$from,$id) = split(/\s+/,$FEXID);
450 if ($windoof and not -f $idf) { &init_id }
451 if (open $idf,$idf) {
458 # convert old idxx file
459 if ($idf and open $idf,$idf.'xx') {
462 if (open $idf,'>>',$idf) {
463 print {$idf} "\n[xx]\n",
473 if ($FEXXX = $ENV{FEXXX}) {
474 $FEXXX = decode_b64($FEXXX) if $FEXXX !~ /\s/;
475 ($fexcgi,$from,$id) = split(/\s+/,$FEXXX);
476 } elsif (open $idf,$idf) {
479 $proxy = $proxy_prefix = '';
491 $proxy = $proxy_prefix = '';
492 open $idf,$idf or die "$0: cannot open $idf - $!\n";
500 die "$0: no [$opt_i] in $idf\n" unless $_;
505 if ($xx) { &show_id }
513 $usage = "usage: $0 -T MB_up[:MB_down] [fexserver]\n";
514 if ($opt_T =~ /^(\d+)$/) {
516 } elsif ($opt_T =~ /^(\d+):(\d+)$/) {
524 nettest($ARGV[0],$up,$down);
526 nettest($fexcgi,$up,$down);
528 nettest('fex.belwue.de',$up,$down);
533 if (@ARGV > 1 and $ARGV[-1] =~ /(^|\/)anonymous/) {
534 $fexcgi = $1 if $ARGV[-1] =~ s:(.+)/::;
535 die "usage: $0 [options] file FEXSERVER/anonymous\n" unless $fexcgi;
536 $anonymous = $from = 'anonymous';
537 $sid = $id = 'ANONYMOUS';
538 } elsif (@ARGV > 1 and $id eq 'PUBLIC') {
539 $public = $sid = $id;
540 } elsif (@ARGV > 1 and $ARGV[-1] =~ m{^(https?://[\w.-]+(:\d+)?/fup\?[sg]key=\w+)}) {
542 $skey = $1 if $fexcgi =~ /skey=(\w+)/;
543 $gkey = $1 if $fexcgi =~ /gkey=(\w+)/;
546 $fexcgi = $opt_u if $opt_u;
548 if (not -e $idf and not ($fexcgi and $from and $id)) {
549 die "$0: no ID file $idf found, use \"fexsend -I\" to create it\n";
553 die "$0: no FEX URL found, use \"$0 -u URL\" or \"$0 -I\"\n";
556 unless ($from and $id) {
557 die "$0: no sender found, use \"$0 -f FROM:ID\" or \"$0 -I\"\n";
560 if ($fexcgi !~ /^http/) {
561 if ($fexcgi =~ /:443/) { $fexcgi = "https://$fexcgi" }
562 else { $fexcgi = "http://$fexcgi" }
570 $port = 443 if $server =~ s{https://}{};
571 $port = $1 if $server =~ s/:(\d+)//;
574 # $opt_s and die "$0: cannot use -s with https due to stunnel bug\n";
575 # $opt_g and die "$0: cannot use -g with https due to stunnel bug\n";
579 $server =~ s{http://}{};
582 # $chunksize = 4*k unless $chunksize;
586 if ($port == 80) { $proxy_prefix = "http://$server" }
587 elsif ($port != 443) { $proxy_prefix = "http://$server:$port" }
590 # xx: special file exchange between own accounts
592 my $transferfile = "$tmpdir/STDFEX";
595 $transferfile = "$tmpdir/xx:xxx";
596 } elsif (@ARGV and $ARGV[0] =~ /^:([\w.=+-]+)$/) {
597 $transferfile = "$tmpdir/xx:$1";
600 open my $lock,'>>',$transferfile
601 or die "$0: cannot write $transferfile - $!\n";
602 flock($lock,LOCK_EX|LOCK_NB)
603 or die "$0: $transferfile is locked by another process\n";
604 truncate $transferfile,0;
605 if (not @ARGV and -t) {
606 &get_xx($transferfile);
608 &send_xx($transferfile);
615 &inquire if $windoof and not @ARGV and not
616 ($opt_l or $opt_L or $opt_Q or $opt_A or $opt_U or $opt_I or
617 $opt_f or $opt_x or $opt_N);
620 $opt_C = "!SHORTMAIL! $opt_C";
623 if ($opt_n or $opt_C =~ /NOMAIL|!#!/) {
627 unless ($skey or $gkey or $anonymous) {
629 $opt_f||$opt_x||$opt_Q||$opt_l||$opt_L||$opt_U||$opt_z||$opt_Z||$opt_A
630 ||$opt_d||${'opt_!'}||${'opt_@'})
631 ) { warn "Server/User: $fexcgi/$from\n" }
634 if ($opt_V and not @ARGV) { exit }
635 if ($opt_f) { &forward }
636 elsif ($opt_x) { &modify }
637 elsif ($opt_N) { &renotify }
638 elsif ($opt_Q) { &query_quotas }
639 elsif ($opt_S) { &query_settings }
640 elsif ($opt_l or $opt_L) { &list }
641 elsif ($opt_U) { &show_URL }
642 elsif ($opt_z or $opt_Z or ${'opt_!'}) { &get_log }
643 elsif ($opt_A) { edit_address_book($from) }
644 elsif (${'opt_@'}) { &show_address_book }
645 elsif ($opt_d and $anonymous) { &purge }
646 elsif ($opt_d and $ARGV[-1] =~ /^\d+$/) { &delete_file_number }
652 # initialize ID file or show ID
662 $fexcgi = $from = $id = '';
664 unless (-d $fexhome) {
665 mkdir $fexhome,0700 or die "$0: cannot create FEXHOME $fexhome - $!\n";
669 if (not $tag and open $idf,$idf) {
672 last if /^\[$opt_i\]/;
680 chomp($fexcgi,$from,$id);
681 $FEXID = encode_b64("$fexcgi $from $id");
683 print "# hint: to edit the ID file $idf use \"$0 -I .\" #\n";
684 print "export FEXID=$FEXID\n";
685 print "history -d \$((HISTCMD-1));history -d \$((HISTCMD-1))\n";
687 print "FEXID=$FEXID\n";
691 die "$0: no ID data found\n";
695 if ($tag and $tag eq '.') { exec $ENV{EDITOR}||'vi',$idf }
697 if ($tag) { print "F*EX server URL for [$tag]: " }
698 else { print "F*EX server URL: " }
700 $fexcgi =~ s/[\s\n]//g;
701 die "you MUST provide a FEX-URL!\n" unless $fexcgi;
702 if ($fexcgi =~ /\?/) {
703 $from = $1 if $fexcgi =~ /\bfrom=(.+?)(&|$)/i;
704 $id = $1 if $fexcgi =~ /\bid=(.+?)(&|$)/i;
705 # $skey = $1 if $fexcgi =~ /\bskey=(.+?)(&|$)/i;
706 # $gkey = $1 if $fexcgi =~ /\bgkey=(.+?)(&|$)/i;
707 die "$0: cannot use GKEY URL in ID file\n" if $fexcgi =~ /gkey=/i;
708 die "$0: cannot use SKEY URL in ID file\n" if $fexcgi =~ /skey=/i;
711 unless ($fexcgi =~ /^[_:=\w\-\.\/\@\%]+$/) {
712 die "\"$fexcgi\" is not a legal FEX-URL!\n";
714 $fexcgi =~ s:/fup/*$::;
715 print "proxy address (hostname:port or empty if none): ";
717 $proxy =~ s/[\s\n]//g;
718 if ($proxy =~ /^[\w.-]+:\d+$/) {
720 } elsif ($proxy =~ /\S/) {
721 die "wrong proxy address format\n";
726 print "proxy POST limit in MB (use 2048 if unknown): ";
736 $from = 'GROUPMEMBER';
740 print "Your e-mail address as registered at $fexcgi: ";
742 $from =~ s/[\s\n]//g;
743 die "you MUST provide your e-mail address!\n" unless $from;
745 unless ($from =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
746 die "\"$from\" is not a legal e-mail address!\n";
749 print "Your auth-ID for $from at $fexcgi: ";
752 die "you MUST provide your ID!\n" unless $id;
755 if (open $idf,'>>',$idf) {
756 print {$idf} "\n[$tag]\n" if $tag and -s $idf;
757 print {$idf} "$fexcgi$proxy\n",
761 print "data written to $idf\n";
763 die "$0: cannot write to $idf - $!\n";
769 my ($fexcgi,$from,$id);
770 if (open $idf,$idf) {
772 # $fexcgi = <$idf> if $fexcgi =~ /^\[.+\]/;
783 die "$0: too few data in $idf" unless defined $id;
787 $FEXXX = encode_b64("$fexcgi $from $id");
789 print "export FEXXX=$FEXXX\n";
790 print "history -d \$((HISTCMD-1));history -d \$((HISTCMD-1))\n";
792 print "FEXXX=$FEXXX\n";
795 die "$0: cannot read $idf - $!\n";
801 my $fs = shift @ARGV or die $usage;
802 my $mail = shift @ARGV or die $usage;
804 my ($server,$user,$id);
806 die "$0: $idf does already exist\n" if -e $idf;
808 if ($fs =~ /^https/) {
809 die "$0: cannot handle https at this time\n";
812 $fs =~ s{^http://}{};
814 if ($fs =~ s/:(\d+)//) { $port = $1 }
817 tcpconnect($fs,$port);
818 sendheader("$fs:$port","GET $proxy_prefix/fur?user=$mail&verify=no HTTP/1.1");
824 printf "<-- $_"if $opt_v;
830 printf "<-- $_"if $opt_v;
831 if (m{http://(.*)/fup\?from=(.+)&ID=(.+)}) {
836 if (open F,">$idf") {
842 print "user data written to $idf\n";
843 print "you can now fex!\n";
846 die "$0: cannot write to $idf - $!\n";
851 die "$0: no account data received from F*EX server\n";
856 # menu for MacOS users
864 print "fexsend-$version\n";
867 if (open $idf,$idf) {
868 $fexcgi = getline($idf) and
869 $from = getline($idf) and
878 print "$from on $fexcgi\n";
883 print "[s] send a file or directory\n";
884 print "[u] update fexsend\n";
885 print "[l] change login data (user, server, auth-ID)\n";
889 print "your choice: ";
894 print "Type [Cmd]W to close this window.\n";
901 "With fexsend you can send files of any size to any e-mail address.\n".
903 "At the recipient or file prompt [RETURN] brings you to this option menu.\n".
905 "To send more than one file:\n".
906 "When you enter * at the file prompt, you will be first asked for an archive name\n".
907 "and then you can drag+drop multiple files.\n".
909 "Do not forget to terminate each input line with [RETURN].\n".
911 "See http://fex.rus.uni-stuttgart.de/ for more information.\n";
916 if ($0 =~ m:(^/client/|/sw/):) {
918 print "use swupdate to update fexsend!\n";
922 system "curl http://fex.belwue.de/download/fexsend>".quote($new);
924 system qw'perl -c',$new;
930 print "cannot install new fexsend\n";
940 if ($key eq 's' or $key eq "\n") {
952 my ($file,$comment,$recipient,$archive,$size,$cmd,$key);
959 &set_ID unless -s $idf;
962 print "Enter [RETURN] after each input line.\n";
966 print "Recipient(s): ";
967 $recipient = <STDIN>;
969 $recipient =~ s/^\s+//;
970 $recipient =~ s/\s+$//;
971 $recipient =~ s/[\s;,]+/,/g;
972 &menu unless $recipient;
973 last if $recipient =~ /\w/ or $recipient eq '.';
978 print "Drag a file into this window or hit [RETURN] ";
979 print $archive ? "to continue.\n" : "for menu options.\n";
980 print "File to send: ";
984 $file =~ s/ $// if $file !~ /\\ $/;
985 &menu unless $file or $archive;
987 print "Archive name: ";
988 $archive = <STDIN>||'';
990 next unless $archive;
991 $archive =~ s/^\s+//g;
992 $archive =~ s/\s+$//g;
993 $archive =~ s/[^\w=.+-]/_/g;
998 $file =~ s/\\\\/\000/g;
1000 $file =~ s/\000/\\/g;
1003 print "\"$file\" is not readable\n";
1006 my $qf = quote($file);
1007 if (`du -ms $qf` =~ /^(\d+)/) {
1009 printf "%d MB\n",$1;
1018 $qfiles = join(' ',map(quote($_),@files));
1027 $comment = <STDIN>||'';
1030 if ($comment =~ s/^:\s*-/-/) {
1031 $cmd = quote($0)." $comment ";
1033 $cmd .= '-a '.quote($archive).' '.$qfiles;
1035 $cmd .= quote($file);
1037 $cmd .= ' '.quote($recipient);
1041 print quote($0)." -C '$comment' ";
1043 printf "-a %s %s %s\n",quote($archive),$qfiles,$recipient;
1044 system $0,'-C',$comment,'-a',$archive,@files,$recipient;
1046 printf "%s %s\n",quote($file),$recipient;
1047 system $0,'-C',$comment,$file,$recipient;
1051 print "[s] send another file to $recipient\n";
1052 print "[n] send another file to another recipient\n";
1055 print "your choice: ";
1058 &ask_file if $key eq 'n';
1059 if ($key eq 's' or $key eq "\n") {
1068 $file = $comment = $archive = '';
1075 my ($server,$port,$user,$logo);
1080 print "F*EX server URL: ";
1082 $server =~ s/[\s\n]//g;
1083 if ($server =~ s:/fup/(\w+)$::) {
1084 $_ = decode_b64($1);
1085 if (/(from|user)=(.+)&id=(.+)/) {
1090 $server =~ s:/fup.*::;
1092 next if $server !~ /\w/;
1093 if ($server =~ s/^https:..// or $server =~ /:443/) {
1096 eval "use IO::Socket::SSL";
1098 print "\nno perl SSL modules installed - cannot use https\n\n";
1101 $SH = IO::Socket::SSL->new(
1102 PeerAddr => $server,
1108 $server =~ s:^http.//::;
1109 if ($server =~ s/:(\d+)//) {
1114 $SH = IO::Socket::INET->new(
1115 PeerAddr => $server,
1121 print "\ncannot connect to $server:$port - $!\n\n";
1126 "GET /logo.jpg HTTP/1.0",
1127 "Connection: close",
1130 unless (/HTTP.1.1 200/) {
1131 print "\nbad server reply: $_\n";
1134 while (<$SH>) { last if /^\s*$/ }
1138 if (length $logo < 9999) {
1139 print "\n$server is not a F*EX server!\n\n";
1142 open $logo,">$tmpdir/fex.jpg";
1143 print {$logo} $logo;
1150 print "Your login (e-mail address): ";
1152 $user =~ s/[\s\n]//g;
1153 if ($user !~ /.@[\w.-]+$/) {
1154 print "\"$user\" is not a valid e-mail address!\n";
1161 print "Your auth-ID for this account: ";
1166 open $idf,'>',$idf or die "$0: cannot write to $idf - $!\n";
1167 print {$idf} "$server\n",
1172 print "Login data written to $idf\n\n";
1173 print "fexing test file to $user:\n\n";
1174 system "$0 -o -M -C test $tmpdir/fex.jpg $user";
1177 print "fexsend failed, login data is invalid, try again\n";
1180 print "fexsend test succeeded!\n";
1192 my ($length,$t0,$t1,$t2,$tt,$tb,$tc,$B,$kBs,$bt);
1194 my $nettest = $sid = 'nettest';
1197 if ($url =~ s:^https.//::) {
1198 $https = $port = 443;
1200 $url =~ s:^http.//::;
1201 $port = $1 if $url =~ s/:(\d+)//;
1203 $url =~ s/[\/:].*//;
1207 serverconnect($server,$port);
1208 checkrecipient($nettest,$nettest);
1209 warn "$0: send to $server:$port\n";
1216 comment => 'NOSTORE',
1221 serverconnect($server,$port);
1222 warn "$0: receive from $server:$port\n";
1223 sendheader("$server:$port","GET $proxy_prefix/ddd/$down HTTP/1.0");
1225 die "$0: no response from fex server $server\n" unless $_;
1228 if (/^HTTP\/[\d.]+ 2/) {
1229 warn "<-- $_" if $opt_v;
1232 print "<-- $_" if $opt_v;
1234 $length = $1 if /^Content-Length:\s*(\d+)/i;
1237 s/HTTP\/[\d.]+ \d+ //;
1238 die "$0: bad server reply: $_";
1242 die "$0: no Content-Length header in server reply\n";
1251 $t0 = $t1 = $t2 = int(time);
1253 while ($B < $length) {
1254 $b = read $SH,$_,$bs or die "$0: cannot read after $B bytes - $!\n";
1255 # defined($_ = <$SH>) or die "$0: cannot read after $B bytes - $!\n";
1260 if (${'opt_+'} and int($t2*10)>$tc) {
1261 print $rrcamel[$tc%2+1];
1264 if (int($t2) > $t1) {
1265 $kBs = int($bt/k/($t2-$t1));
1268 printf STDERR "nettest: %d MB (%d%%) %d kB/s \r",
1269 int($B/M),int(100*$B/$length),$kBs;
1275 $kBs = int($B/k/($tt||1));
1280 printf STDERR "nettest: %d MB in %d s = %d kB/s \n",
1286 # read one key from terminal in raw mode
1289 local $SIG{INT} = sub { stty('reset'); exit };
1292 # loop necessary for ESXi support
1293 while (not defined $key) {
1302 if (shift eq 'raw') {
1303 system qw'stty -echo -icanon eol',"\001";
1305 system qw'stty echo icanon eol',"\000";
1311 my $transferfile = shift;
1315 $SIG{PIPE} = $SIG{INT} = sub {
1316 unlink $transferfile;
1320 if ($0 eq 'xxx') { @tar = qw'tar -cv' }
1321 else { @tar = qw'tar -cvz' }
1324 if ("@ARGV" eq '-') {
1325 # store STDIN to transfer file
1326 shelldo("cat >> $transferfile");
1328 print "making tar transfer file $transferfile :\n";
1329 # single file? then add this directly
1330 if (scalar @ARGV == 1) {
1331 # strip path if not ending with /
1332 if ($ARGV[0] =~ m:(.+)/(.+): and $2 !~ m:/$:) {
1333 ($dir,$file) = ($1,$2);
1334 chdir $dir or die "$0: $dir - $!\n";
1339 shelldo(@tar,qw'--dereference -f',$transferfile,$file);
1341 shelldo(@tar,'-f',$transferfile,$file);
1344 shelldo(@tar,'-f',$transferfile,@ARGV);
1347 unlink $transferfile;
1349 die "$0: interrupted making tar transfer file\n";
1351 die "$0: error while making tar transfer file\n";
1356 # write input from pipe to transfer file
1357 shelldo("cat >> $transferfile");
1360 die "$0: no transfer file\n" unless -s $transferfile;
1362 serverconnect($server,$port);
1363 query_sid($server,$port);
1369 file => $transferfile,
1370 comment => 'NOMAIL',
1371 autodelete => $transferfile =~ /STDFEX/ ? 'NO' : 'DELAY',
1374 # open P,'|w3m -T text/html -dump' or die "$0: w3m - $!\n";
1377 if ($transferfile =~ /:/ and $0 ne 'xxx') {
1378 if ("@r" =~ /\s(X-)?Location: (http.*)\s/) {
1379 print "wget -O- $2 | tar xvzf -\n";
1383 unlink $transferfile;
1391 female_mode("query quotas?") if $opt_F;
1399 die "$0: no response from fex server $server\n" unless @r;
1401 unless (/^HTTP.* 2/) {
1403 die "$0: server response: $_\n";
1405 if (($_) = grep(/^X-Sender-Quota/,@r) and /(\d+)\s+(\d+)/) {
1406 print "sender quota (used): $1 ($2) MB\n";
1408 print "sender quota: unlimited\n";
1410 if (($_) = grep(/^X-Recipient-Quota/,@r) and /(\d+)\s+(\d+)/) {
1411 print "recipient quota (used): $1 ($2) MB\n";
1413 print "recipient quota: unlimited\n";
1418 sub query_settings {
1422 female_mode("query settings?") if $opt_F;
1425 print "ID data from \$FEXID\n";
1427 print "ID data from $idf\n";
1429 die "$0: found no ID\n";
1431 print "server: $fexcgi\n";
1432 print "user: $from\n";
1433 print "auth-ID: $id\n";
1434 print "login URL: ";
1443 die "$0: no response from fex server $server\n" unless @r;
1445 unless (/^HTTP.* 2/) {
1447 die "$0: server response: $_\n";
1449 if (($_) = grep(/^X-Autodelete/,@r) and /:\s+(\w+)/) {
1450 print "autodelete: $1\n";
1452 if (($_) = grep(/^X-Default-Keep/,@r) and /(\d+)/) {
1453 print "default keep: $1 days\n";
1455 if (($_) = grep(/^X-Default-Locale/,@r) and /:\s+(\w+)/) {
1456 print "default locale: $1\n";
1458 if (($_) = grep(/^X-MIME/,@r) and /:\s+(\w+)/) {
1459 print "display file with browser: $1\n";
1461 if (($_) = grep(/^X-Sender-Quota/,@r) and /(\d+)\s+(\d+)/) {
1462 print "sender quota (used): $1 ($2) MB\n";
1464 print "sender quota: unlimited\n";
1466 if (($_) = grep(/^X-Recipient-Quota/,@r) and /(\d+)\s+(\d+)/) {
1467 print "recipient quota (used): $1 ($2) MB\n";
1469 print "recipient quota: unlimited\n";
1480 my $a = shift @ARGV || '.';
1483 female_mode("list spooled files?") if $opt_F;
1486 if ($a =~ /^\d+$/) {
1487 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
1488 while (<$fexlist>) {
1489 if (/^\s*(\d+)\) (\w+) (.+)/ and $1 eq $a) {
1490 serverconnect($server,$port) unless $SH;
1493 "GET $proxy_prefix/fop/$2/$2?LIST HTTP/1.1",
1497 print "<-- $_" if $opt_v;
1498 if (/^HTTP.* 200/) {
1499 print "<-- $_" if $opt_v;
1507 } elsif (s:HTTP/[\d\. ]+::) {
1508 die "$0: server response: $_";
1510 die "$0: no response from fex server $server\n";
1515 die "$0: file \#$a not found in fexlist\n";
1521 to => $opt_l ? '*' : $from,
1524 die "$0: no response from fex server $server\n" unless @r;
1526 unless (/^HTTP.* 200/) {
1528 die "$0: server response: $_\n";
1533 open $fexlist,">$fexlist" or die "$0: cannot write $fexlist - $!\n";
1535 next unless /<pre>/ or $data;
1538 if (/<a href=".*dkey=(\w+).*?">/) { $dkey = $1 }
1540 # $_ = encode_utf8($_);
1545 if (/^(to (.+) :)/) {
1547 print "\n$_\n" if $s;
1548 print {$fexlist} "\n$_\n";
1549 } elsif (m/(\d+) MB (.+)/) {
1551 printf "%4s) %8d MB %s\n","#$n",$1,$2 if $s;
1552 printf {$fexlist} "%3d) %s %s\n",$n,$dkey,$2;
1558 # list received files
1561 next unless /<pre>/ or $data;
1565 if (/(from .* :)/) {
1568 if (m{(\d+) (MB.*)<a href="(https?://.*/fop/\w+/.+)">(.+)</a>( ".*")?}) {
1569 printf "%8d %s%s%s\n",$1,$2,$3,($5||'');
1577 printf "%s/fup/%s\n",$fexcgi,encode_b64("from=$from&id=$id");
1591 die "$0: no response from fex server $server\n" unless @r;
1593 unless (/^HTTP.* 200/) {
1595 die "$0: server response: $_\n";
1598 foreach (@r) { print "$_\n" }
1602 sub show_address_book {
1607 %AB = query_address_book($server,$port,$from);
1608 foreach $alias (sort keys %AB) {
1609 next if $alias eq 'ADDRESS_BOOK';
1610 $_ = sprintf "%s = %s (%s) # %s\n",
1613 $AB{$alias}->{options},
1614 $AB{$alias}->{comment};
1623 die "$0: not yet implemented\n";
1627 sub delete_file_number {
1631 $opt_d = shift @ARGV;
1632 die "usage: $0 -d #\n" if $opt_d !~ /^\d+$/;
1634 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
1635 while (<$fexlist>) {
1636 if (/^to (.+\@.+) :/) {
1638 } elsif (/^\s*(\d+)\) (\w+) (.+)/ and $1 eq $opt_d) {
1639 serverconnect($server,$port) unless $SH;
1642 "GET $proxy_prefix/fop/$2/$2?DELETE HTTP/1.1",
1646 print "<-- $_" if $opt_v;
1647 if (/^HTTP.* 200/) {
1650 last if /^\n/; # ignore HTML output
1651 print "<-- $_" if $opt_v;
1652 if (/^X-File:.*\/(.+)/) {
1653 printf "%s deleted\n",decode_utf8(urldecode($1));
1657 } elsif (s:HTTP/[\d\. ]+::) {
1658 die "$0: server response: $_";
1660 die "$0: no response from fex server $server\n";
1666 sleep 1; # do not overrun server
1674 my ($from,$to,$file) = @_;
1678 serverconnect($server,$port);
1679 query_sid($server,$port) unless $anonymous;
1682 $file = urlencode($file);
1685 "GET $proxy_prefix/fop/$to/$from/$file?id=$sid&DELETE HTTP/1.1",
1690 printf "<-- $_"if $opt_v;
1698 s/([^_=:,;<>()+.\w\-])/'%'.uc(unpack("H2",$1))/ge;
1707 my ($data,$aname,$alias);
1714 if ($from =~ /^SUBUSER|GROUPMEMBER$/) {
1717 # look for single @ in arguments
1718 for (my $i=1; $i<$#ARGV; $i++) {
1719 if ($ARGV[$i] eq '@') {
1720 $ARGV[$i] = join(',',@ARGV[$i+1 .. $#ARGV]);
1725 $to = pop @ARGV or die $usage;
1728 $nomail = $opt_C ||= 'NOMAIL';
1732 $nomail = $opt_C ||= 'NOMAIL';
1734 if ($opt_g and $to =~ /,/) {
1735 die "$0: encryption is supported to only one recipient\n";
1737 if ($to =~ m{^https?://.*/fup\?skey=(\w+)}) {
1742 if ($to =~ m{^https?://.*/fup\?gkey=(\w+)}) {
1743 $from = 'GROUPMEMBER';
1748 @to = split(',',lc($to));
1750 die $usage unless @ARGV or $opt_a or $opt_s;
1751 die $usage if $opt_s and @ARGV;
1753 # early serverconnect necessary for X-Features info
1754 serverconnect($server,$port);
1758 sendheader("$server:$port","OPTIONS /FEX HTTP/1.1");
1761 die "$0: no response from fex server $server\n" unless $_;
1762 print "<-- $_" if $opt_v;
1763 if (/^HTTP.* 201/) {
1766 print "<-- $_" if $opt_v;
1768 $aok = $_ if /X-Features:.*ANONYMOUS/;
1770 die "$0: no anonymous support on server $server\n" unless $aok;
1772 die "$0: bad response from server $server : $_\n";
1777 query_sid($server,$port);
1779 if ($from eq 'SUBUSER') {
1781 # die "skey=$skey\nid=$id\nsid=$sid\n";
1784 if ($from eq 'GROUPMEMBER') {
1790 $opt_C ||= 'NOMAIL';
1791 } elsif ($to =~ m:^(//.*):) {
1793 if ($features =~ /XKEY/) {
1797 die "$0: server does not support XKEY\n";
1799 } elsif (grep /^[^@]*$/,@to and not $skey and not $gkey) {
1800 %AB = query_address_book($server,$port,$from);
1802 serverconnect($server,$port);
1803 query_sid($server,$port);
1806 # alias in local config?
1808 if ($alias{$to} =~ /(.+?):(.+)/) {
1813 # special extra upload
1814 system $0,split(/\s/,$opt),@argv,$ato;
1820 # alias in server address book?
1822 # do not substitute alias with expanded addresses because then
1823 # keep and autodelete options from address book will get lost
1826 # look for mutt aliases
1827 elsif ($to !~ /@/ and $to ne $from) {
1828 $to = get_mutt_alias($to);
1833 $to = join(',',grep /./,@to) or exit;
1834 # warn "Server/User: $fexcgi/$from\n" unless $opt_q;
1837 not $skey and not $gkey
1839 and $features =~ /CHECKRECIPIENT/
1840 and $opt_C !~ /^(DELETE|LIST|RECEIVEDLOG|SENDLOG|FOPLOG)$/
1842 checkrecipient($from,$to);
1844 serverconnect($server,$port);
1845 query_sid($server,$port);
1850 if (@ARGV > 1 and not ($opt_a or $opt_s or $opt_d)) {
1851 print "Archive name (name.tar, name.tgz or name.zip) or [RETURN] to send file for file:\n";
1858 if ($macos and not $opt_a and -d "@ARGV") {
1860 my $qdir = quote($dir);
1861 if (`du -s $qdir` =~ /^(\d+)/ and $1 < 2**21) {
1862 $opt_a = "$dir.zip";
1864 $opt_a = "$dir.tar";
1871 $opt_s =~ s/[^\w_.+-]/_/g;
1876 $opt_a =~ s/[^\w_.+-]/_/g;
1877 if ($opt_a =~ /(.+)\.(zip|tar|tgz|7z)$/) {
1881 die "$0: archive name must be one of ".
1882 "$opt_a.tar $opt_a.tgz $opt_a.zip\n";
1884 # no file argument left?
1886 # use file name as archive name
1891 foreach my $file (@ARGV) {
1892 die "$0: cannot read \"$file\"\n" unless -l $file or -r $file;
1894 $opt_a .= ".$atype" if $opt_a !~ /\.$atype$/;
1895 $transferfile = "$tmpdir/$opt_a";
1896 unlink $transferfile;
1897 print "Making fex archive ($opt_a):\n";
1898 if ($atype eq 'zip') {
1900 # if ($opt_c) { system(qw'7z a -tzip',$transferfile,@ARGV) }
1901 # else { system(qw'7z a -tzip -mm=copy',$transferfile,@ARGV) }
1902 system(qw'7z a -tzip',$transferfile,@ARGV);
1903 @files = ($transferfile);
1904 } elsif ($macos and scalar(@ARGV) == 1) {
1905 ## ditto-zip is now handled by formdatapost()
1909 # zip archives must be < 2 GB, so split as necessary
1910 @files = zipsplit($transferfile,@ARGV);
1911 if (scalar(@files) == 1) {
1912 $transferfile = $files[0];
1913 $transferfile =~ s/_1.zip$/.zip/;
1914 rename $files[0],$transferfile;
1915 @files = ($transferfile);
1918 @transferfiles = @files;
1919 } elsif ($atype eq '7z') {
1920 # http://www.7-zip.org/
1921 my @X = (); # exclude list
1923 foreach my $x (split('#',${'opt_#'})) {
1927 if ($opt_c) { system(qw'7z a',@X,$transferfile,@ARGV) }
1928 else { system(qw'7z a -t7z -mx0',@X,$transferfile,@ARGV) }
1929 @transferfiles = @files = ($transferfile);
1930 } elsif ($atype eq 'tar') {
1932 system(qw'7z a -ttar',$transferfile,@ARGV);
1933 @transferfiles = @files = ($transferfile);
1935 ## tar is now handled by formdatapost()
1936 # system(qw'tar cvf',$transferfile,@ARGV);
1940 } elsif ($atype eq 'tgz') {
1942 die "$0: archive type tgz not available, use tar, zip or 7z\n";
1944 ## tgz is now handled by formdatapost()
1945 # system(qw'tar cvzf',$transferfile,@ARGV);
1949 die "$0: unknown archive format \"$atype\"\n";
1952 if (@transferfiles) {
1954 # error in making transfer archive?
1956 unlink @transferfiles;
1957 die "$0: $! - aborting upload\n";
1960 # maybe timeout, so make new connect
1961 if (time-$t0 >= $timeout) {
1962 serverconnect($server,$port);
1963 query_sid($server,$port) unless $anonymous;
1983 die "$0: \"$file\" is not a regular file, try option -a\n"
1985 die "$0: \"$file\" does not exist\n";
1988 die "$0: cannot read \"$file\"\n" unless -r $file;
1995 foreach my $file (@files) {
1996 my @s = stat($file);
1997 unless (@s and ($s[2] & S_IROTH) and -r $file) {
1998 die "$0: \"$file\" is not world readable\n";
2003 foreach my $file (@files) {
2004 sleep 1; # do not overrun server!
2005 unless (-s $file or $opt_d or $opt_a or $opt_s) {
2006 die "$0: cannot send empty file \"$file\"\n";
2008 female_mode("send file $file?") if $opt_F;
2017 autodelete => $opt_D,
2020 if (not @r or not grep /\w/,@r) {
2021 die "$0: no response from server\n";
2023 next if "@r" eq '0'; # already transfered
2024 if (($r) = grep /^ERROR:/,@r) {
2025 if ($anonymous and $r =~ /purge it/) {
2026 die "$0: file is already on server for $to - use another anonymous recipent\n";
2027 } elsif ($r =~ /timeout/i) {
2033 die "$0: server error: $r\n";
2037 if (scalar(@r) == 1) {
2038 die "$0: server error: @r\n";
2040 if ($r[0] !~ /HTTP.1.. 2/) {
2041 if ($r[0] =~ /HTTP.[\s\d.]+(.+)/) {
2042 die "$0: server error: $1\n";
2044 die "$0: server error:\n".join("\n",@r)."\n";
2049 if (($r) = grep /<h3>\Q$file/,@r) {
2053 if ($opt_a !~ /^afex_\d+\.tar$/ and $file !~ /afex_\d+\.tar$/) {
2054 # print grep({s/^(X-Recipient:.*\((.+)\))/Parameters: $2\n/i} @r);
2059 if (/^(X-)?(Recipient.*)/i) {
2061 if (/notification=no/i) { $nonot = 1 }
2064 if (/^(X-)?(Location.*)/i) {
2068 if ($from eq $to or $from =~ /^\Q$to\E@/i
2069 or $nomail or $anonymous or $nonot)
2071 print "$recipient\n" if $recipient;
2072 print "$location\n" if $location;
2077 # delete transfer tmp file
2078 unlink $transferfile if $transferfile;
2084 my ($to,$n,$dkey,$file,$req);
2088 # look for single @ in arguments
2089 for (my $i=1; $i<$#ARGV; $i++) {
2090 if ($ARGV[$i] eq '@') {
2091 $ARGV[$i] = join(',',@ARGV[$i+1 .. $#ARGV]);
2097 # if ($windoof and not @ARGV) { &inquire }
2098 $to = pop @ARGV or die $usage;
2099 $to = $from if $to eq '.';
2100 if ($to !~ /@/ and $to ne $from) {
2101 $to = get_mutt_alias($to);
2104 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
2105 while (<$fexlist>) {
2106 if (/^\s*(\d+)\) (\w+) .\s*\d+ d. ([+-] )?(.+)/ and $1 eq $opt_f) {
2110 if ($file =~ s/ "(.*)"$//) {
2111 $opt_C ||= $1 if $1 ne 'NOMAIL';
2119 die "$0: file #$opt_f not found in fexlist\n";
2122 female_mode("forward file #$opt_f?") if $opt_F;
2124 serverconnect($server,$port);
2125 query_sid($server,$port);
2127 $req = "GET $proxy_prefix/fup?"
2128 ."from=$from&ID=$sid&to=$to&dkey=$dkey&command=FORWARD";
2129 $req .= "&comment=$opt_C" if $opt_C;
2130 $req .= "&keep=$opt_k" if $opt_k;
2131 $req .= "&autodelete=$opt_D" if $opt_D;
2132 $req .= "&$opt_X" if $opt_X;
2133 $req .= " HTTP/1.1";
2134 sendheader("$server:$port",$req);
2137 $fp =~ s/[^\w_.-]/.+/g; # because of UTF8 filename
2140 $status = 0 if /"$fp"/;
2141 print if $opt_v or /"$fp"/;
2145 die "$0: server failed, rerun command with option -v\n";
2153 my ($to,$n,$dkey,$file,$req,$recipient);
2156 die $usage if @ARGV;
2158 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
2159 while (<$fexlist>) {
2160 if (/^\s*(\d+)\) (\w+) .\s*\d+ d. (.+)/ and $1 eq $opt_N) {
2169 die "$0: file #$opt_N not found in fexlist\n";
2172 female_mode("resend notification for file #$opt_N?") if $opt_F;
2174 serverconnect($server,$port);
2175 query_sid($server,$port);
2177 $req = "GET $proxy_prefix/fup?"
2178 ."from=$from&ID=$sid&dkey=$dkey&command=RENOTIFY"
2180 sendheader("$server:$port",$req);
2184 print "<-- $_" if $opt_v;
2186 if (/^X-Notify: (.+)\/(.+)\/(.+)/) {
2193 print "notification e-mail for $file has been resent to $recipient\n";
2196 die "$0: server failed\n";
2198 die "$0: server failed, rerun command with option -v\n";
2208 my ($n,$dkey,$file,$req);
2211 die $usage if @ARGV;
2212 die $usage unless $opt_C or $opt_k or $opt_D;
2214 open $fexlist,$fexlist or die "$0: $fexlist - $!\n";
2215 while (<$fexlist>) {
2216 if (/^\s*(\d+)\) (\w+) .\s*\d+ d. (.+)/ and $1 eq $opt_x) {
2220 $file =~ s/ "(.*)"$//;
2227 die "$0: file #$opt_x not found in fexlist\n";
2230 female_mode("modify file #$opt_x?") if $opt_F;
2232 serverconnect($server,$port);
2233 query_sid($server,$port);
2235 $req = "GET $proxy_prefix/fup?"
2236 ."from=$from&ID=$sid&dkey=$dkey&command=MODIFY";
2237 $req .= "&comment=$opt_C" if $opt_C;
2238 $req .= "&keep=$opt_k" if $opt_k;
2239 $req .= "&autodelete=$opt_D" if $opt_D;
2240 $req .= " HTTP/1.1";
2241 sendheader("$server:$port",$req);
2256 my $transferfile = shift;
2260 # get transfer file from FEX server
2262 serverconnect($server,$port);
2263 query_sid($server,$port);
2266 xxget($from,$sid,$transferfile);
2269 unless (-s $transferfile) {
2270 unlink $transferfile;
2274 # no further processing if delivering to pipe
2275 exec 'cat',$transferfile unless -t STDOUT;
2277 if ($ft = `file $transferfile 2>/dev/null`) {
2278 if ($ft =~ /compressed/) {
2279 rename $transferfile,"$transferfile.gz";
2280 shelldo(ws("gunzip $transferfile.gz"));
2282 $ft = `file $transferfile`;
2284 # file command failed, so we look ourself into the file...
2285 elsif (open $transferfile,$transferfile) {
2286 read $transferfile,$_,4;
2287 close $transferfile;
2289 if (/\x1F\x8B\x08\x00/) {
2290 rename $transferfile,"$transferfile.gz";
2291 shelldo(ws("gunzip $transferfile.gz"));
2293 $ft = 'tar archive';
2296 if ($ft =~ /tar archive/) {
2297 rename $transferfile,"$transferfile.tar";
2298 $transferfile .= '.tar';
2302 print "Files in transfer-container:\n\n";
2303 shelldo(ws("tar tvf $transferfile"));
2304 print "\nExtract these files? [Yn] ";
2308 print "keeping $transferfile\n";
2310 my $untar = "tar xvf";
2311 # if ($> == 0 and `tar --help 2>&1` =~ /gnu/) {
2312 # $untar = "tar --no-same-owner -xvf";
2314 system("$untar $transferfile && rm $transferfile");
2315 die "$0: error while untaring, see $transferfile\n" if -f $transferfile;
2318 exec 'cat',$transferfile;
2326 my ($boundary,$filename,$length,$buf,$file,$fpsize,$resume,$seek,$nettest);
2328 my (@hh,@hb,@r,@pv,$to);
2329 my ($bytes,$b,$t,$bt);
2330 my ($t0,$t1,$t2,$tt,$tc);
2331 my $bs = 2**16; # blocksize for reading and sending file
2332 my $fileid = int(time);
2335 my $connection = '';
2338 my ($tar,$ditto,$aname,$atype,$list,$error,$location,$transferfile);
2341 if (defined($file = $P{file})) {
2343 $to = $AB{$P{to}} || $P{to}; # for gpg
2345 # special file: stream from STDIN
2347 $filename = encode_utf8($file);
2355 $if =~ s/([^_\w\.\-])/\\$1/g;
2356 $transferfile = $tmpdir . '/' . basename($file) . '.gz';
2357 $of = $transferfile;
2358 $of =~ s/([^_\w\.\-])/\\$1/g;
2359 shelldo("gzip <$if>$of");
2360 $filesize = -s $transferfile;
2361 die "$0: cannot gzip \"$file\"\n" unless $filesize;
2362 $file = $transferfile;
2365 # special file: tar-on-the-fly
2366 if (not $windoof and $opt_a and $file =~ /(.+)\.(tar|tgz)$/) {
2369 $list = "$tmpdir/$aname.list";
2370 $error = "$tmpdir/$aname.error";
2372 $tar .= 'z' if $atype eq 'tgz';
2373 if (`tar --help 2>/dev/null` =~ /--index-file/) {
2374 $tar .= " --index-file=$list -f-";
2379 foreach my $x (split('#',${'opt_#'})) {
2380 $tar .= " --exclude=$x";
2384 $tar .= ' '.quote($_);
2386 # print "calculating archive size... ";
2387 open $tar,"$tar 2>$error|" or die "$0: cannot run tar - $!\n";
2388 $t0 = int(time) if -t STDOUT;
2389 while ($b = read $tar,$_,$bs) {
2394 printf "Archive size: %d MB\r",int($filesize/M);
2399 printf "Archive size: %d MB\n",int($filesize/M) if -t STDOUT;
2400 unless (close $tar) {
2402 if (open $error,$error) {
2407 unlink $list,$error;
2408 die "$0: tar error:\n$_";
2410 $file = "$aname.$atype";
2411 $filename = encode_utf8($file);
2412 undef $SH; # force reconnect (timeout!)
2415 # special file: ditto-zip-on-the-fly
2416 # ditto: Can't archive multiple sources
2417 elsif ($macos and $opt_a and $file =~ /(.+)\.(zip)$/ and scalar(@ARGV) == 1) {
2420 $list = "$tmpdir/$aname.list";
2421 $error = "$tmpdir/$aname.error";
2422 $ditto = 'ditto -c -k --sequesterRsrc --keepParent';
2423 if (-d "@ARGV" and "@ARGV" =~ m:^(.+)/(.+):) {
2426 $file =~ s/([^\w\-\@\#%,.=+_:])/\\$1/g;
2427 $ditto .= ' '.$file;
2431 $file =~ s/([^\w\-\@\#%,.=+_:])/\\$1/g;
2432 $ditto .= ' '.$file;
2435 # print "calculating archive size... ";
2436 debug("cd $dittodir;$ditto -");
2437 open $ditto,"cd $dittodir;$ditto - 2>$error|"
2438 or die "$0: cannot run ditto - $!\n";
2439 $t0 = int(time) if -t STDOUT;
2440 while ($b = read $ditto,$_,$bs) {
2445 printf "Archive size: %d MB\r",int($filesize/M);
2450 printf "Archive size: %d MB\n",int($filesize/M) if -t STDOUT;
2451 unless (close $ditto) {
2453 if (-s $error and open $error,$error) {
2458 unlink $list,$error;
2459 die "$0: ditto-zip error:\n$_";
2461 unlink $list,$error;
2462 $file = "$aname.$atype";
2463 $filename = encode_utf8($file);
2464 undef $SH; # force reconnect (timeout!)
2467 elsif ($P{to} eq 'nettest') {
2468 $filename = $nettest = 'nettest';
2469 $filesize = $P{size};
2475 $filename = encode_utf8(${'opt_='} || $file);
2478 $filename =~ s/^[a-z]://;
2479 $filename =~ s/.*\\//;
2481 $filename =~ s:.*/::;
2482 $filename =~ s:[\r\n]+: :g;
2485 } elsif (not $opt_g and not $opt_s) {
2486 $filesize = -s $file or die "$0: \"$file\" is empty or not readable\n";
2490 $filename .= '.gpg' if $opt_g;
2492 unless ($opt_d or $nettest) {
2495 $fileid = int(time);
2498 $fileid = md5_hex(fmd(@ARGV));
2500 $fileid = fileid($file);
2506 $file = $filename = '';
2512 @hh = (); # HTTP header
2513 @hb = (); # HTTP body
2520 serverconnect($server,$port);
2521 query_sid($server,$port) unless $anonymous or $nettest;
2524 $P{id} = $sid; # ugly hack!
2526 $filename =~ s/\\/_/g; # \ is a illegal character for fexsrv
2528 # ask server if this file has been already sent
2529 if ($file and not $xx and not $nettest) {
2530 if (not $opt_d and $opt_o) {
2531 # delete before overwrite
2532 delete_file($from,$to,$filename);
2533 serverconnect($server,$port);
2534 query_sid($server,$port) unless $anonymous;
2535 $P{id} = $sid; # ugly hack!
2536 } elsif (not($opt_s or $opt_g or $opt_d or $opt_l or $opt_L or ${'opt_/'})) {
2537 ($seek,$location) = query_file($server,$port,
2538 $frecipient||$P{to},$P{from},$P{id},$filename,$fileid);
2539 if ($filesize == $seek) {
2540 print "Location: $location\n" if $location and $nomail;
2541 warn "$0: $file has been already transferred\n";
2543 } elsif ($seek and $seek < $filesize) {
2544 $resume = " (resuming at byte $seek)";
2545 } elsif ($filesize <= $seek) {
2550 sleep 1; # do not overrun proxy
2551 serverconnect($server,$port);
2556 if ($chunksize and $proxy and $port != 443
2557 and $filesize - $seek > $chunksize - $bs) {
2558 if ($features !~ /MULTIPOST/) {
2559 die sprintf("$0: server does not support chunked multi-POST needed for"
2560 ." files > %d MB via proxy\n",$chunksize/M);
2562 $opt_o = 0; # no overwriting mode for next chunks
2563 $fpsize = $chunksize - $bs;
2565 $fpsize = $filesize - $seek;
2568 $boundary = randstring(48);
2571 $P{filesize} = $filesize;
2573 # send HTTP POST variables
2576 @pv = qw'from to skey keep autodelete comment seek filesize';
2579 @pv = qw'from to gkey keep autodelete comment seek filesize';
2581 @pv = qw'from to id replyto keep autodelete comment command seek filesize';
2583 foreach my $v (@pv) {
2586 push @hb,"--$boundary";
2587 push @hb,"Content-Disposition: form-data; name=\"$name\"";
2589 # push @hb,encode_utf8($P{$v});
2594 # at last, POST the file
2596 push @hb,"--$boundary";
2597 push @hb,"Content-Disposition: form-data; name=\"FILE\"; filename=\"$filename\"";
2599 if ($opt_M) { push @hb,"Content-Type: application/x-mime" }
2600 else { push @hb,"Content-Type: application/octet-stream" }
2602 $flink = abs_path($file);
2603 push @hb,"Content-Location: $flink";
2605 # push @hb,"Content-Length: " . ((-s $file||0) - $seek); # optional header!
2606 push @hb,"Content-Length: $fpsize"; # optional header! NOT filesize!
2607 push @hb,"X-File-ID: $fileid";
2612 # prevent proxy chunked mode reply
2613 $connection = "close";
2616 push @hb,"--$boundary--";
2621 $length = length(join('',@hb)) + scalar(@hb)*2 + $fpsize;
2624 if ($file and not $opt_d) {
2625 if ($flink) { $hb[-2] = $flink }
2626 else { $hb[-2] = '(file content)' }
2628 # any other extra URL arguments
2630 $opt_X = "?$::opt_X" if $::opt_X and $file;
2633 push @hh,"POST $proxy_prefix/fup$opt_X HTTP/1.1";
2634 push @hh,"Host: $server:$port";
2635 push @hh,"User-Agent: $useragent";
2636 push @hh,"Content-Length: $length";
2637 push @hh,"Content-Type: multipart/form-data; boundary=$boundary";
2638 push @hh,"Connection: $connection" if $connection;
2642 print "--> $_\n" foreach (@hh,@hb);
2645 $SIG{PIPE} = \&sigpipehandler;
2646 # foreach $sig (keys %SIG) {
2647 # eval '$SIG{$sig} = sub { print "\n!!! SIGNAL '.$sig.' !!!\n"; exit; }';
2652 pop @hb unless $flink;
2653 nvtsend(@hh,@hb) or do {
2654 warn "$0: server has closed the connection, reconnecting...\n";
2656 goto FORMDATAPOST; # necessary: new $sid ==> new @hh
2659 unless ($opt_d or $flink) {
2661 $t0 = $t2 = int(time);
2668 open $file,"gpg -e -r $to|" or die "$0: cannot run gpg - $!\n";
2670 open $file,'>&=STDIN' or die "$0: cannot open STDIN - $!\n";
2674 open $file,"$tar|gpg -e -r $to|" or die "$0: cannot run tar&gpg - $!\n";
2676 open $file,"$tar|" or die "$0: cannot run tar - $!\n";
2680 if (defined $tpid and $tpid == 0) {
2682 if (open $list,$list) {
2683 # print "\n$tar|\n"; system "ls -l $list";
2686 print ' 'x(length($file)+40),"\r",$_;
2693 $SIG{CHLD} = 'IGNORE';
2696 print "Fast forward to byte $seek (resuming)\n";
2697 readahead($file,$seek);
2700 $ditto =~ s/ditto/ditto -V/;
2701 open $file,"cd $dittodir;$ditto -|" or die "$0: cannot run ditto - $!\n";
2703 print "Fast forward to byte $seek (resuming)\n";
2704 readahead($file,$seek);
2706 } elsif ($nettest) {
2710 my $fileq = quote($file);
2711 open $file,"gpg -e -r $to <$fileq|" or die "$0: cannot run gpg - $!\n";
2713 open $file,$file or die "$0: cannot read \"$file\" - $!\n";
2722 print $rcamel[0] if ${'opt_+'};
2724 $buf = '#' x $bs if $nettest;
2726 $SIG{ALRM} = sub { retry("timed out") };
2728 while ($bytes < $fpsize) {
2732 $b = read $file,$buf,$bs;
2737 print {$SH} $buf or &sigpipehandler;
2739 syswrite $SH,$buf or &sigpipehandler;
2743 if (not $nettest and $filesize > 0 and $bytes+$seek > $filesize) {
2748 die "$0: \"$file\" filesize has grown while uploading\n";
2752 if (${'opt_+'} and int($t2*10)>$tc) {
2753 print $rcamel[$tc%2+1];
2756 if (not $opt_q and -t STDOUT and int($t2)>$t1) {
2757 &sigpipehandler unless $SH->connected;
2758 # smaller block size is better on slow links
2759 $bs = 4096 if $t1 and $bs>4096 and $bytes/($t2-$t0)<65536;
2760 if ($filesize > 0) {
2761 $pct = sprintf "(%d%%)",int(($bytes+$seek)/$filesize*100);
2763 if ($bytes>2*M and $bs>4096) {
2764 printf STDERR "%s: %d MB of %d MB %s %d kB/s \r",
2765 $opt_s||$opt_a||$file,
2766 int(($bytes+$seek)/M),
2769 int($bt/k/($t2-$tt));
2771 printf STDERR "%s: %d kB of %d MB %s %d kB/s \r",
2772 $opt_s||$opt_a||$file,
2773 int(($bytes+$seek)/k),
2776 int($bt/k/($t2-$tt));
2779 # time window for transfer rate calculation
2785 last if $filesize > 0 and $bytes >= $fpsize;
2786 sleep 1 while ($opt_m and $bytes/k/(time-$t0||1) > $opt_m);
2789 close $file unless $nettest;
2793 print $rcamel[2] if ${'opt_+'};
2795 # terminate tar verbose output job
2802 if ($fileid =~ /[a-z]/ and not ($opt_s or $opt_g)) {
2804 if ($fileid ne md5_hex(fmd(@ARGV))) {
2805 print "\n" unless $opt_q;
2806 die "$0: files have been modified while uploading\n";
2809 if ($fileid ne fileid($file)) {
2810 print "\n" unless $opt_q;
2811 die "$0: file has been modified while uploading\n";
2817 if (not $chunksize and $bytes+$seek < $filesize) {
2818 die "$0: \"$file\" filesize has shrunk while uploading\n";
2821 if ($seek or $chunksize and $chunksize < $filesize) {
2823 printf STDERR "%s: %d MB in %d s = %d kB/s",
2824 $opt_s||$opt_a||$file,
2828 if ($bytes+$seek == $filesize) {
2829 printf STDERR ", total %d MB\n",int($filesize/M);
2831 printf STDERR ", chunk #%d : %d MB\n",
2832 $chunk,int(($bytes+$seek)/M);
2835 printf STDERR "%s: %d kB in %d s = %d kB/s",
2836 $opt_s||$opt_a||$file,
2840 if ($bytes+$seek == $filesize) {
2841 printf STDERR ", total %d kB\n",int($filesize/k);
2843 printf STDERR ", chunk #%d : %d kB\n",
2844 $chunk,int(($bytes+$seek)/k);
2849 printf STDERR "%s: %d MB in %d s = %d kB/s \n",
2850 $opt_s||$opt_a||$file,
2855 printf STDERR "%s: %d kB in %d s = %d kB/s \n",
2856 $opt_s||$opt_a||$file,
2863 if (-t STDOUT and not ($opt_s or $opt_g or $nettest)) {
2864 print STDERR "waiting for server ok..."
2870 print {$SH} "\r\n--$boundary--\r\n";
2871 # return if $nettest;
2873 # special handling of streaming file because of stunnel tcp shutdown bug
2874 if ($opt_s or $opt_g) {
2877 serverconnect($server,$port);
2878 query_sid($server,$port) unless $anonymous;
2879 ($seek,$location) = query_file($server,$port,$P{to},$P{from},$sid,
2881 if ($seek != $bytes) {
2882 die "$0: streamed $bytes bytes but server received $seek bytes\n";
2884 return "X-Location: $location\n";
2890 printf STDERR "%s: %d MB\n",$flink,int($bytes/M);
2892 printf STDERR "%s: %d kB\n",$flink,int($bytes/k);
2900 # SuSe: Can't locate object method "BINMODE" via package "IO::Socket::SSL::SSL_HANDLE"
2901 # binmode $SH,':utf8';
2903 if (not $opt_q and $file and -t STDOUT) {
2904 print STDERR "\r \r";
2908 print "<-- $_\n" if $opt_v;
2909 last if @r and $r[0] =~ / 204 / and /^$/ or /<\/html>/i;
2910 push @r,decode_utf8($_);
2916 if ($proxy and $fpsize+$seek < $filesize) {
2927 my @rc = ('A'..'Z','a'..'z',0..9 );
2931 for (1..$n) { $rs .= $rc[int(rand($rn))] };
2937 my $zipbase = shift;
2941 my ($zsize,$size,$n);
2943 $zipbase =~ s/\.zip$//;
2944 map { s/([^_\w\+\-\.])/\\$1/g } @files;
2946 open my $ff,"find @files|" or die "$0: cannot search for @_ - $!\n";
2953 die "$0: too many zip-archives\n";
2956 while ($file = <$ff>) {
2958 # next if -l $file or not -f $file;
2959 next unless -f $file;
2961 if ($size > 2147480000) {
2963 die "$0: \"$file\" too big for zip\n";
2965 if ($zsize + $size > 2147000000) {
2966 push @zipfiles,zip($zipbase.'_'.$n.'.zip',@files);
2977 push @zipfiles,zip($zipbase.'_'.$n.'.zip',@files);
2989 # if ($opt_c) { $cmd = "zip -@ $zip" }
2990 # else { $cmd = "zip -0 -@ $zip" }
2991 $cmd = "zip -@ $zip";
2993 ${'opt_#'} =~ s/#/ /g;
2994 $cmd .= " -x ".${'opt_#'};
2996 print $cmd,"\n" if $opt_v;
2997 open $cmd,"|$cmd" or die "$0: cannot create $zip - $!\n";
2999 print {$cmd} $_."\n";
3000 print " $_\n" if $opt_v;
3002 close $cmd or die "$0: zip failed - $!\n";
3018 return $_ if length($_);
3025 my ($server,$port,$to,$from,$id,$filename,$fileid) = @_;
3028 my ($head,$location);
3029 my ($response,$fexsrv,$cc);
3033 $to = $AB{$to} if $AB{$to};
3034 $filename =~ s/([^_=:,;<>()+.\w\-])/'%'.uc(unpack("H2",$1))/ge; # urlencode
3036 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??SKEY=$id HTTP/1.1";
3038 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??GKEY=$id HTTP/1.1";
3040 $head = "HEAD $proxy_prefix/fop/$to/$from/$filename??ID=$id HTTP/1.1";
3042 sendheader("$server:$port",$head);
3044 unless (defined $_ and /\w/) {
3045 die "$0: no response from server\n";
3048 print "<-- $_" if $opt_v;
3049 unless (/^HTTP.* 200/) {
3054 print "<-- $_" if $opt_v;
3055 $fexsrv = $_ if /^(Server: fexsrv|X-Features:)/;
3058 die "$0: no fexserver at $server:$port\n" unless $fexsrv;
3059 die "$0: server response: $response";
3063 print "<-- $_" if $opt_v;
3065 if (/^Content-Length:\s+(\d+)/) { $seek = $1 }
3066 if (/^X-File-ID:\s+(.+)/) { $qfileid = $1 }
3067 if (/^X-Features:\s+(.+)/) { $features = $1 }
3068 if (/^X-Location:\s+(.+)/) { $location = $1 }
3069 if (/^Connection: close/) { $cc = $_ }
3072 # return true seek only if file is identified
3073 $seek = 0 if $qfileid and $qfileid ne $fileid;
3076 serverconnect($server,$port);
3080 return ($seek,$location);
3084 sub edit_address_book {
3087 my $ab = "$fexhome/ADDRESS_BOOK";
3091 die "$0: address book not available for subusers\n" if $skey;
3092 die "$0: address book not available for group members\n" if $gkey;
3094 female_mode("edit your address book?") if $opt_F;
3096 %AB = query_address_book($server,$port,$user);
3097 if ($AB{ADDRESS_BOOK} !~ /\w/) {
3099 "# Format: alias e-mail-address # Comment\n".
3101 "framstag framstag\@rus.uni-stuttgart.de\n";
3103 open $ab,">$ab" or die "$0: cannot write to $ab - $!\n";
3104 print {$ab} $AB{ADDRESS_BOOK};
3107 system "$editor $ab";
3112 serverconnect($server,$port);
3113 query_sid($server,$port);
3126 sub query_address_book {
3127 my ($server,$port,$user) = @_;
3128 my ($req,$alias,$address,$options,$comment,$cl,$ab,$b);
3133 serverconnect($server,$port);
3134 query_sid($server,$port);
3137 $req = "GET $proxy_prefix/fop/$user/$user/ADDRESS_BOOK?ID=$sid HTTP/1.1";
3138 sendheader("$server:$port",$req);
3140 unless (defined $_ and /\w/) {
3141 die "$0: no response from server\n";
3144 print "<-- $_" if $opt_v;
3145 unless (/^HTTP.* 200/) {
3146 if (/^HTTP.* 404/) {
3147 while (<$SH>) { last if /^\r?\n/ }
3150 # s:HTTP/[\d\. ]+::;
3151 # die "$0: server response: $_";
3159 print "<-- $_" if $opt_v;
3161 $cl = $1 if /^Content-Length: (\d+)/;
3171 print "<-- $_\n" if $opt_v;
3175 ($alias,$address,$options) = split;
3177 if ($options) { $options =~ s/[()]//g }
3178 else { $options = '' }
3179 $AB{$alias} = $address;
3180 $AB{$alias}->{options} = $options||'';
3181 $AB{$alias}->{comment} = $comment||'';
3182 if ($options and $options =~ /keep=(\d+)/i) {
3183 $AB{$alias}->{keep} = $1;
3185 if ($options and $options =~ /autodelete=(\w+)/i) {
3186 $AB{$alias}->{autodelete} = $1;
3194 $AB{ADDRESS_BOOK} = $ab;
3200 # sets global $sid $features $timeout # ugly hack! :-}
3202 my ($server,$port) = @_;
3208 if ($port eq 443 or $proxy) {
3209 return if $features; # early return if we know enough
3210 $req = "OPTIONS /FEX HTTP/1.1";
3211 $req = "HEAD /index.html HTTP/1.1";
3213 $req = "GET /SID HTTP/1.1";
3216 sendheader("$server:$port",$req);
3218 unless (defined $_ and /\w/) {
3219 print "\n" if $opt_v;
3220 die "$0: no response from server\n";
3223 print "<-- $_" if $opt_v;
3225 if ($req =~ /OPTIONS/ and /^HTTP.* 502 /) {
3226 # (reverse) proxy error
3228 serverconnect($server,$port);
3229 $req = "GET /SID HTTP/1.0";
3230 sendheader("$server:$port",$req);
3232 unless (defined $_ and /\w/) {
3233 print "\n" if $opt_v;
3234 die "$0: no response from server\n";
3237 print "<-- $_" if $opt_v;
3240 print "<-- $_" if $opt_v;
3241 $features = $1 if /^X-Features: (.+)/;
3242 $timeout = $1 if /^X-Timeout: (\d+)/;
3246 serverconnect($server,$port);
3247 } elsif (/^HTTP.* [25]0[01] /) {
3248 if (not $proxy and $port ne 443 and /^HTTP.* 201 (.+)/) {
3249 $sid = 'MD5H:'.md5_hex($id.$1);
3254 print "<-- $_" if $opt_v;
3255 $features = $1 if /^X-Features: (.+)/;
3256 $timeout = $1 if /^X-Timeout: (\d+)/;
3257 $cc = $_ if /^Connection: close/;
3261 serverconnect($server,$port);
3264 } elsif (/^HTTP.* 301 /) {
3265 while (<$SH>) { last if /Location/ }
3266 die "$0: cannot use $server:$port because server has a redirection to\n".$_;
3268 # no SID support - perhaps transparent web proxy?
3271 print "<-- $_" if $opt_v;
3272 $fexsrv = $_ if /^(Server: fexsrv|X-Features:)/;
3275 die "$0: no fexserver at $server:$port\n" unless $fexsrv;
3276 serverconnect($server,$port);
3280 # warn "proxy: $proxy\n";
3282 serverconnect($server,$port);
3290 my ($from,$id,$save) = @_;
3293 my ($url,$B,$b,$t0,$t1,$cl);
3298 $url = "$proxy_prefix/fop/$from/$from/$xx?ID=$id";
3300 sendheader("$server:$port","GET $url HTTP/1.0");
3304 print "<-- $_" if $opt_v;
3305 $cl = $1 if /^Content-Length:\s(\d+)/;
3306 # $ft = $1 if /^X-File-Type:\s(.+)/;
3310 die "$0: no Content-Length in server-reply\n" unless $cl;
3312 open $save,">$save" or die "$0: cannot write to $save - $!\n";
3315 $t0 = $t1 = int(time);
3318 while ($b = read($SH,$_,$bs)) {
3321 if (int(time) > $t1) {
3325 print STDERR $ts,"\r";
3329 sleep 1 while ($opt_m and $B/k/(time-$t0||1) > $opt_m);
3332 print STDERR ts($B,$cl),"\n";
3340 return sprintf("transferred: %d MB (%d%%)",int($b/M),int($b/$tb*100));
3344 sub sigpipehandler {
3350 local $SIG{ALRM} = sub { };
3356 kill 9,$tpid if $tpid;
3357 if (@r and $opt_v) {
3358 die "\n$0: ($$) server error: @r\n";
3360 if (@r and $r[0] =~ /^HTTP.* \d+ (.*)/) {
3361 die "\n$0: server error: $1\n";
3365 warn "\n$0: connection to $server $reason\n";
3366 warn "retrying after $timeout seconds...\n";
3368 if ($windoof) { exec $^X,$0,@_ARGV }
3369 else { exec $_0,@_ARGV }
3374 sub checkrecipient {
3375 my ($from,$to) = @_;
3383 command => 'CHECKRECIPIENT',
3386 $_ = shift @r or die "$0: no reply from server\n";
3389 return if $to eq 'nettest';
3392 if (s/X-(Recipient: .+)/$1\n/) {
3393 s/autodelete=\w+/autodelete=$opt_D/ if $opt_D;
3394 s/keep=\d+/keep=$opt_k/ if $opt_k;
3396 $frecipient ||= (split)[1];
3400 http_response($_,@r);
3405 # get ID data from ID file
3409 $fexcgi = getline($idf) || die "$0: no FEX-URL in $idf\n";
3410 $from = getline($idf) || die "$0: no FROM in $idf\n";
3411 $id = getline($idf) || die "$0: no ID in $idf\n";
3412 if ($fexcgi =~ s/!([\w.-]+:\d+)(:(\d+))?//) {
3414 $chunksize = $3 || 0;
3416 unless ($fexcgi =~ /^[_:=\w\-\.\/\@\%]+$/) {
3417 die "$0: illegal FEX-URL \"$fexcgi\" in $idf\n";
3419 unless ($from =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
3420 die "$0: illegal FROM \"$from\" in $idf\n";
3430 print "file to send: ";
3431 chomp($file = <STDIN>);
3435 warn "$file does not exist\n";
3437 print "recipient (e-mail address): ";
3438 chomp($to = <STDIN>);
3439 die $usage unless $to;
3442 chomp($opt_C = <STDIN>);
3444 @ARGV = ($file,$to);
3449 if (system(@_) < 0) { die "failed: @_\n" }
3453 # emulate seek on a pipe
3455 my $fh = shift; # filehandle
3456 my $ba = shift; # bytes ahead
3464 $n = $bs if $n > $bs;
3465 $s += read $fh,$_,$n;
3472 my @s = stat($file);
3475 return md5_hex($file.$s[0].$s[1].$s[7].$s[9]);
3477 warn "$0: $file - $!\n";
3483 sub get_mutt_alias {
3485 my $ma = $HOME.'/.mutt/aliases';
3486 my ($alias,$options);
3489 $to =~ s/(:.+)// and $options = $1;
3490 open $ma,$ma or return $to;
3492 if (/^alias \Q$to\E\s/i) {
3500 warn "$0: ignoring mutt multi-alias $to = $_\n";
3505 warn "$0: found mutt alias $to = $alias\n";
3506 $alias .= $options if $options;
3512 $to = "$to:$options" if $options;
3513 return ($alias||$to);
3517 # collect (hashed) file meta data
3523 foreach $file (@files) {
3524 if (not -l $file and -d $file) {
3526 if (opendir $dir,$dir) {
3527 while (defined ($file = readdir($dir))) {
3528 next if $file eq '..';
3530 $fmd .= fileid($dir);
3532 $fmd .= fmd("$dir/$file");
3538 $fmd .= fileid($file);
3546 # from MIME::Base64::Perl
3552 tr|A-Za-z0-9+=/||cd;
3554 tr|A-Za-z0-9+/| -_|;
3555 return "" unless length;
3558 for ($i = 0; $i <= $l; $i += 60) {
3559 $uu .= "M" . substr($_,$i,60);
3563 $uu .= chr(32+(length)*3/4) . $_;
3565 return unpack("u",$uu);
3571 if (open my $tty,'/dev/tty') {
3575 " [p] perhaps - don't know\n",
3579 if (/^y/i) { return }
3581 if (/^p/i) { int(rand(2)) ? return : exit }
3588 local $_ = shift || <$SH>;
3592 $_ = <$SH> unless $_;
3593 unless (defined $_ and /\w/) {
3594 die "$0: no response from server\n";
3597 print "<-- $_\n" if $opt_v;
3598 # CGI fatalsToBrowser
3599 if (/^HTTP.* 500/) {
3600 @r = <$SH> unless @r;
3602 die "$0: server error: $_\n@r\n";
3604 unless (/^HTTP.* 200/) {
3606 $error =~ s/HTTP.[\s\d.]+//;
3607 @r = <$SH> unless @r;
3611 $error .= "\n".$_ if /^Location/;
3612 print "<-- $_\n" if $opt_v;
3614 die "$0: server error: $error\n";
3628 my $cfb = '### common functions ###';
3633 open $0,$0 or die "cannot read $0 - $!\n";
3636 $cfc =~ s/.*\n$cfb\n//s;
3638 foreach my $p (qw'fexget sexsend') {
3639 open $p,$p or die "cannot read $p - $!\n";
3642 s/\n$cfb.*/\n$cfb\n$cfc/s;
3644 open $p,'>',$p or die "cannot write $p - $!\n";
3649 exec "l fexsend fexget sexsend";
3653 ### common functions ###
3657 my @d = localtime((stat shift)[9]);
3658 return sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]);
3664 s/\%([a-f\d]{2})/chr(hex($1))/ige;
3670 # set SSL/TLS options
3671 $SSL{SSL_verify_mode} = $ENV{SSLVERIFY} if defined($ENV{SSLVERIFY});
3672 foreach my $opt (qw(
3681 $SSL{$opt} = $ENV{$env} if defined($ENV{$env});
3684 if ($SSL{SSL_verify_mode}) {
3686 unless ($SSL{SSL_ca_path} or $SSL{SSL_ca_file}) {
3687 die "$0: \$SSLVERIFYMODE, but not valid \$SSLCAPATH or \$SSLCAFILE\n";
3689 } elsif (defined($SSL{SSL_verify_mode})) {
3690 # user has set SSLVERIFY=0 !
3693 $SSL{SSL_verify_mode} = 1 if $SSL{SSL_ca_path} or $SSL{SSL_ca_file};
3699 return if $SSL{SSL_ca_file} or $SSL{SSL_ca_path};
3700 foreach (qw(/etc/ssl/certs/ca-certificates.crt)) {
3702 $SSL{SSL_ca_file} = $_;
3706 foreach (qw(/etc/ssl/certs /etc/pki/tls/certs)) {
3708 $SSL{SSL_ca_path} = $_;
3716 my ($server,$port) = @_;
3717 my $connect = "CONNECT $server:$port HTTP/1.1";
3721 tcpconnect(split(':',$proxy));
3723 printf "--> %s\n",$connect if $opt_v;
3724 nvtsend($connect,"");
3727 printf "<-- $_"if $opt_v;
3728 unless (/^HTTP.1.. 200/) {
3729 die "$0: proxy error : $_";
3732 $SH = IO::Socket::SSL->start_SSL($SH,%SSL);
3735 tcpconnect($server,$port);
3737 # if ($https and $opt_v) {
3738 # printf "%s\n",$SH->get_cipher();
3743 # set up tcp/ip connection
3745 my ($server,$port) = @_;
3753 # eval "use IO::Socket::SSL qw(debug3)";
3755 $SH = IO::Socket::SSL->new(
3756 PeerAddr => $server,
3762 $SH = IO::Socket::INET->new(
3763 PeerAddr => $server,
3773 die "$0: cannot connect $server:$port - $@\n";
3776 print "TCPCONNECT to $server:$port\n" if $opt_v;
3781 eval "use IO::Socket::SSL";
3782 die "$0: cannot load IO::Socket::SSL\n" if $@;
3783 eval '$SSL{SSL_verify_mode} = 0 if Net::SSLeay::SSLeay() <= 9470143';
3785 foreach my $v (keys %SSL) {
3786 printf "%s => %s\n",$v,$SSL{$v};
3797 push @head,"Host: $sp";
3798 push @head,"User-Agent: $useragent";
3800 foreach $head (@head) {
3802 print "--> $head\n" if $opt_v;
3803 print {$SH} $head,"\r\n";
3805 print "-->\n" if $opt_v;
3811 local $SIG{PIPE} = sub { $sigpipe = "@_" };
3815 die "$0: internal error: no active network handle\n" unless $SH;
3816 die "$0: remote host has closed the link\n" unless $SH->connected;
3818 foreach my $line (@_) {
3819 print {$SH} $line,"\r\n";
3832 s/([^\w\@\/%^,.=+_:+-])/\\$1/g;
3838 print "## DEBUG: @_\n" if $DEBUG;
3842 # from MIME::Base64::Perl
3849 $res = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
3850 $res =~ tr|` -_|AA-Za-z0-9+/|;
3851 $padding = (3-length($_[0])%3)%3;
3852 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;