diff --git a/clainsafecli/clainsafecli.c b/clainsafecli/clainsafecli.c index 650405c..28ebff4 100644 --- a/clainsafecli/clainsafecli.c +++ b/clainsafecli/clainsafecli.c @@ -5,47 +5,10 @@ #include #include #include +#include #include "options.h" - -size_t static write_data(void *buffer, size_t size, size_t nmemb, - void *userp) -{ - memcpy(userp, buffer, nmemb*size); - return 0; -} - -void -print_usage() -{ - printf("USAGE: clainsafecli [--tor|--i2p] [--server] file\n"); - return; -} - -int -store_link(const char *path, const char *buf) -{ - FILE *fp = fopen(path,"a+"); - if(fp == NULL) { - fprintf(stderr,"Error opening file %i: %s\n",errno, - strerror(errno)); - return -1; - } - fwrite(buf,strlen(buf),1,fp); - fputc('\n',fp); - return 0; -} - -void -print_help() -{ - printf("--server : specifies the lainsafe server\n%s\n%s\n%s", - "--tor: uses tor", - "--help: print this message\n", - "--i2p: uses i2p HTTP proxy" - ); - return; -} +#include "clainsafecli.h" int main(int argc, char **argv) @@ -55,22 +18,23 @@ main(int argc, char **argv) int tor_flag, i2p_flag; tor_flag = i2p_flag = 0; - + char *buffer = (char *)calloc(1024,sizeof(char)); if(buffer == NULL) { fprintf(stderr,"Error allocating memory!\n"); + return -1; } CURL *easy_handle = curl_easy_init(); - + if(!easy_handle) { fprintf(stderr,"Error initializing libcurl\n"); return -1; } if(argc == optind) { - print_usage(); + print_usage(); return -1; } - + int option_index = 0; static struct option long_options[] = { {"server",required_argument,0,'s'}, @@ -113,14 +77,14 @@ main(int argc, char **argv) fprintf(stderr,"Error opening file\n"); return -1; } - + /* curl options */ - curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(easy_handle,CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(easy_handle,CURLOPT_WRITEDATA,buffer); curl_easy_setopt(easy_handle,CURLOPT_URL,server); /* Proxy options */ - + if(tor_flag && i2p_flag) { fprintf(stderr,"Tor and I2P can't be used at once\n"); return -1; @@ -147,12 +111,20 @@ main(int argc, char **argv) CURLFORM_COPYCONTENTS,argv[optind], CURLFORM_END); + /* Progress bar + * + * TODO: Use a custom progress bar rather than + * default curl progress bar + * + */ + + curl_easy_setopt(easy_handle,CURLOPT_NOPROGRESS,0L); curl_easy_setopt(easy_handle,CURLOPT_HTTPPOST,post); - + curl_easy_perform(easy_handle); puts(buffer); - + curl_formfree(post); curl_easy_cleanup(easy_handle); @@ -165,3 +137,31 @@ main(int argc, char **argv) free(buffer); return 0; } + +size_t static write_data(void *buffer, size_t size, size_t nmemb, + void *userp) +{ + memcpy(userp, buffer, nmemb*size); + return 0; +} + +void +print_usage() +{ + printf("USAGE: clainsafecli [--tor|--i2p] [--server] file\n"); + return; +} + +int +store_link(const char *path, const char *buf) +{ + FILE *fp = fopen(path,"a+"); + if(fp == NULL) { + fprintf(stderr,"Error opening file %i: %s\n",errno, + strerror(errno)); + return -1; + } + fwrite(buf,strlen(buf),1,fp); + fputc('\n',fp); + return 0; +} diff --git a/clainsafecli/clainsafecli.h b/clainsafecli/clainsafecli.h new file mode 100644 index 0000000..2285a6b --- /dev/null +++ b/clainsafecli/clainsafecli.h @@ -0,0 +1,17 @@ +#include + +size_t +static write_data(void *buffer, size_t size, size_t nmemb, + void *userp); + +void +print_usage(); + +int progress_func(void* ptr, double TotalToDownload, double NowDownloaded, + double TotalToUpload, double NowUploaded); + +int +store_link(const char *path, const char *buf); + +void +print_help(); diff --git a/lainsafecli b/lainsafecli index 291446e..d418338 100755 --- a/lainsafecli +++ b/lainsafecli @@ -16,6 +16,8 @@ # You should have received a copy of the GNU General Public License # along with lainsafe. If not, see . +# This script is kind of deprecated. Use clainsafecli instead. + use Getopt::Long; use LWP::UserAgent; my $ua = LWP::UserAgent->new; @@ -66,24 +68,24 @@ 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; - } + { + 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'); - } + { + my $checker = $ua->proxy([qw(http https)] => 'socks://localhost:9050'); + } sub enable_i2p - { - my $checker = $ua->proxy([qw(http https)] => 'http://localhost:4444'); - } + { + my $checker = $ua->proxy([qw(http https)] => 'http://localhost:4444'); + } ## PROGRAM @@ -92,12 +94,12 @@ GetOptions ("server=s" => \$DEFAULT_SERVER, "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; + 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; @@ -121,30 +123,31 @@ substr($DEFAULT_SERVER, 0, 0, 'https://') unless $DEFAULT_SERVER =~ /^(http|http 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; + 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 ], - ], - ); + "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"; + exit; + } print $req->{_content} . "\n"; if ($STORE_LINKS) { - open FILE,'>>',$LINKS_FILE or die $!; - print FILE $req->{_content} . " $file" ."\n"; - close FILE; + open FILE,'>>',$LINKS_FILE or die $!; + print FILE $req->{_content} . " $file" ."\n"; + close FILE; } +