]> git.treefish.org Git - fex.git/blob - bin/afex
Original release 20150120
[fex.git] / bin / afex
1 #!/usr/bin/perl -w
2
3 # client for anonymous FEX or SEX
4 #
5 # Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
6 #
7 # Perl Artistic Licence
8
9 $fexserver = '';
10
11 $0 =~ s:.*/::;;
12
13 $u = $ENV{AFEX} || $fexserver || &guessserver;
14 $u = "http://$u" if $u !~ /^http/;
15
16 if (@ARGV and $ARGV[0] eq '-h' or not @ARGV and -t STDIN) { &usage }
17
18 $opt_v = (@ARGV and $ARGV[0] eq '-v') ? shift @ARGV : '';
19
20 if ($0 eq 'asex') {
21   $a = "$u/anonymous";
22   if (-t STDIN and @ARGV and "@ARGV" =~ /^(\d+)( -)?$/) {
23     $id = $1;
24     if ($2) {
25       vexec("sexget $opt_v $a $id | tar xvf -");
26     } else {
27       vexec(ws("sexget $opt_v $a $id"));
28     }
29   } else {
30     if (@ARGV and $ARGV[0] =~ /^\d+$/) {
31       $id = shift @ARGV;
32     } else {
33       $id = sprintf("%06d",rand(1000000));
34     }
35     print "# commands for SEX recipient:\n";
36     if (@ARGV) {
37       print "wget -qO- $u/sex?anonymous=$id | tar xvf -\n";
38       print "asex $id -\n";
39       print "# streaming files and waiting for SEX recipient:\n";
40       vexec("tar cvf - @ARGV | sexsend -q $opt_v $a $id");
41     } else {
42       print "wget -qO- $u/sex?anonymous=$id\n";
43       print "asex $id\n";
44       vexec(ws("sexsend -q $opt_v $a $id"));
45     }
46   }
47   exit;
48 }
49
50 if ($0 eq 'afex') {
51
52   if ("@ARGV" =~ /^-(\d+)$/) {
53     $n = $1;
54     $cmd = "wget -qO/dev/null $u/fop/anonymous/anonymous/afex_$n.tar?DELETE";
55     vsystem(ws($cmd));
56     print "not " if $?;
57     print "deleted\n";
58     exit;
59   }
60
61   # download
62   if (-t STDIN and "@ARGV" =~ /^(\d+)( -)?$/) {
63     $id = $1;
64     $nq = $2;
65     $u .= "/fop/anonymous/anonymous/afex_$id.tar";
66     if ($nq) {
67       vexec("fexget $opt_v -s- $u | tar xvf -");
68     } else {
69       $aft = "/tmp/afex_$id.tar";
70       $cmd = "fexget $opt_v -s- $u >$aft";
71       vsystem($cmd);
72       if (`file $aft` =~ /tar archive/) {
73         print "Files in archive:\n";
74         $cmd = "tar tvf $aft";
75         vsystem(ws($cmd));
76         print "extract these files (Y/n)? ";
77         if ((<STDIN>||'y') =~ /^[Yy\n]/) {
78           $cmd = "tar xvf $aft";
79           vsystem(ws($cmd));
80           unlink $aft;
81         } else {
82           print "keeping $aft\n";
83         }
84       } else {
85         open $aft,$aft or die "$0: cannot open $aft = $!\n";
86         unlink $aft;
87         print while read($aft,$_,65536);
88       }
89     }
90     exit;
91   }
92
93   # upload
94   else {
95     if (@ARGV and $ARGV[0] =~ /^\d+$/) {
96       $id = shift @ARGV;
97     } else {
98       $id = sprintf("%06d",rand(1000000));
99     }
100     $aft = "afex_$id.tar";
101     my $durl ="$u/fop/anonymous/anonymous/$aft??ID=ANONYMOUS";
102     if (`wget -S --spider $durl 2>&1` =~ /X-Location:/) {
103       die "$0: afex $id already exists - choose another ID\n";
104     }
105     @fexsend = ws("fexsend $opt_v -o -K -k 1");
106     if (@ARGV) {
107       vsystem(@fexsend,'-a',$aft,@ARGV,"$u/anonymous");
108       exit $? if $?;
109     } elsif (not -t STDIN) {
110       $aft = "/tmp/$aft";
111       open $aft,'>',$aft or die "$0: cannot write $aft - $!\n";
112       print {$aft} $_ while read(STDIN,$_,65536);
113       close $aft;
114       $s = vsystem(@fexsend,$aft,"$u/anonymous");
115       unlink $aft;
116       exit $s if $s;
117     } else {
118       die "say captain, say WHOT?!";
119     }
120     print "For download use:\n";
121     print "$u//$aft\n" if -t STDIN;
122     print "afex $id\n";
123   }
124   exit;
125 }
126
127 &usage;
128
129 sub guessserver {
130   my $fexserver = '';
131   my $rc = '/etc/resolv.conf';
132   local $_;
133   
134   open $rc,$rc or die "$0: cannot open $rc - $!\n";
135   while (<$rc>) {
136     if (/^\s*domain\s+([\w.-]+)/) {
137       $fexserver = "http://fex.$1";
138       last;
139     }
140     if (/^\s*search\s+([\w.-]+)/) {
141       $fexserver = "http://fex.$1";
142     }
143   }
144   close $rc;
145   return $fexserver;
146 }
147
148 sub ws {
149   local $_ = shift;
150   return split;
151 }
152
153 sub vexec {
154   warn "@_\n" if $opt_v;
155   exec @_;
156 }
157
158 sub vsystem {
159   warn "@_\n" if $opt_v;
160   system @_;
161 }
162
163 sub usage {
164   if ($0 eq 'afex') {
165     print "file input usage:  $0 [ID] files...\n";
166     print "file output usage: $0 ID [-]\n";
167     print "pipe input usage:  ... | $0 [ID]\n";
168     print "pipe output usage: $0 ID | ...\n";
169     print "delete usage:      $0 -ID\n";
170     exit;
171   }
172   if ($0 eq 'asex') {
173     print "file input usage:  $0 [ID] files...\n";
174     print "file output usage: $0 ID -\n";
175     print "pipe input usage:  ... | $0 [ID]\n";
176     print "pipe output usage: $0 ID | ...\n";
177     exit;
178   }
179   die "program name must be a afex or asex, not $0\n";
180 }