#!/usr/bin/perl
# Lainsafe cli

# This file is part of lainsafe.

# lainsafe is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# lainsafe is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with lainsafe.  If not, see <https://www.gnu.org/licenses/>.

# This script is kind of deprecated. Use clainsafecli instead.

use Getopt::Long;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
use strict;
use warnings;
# variables
my $help;
my $tor;
my $i2p;
my $get_response;
my $DEFAULT_SERVER;
my $file;
my $DISPLAY_ASCII;
my $STORE_LINKS;
my $LINKS_FILE;
my $proxy_enabled = 1;
# Default options, if no specified.
$DEFAULT_SERVER = "https://lainsafe.delegao.moe";
$DISPLAY_ASCII = 1; # 0 if you don't want the ascii
$STORE_LINKS = 1;   # 0 if you don't want to keep track of your upload
$LINKS_FILE = "$ENV{HOME}/.cache/lainsafelinks";

eval "use LWP::Protocol::socks; 1" or $proxy_enabled = 0;
my $ASCII_ART = <<'EOF';
           _..-- ----- --.._
        ,-''                `-.
       ,                       \
      /                         \
     /        ` .                \
    '         /  ||               ;
    ;      ^/|   |/        |      |
    |      /v    /\`-'v√\'-|\     ,
    |   /v` ,---      ---- .^.|    ;
    :  |   /´@@`,     ,@@`\  |    ;
    '  |  '. @@ /     \@@ /  |\  |;
    | ^|    -----      ---   | \/||
    ` |`                     | /\ /
    \    \                   |/ |,
    ' ;   \                 /|  |
      `    \      --       / |  |
      `     `.          .-'  |  /
       v,-    `;._   _.;     | |
          `'`\ |-_      -^'^'| |
                  ------     |/

EOF

# Subs

sub help
    {
	   print "lainsafecli, a command line interface for lainsafe.\n";
	   print "USAGE: lainsafecli [--tor | --i2p] [--server] FILE\n\n";
	   print "if --server not given, $DEFAULT_SERVER is used.\n";
	   print "--tor and --i2p are available\n" if $proxy_enabled;
	   print "--tor and --i2p are unavailable, flag are ignored\n" unless $proxy_enabled;
	   exit;
    }

sub enable_tor
    {
	   my $checker = $ua->proxy([qw(http https)] => 'socks://localhost:9050');
    }

sub enable_i2p
    {
	   my $checker = $ua->proxy([qw(http https)] => 'http://localhost:4444');
    }

## PROGRAM

GetOptions ("server=s" => \$DEFAULT_SERVER,
            "help|" => \$help,
            "tor"=> \$tor,
            "i2p"=>\$i2p,
            "get-response"=>\$get_response
	   );

&help if $help || not defined $ARGV[0];
if ($i2p and $tor) {
    print "What are you trying to do? I don't really get you sometimes...\n";
    exit;
}
&enable_tor if $tor and $proxy_enabled;
&enable_i2p if $i2p and $proxy_enabled;


# check if file is given

$file = $ARGV[@ARGV-1];

die "File does not exist\n" unless -e $file;


my $req;

# Fake user agent
$ua->agent("Mozilla/5.0 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0");

# modify url if necessary
substr($DEFAULT_SERVER, 0, 0, 'https://') unless $DEFAULT_SERVER =~ /^(http|https):\/\//;
# check if server is running lainsafe

my $url_to_upload = $DEFAULT_SERVER . "/upload.cgi";
if (!$ua->get($url_to_upload)->is_success) {
    print "$url_to_upload is not running lainsafe. (--get-response to check the error)\n";
    print $ua->get($url_to_upload)->decoded_content if $get_response;
    exit;
}


$req = $ua->post($url_to_upload,
                 Content_Type => 'form-data',
                 Content => [
				 "file" => [ $file ],
			  ],
		   );


print $ASCII_ART if $DISPLAY_ASCII;
if ($req->{_content} =~ /instance/) # If someone knows how to do it another way, I'm all ears
    {
	   print $req->{_content} . "\n";
	   exit;
    }
print $req->{_content} . "\n";

if ($STORE_LINKS) {
    open FILE,'>>',$LINKS_FILE or die $!;
    print FILE $req->{_content} . " $file"  ."\n";
    close FILE;
}