fix grammar, lol and isntructions on using curl and hlainsafecli
[lainsafe.git] / lainsafecli
1 #!/usr/bin/perl
2 # Lainsafe cli
3
4 # This file is part of lainsafe.
5
6 # lainsafe is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # lainsafe is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with lainsafe. If not, see <https://www.gnu.org/licenses/>.
18
19 use Getopt::Long;
20 use LWP::UserAgent;
21 my $ua = LWP::UserAgent->new;
22 use strict;
23 use warnings;
24 # variables
25 my $help;
26 my $tor;
27 my $i2p;
28 my $get_response;
29 my $DEFAULT_SERVER;
30 my $file;
31 my $DISPLAY_ASCII;
32 my $STORE_LINKS;
33 my $LINKS_FILE;
34 my $proxy_enabled = 1;
35 # Default options, if no specified.
36 $DEFAULT_SERVER = "https://lainsafe.delegao.moe";
37 $DISPLAY_ASCII = 1; # 0 if you don't want the ascii
38 $STORE_LINKS = 1; # 0 if you don't want to keep track of your upload
39 $LINKS_FILE = "$ENV{HOME}/.cache/lainsafelinks";
40
41 eval "use LWP::Protocol::socks; 1" or $proxy_enabled = 0;
42 my $ASCII_ART = <<'EOF';
43 _..-- ----- --.._
44 ,-'' `-.
45 , \
46 / \
47 / ` . \
48 ' / || ;
49 ; ^/| |/ | |
50 | /v /\`-'v√\'-|\ ,
51 | /v` ,--- ---- .^.| ;
52 : | /´@@`, ,@@`\ | ;
53 ' | '. @@ / \@@ / |\ |;
54 | ^| ----- --- | \/||
55 ` |` | /\ /
56 \ \ |/ |,
57 ' ; \ /| |
58 ` \ -- / | |
59 ` `. .-' | /
60 v,- `;._ _.; | |
61 `'`\ |-_ -^'^'| |
62 ------ |/
63
64 EOF
65
66 # Subs
67
68 sub help
69 {
70 print "lainsafecli, a command line interface for lainsafe.\n";
71 print "USAGE: lainsafecli [--tor | --i2p] [--server] FILE\n\n";
72 print "if --server not given, $DEFAULT_SERVER is used.\n";
73 print "--tor and --i2p are available\n" if $proxy_enabled;
74 print "--tor and --i2p are unavailable, flag are ignored\n" unless $proxy_enabled;
75 exit;
76 }
77
78 sub enable_tor
79 {
80 my $checker = $ua->proxy([qw(http https)] => 'socks://localhost:9050');
81 }
82
83 sub enable_i2p
84 {
85 my $checker = $ua->proxy([qw(http https)] => 'http://localhost:4444');
86 }
87
88 ## PROGRAM
89
90 GetOptions ("server=s" => \$DEFAULT_SERVER,
91 "help|" => \$help,
92 "tor"=> \$tor,
93 "i2p"=>\$i2p,
94 "get-response"=>\$get_response
95 );
96
97 &help if $help || not defined $ARGV[0];
98 if ($i2p and $tor) {
99 print "What are you trying to do? I don't really get you sometimes...\n";
100 exit;
101 }
102 &enable_tor if $tor and $proxy_enabled;
103 &enable_i2p if $i2p and $proxy_enabled;
104
105
106 # check if file is given
107
108 $file = $ARGV[@ARGV-1];
109
110 die "File does not exist\n" unless -e $file;
111
112
113 my $req;
114
115 # Fake user agent
116 $ua->agent("Mozilla/5.0 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0");
117
118 # modify url if necessary
119 substr($DEFAULT_SERVER, 0, 0, 'https://') unless $DEFAULT_SERVER =~ /^(http|https):\/\//;
120 # check if server is running lainsafe
121
122 my $url_to_upload = $DEFAULT_SERVER . "/upload.cgi";
123 if (!$ua->get($url_to_upload)->is_success) {
124 print "$url_to_upload is not running lainsafe. (--get-response to check the error)\n";
125 print $ua->get($url_to_upload)->decoded_content if $get_response;
126 exit;
127 }
128
129
130 $req = $ua->post($url_to_upload,
131 Content_Type => 'form-data',
132 Content => [
133 "file" => [ $file ],
134 ],
135 );
136
137
138 print $ASCII_ART if $DISPLAY_ASCII;
139 if ($req->{_content} =~ /instance/) # If someone knows how to do it another way, I'm all ears
140 {
141 print $req->{_content} . "\n";
142 exit;
143 }
144 print $req->{_content} . "\n";
145
146 if ($STORE_LINKS) {
147 open FILE,'>>',$LINKS_FILE or die $!;
148 print FILE $req->{_content} . " $file" ."\n";
149 close FILE;
150 }