| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# CLI client for the FEX service for retrieving files |
| 4 |
# |
| 5 |
# see also: fexsend |
| 6 |
# |
| 7 |
# Author: Ulli Horlacher <[email protected]> |
| 8 |
# |
| 9 |
# Perl Artistic Licence |
| 10 |
|
| 11 |
use 5.010; |
| 12 |
use strict qw'vars subs'; |
| 13 |
use Config; |
| 14 |
use POSIX; |
| 15 |
use Encode; |
| 16 |
use Cwd 'abs_path'; |
| 17 |
use Getopt::Std; |
| 18 |
use File::Basename; |
| 19 |
use Socket; |
| 20 |
use IO::Handle; |
| 21 |
use IO::Socket::INET; |
| 22 |
use Time::HiRes 'time'; |
| 23 |
use constant k => 2**10; |
| 24 |
use constant M => 2**20; |
| 25 |
|
| 26 |
eval 'use Net::INET6Glue::INET_is_INET6'; |
| 27 |
|
| 28 |
$| = 1; |
| 29 |
|
| 30 |
our $SH; |
| 31 |
our ($fexhome,$idf,$tmpdir,$windoof,$useragent,$cpid); |
| 32 |
our ($xv,%autoview); |
| 33 |
our $bs = 2**16; # blocksize for tcp-reading and writing file |
| 34 |
our $version = 20200429; |
| 35 |
our $CTYPE = 'ISO-8859-1'; |
| 36 |
our $fexsend = $ENV{FEXSEND}; |
| 37 |
our $del = ''; |
| 38 |
our $DEBUG = $ENV{DEBUG}; |
| 39 |
our $_0 = $0; |
| 40 |
our $prg = $0; |
| 41 |
our $proxy = ''; |
| 42 |
our $proxy_prefix = ''; |
| 43 |
our $chunksize = 0; |
| 44 |
our $sleepwait = 1; |
| 45 |
|
| 46 |
our ($opt_h,$opt_v,$opt_l,$opt_d,$opt_m,$opt_z,$opt_K,$opt_o,$opt_a,$opt_t); |
| 47 |
our ($opt_q,$opt_s,$opt_k,$opt_i,$opt_V,$opt_X,$opt_f,$opt_P,$opt_L,$opt_H); |
| 48 |
our ($opt_p,$opt_w,$opt_W); |
| 49 |
|
| 50 |
our %SSL = (SSL_version => 'TLSv1_2'); |
| 51 |
|
| 52 |
my $sigpipe; |
| 53 |
my $atype = '\.(tgz|tar|zip|7z)$'; |
| 54 |
my $status = 0; |
| 55 |
|
| 56 |
unless ($version) { |
| 57 |
my @d = localtime((stat $0)[9]); |
| 58 |
$version = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]) |
| 59 |
} |
| 60 |
$version .= '_sw' if abs_path($0) =~ m:/sw/:; |
| 61 |
|
| 62 |
$ENV{LANGUAGE} = 'en'; |
| 63 |
$ENV{LC_MESSAGES} = 'C'; |
| 64 |
|
| 65 |
# inquire default character set |
| 66 |
# cannot use "use I18N::Langinfo" because of no windows support! |
| 67 |
eval { |
| 68 |
local $^W = 0; |
| 69 |
require I18N::Langinfo; |
| 70 |
I18N::Langinfo->import(qw'langinfo CODESET'); |
| 71 |
$CTYPE = langinfo(CODESET()); |
| 72 |
}; |
| 73 |
|
| 74 |
if ($Config{osname} =~ /^mswin/i) { |
| 75 |
$windoof = $Config{osname}; |
| 76 |
$ENV{HOME} = $ENV{USERPROFILE}; |
| 77 |
$fexhome = $ENV{FEXHOME} || $ENV{HOME}.'/fex'; |
| 78 |
$tmpdir = $ENV{FEXTMP} || $ENV{TMP} || "$fexhome/tmp"; |
| 79 |
$idf = "$fexhome/id"; |
| 80 |
$useragent = sprintf("fexget-$version (%s %s)", |
| 81 |
$Config{osname},$Config{archname}); |
| 82 |
$SSL{SSL_verify_mode} = 0; |
| 83 |
chdir $ENV{USERPROFILE}.'\Desktop'; |
| 84 |
# open XX,'>XXXXXX';close XX; |
| 85 |
} else { |
| 86 |
# $fexget = $0; |
| 87 |
$prg =~ s:(.*)/::; |
| 88 |
$fexsend ||= "$1/fexsend"; |
| 89 |
$del = "$1/del"; |
| 90 |
$0 = "$prg @ARGV"; |
| 91 |
$fexhome = $ENV{FEXHOME} || $ENV{HOME}.'/.fex'; |
| 92 |
$tmpdir = $ENV{FEXTMP} || "$fexhome/tmp"; |
| 93 |
$idf = "$fexhome/id"; |
| 94 |
if ($Config{osname} =~ /^darwin/i or $ENV{MACOS}) { |
| 95 |
$_ = `sw_vers -productVersion 2>/dev/null`||''; |
| 96 |
chomp; |
| 97 |
$useragent = "fexget-$version (MacOS $_)"; |
| 98 |
} else { |
| 99 |
my $os; |
| 100 |
if (open my $osr,'/etc/os-release') { |
| 101 |
while (<$osr>) { |
| 102 |
$os = $1 if /PRETTY_NAME="(.+)"/; |
| 103 |
} |
| 104 |
close $osr; |
| 105 |
} |
| 106 |
unless ($os) { |
| 107 |
$os = `(lsb_release -d||uname -a)2>/dev/null`||''; |
| 108 |
chomp $os; |
| 109 |
$os =~ s/^Description:\s+//; |
| 110 |
} |
| 111 |
$useragent = "$prg-$version ($os)"; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (-f ($_ = '/etc/fex/config.pl')) { |
| 116 |
eval { require } or warn $@; |
| 117 |
} |
| 118 |
|
| 119 |
if ($Config{osname} eq 'cygwin') { |
| 120 |
$SSL{SSL_verify_mode} = 0; |
| 121 |
} |
| 122 |
|
| 123 |
if (my $fua = $ENV{FUA}) { $useragent =~ s:^:$fua/: } |
| 124 |
$ENV{FUA} = $useragent; |
| 125 |
$ENV{FUA} =~ s/ .*//; |
| 126 |
|
| 127 |
# import common functions |
| 128 |
# if (open $fexsend,$fexsend) { |
| 129 |
# local $/; |
| 130 |
# $_ = <$fexsend>; |
| 131 |
# close $fexsend; |
| 132 |
# if (/\n(\#package FEX.*)/s) { |
| 133 |
# eval $1 or die $@; |
| 134 |
# } else { |
| 135 |
# die "$prg: package FEX missing in $fexsend\n"; |
| 136 |
# } |
| 137 |
# } else { |
| 138 |
# die "$prg: cannot open $fexsend - $!\n"; |
| 139 |
# } |
| 140 |
|
| 141 |
my $usage = <<EOD; |
| 142 |
usage: |
| 143 |
$prg [options] URL|NUMBER |
| 144 |
$prg [options] [-w] [-W] SENDER ['FILE'] ['COMMENT'] |
| 145 |
$prg [options] -f URL|NUMBER EMAIL-ADDRESS |
| 146 |
$prg [options] -l [SENDER] |
| 147 |
$prg [options] -L |
| 148 |
$prg [options] -t NUMBER |
| 149 |
$prg -H |
| 150 |
options: |
| 151 |
-v verbose mode |
| 152 |
-q quiet mode: no transfer status |
| 153 |
-i account use ID data [account] from ID file |
| 154 |
-m max max kB/s download rate |
| 155 |
-p size:wait get file in chunks (size in MB, wait interval in seconds) |
| 156 |
-P proxy:port use proxy for connection to the F*EX server |
| 157 |
-s filename save to filename (-s- means: write to STDOUT/pipe) |
| 158 |
-o overwrite existing file |
| 159 |
-k keep on server after download |
| 160 |
-X do not extract archive files or autoview file |
| 161 |
-d delete without download |
| 162 |
-f forward a file to another recipient |
| 163 |
-l list files on server (prefixed with number) |
| 164 |
-L list download URLs |
| 165 |
-t list content of an archive file (.tar .tgz .zip .7z) |
| 166 |
-w watch for one new file and then get it |
| 167 |
-W watch for new files and then get them |
| 168 |
-H show hints |
| 169 |
arguments: |
| 170 |
URL F*EX URL |
| 171 |
NUMBER number from last "$prg -l" |
| 172 |
SENDER sender address pattern (must contain @) |
| 173 |
FILE filename pattern |
| 174 |
COMMENT comment pattern |
| 175 |
examples: |
| 176 |
$prg -l # list files on server |
| 177 |
$prg -t 3 # list content of archive file number 3 |
| 178 |
$prg 3 # get file number 3 |
| 179 |
$prg 'framstag\@' # get all files from framstag |
| 180 |
$prg -d @ '*.tmp' # delete *.tmp files from all senders |
| 181 |
EOD |
| 182 |
|
| 183 |
my $hints = <<'EOD'; |
| 184 |
SENDER, FILE and COMMENT are sh-like pattern with ? and * special characters. |
| 185 |
For SENDER * is automatically added to @ |
| 186 |
Example: the sender [email protected] is matched by: |
| 187 |
|
| 188 |
[email protected] |
| 189 |
*tag@rus* |
| 190 |
framstag@ |
| 191 |
@rus.uni-stuttgart.de |
| 192 |
@ |
| 193 |
|
| 194 |
Instead of sh-like pattern you can also use Perl regular expression when you |
| 195 |
embed it in //, example: '/framstag@[\w.]+uni-stuttgart.de/' |
| 196 |
|
| 197 |
In $HOME/.fex/config.pl you can set your prefered autoview applications: |
| 198 |
|
| 199 |
%autoview = ( |
| 200 |
'\.(gif|jpg|png|tiff?)' => 'view', |
| 201 |
'\.(avi|mp4|mov)' => 'vlc -f', |
| 202 |
'\.pdf' => 'evince', |
| 203 |
); |
| 204 |
|
| 205 |
For HTTPS you can set the environment variables: |
| 206 |
SSLVERIFY=1 # activate server identity verification |
| 207 |
SSLVERSION=TLSv1_2 # this is the default |
| 208 |
SSLCAPATH=/etc/ssl/certs # path to trusted (root) certificates |
| 209 |
SSLCAFILE=/etc/ssl/cert.pem # file with trusted (root) certificates |
| 210 |
SSLCIPHERLIST=HIGH:!3DES # see http://www.openssl.org/docs/apps/ciphers.html |
| 211 |
|
| 212 |
You can set these environment variables also in $HOME/.fex/config.pl, as well as |
| 213 |
the $opt_* variables, e.g.: |
| 214 |
|
| 215 |
$ENV{SSLVERSION} = 'TLSv1_2'; |
| 216 |
${'opt_+'} = 1; |
| 217 |
$opt_m = 200; |
| 218 |
EOD |
| 219 |
|
| 220 |
if ($windoof and not @ARGV and not $ENV{PROMPT}) { |
| 221 |
# restart with cmd.exe to have mouse cut+paste |
| 222 |
my $cmd = "cmd /k \"$prg\""; |
| 223 |
# print "$cmd\n"; |
| 224 |
exec $cmd; |
| 225 |
exit; |
| 226 |
} |
| 227 |
|
| 228 |
die $usage unless @ARGV; |
| 229 |
|
| 230 |
$opt_m = $opt_h = $opt_v = $opt_l = $opt_d = $opt_K = $opt_o = $opt_a = 0; |
| 231 |
$opt_V = $opt_X = $opt_f = $opt_L = $opt_H = $opt_t = $opt_q = 0; |
| 232 |
$opt_w = $opt_W = 0; |
| 233 |
${'opt_+'} = 0; |
| 234 |
$opt_s = $opt_k = $opt_i = $opt_P = $opt_p = ''; |
| 235 |
$_ = "$fexhome/config.pl"; require if -f; |
| 236 |
getopts('hvVHlLdtqkzoaXVwWf+m:s:i:K:P:p:') or die $usage; |
| 237 |
$opt_k = '?KEEP' if $opt_k; |
| 238 |
|
| 239 |
if ($opt_m =~ /(\d+)/) { |
| 240 |
$opt_m = $1 |
| 241 |
} else { |
| 242 |
$opt_m = 0 |
| 243 |
} |
| 244 |
|
| 245 |
if ($opt_p =~ /^(\d+)$/) { |
| 246 |
$chunksize = $1; |
| 247 |
$sleepwait = 1; |
| 248 |
} elsif ($opt_p =~ /^(\d+):?(\d*)$/) { |
| 249 |
$chunksize = $1; |
| 250 |
$sleepwait = $2; |
| 251 |
} |
| 252 |
$chunksize *= 1024*1024; |
| 253 |
|
| 254 |
if ($opt_V) { |
| 255 |
print "Version: $version\n"; |
| 256 |
exit; |
| 257 |
} |
| 258 |
|
| 259 |
if ($opt_h) { |
| 260 |
print $usage; |
| 261 |
exit; |
| 262 |
} |
| 263 |
|
| 264 |
if ($opt_H) { |
| 265 |
print $hints; |
| 266 |
exit; |
| 267 |
} |
| 268 |
|
| 269 |
# &get_ssl_env; |
| 270 |
|
| 271 |
my $ffl = "$tmpdir/fexget"; # F*EX files list (cache) |
| 272 |
|
| 273 |
my @rcamel = ( |
| 274 |
'[A |
| 275 |
(_*p _ _ |
| 276 |
\\\\/ \/ \\ |
| 277 |
\ __ )=* |
| 278 |
//\\\\//\\\\ |
| 279 |
', |
| 280 |
"[A \\\\/\\\\/ \n", |
| 281 |
"[A //\\\\//\\\\\n" |
| 282 |
); |
| 283 |
|
| 284 |
if ($opt_P) { |
| 285 |
if ($opt_P =~ /^([\w.-]+:\d+)(:(\d+))?/) { |
| 286 |
$proxy = $1; |
| 287 |
$chunksize = $3 || 0; |
| 288 |
} else { |
| 289 |
die "$prg: proxy must be: SERVER:PORT\n"; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
# get fexlog |
| 294 |
if ($opt_z) { |
| 295 |
my $cmd = "$fexsend -Z"; |
| 296 |
$cmd .= " -i $opt_i" if $opt_i; |
| 297 |
warn "\$ $cmd\n" if $opt_v; |
| 298 |
exec $cmd; |
| 299 |
die "$prg: cannot run $cmd : $!\n"; |
| 300 |
} |
| 301 |
|
| 302 |
if ($opt_l) { |
| 303 |
list(@ARGV); |
| 304 |
exit; |
| 305 |
} |
| 306 |
|
| 307 |
if ($opt_L) { |
| 308 |
my $cmd = "$fexsend -L"; |
| 309 |
$cmd .= " -i $opt_i" if $opt_i; |
| 310 |
warn "\$ $cmd\n" if $opt_v; |
| 311 |
exec $cmd; |
| 312 |
die "$prg: cannot run $cmd : $!\n"; |
| 313 |
} |
| 314 |
|
| 315 |
if (not($opt_a or $opt_s or $opt_t or $opt_d or $opt_o or $opt_f or |
| 316 |
$opt_w or $opt_W or @ARGV)) { |
| 317 |
&list; |
| 318 |
exit; |
| 319 |
} |
| 320 |
|
| 321 |
if ($opt_a) { |
| 322 |
$opt_X = $opt_a; |
| 323 |
die $usage if @ARGV; |
| 324 |
&list; |
| 325 |
print "\n"; |
| 326 |
if (open $ffl,$ffl) { |
| 327 |
while (<$ffl>) { |
| 328 |
push @ARGV,$1 if /^\s+(\d+)/; |
| 329 |
} |
| 330 |
close $ffl; |
| 331 |
} |
| 332 |
} else { |
| 333 |
unless (@ARGV) { |
| 334 |
if ($windoof) { |
| 335 |
my $url; |
| 336 |
for (;;) { |
| 337 |
print "download-URL: "; |
| 338 |
chomp($url = <STDIN>); |
| 339 |
if ($url =~ /^http/) { |
| 340 |
@ARGV = ($url); |
| 341 |
last; |
| 342 |
} |
| 343 |
} |
| 344 |
} else { |
| 345 |
die $usage; |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
my ($file,%files,%myfile,%url,$download,$server,$port,$fop,$https); |
| 351 |
|
| 352 |
if ($opt_w||$opt_W and $ARGV[0] !~ /@/) { |
| 353 |
die "$prg: no @ in sender, see: fexget -h\n"; |
| 354 |
} |
| 355 |
|
| 356 |
if ($opt_f) { |
| 357 |
unless ($ENV{FEXID} or -f $ENV{HOME}.'/.fex/id') { |
| 358 |
die "$prg: no local FEXID\n"; |
| 359 |
} |
| 360 |
$opt_f = pop(@ARGV); |
| 361 |
if ($opt_f =~ /^\d+$|^https?:/) { |
| 362 |
die "$prg: $opt_f is not an e-mail address\n"; |
| 363 |
} |
| 364 |
} elsif ($ARGV[0] =~ /@/ and $ARGV[0] !~ m:/fop/:) { |
| 365 |
my $from = shift @ARGV; |
| 366 |
my $file = shift @ARGV || '*'; |
| 367 |
my $comment = shift @ARGV || '*'; |
| 368 |
|
| 369 |
if (@ARGV) { |
| 370 |
die "usage: $prg [options] [-w] [-W] SENDER ['FILE'] ['COMMENT']\n"; |
| 371 |
} |
| 372 |
|
| 373 |
$from =~ s/^\@/*@/; |
| 374 |
$from =~ s/\@$/@*/; |
| 375 |
my $from_re = pre($from); |
| 376 |
my $file_re = pre($file); |
| 377 |
my $comment_re = pre($comment); |
| 378 |
|
| 379 |
if ($opt_w or $opt_W) { |
| 380 |
$opt_o = 1; |
| 381 |
watch($from_re,$file_re,$comment_re); |
| 382 |
exit $status; |
| 383 |
} |
| 384 |
|
| 385 |
@ARGV = (); |
| 386 |
my $url; |
| 387 |
my $user = ''; |
| 388 |
my $cmd = "$fexsend -/L"; |
| 389 |
$cmd .= " -i $opt_i" if $opt_i; |
| 390 |
if ($opt_v) { |
| 391 |
# $cmd .= " -v"; |
| 392 |
warn "\$ $cmd\n"; |
| 393 |
} |
| 394 |
open $cmd,"$cmd|" or die "$prg: cannot run $cmd : $!\n"; |
| 395 |
while (<$cmd>) { |
| 396 |
if (m{^Server/User: http.*/(.+)}i) { |
| 397 |
$user = $1; |
| 398 |
next; |
| 399 |
} |
| 400 |
if (/^from ($from_re) :/) { |
| 401 |
my $from = $1; |
| 402 |
while (<$cmd>) { |
| 403 |
if (/\w/) { |
| 404 |
next if $from =~ /^\Q$user/ and /\/xxx_\d{8}_\d{6}\./; |
| 405 |
s/\n/ ""\n/ unless /\"/; |
| 406 |
if (m:( *\d+ MB).*(http\S+/fop/\w+/($file_re)) ".*":) { |
| 407 |
my $mb = $1; |
| 408 |
my $url = $2; |
| 409 |
my $file = $3; |
| 410 |
if (/("$comment_re")$/) { |
| 411 |
$url{$url} = "$from:$file"; |
| 412 |
push @{$files{$from}},"$mb $file $1"; |
| 413 |
push @ARGV,$url; |
| 414 |
} |
| 415 |
} |
| 416 |
} else { |
| 417 |
last; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
close $cmd; |
| 423 |
if (%files) { |
| 424 |
print "\n"; |
| 425 |
foreach my $from (keys %files) { |
| 426 |
print "from $from :\n"; |
| 427 |
foreach my $file (@{$files{$from}}) { |
| 428 |
print "$file\n"; |
| 429 |
} |
| 430 |
print "\n"; |
| 431 |
} |
| 432 |
print 'continue? '; |
| 433 |
for (;;) { |
| 434 |
my $k = &ReadKey; |
| 435 |
if ($k =~ /y|\n/i) { |
| 436 |
print "yes\n"; |
| 437 |
last; |
| 438 |
} |
| 439 |
if ($k =~ /n/i) { |
| 440 |
print "no\n"; |
| 441 |
exit; |
| 442 |
} |
| 443 |
} |
| 444 |
print "\n"; |
| 445 |
} else { |
| 446 |
die "$prg: no matching files found for '$from' '$file' '$comment'\n"; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
my $nexturl = 0; |
| 451 |
|
| 452 |
foreach my $url (@ARGV) { |
| 453 |
|
| 454 |
# do not overrun server |
| 455 |
if ($nexturl) { |
| 456 |
sleep 1; |
| 457 |
warn "\n"; |
| 458 |
} |
| 459 |
$nexturl++; |
| 460 |
|
| 461 |
if ($url !~ /^http/) { |
| 462 |
&readfileslist unless %files; |
| 463 |
if ($url =~ /^(\d+)$/) { |
| 464 |
unless ($url = ${files{all}}[$1-1]) { |
| 465 |
warn "$prg: unknown file number $1\n"; |
| 466 |
next; |
| 467 |
} |
| 468 |
} else { |
| 469 |
warn "$prg: $url is neither a F*EX URL nor a number\n"; |
| 470 |
next; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
getfile($url); |
| 475 |
} |
| 476 |
|
| 477 |
exit $status; |
| 478 |
|
| 479 |
|
| 480 |
sub getfile { |
| 481 |
my $url = shift; |
| 482 |
|
| 483 |
warn "$url{$url}\n" if $url{$url}; |
| 484 |
|
| 485 |
if ($url =~ m{^http(s?)://([\w\.\-]+)(:(\d+))?(/.*fop/\S+)}) { |
| 486 |
$https = $1; |
| 487 |
$server = $2; |
| 488 |
$port = $4 || ($1?443:80); |
| 489 |
$fop = $5; |
| 490 |
} else { |
| 491 |
die "$prg: unknown F*EX URL $url\n"; |
| 492 |
} |
| 493 |
|
| 494 |
if ($proxy) { |
| 495 |
if ($port == 80) { $proxy_prefix = "http://$server" } |
| 496 |
elsif ($port == 443) { $proxy_prefix = "" } |
| 497 |
else { $proxy_prefix = "http://$server:$port" } |
| 498 |
} |
| 499 |
|
| 500 |
serverconnect($server,$port); |
| 501 |
|
| 502 |
if ($opt_f) { |
| 503 |
forward($url); |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
if ($opt_t) { |
| 508 |
if ($url =~ m{^http.*/fop/.*\.(tar|tgz|tar\.gz|zip|7z)$}) { |
| 509 |
listarchive($url); |
| 510 |
exit; |
| 511 |
} else { |
| 512 |
die "$prg: $url is not a F*EX archive URL\n"; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
if ($opt_d) { |
| 517 |
if ($myfile{$url}) { |
| 518 |
vsystem(qw'fexsend -qd',$url); |
| 519 |
} else { |
| 520 |
del($url); |
| 521 |
} |
| 522 |
return; |
| 523 |
} |
| 524 |
|
| 525 |
if ($opt_K) { |
| 526 |
my @r = keep($url); |
| 527 |
$_ = shift @r; |
| 528 |
if (/^HTTP.* 200/) { |
| 529 |
$file = $url; |
| 530 |
$file =~ s:.*/::; |
| 531 |
print "$file kept\n"; |
| 532 |
} else { |
| 533 |
s:HTTP/[\d\. ]+::; |
| 534 |
die "$prg: server response: $_"; |
| 535 |
} |
| 536 |
return; |
| 537 |
} |
| 538 |
|
| 539 |
$download = download($server,$port,$fop); |
| 540 |
return unless defined $download; |
| 541 |
exit if $opt_s eq '-'; |
| 542 |
unlink $download unless -s $download; |
| 543 |
return unless -f $download; |
| 544 |
|
| 545 |
if ($windoof) { |
| 546 |
print "READY\n"; |
| 547 |
exit; |
| 548 |
} |
| 549 |
|
| 550 |
if (not $opt_X and $download =~ /\.gpg$/) { |
| 551 |
if (-t) { |
| 552 |
print "decrypt \"$download\"? "; |
| 553 |
$_ = <STDIN>||'y'; |
| 554 |
unless (/^[y\n]/i) { |
| 555 |
print "keeping \"$download\"\n"; |
| 556 |
return 0; |
| 557 |
} |
| 558 |
} |
| 559 |
if (system('gpg',$download) == 0) { |
| 560 |
unlink $download; |
| 561 |
$download =~ s/\.gpg$//; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
unless ($opt_X) { |
| 566 |
|
| 567 |
foreach my $a (keys %autoview) { |
| 568 |
if ($download =~ /$a$/i and $autoview{$a}) { |
| 569 |
printf "run \"%s %s\" [Yn] ? ",$autoview{$a},basename($download); |
| 570 |
$_ = <STDIN>||''; |
| 571 |
if (/^y|^$/i) { |
| 572 |
system sprintf("%s %s",$autoview{$a},shellquote($download)) |
| 573 |
} |
| 574 |
return; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
if (0 and $ENV{DISPLAY} and $download =~ /\.(gif|jpg|png|tiff?)$/i) { |
| 579 |
# see also mimeopen and xdg-mime |
| 580 |
# http://unix.stackexchange.com/questions/144047/how-does-xdg-open-do-its-work |
| 581 |
if (my $xv = $xv || pathsearch('xv') || pathsearch('xdg-open')) { |
| 582 |
printf "run \"%s %s\" [Yn] ? ",basename($xv),basename($download); |
| 583 |
$_ = <STDIN>||''; |
| 584 |
system $xv,$download if /^y|^$/i; |
| 585 |
return; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
if ($download =~ /$atype/) { |
| 590 |
if ($download =~ /\.(tgz|tar.gz)$/) { |
| 591 |
if ($opt_o) { |
| 592 |
extract('tar tvzf','tar xvzf') |
| 593 |
} else { |
| 594 |
extract('tar tvzf','tar xvzkf') |
| 595 |
} |
| 596 |
} elsif ($download =~ /\.tar$/) { |
| 597 |
if ($opt_o) { |
| 598 |
extract('tar tvf','tar xvf') |
| 599 |
} else { |
| 600 |
extract('tar tvf','tar xvkf') |
| 601 |
} |
| 602 |
} elsif ($download =~ /\.zip$/i) { |
| 603 |
if ($opt_o) { |
| 604 |
extract('unzip -l','unzip -o') |
| 605 |
} else { |
| 606 |
extract('unzip -l','unzip') |
| 607 |
} |
| 608 |
} elsif ($download =~ /\.7z$/i) { |
| 609 |
if ($opt_o) { |
| 610 |
extract('7z l','7z x -y') |
| 611 |
} else { |
| 612 |
extract('7z l','7z x') |
| 613 |
} |
| 614 |
} else { |
| 615 |
die "$prg: unknown archive \"$download\"\n" |
| 616 |
} |
| 617 |
if ($? == 0) { |
| 618 |
unlink $download; |
| 619 |
} else { |
| 620 |
die "$prg: keeping \"$download\"\n"; |
| 621 |
} |
| 622 |
} elsif ($download =~ /\/fp_\d+\.gz$/) { |
| 623 |
system("gunzip < $download"); |
| 624 |
if ($? == 0) { |
| 625 |
unlink $download; |
| 626 |
} else { |
| 627 |
die "$prg: keeping \"$download\"\n"; |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
return 0; |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
sub extract { |
| 637 |
my $l = shift; |
| 638 |
my $x = shift; |
| 639 |
my $d = $download; |
| 640 |
my $xd = abs_path('.'); |
| 641 |
local $_; |
| 642 |
|
| 643 |
if (-t and not $windoof and not $opt_W and $useragent !~ /xxx/) { |
| 644 |
print "Files in archive:\n"; |
| 645 |
system(split(' ',$l),$download); |
| 646 |
$d =~ s:.*/:./:; |
| 647 |
$d =~ s/\.[^.]+$//; |
| 648 |
$d =~ s:/*$:/:; |
| 649 |
$d =~ s:/xxx_\d+/:/:; |
| 650 |
for (;;) { |
| 651 |
$xd = inquire("extract to directory (\"-\" to keep archive): ",$d); |
| 652 |
last if $xd =~ s:^(\./*)*!?$::; |
| 653 |
if ($xd eq '-') { |
| 654 |
print "keeping $download\n"; |
| 655 |
exit; |
| 656 |
} |
| 657 |
if ($xd !~ s/!$//) { |
| 658 |
if (-d $xd) { |
| 659 |
print "directory $xd does already exist, add \"!\" to overwrite\n"; |
| 660 |
redo; |
| 661 |
} |
| 662 |
unless (mkdir $xd) { |
| 663 |
print "cannot mkdir $xd - $!\n"; |
| 664 |
redo; |
| 665 |
} |
| 666 |
} |
| 667 |
unless (chdir $xd) { |
| 668 |
print "cannot chdir $xd - $!\n"; |
| 669 |
redo; |
| 670 |
} |
| 671 |
last; |
| 672 |
} |
| 673 |
} |
| 674 |
print "extracting to $xd :\n" if $xd; |
| 675 |
vsystem(split(' ',$x),$download); |
| 676 |
# print "(extracted to $xd)\n" if $xd; |
| 677 |
} |
| 678 |
|
| 679 |
sub del { |
| 680 |
my $url = shift; |
| 681 |
my ($server,$port); |
| 682 |
my ($del,$file); |
| 683 |
my @r; |
| 684 |
local $_; |
| 685 |
|
| 686 |
if ($url =~ m{^http(s?)://([\w\.\-]+)(:(\d+))?(/fop/.+)}) { |
| 687 |
$server = $2; |
| 688 |
$port = $4 || ($1?443:80); |
| 689 |
$del = $5.'?DELETE'; |
| 690 |
} else { |
| 691 |
die "$prg: unknown F*EX URL $url\n"; |
| 692 |
} |
| 693 |
|
| 694 |
sendheader("$server:$port","GET $del HTTP/1.1"); |
| 695 |
while (<$SH>) { |
| 696 |
s/\r//; |
| 697 |
last if /^\n/; # ignore HTML output |
| 698 |
warn "<-- $_" if $opt_v; |
| 699 |
push @r,$_; |
| 700 |
} |
| 701 |
die "$prg: no response from fex server $server:$port\n" unless @r; |
| 702 |
|
| 703 |
$_ = shift @r; |
| 704 |
if (/^HTTP.* 200/) { |
| 705 |
($file) = grep { $_ = $1 if /^X-File:\s+(.+)/ } @r; |
| 706 |
$file = $url unless $file; |
| 707 |
if ($file =~ m:SHARE/(.+)/archives/(.+)/(.+):) { |
| 708 |
printf "%s:%s_%s deleted\n",$1,$2,$3; |
| 709 |
} else { |
| 710 |
$file =~ s:.*/::; |
| 711 |
printf "%s deleted\n",locale(decode_utf8(urldecode($file))); |
| 712 |
} |
| 713 |
} elsif (/^HTTP.*need auth-ID/ and $url =~ m:.*/(.+):) { |
| 714 |
# warn "$prg: server needs auth-ID for deleting\n"; |
| 715 |
my $file = $1; |
| 716 |
my @fexsend = ('fexsend'); |
| 717 |
# push @fexsend,'-v' if $opt_v; |
| 718 |
push @fexsend,('-i',$opt_i) if $opt_i; |
| 719 |
my $cmd = "@fexsend -d $file ."; |
| 720 |
warn "\$ $cmd\n" if $opt_v; |
| 721 |
system $cmd; |
| 722 |
} else { |
| 723 |
s:HTTP/[\d\. ]+::; |
| 724 |
die "$prg: server response: $_"; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
sub forward { |
| 730 |
my $url = shift; |
| 731 |
my ($server,$port); |
| 732 |
my ($uri,$dkey,$list,$cmd,$n,$copy); |
| 733 |
my @r; |
| 734 |
|
| 735 |
if ($url =~ m{^http(s?)://([\w\.\-]+)(:(\d+))?(/fop/.+)}) { |
| 736 |
$server = $2; |
| 737 |
$port = $4 || ($1?443:80); |
| 738 |
$uri = $5; |
| 739 |
} else { |
| 740 |
die "$prg: unknown F*EX URL $url\n"; |
| 741 |
} |
| 742 |
|
| 743 |
sendheader( |
| 744 |
"$server:$port", |
| 745 |
"GET $uri?COPY HTTP/1.1", |
| 746 |
); |
| 747 |
|
| 748 |
$_ = <$SH>; |
| 749 |
die "$prg: no reply from fex server $server:$port\n" unless $_; |
| 750 |
warn "<-- $_" if $opt_v; |
| 751 |
|
| 752 |
if (/^HTTP.*already exists/) { |
| 753 |
if ($uri =~ m:/fop/(\w+)/:) { |
| 754 |
$dkey = $1; |
| 755 |
} |
| 756 |
} elsif (/^HTTP.*200/) { |
| 757 |
# ok! |
| 758 |
} else { |
| 759 |
s/^HTTP.... \d+ //; |
| 760 |
die "$prg: $_"; |
| 761 |
} |
| 762 |
|
| 763 |
while (<$SH>) { |
| 764 |
s/\r//; |
| 765 |
last if /^\n/; # ignore HTML output |
| 766 |
$dkey = $1 if /^Location:.*\/(\w+)\/.+/; |
| 767 |
warn "<-- $_" if $opt_v; |
| 768 |
} |
| 769 |
|
| 770 |
warn "\$ fexsend -l\n" if $opt_v; |
| 771 |
system 'fexsend -l >/dev/null 2>&1'; |
| 772 |
$list = $ENV{HOME}.'/.fex/tmp/fexlist'; |
| 773 |
open $list,$list or die "$prg: cannot open $list - $!\n"; |
| 774 |
while (<$list>) { |
| 775 |
if (/^\s+(\d+)\) (\w+)/ and $2 eq $dkey) { |
| 776 |
$n = $1; |
| 777 |
$cmd = "fexsend -b $n $opt_f"; |
| 778 |
warn "\$ $cmd\n" if $opt_v; |
| 779 |
system $cmd; |
| 780 |
last; |
| 781 |
} |
| 782 |
} |
| 783 |
close $list; |
| 784 |
|
| 785 |
if ($n) { |
| 786 |
$cmd = "fexsend -d $n >/dev/null 2>&1"; |
| 787 |
warn "\$ $cmd\n" if $opt_v; |
| 788 |
system $cmd; |
| 789 |
} else { |
| 790 |
warn "$prg: forwarding failed\n"; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
sub keep { |
| 796 |
my $url = shift; |
| 797 |
my ($server,$port); |
| 798 |
my $keep; |
| 799 |
my (@hh,@r); |
| 800 |
|
| 801 |
if ($url =~ m{^http(s?)://([\w\.\-]+)(:(\d+))?(/fop/.+)}) { |
| 802 |
$server = $2; |
| 803 |
$port = $4 || ($1?443:80); |
| 804 |
$keep = "$5?KEEP=$opt_K"; |
| 805 |
} else { |
| 806 |
die "$prg: unknown F*EX URL $url\n"; |
| 807 |
} |
| 808 |
|
| 809 |
push @hh,"GET $keep HTTP/1.1", |
| 810 |
"Host: $server:$port", |
| 811 |
"User-Agent: $useragent", |
| 812 |
""; |
| 813 |
|
| 814 |
foreach (@hh) { |
| 815 |
warn $_,"\n" if $opt_v; |
| 816 |
print $SH $_,"\r\n"; |
| 817 |
} |
| 818 |
while (<$SH>) { |
| 819 |
s/\r//; |
| 820 |
last if /^\n/; |
| 821 |
push @r,$_; |
| 822 |
} |
| 823 |
die "$prg: no response from fex server $server\n" unless @r; |
| 824 |
grep { warn "\t$_" } @r if $opt_v; |
| 825 |
return @r; |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
sub download { |
| 830 |
my ($server,$port,$fop,$nocheck) = @_; |
| 831 |
my ($file,$download,$ssl,$pipe,$filesize,$checkstorage,$dkey); |
| 832 |
my (@hh,@r); |
| 833 |
my ($t0,$t1,$t2,$tt,$tm,$ts,$kBs,$b,$bt,$tb,$B,$buf); |
| 834 |
my $length = 0; |
| 835 |
my $seek = 0; |
| 836 |
my $tc = 0; |
| 837 |
local $_; |
| 838 |
local *X; |
| 839 |
|
| 840 |
if ($opt_s) { |
| 841 |
$file = $opt_s; |
| 842 |
if ($opt_s eq '-') { |
| 843 |
$pipe = $download = $opt_s; |
| 844 |
} elsif (-p $opt_s or -c $opt_s) { |
| 845 |
$download = $opt_s; |
| 846 |
$nocheck = 'pipe or character device'; |
| 847 |
} else { |
| 848 |
$download = $file.'.tmp'; |
| 849 |
$seek = -s $download || 0; |
| 850 |
} |
| 851 |
} else { |
| 852 |
# ask server for real file name |
| 853 |
sendheader( |
| 854 |
"$server:$port", |
| 855 |
"HEAD $proxy_prefix$fop HTTP/1.1", |
| 856 |
); |
| 857 |
my $reply = $_ = <$SH>; |
| 858 |
unless (defined $_ and /\w/) { |
| 859 |
die "$prg: no response from server\n"; |
| 860 |
} |
| 861 |
warn "<-- $_" if $opt_v; |
| 862 |
unless (/^HTTP\/[\d.]+ 200/) { |
| 863 |
s:HTTP/[\d. ]+::; |
| 864 |
die "$prg: server response: $_"; |
| 865 |
} |
| 866 |
while (<$SH>) { |
| 867 |
s/\r//; |
| 868 |
$_ = urldecode($_) if /%[A-Z0-9]{2}/; |
| 869 |
warn "<-- $_" if $opt_v; |
| 870 |
last if /^\r?\n/; |
| 871 |
if (/^Content-Disposition: attachment; filename="(.+)"/i) { |
| 872 |
$file = $1; |
| 873 |
# $file = locale(decode_utf8($file)); |
| 874 |
$file =~ s:.*/::; |
| 875 |
} |
| 876 |
} |
| 877 |
unless ($file) { |
| 878 |
$file = $fop; |
| 879 |
$file =~ s:.*/::; |
| 880 |
} |
| 881 |
$download = $file.'.tmp'; |
| 882 |
$seek = -s $download || 0; |
| 883 |
} |
| 884 |
|
| 885 |
$fop =~ m:/fop/(\w+)/: and $dkey=$1 or $dkey=''; |
| 886 |
|
| 887 |
push @hh,"GET $proxy_prefix$fop$opt_k HTTP/1.1"; |
| 888 |
push @hh,"Cookie: dkey=$dkey" if $dkey; |
| 889 |
push @hh,"Connection: close"; |
| 890 |
push @hh,"Range: bytes=$seek-" if $seek; |
| 891 |
|
| 892 |
# HTTPS needs a new connection for actually downloading the file |
| 893 |
serverconnect($server,$port) if $opt_P and $port == 443; |
| 894 |
sendheader("$server:$port",@hh); |
| 895 |
$_ = <$SH>; |
| 896 |
die "$prg: no response from fex server $server\n" unless $_; |
| 897 |
s/\r//; |
| 898 |
|
| 899 |
if (/^HTTP\/[\d.]+ 2/) { |
| 900 |
warn "<-- $_" if $opt_v; |
| 901 |
while (<$SH>) { |
| 902 |
s/\r//; |
| 903 |
warn "<-- $_" if $opt_v; |
| 904 |
last if /^\r?\n/; |
| 905 |
if (/^Content-length:\s*(\d+)/i) { |
| 906 |
$length = $1; |
| 907 |
} elsif (/^X-Size: (\d+)/i) { |
| 908 |
$filesize = $1; |
| 909 |
} |
| 910 |
} |
| 911 |
} else { |
| 912 |
s/HTTP\/[\d.]+ \d+ //; |
| 913 |
die "$prg: bad server reply: $_"; |
| 914 |
} |
| 915 |
|
| 916 |
if ($pipe) { |
| 917 |
*X = *STDOUT; |
| 918 |
} else { |
| 919 |
if ($opt_s and $opt_s eq $download) { |
| 920 |
open X,'>',$download or die "$prg: cannot write to \"$download\" - $!\n"; |
| 921 |
$checkstorage = $filesize unless $nocheck; |
| 922 |
} else { |
| 923 |
my $df = qq'destination file "$file"'; |
| 924 |
if (my @s = lstat $file) { |
| 925 |
unless ($opt_o) { |
| 926 |
warn "$prg: $df does already exist\n"; |
| 927 |
$status = 1; |
| 928 |
return; |
| 929 |
} |
| 930 |
if (-l $file) { |
| 931 |
warn "$prg: $df is a symbolic link\n"; |
| 932 |
$status = 1; |
| 933 |
return; |
| 934 |
} |
| 935 |
if (-d $file) { |
| 936 |
warn "$prg: $df is a directory\n"; |
| 937 |
$status = 1; |
| 938 |
return; |
| 939 |
} |
| 940 |
unless (-f $file) { |
| 941 |
warn "$prg: $df is not a regular file\n"; |
| 942 |
$status = 1; |
| 943 |
return; |
| 944 |
} |
| 945 |
if ($s[3] > 1) { |
| 946 |
warn "$prg: $df has hard links\n"; |
| 947 |
$status = 1; |
| 948 |
return; |
| 949 |
} |
| 950 |
} |
| 951 |
if ($seek) { |
| 952 |
open X,'>>',$download or die "$prg: cannot write to \"$download\" - $!\n"; |
| 953 |
} else { |
| 954 |
open X,'>',$download or die "$prg: cannot write to \"$download\" - $!\n"; |
| 955 |
$checkstorage = $filesize unless $nocheck; |
| 956 |
} |
| 957 |
} |
| 958 |
if ($checkstorage and not $nocheck) { |
| 959 |
my $t0 = my $t1 = my $t2 = time; |
| 960 |
my $n = 0; |
| 961 |
my $buf = '.' x M; |
| 962 |
my $storagetest = $file.'.test'; |
| 963 |
my $error = "$prg: cannot write \"$storagetest\""; |
| 964 |
open $storagetest,'>',$storagetest or die "$error - $!\n"; |
| 965 |
print STDERR "checking storage...\r"; |
| 966 |
while (-s $storagetest < $checkstorage) { |
| 967 |
syswrite $storagetest,$buf or do { |
| 968 |
unlink $storagetest; |
| 969 |
die "\n$error - $!\n"; |
| 970 |
}; |
| 971 |
$n++; |
| 972 |
$t2 = int(time); |
| 973 |
if ($t2 > $t1) { |
| 974 |
print STDERR "checking storage... ".$n." MB\r"; |
| 975 |
$t1 = $t2; |
| 976 |
} |
| 977 |
} |
| 978 |
close $storagetest or do { |
| 979 |
unlink $storagetest; |
| 980 |
die "\n$error - $!\n"; |
| 981 |
}; |
| 982 |
print STDERR "checking storage... ".$n." MB ok!\n"; |
| 983 |
unlink $storagetest; |
| 984 |
if (time-$t0 > 25) { |
| 985 |
# retry after timeout |
| 986 |
serverconnect($server,$port); |
| 987 |
return(download($server,$port,$fop,'nocheck')) |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
$t0 = $t1 = $t2 = int(time); |
| 993 |
$tb = $B = 0; |
| 994 |
printf STDERR "$prg: resuming at byte %s\n",$seek if $seek and not $chunksize; |
| 995 |
print $rcamel[0] if ${'opt_+'}; |
| 996 |
while ($B < $length and $b = read $SH,$buf,$bs) { |
| 997 |
syswrite X,$buf; |
| 998 |
$B += $b; |
| 999 |
$tb += $b; |
| 1000 |
$bt += $b; |
| 1001 |
$t2 = time; |
| 1002 |
if (${'opt_+'} and int($t2*10)>$tc) { |
| 1003 |
print $rcamel[$tc%2+1]; |
| 1004 |
$tc = int($t2*10); |
| 1005 |
} |
| 1006 |
if (not $opt_q and int($t2) > $t1) { |
| 1007 |
$kBs = int($bt/k/($t2-$t1)); |
| 1008 |
$kBs = int($tb/k/($t2-$t0)) if $kBs < 10; |
| 1009 |
$t1 = $t2; |
| 1010 |
$bt = 0; |
| 1011 |
# smaller block size is better on slow links |
| 1012 |
$bs = 4096 if $bs>4096 and $tb/($t2-$t0)<65536; |
| 1013 |
if ($tb<10*M) { |
| 1014 |
printf STDERR "%s: %d kB (%d%%) %d kB/s \r", |
| 1015 |
$download, |
| 1016 |
int(($tb+$seek)/k), |
| 1017 |
int(($tb+$seek)/($length+$seek)*100), |
| 1018 |
$kBs; |
| 1019 |
} else { |
| 1020 |
printf STDERR "%s: %d MB (%d%%) %d kB/s \r", |
| 1021 |
$download, |
| 1022 |
int(($tb+$seek)/M), |
| 1023 |
int(($tb+$seek)/($length+$seek)*100), |
| 1024 |
$kBs; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
if ($opt_m) { |
| 1028 |
if ($t2 == $t0 and $B > $opt_m*k) { |
| 1029 |
warn "\nsleeping...\r" if $opt_v; |
| 1030 |
sleep 1; |
| 1031 |
} else { |
| 1032 |
while ($t2 > $t0 and $tb/k/($t2-$t0) > $opt_m) { |
| 1033 |
warn "\nsleeping...\r" if $opt_v; |
| 1034 |
sleep 1; |
| 1035 |
$t2 = time; |
| 1036 |
} |
| 1037 |
} |
| 1038 |
} |
| 1039 |
last if $chunksize and $B >= $chunksize; |
| 1040 |
} |
| 1041 |
close $SH; |
| 1042 |
close X; |
| 1043 |
|
| 1044 |
print $rcamel[2] if ${'opt_+'}; |
| 1045 |
|
| 1046 |
$tt = $t2-$t0; |
| 1047 |
$tm = int($tt/60); |
| 1048 |
$ts = $tt-$tm*60; |
| 1049 |
$kBs = int($tb/k/($tt||1)); |
| 1050 |
unless ($opt_q) { |
| 1051 |
if ($seek) { |
| 1052 |
printf STDERR "%s: %d MB, last %d MB in %d s (%d kB/s) \n", |
| 1053 |
$file,int(($tb+$seek)/M),int($tb/M),$tt,$kBs; |
| 1054 |
} else { |
| 1055 |
printf STDERR "%s: %d MB in %d s (%d kB/s) \n", |
| 1056 |
$file,int($tb/M),$tt,$kBs; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
if ($tb < $length) { |
| 1061 |
if ($windoof) { |
| 1062 |
sleep(3); |
| 1063 |
exec "\"$prg\" @ARGV"; |
| 1064 |
exit; |
| 1065 |
} else { |
| 1066 |
if ($chunksize) { |
| 1067 |
if ($sleepwait) { |
| 1068 |
my $wait = $sleepwait; |
| 1069 |
while ($wait) { |
| 1070 |
print STDERR "next connect in $wait s \r"; |
| 1071 |
sleep 1; |
| 1072 |
$wait--; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
} else { |
| 1076 |
warn "$prg: $server annouced $length bytes, but only $tb bytes has been read\n"; |
| 1077 |
warn "$prg: retrying after 30 s\n"; |
| 1078 |
sleep(30); |
| 1079 |
} |
| 1080 |
serverconnect($server,$port); |
| 1081 |
return(download($server,$port,$fop,'nocheck')) |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
unless ($pipe or -p $download or -c $download) { |
| 1086 |
my @s = stat $file if -e $file; |
| 1087 |
rename $download,$file |
| 1088 |
or die "$prg: cannot rename \"$download\" to \"$file\" - $!\n"; |
| 1089 |
chmod $s[2],$file if @s; |
| 1090 |
} |
| 1091 |
|
| 1092 |
$_ = sprintf("%s/%s",getcwd(),$file); |
| 1093 |
s:/+:/:g; |
| 1094 |
return $_; |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
sub list { |
| 1099 |
my $sender = shift; |
| 1100 |
my $user = ' '; |
| 1101 |
my $cmd = "$fexsend -/L"; |
| 1102 |
my $n; |
| 1103 |
|
| 1104 |
$cmd .= " -i $opt_i" if $opt_i; |
| 1105 |
if ($opt_v) { |
| 1106 |
# $cmd .= " -v"; |
| 1107 |
warn "\$ $cmd\n"; |
| 1108 |
} |
| 1109 |
|
| 1110 |
open $cmd,"$cmd|" or die "$prg: cannot run $cmd : $!\n"; |
| 1111 |
open $ffl,'>',$ffl or die "$prg: cannot open $ffl : $!\n"; |
| 1112 |
|
| 1113 |
$sender //= '@'; |
| 1114 |
|
| 1115 |
while (<$cmd>) { |
| 1116 |
if (m{^Server/User: http.*/(.+)}i) { |
| 1117 |
$user = $1; |
| 1118 |
warn $_ if $opt_i; |
| 1119 |
print {$ffl} $_; |
| 1120 |
if ($sender eq '.') { |
| 1121 |
$sender = "\\Q$user\\E"; |
| 1122 |
} else { |
| 1123 |
$sender .= '@' if $sender !~ /@/; |
| 1124 |
$sender =~ s/^\@/.*@/; |
| 1125 |
$sender =~ s/\@$/@.*/; |
| 1126 |
} |
| 1127 |
next; |
| 1128 |
} |
| 1129 |
if (/^from /) { |
| 1130 |
if (not $opt_l and /^from \Q$user :/) { |
| 1131 |
while (<$cmd>) { last if /^$/ } next; |
| 1132 |
} |
| 1133 |
#? if (length $sender and not /^from \Q$sender :/) { |
| 1134 |
#? while (<$cmd>) { last if /^$/ } next; |
| 1135 |
#? } |
| 1136 |
} |
| 1137 |
if (/\d MB .*http/) { |
| 1138 |
s/\n/ ""\n/ unless /\"/; |
| 1139 |
next if /\/xxx_\d{8}_\d{6}\.(gz|tgz|tar) /; |
| 1140 |
$n++; |
| 1141 |
$_ = sprintf "%5s %s","#$n",$_; |
| 1142 |
print {$ffl} $_; |
| 1143 |
s: http[^\"]*/: :; |
| 1144 |
print; |
| 1145 |
} else { |
| 1146 |
print {$ffl} $_; |
| 1147 |
print; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
close $ffl; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
sub watch { |
| 1155 |
my $from_re = shift; |
| 1156 |
my $file_re = shift; |
| 1157 |
my $comment_re = shift; |
| 1158 |
my $sp = "^($from_re) \\d+ (http\\S+/($file_re)) \"$comment_re\""; |
| 1159 |
my $cmd = "$fexsend -~ FUPWATCH"; |
| 1160 |
|
| 1161 |
$cmd .= " -i $opt_i" if $opt_i; |
| 1162 |
if ($opt_v) { |
| 1163 |
# $cmd .= " -v"; |
| 1164 |
warn "\$ $cmd | fpg '$sp'|\n"; |
| 1165 |
} |
| 1166 |
|
| 1167 |
$cpid = open $cmd,"$cmd|" or die "$prg: cannot run $cmd : $!\n"; |
| 1168 |
sleep 1; |
| 1169 |
warn "\n" if $opt_v; |
| 1170 |
while (<$cmd>) { |
| 1171 |
warn $_ if $opt_v; |
| 1172 |
if (/$sp/) { |
| 1173 |
my $durl = $2; |
| 1174 |
if ($opt_v) { |
| 1175 |
$opt_v = 0; |
| 1176 |
getfile($durl); |
| 1177 |
$opt_v = 1; |
| 1178 |
warn "\n"; |
| 1179 |
} else { |
| 1180 |
$url{$durl} = "$1:$3"; |
| 1181 |
warn "\n"; |
| 1182 |
getfile($durl); |
| 1183 |
} |
| 1184 |
exit $status if $opt_w; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
sub del_re { |
| 1191 |
my $from_re = shift; |
| 1192 |
my $file_re = shift; |
| 1193 |
my $comment_re = shift; |
| 1194 |
my $sp = "^($from_re) \\d+ (http\\S+/($file_re)) \"$comment_re\""; |
| 1195 |
|
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
sub listarchive { |
| 1200 |
my $url = shift; |
| 1201 |
my $list = ''; |
| 1202 |
my $server = ''; |
| 1203 |
my $cmd = "wget -qSO- $url?!"; |
| 1204 |
my $uri; |
| 1205 |
local $_; |
| 1206 |
|
| 1207 |
if ($url =~ m{^http(s?)://([\w\.\-]+)(:(\d+))?(/fop/.+)}) { |
| 1208 |
$server = $2; |
| 1209 |
$port = $4 || ($1?443:80); |
| 1210 |
$uri = $5; |
| 1211 |
} else { |
| 1212 |
die "$prg: unknown F*EX URL $url\n"; |
| 1213 |
} |
| 1214 |
|
| 1215 |
sendheader( |
| 1216 |
"$server:$port", |
| 1217 |
"GET $uri?LISTARCHIVE HTTP/1.1", |
| 1218 |
); |
| 1219 |
|
| 1220 |
$_ = <$SH>; |
| 1221 |
die "$prg: no reply from fex server $server:$port\n" unless $_; |
| 1222 |
warn "<-- $_" if $opt_v; |
| 1223 |
|
| 1224 |
if (/^HTTP.* Bad Request/) { |
| 1225 |
die "$prg: fexserver $server:$port does not support LISTARCHIVE\n"; |
| 1226 |
} elsif (/^HTTP.*200/) { |
| 1227 |
# ok! |
| 1228 |
} else { |
| 1229 |
s/^HTTP.... \d+ //; |
| 1230 |
die "$prg: $_"; |
| 1231 |
} |
| 1232 |
|
| 1233 |
while (<$SH>) { |
| 1234 |
s/\r//; |
| 1235 |
warn "<-- $_" if $opt_v; |
| 1236 |
if (/^X-Message: Bad Request/) { |
| 1237 |
die "$prg: fexserver $server:$port does not support LISTARCHIVE\n"; |
| 1238 |
} |
| 1239 |
last if /^<pre>/; |
| 1240 |
printf "%-10s %s\n",$1,$2 if /<!-- (\S+:) (.*) -->/; |
| 1241 |
} |
| 1242 |
print "\n"; |
| 1243 |
while (<$SH>) { |
| 1244 |
last if /^</; |
| 1245 |
s/&/&/g; |
| 1246 |
s/</</g; |
| 1247 |
$list .= $_; |
| 1248 |
} |
| 1249 |
print $list if $list; |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
sub readfileslist { |
| 1254 |
my $from = ''; |
| 1255 |
my $user = ''; |
| 1256 |
|
| 1257 |
%files = (); |
| 1258 |
%myfile = (); |
| 1259 |
|
| 1260 |
open $ffl,$ffl or die "$prg: no $ffl, use first: $prg -l\n"; |
| 1261 |
while (<$ffl>) { |
| 1262 |
if (m{^server/user: .*/(.+)}i) { |
| 1263 |
$user = $1; |
| 1264 |
} elsif (/^from (.+) :$/) { |
| 1265 |
$from = $1; |
| 1266 |
} elsif (/#(\d+)\s+\d+ MB.* (http\S+\/(\S+)) "(.*)"/) { |
| 1267 |
push @{$files{all}},$2; |
| 1268 |
push @{$files{$from}},$2; |
| 1269 |
$url{$2} = "$from:$3"; |
| 1270 |
$myfile{$2} = $2 if $from eq $user; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
close $ffl; |
| 1274 |
} |
| 1275 |
|
| 1276 |
|
| 1277 |
sub pathsearch { |
| 1278 |
my $prg = shift; |
| 1279 |
|
| 1280 |
foreach my $dir (split(':',$ENV{PATH})) { |
| 1281 |
return "$dir/$prg" if -x "$dir/$prg"; |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
|
| 1286 |
{ |
| 1287 |
my $tty; |
| 1288 |
|
| 1289 |
sub inquire { |
| 1290 |
my $prompt = shift; |
| 1291 |
my $default = shift; |
| 1292 |
local $| = 1; |
| 1293 |
local $_; |
| 1294 |
|
| 1295 |
if (defined $default) { |
| 1296 |
unless ($tty) { |
| 1297 |
chomp($tty = `tty 2>/dev/null`); |
| 1298 |
eval { local $^W; require "sys/ioctl.ph"; }; |
| 1299 |
} |
| 1300 |
|
| 1301 |
if (defined(&TIOCSTI) and $tty and open($tty,'>',$tty)) { |
| 1302 |
print $prompt; |
| 1303 |
# push default answer into keyboard buffer |
| 1304 |
foreach my $a (split("",$default)) { ioctl($tty,&TIOCSTI,$a) } |
| 1305 |
chomp($_ = <STDIN>||''); |
| 1306 |
} else { |
| 1307 |
$prompt =~ s/([\?:=]\s*)/ [$default]$1/ or $prompt .= " [$default] "; |
| 1308 |
print $prompt; |
| 1309 |
chomp($_ = <STDIN>||''); |
| 1310 |
$_ = $default unless length; |
| 1311 |
} |
| 1312 |
} else { |
| 1313 |
print $prompt; |
| 1314 |
chomp($_ = <STDIN>||''); |
| 1315 |
} |
| 1316 |
|
| 1317 |
return $_; |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
# sh pattern to Perl regular expression |
| 1323 |
sub pre { |
| 1324 |
local $_ = shift; |
| 1325 |
if (m:^/(.+)/$:) { |
| 1326 |
return $1; |
| 1327 |
} else { |
| 1328 |
s/\./\\./g; |
| 1329 |
s/(?<!\\)\?/./g; |
| 1330 |
s/(?<!\\)\*/.*/g; |
| 1331 |
s/([()+{}\|])/\\$1/g; |
| 1332 |
s/^\^/\\^/; |
| 1333 |
s/\$$/\\\$/; |
| 1334 |
return $_; |
| 1335 |
} |
| 1336 |
} |
| 1337 |
|
| 1338 |
### common functions ### |
| 1339 |
|
| 1340 |
sub mtime { |
| 1341 |
my @d = localtime((stat shift)[9]); |
| 1342 |
return sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]); |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
sub urldecode { |
| 1347 |
local $_ = shift; |
| 1348 |
s/\%([a-f\d]{2})/chr(hex($1))/ige; |
| 1349 |
return $_; |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
sub serverconnect { |
| 1354 |
my ($server,$port) = @_; |
| 1355 |
my $connect = "CONNECT $server:$port HTTP/1.1"; |
| 1356 |
local $_; |
| 1357 |
|
| 1358 |
if ($proxy) { |
| 1359 |
tcpconnect(split(':',$proxy)); |
| 1360 |
if ($port == 443) { |
| 1361 |
nvtsend($connect,""); |
| 1362 |
$_ = <$SH>; |
| 1363 |
s/\r//; |
| 1364 |
warn "<-- $_"if $opt_v; |
| 1365 |
unless (/^HTTP.1.. 200/) { |
| 1366 |
die "$prg: proxy error : $_"; |
| 1367 |
} |
| 1368 |
&enable_ssl; |
| 1369 |
$SH = IO::Socket::SSL->start_SSL($SH,%SSL); |
| 1370 |
} |
| 1371 |
} else { |
| 1372 |
tcpconnect($server,$port); |
| 1373 |
} |
| 1374 |
} |
| 1375 |
|
| 1376 |
|
| 1377 |
# set up tcp/ip connection |
| 1378 |
sub tcpconnect { |
| 1379 |
my ($server,$port) = @_; |
| 1380 |
|
| 1381 |
if ($SH) { |
| 1382 |
close $SH; |
| 1383 |
undef $SH; |
| 1384 |
} |
| 1385 |
|
| 1386 |
if ($port == 443) { |
| 1387 |
# eval "use IO::Socket::SSL qw(debug3)"; |
| 1388 |
&enable_ssl; |
| 1389 |
$SH = IO::Socket::SSL->new( |
| 1390 |
PeerAddr => $server, |
| 1391 |
PeerPort => $port, |
| 1392 |
Proto => 'tcp', |
| 1393 |
%SSL |
| 1394 |
); |
| 1395 |
} else { |
| 1396 |
$SH = IO::Socket::INET->new( |
| 1397 |
PeerAddr => $server, |
| 1398 |
PeerPort => $port, |
| 1399 |
Proto => 'tcp', |
| 1400 |
); |
| 1401 |
} |
| 1402 |
|
| 1403 |
if ($SH) { |
| 1404 |
autoflush $SH 1; |
| 1405 |
binmode $SH; |
| 1406 |
} else { |
| 1407 |
die "$prg: cannot connect $server:$port - $@\n"; |
| 1408 |
} |
| 1409 |
|
| 1410 |
warn "TCPCONNECT to $server:$port\n" if $opt_v; |
| 1411 |
} |
| 1412 |
|
| 1413 |
|
| 1414 |
sub sendheader { |
| 1415 |
my $sp = shift; |
| 1416 |
my @head = @_; |
| 1417 |
my $head; |
| 1418 |
|
| 1419 |
push @head,"Host: $sp"; |
| 1420 |
push @head,"User-Agent: $useragent"; |
| 1421 |
|
| 1422 |
foreach $head (@head) { |
| 1423 |
chomp $head; |
| 1424 |
warn "--> $head\n" if $opt_v; |
| 1425 |
print {$SH} $head,"\r\n"; |
| 1426 |
} |
| 1427 |
warn "-->\n" if $opt_v; |
| 1428 |
print {$SH} "\r\n"; |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
sub nvtsend { |
| 1433 |
local $SIG{PIPE} = sub { $sigpipe = "@_" }; |
| 1434 |
|
| 1435 |
$sigpipe = ''; |
| 1436 |
|
| 1437 |
die "$prg: internal error: no active network handle\n" unless $SH; |
| 1438 |
die "$prg: remote host has closed the link\n" unless $SH->connected; |
| 1439 |
|
| 1440 |
foreach my $line (@_) { |
| 1441 |
warn "--> $line\n" if $opt_v; |
| 1442 |
print {$SH} $line,"\r\n"; |
| 1443 |
if ($sigpipe) { |
| 1444 |
undef $SH; |
| 1445 |
return 0; |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
return 1; |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
sub shellquote { |
| 1454 |
local $_ = shift; |
| 1455 |
s/([^\w\@\/!^%:_.,=+-])/\\$1/g; |
| 1456 |
return $_; |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
sub debug { |
| 1461 |
print "## DEBUG: @_\n" if $DEBUG; |
| 1462 |
} |
| 1463 |
|
| 1464 |
|
| 1465 |
sub locale { |
| 1466 |
my $string = shift; |
| 1467 |
|
| 1468 |
# my @x = Encode->encodings(':all'); die "$CTYPE - @x"; |
| 1469 |
if ($CTYPE) { |
| 1470 |
if ($CTYPE =~ /UTF-?8/i) { |
| 1471 |
return $string; |
| 1472 |
} elsif (grep { $CTYPE =~ /^$_$/i } Encode->encodings(':all')) { |
| 1473 |
return encode($CTYPE,decode('UTF8',$string)); |
| 1474 |
} else { |
| 1475 |
return encode('ISO-8859-1',decode('UTF8',$string)); |
| 1476 |
} |
| 1477 |
} |
| 1478 |
|
| 1479 |
return $string; |
| 1480 |
} |
| 1481 |
|
| 1482 |
|
| 1483 |
sub enable_ssl { |
| 1484 |
eval "use IO::Socket::SSL"; |
| 1485 |
die "$prg: cannot load IO::Socket::SSL\n" if $@; |
| 1486 |
# needed for CentOS (Redhat, too?) |
| 1487 |
foreach (qw'/etc/ssl/cert.pem /etc/ssl/certs/ca-bundle.crt') { |
| 1488 |
$SSL{SSL_ca_file} = $_ if -f; |
| 1489 |
} |
| 1490 |
&get_ssl_env; |
| 1491 |
eval '$SSL{SSL_verify_mode} = 0 if Net::SSLeay::SSLeay() <= 9470143'; |
| 1492 |
if ($opt_v) { |
| 1493 |
foreach my $v (keys %SSL) { |
| 1494 |
printf STDERR "%s => %s\n",$v,$SSL{$v}; |
| 1495 |
} |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
|
| 1500 |
sub get_ssl_env { |
| 1501 |
# set SSL/TLS options |
| 1502 |
if (defined($ENV{SSLVERIFY})) { |
| 1503 |
$SSL{SSL_verify_mode} = $ENV{SSLVERIFY}; |
| 1504 |
} |
| 1505 |
foreach my $opt (qw( |
| 1506 |
SSL_version |
| 1507 |
SSL_cipher_list |
| 1508 |
SSL_verify_mode |
| 1509 |
SSL_ca_path |
| 1510 |
SSL_ca_file) |
| 1511 |
) { |
| 1512 |
my $env = uc($opt); |
| 1513 |
$env =~ s/_//g; |
| 1514 |
$SSL{$opt} = $ENV{$env} if defined($ENV{$env}); |
| 1515 |
} |
| 1516 |
|
| 1517 |
if ($SSL{SSL_verify_mode} and $SSL{SSL_verify_mode} ne 'SSL_VERIFY_NONE') { |
| 1518 |
&search_ca; |
| 1519 |
unless ($SSL{SSL_ca_path} or $SSL{SSL_ca_file}) { |
| 1520 |
die "$prg: \$SSLVERIFYMODE, but not valid \$SSLCAPATH or \$SSLCAFILE\n"; |
| 1521 |
} |
| 1522 |
} elsif (defined($SSL{SSL_verify_mode})) { |
| 1523 |
# user has set SSLVERIFY=0 ! |
| 1524 |
} else { |
| 1525 |
&search_ca; |
| 1526 |
$SSL{SSL_verify_mode} = 1 if $SSL{SSL_ca_path} or $SSL{SSL_ca_file}; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
|
| 1530 |
|
| 1531 |
sub search_ca { |
| 1532 |
local $_; |
| 1533 |
|
| 1534 |
return if $SSL{SSL_ca_file} or $SSL{SSL_ca_path}; |
| 1535 |
|
| 1536 |
foreach (qw(/etc/ssl/certs/ca-certificates.crt)) { |
| 1537 |
if (-f) { |
| 1538 |
$SSL{SSL_ca_file} = $_; |
| 1539 |
return; |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
foreach (qw(/etc/ssl/certs /etc/pki/tls/certs)) { |
| 1544 |
if (-d) { |
| 1545 |
$SSL{SSL_ca_path} = $_; |
| 1546 |
return; |
| 1547 |
} |
| 1548 |
} |
| 1549 |
} |
| 1550 |
|
| 1551 |
|
| 1552 |
# from MIME::Base64::Perl |
| 1553 |
sub encode_b64 { |
| 1554 |
my $res = ""; |
| 1555 |
my $eol = "\n"; |
| 1556 |
my $padding; |
| 1557 |
|
| 1558 |
pos($_[0]) = 0; |
| 1559 |
$res = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs)); |
| 1560 |
$res =~ tr|\` -_|AA-Za-z0-9+/|; |
| 1561 |
$padding = (3-length($_[0])%3)%3; |
| 1562 |
$res =~ s/.{$padding}$/'=' x $padding/e if $padding; |
| 1563 |
return $res; |
| 1564 |
} |
| 1565 |
|
| 1566 |
|
| 1567 |
# from MIME::Base64::Perl |
| 1568 |
sub decode_b64 { |
| 1569 |
local $_ = shift; |
| 1570 |
my $uu = ''; |
| 1571 |
my ($i,$l); |
| 1572 |
|
| 1573 |
tr|A-Za-z0-9+=/||cd; |
| 1574 |
s/=+$//; |
| 1575 |
tr|A-Za-z0-9+/| -_|; |
| 1576 |
return "" unless length; |
| 1577 |
|
| 1578 |
$l = (length)-60; |
| 1579 |
for ($i = 0; $i <= $l; $i += 60) { |
| 1580 |
$uu .= "M" . substr($_,$i,60); |
| 1581 |
} |
| 1582 |
$_ = substr($_,$i); |
| 1583 |
if (length) { |
| 1584 |
$uu .= chr(32+(length)*3/4) . $_; |
| 1585 |
} |
| 1586 |
return unpack("u",$uu); |
| 1587 |
} |
| 1588 |
|
| 1589 |
|
| 1590 |
sub inputline { |
| 1591 |
my $prompt = shift; |
| 1592 |
my $default = shift||''; |
| 1593 |
my $term = new Term::ReadLine $prg; |
| 1594 |
|
| 1595 |
$term->ornaments(0) unless $ENV{PERL_RL}; |
| 1596 |
return $term->readline($prompt,$default)||''; |
| 1597 |
} |
| 1598 |
|
| 1599 |
|
| 1600 |
sub vexec { |
| 1601 |
vsystem(@_); |
| 1602 |
exit $?; |
| 1603 |
} |
| 1604 |
|
| 1605 |
|
| 1606 |
sub vsystem { |
| 1607 |
my @cmd = @_; |
| 1608 |
my $cmd; |
| 1609 |
my $shellmeta = '[\\\'"`*?&|<>(){};]'; |
| 1610 |
local $_; |
| 1611 |
|
| 1612 |
if ($opt_v) { |
| 1613 |
if (-t STDIN) { print STDERR '$ ' } |
| 1614 |
else { print STDERR '| ' } |
| 1615 |
if (scalar(@cmd) == 1) { |
| 1616 |
warn "@cmd\n"; |
| 1617 |
} else { |
| 1618 |
my @w; |
| 1619 |
foreach (@cmd) { |
| 1620 |
if (/\'/) { |
| 1621 |
push @w,shellquote($_); |
| 1622 |
} elsif (/[^\w\/:=~^%@,.+-]/) { |
| 1623 |
push @w,"'$_'"; |
| 1624 |
} else { |
| 1625 |
push @w,$_; |
| 1626 |
} |
| 1627 |
} |
| 1628 |
warn "@w\n"; |
| 1629 |
} |
| 1630 |
} |
| 1631 |
|
| 1632 |
$_ = "@cmd"; |
| 1633 |
if (scalar(@cmd) == 1 and /\s/ and not (/^\w+=/ or /$shellmeta/)) { |
| 1634 |
@cmd = split; |
| 1635 |
} |
| 1636 |
|
| 1637 |
if (scalar(@cmd) == 1) { |
| 1638 |
$cmd = "@cmd"; |
| 1639 |
$cmd =~ s/\|&$/ 2>&1|/; |
| 1640 |
if ($cmd =~ s/\|$//) { |
| 1641 |
my @a = `$cmd`; |
| 1642 |
if (wantarray) { |
| 1643 |
map { chomp } @a; |
| 1644 |
return @a; |
| 1645 |
} else { |
| 1646 |
chomp $a[0] if scalar(@a) == 1; |
| 1647 |
return join('',@a); |
| 1648 |
} |
| 1649 |
} else { |
| 1650 |
system $cmd; |
| 1651 |
} |
| 1652 |
} else { |
| 1653 |
system @cmd; |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
|
| 1658 |
# read one key from terminal in raw mode |
| 1659 |
sub ReadKey { |
| 1660 |
my $key; |
| 1661 |
local $SIG{INT} = sub { stty('reset'); exit }; |
| 1662 |
|
| 1663 |
stty('raw'); |
| 1664 |
# loop necessary for ESXi support |
| 1665 |
while (not defined $key) { $key = getc(STDIN) } |
| 1666 |
stty('reset'); |
| 1667 |
return $key; |
| 1668 |
} |
| 1669 |
|
| 1670 |
|
| 1671 |
sub stty { |
| 1672 |
if (shift eq 'raw') { |
| 1673 |
system qw'stty -echo -icanon eol',"\001"; |
| 1674 |
} else { |
| 1675 |
system qw'stty echo icanon eol',"\000"; |
| 1676 |
} |
| 1677 |
} |
| 1678 |
|
| 1679 |
|
| 1680 |
END { kill 9,$cpid if $cpid } |