summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--scripts/xdcc_autoget.pl133
1 files changed, 88 insertions, 45 deletions
diff --git a/scripts/xdcc_autoget.pl b/scripts/xdcc_autoget.pl
index b7cef39..5625243 100644
--- a/scripts/xdcc_autoget.pl
+++ b/scripts/xdcc_autoget.pl
@@ -4,7 +4,8 @@
# if you encounter any problems, fix it yourself you lazy bastard (or get me to), then contact me so I can add your fix and bump that version #
# BeerWare License. Use any part you want, but buy me a beer if you ever meet me and liked this hacked together broken PoS
# Somewhat based off of DCC multiget by Kaveh Moini.
-# USE: for help : ag_help
+# USE: to add the statusbar : /statusbar [name] add ag_statusbar
+# for help : ag_help
# to run : ag_run
# to halt at next : ag_stop
# to reset all settings: ag_reset
@@ -27,6 +28,7 @@ use strict;
use warnings;
use Irssi;
+use Irssi::TextUI;
use Text::ParseWords;
use autodie; # die if problem reading or writing a file
use File::HomeDir;
@@ -34,7 +36,7 @@ use File::Copy;
use Irssi 20090331;
use vars qw($VERSION %IRSSI);
-$VERSION = 1.2;
+$VERSION = 1.4;
%IRSSI = (
name => "autoget",
description => "XDCC Autoget, for automated searching and downloading of xdcc packs",
@@ -91,6 +93,7 @@ my @terms; #lists of search terms, bots, and pack numbers (for current bot only
my @bots;
my @packs;
my @finished; #list of packs already downloaded and their filenames
+my $statusbarmessage = "Inactive";
my $termcounter = 0; #counters for array position
my $botcounter = 0;
@@ -140,6 +143,7 @@ sub ag_server #init server
sub ag_help
{
+ Irssi::print "to add the statusbar : /statusbar [name] add ag_statusbar";
Irssi::print "for this help : ag_help";
Irssi::print "to run : ag_run";
Irssi::print "to halt at next : ag_stop";
@@ -201,6 +205,7 @@ sub ag_search #searches bots for packs
if($episodicflag) #episodic searches are complicated
{
my $ep = sprintf("%.2d", $episode);
+ $statusbarmessage = "Searching $bots[$botcounter] for \"$terms[$termcounter]\" episode $ep in $format";
if ($format ne "" and $formatflag)
{
ag_message("msg $bots[$botcounter] $findprefix $terms[$termcounter] $ep $format"); #first search with format
@@ -215,12 +220,14 @@ sub ag_search #searches bots for packs
}
else
{
+ $statusbarmessage = "Searching $bots[$botcounter] for \"$terms[$termcounter]\" episode $ep";
ag_message("msg $bots[$botcounter] $findprefix $terms[$termcounter] $ep" );
push(@totags, Irssi::timeout_add_once($botdelay * 1000, sub { &ag_skip; } , []));
}
}
else #if not episodic, just search and skip
{
+ $statusbarmessage = "Searching $bots[$botcounter] for \"$terms[$termcounter]\"";
ag_message("msg $bots[$botcounter] $findprefix $terms[$termcounter]" );
push(@totags, Irssi::timeout_add_once($botdelay * 1000, sub { &ag_skip; } , []));
}
@@ -282,8 +289,9 @@ sub ag_packrequest #sends the xdcc send request, and retries on failure
{
$reqpackflag = 1;
Irssi::signal_add("dcc get receive", "ag_opendcc"); #init DCC recieve init flag
+ $statusbarmessage = "Getting pack #$packs[$packcounter] on $bots[$botcounter]";
ag_message("msg $bots[$botcounter] $sendprefix $packs[$packcounter]");
- push(@totags, Irssi::timeout_add_once($botdelay * 1000, sub { if (!$downloadflag) { Irssi::print "AG | Connection failed"; Irssi::print "AG | retrying: " . $bots[$botcounter] . " " .$packs[$packcounter]; &ag_packrequest; } } , []));
+ push(@totags, Irssi::timeout_add_once($botdelay * 1000, sub { if (!$downloadflag) { &ag_packrequest; } } , []));
}
}
@@ -291,6 +299,7 @@ sub ag_opendcc #runs on DCC recieve init
{
my ($gdcc) = @_; #current pack
my $botname = $gdcc->{'nick'};
+ my $filename = $gdcc->{'arg'};
$botname =~ tr/[A-Z]/[a-z]/;
$bots[$botcounter] =~ tr/[A-Z]/[a-z]/;
@@ -301,7 +310,7 @@ sub ag_opendcc #runs on DCC recieve init
&ag_remtimeouts;
$dccflag = 0;
$downloadflag = 1;
- Irssi::print "AG | received connection for bot: " . $botname . ", #" . $packs[$packcounter];
+ $statusbarmessage = "Downloading $filename from $botname, #$packs[$packcounter]";
foreach my $n (@finished) #don't redownload finished packs
{
if ($n eq $gdcc->{'arg'}) #if file already downloaded, emulate an already finished dcc transfer (in case file deleted) and cancel
@@ -354,7 +363,7 @@ sub ag_skip
$botcounter = 0;
$termcounter = 0;
$packcounter = 0;
- Irssi::print "AG | Waiting " . $exedelay . " minutes until next search";
+ $statusbarmessage = "Waiting $exedelay minutes";
Irssi::timeout_add_once($exedelay * 1000 * 60, sub { &ag_run; } , []);
$runningflag = 0;
}
@@ -387,7 +396,7 @@ sub ag_skip
$botcounter = 0;
$termcounter = 0;
$packcounter = 0;
- Irssi::print "AG | Waiting " . $exedelay . " minutes until next search";
+ $statusbarmessage = "Waiting $exedelay minutes";
Irssi::timeout_add_once($exedelay * 1000 * 60, sub { &ag_run; } , []);
$runningflag = 0;
}
@@ -414,7 +423,6 @@ sub ag_closedcc
}
if ($dcc->{'transfd'} == $dcc->{'size'})
{
- Irssi::print "AG | transfer successful";
ag_addfinished($dcc->{'arg'});
}
@@ -423,15 +431,15 @@ sub ag_closedcc
@packs = (); #delete packlist
$packcounter = 0;
$episode++;
- Irssi::print "AG | waiting " . $nexdelay . " seconds";
- push(@totags, Irssi::timeout_add_once($nexdelay * 1000, sub { Irssi::print "AG | Getting next episode"; &ag_search; }, []));
+ $statusbarmessage = "Waiting $nexdelay seconds";
+ push(@totags, Irssi::timeout_add_once($nexdelay * 1000, sub { &ag_search; }, []));
}
elsif ($dcc->{'transfd'} == $dcc->{'size'})
{
if ($packcounter < $#packs)
{
$packcounter++;
- Irssi::print "AG | Getting next pack in list in " . $nexdelay . " seconds ";
+ $statusbarmessage = "Waiting $nexdelay seconds";
push(@totags, Irssi::timeout_add_once($nexdelay * 1000, sub { &ag_reqpack; }, []));
}
elsif ($termcounter < $#terms)
@@ -439,7 +447,7 @@ sub ag_closedcc
@packs = (); #delete last terms packlist
$termcounter++;
$packcounter = 0;
- Irssi::print "AG | Packlist finished. Searching next term in " . $nexdelay . " seconds";
+ $statusbarmessage = "Waiting $nexdelay seconds";
push(@totags, Irssi::timeout_add_once($nexdelay * 1000, sub { &ag_search; }, []));
}
elsif ($botcounter < $#bots)
@@ -448,7 +456,7 @@ sub ag_closedcc
$botcounter++;
$termcounter = 0;
$packcounter = 0;
- Irssi::print "AG | Search term lidt finished. Searching nect bot in " . $nexdelay . " seconds";
+ $statusbarmessage = "Waiting $nexdelay seconds";
push(@totags, Irssi::timeout_add_once($nexdelay * 1000, sub { &ag_search; }, []));
}
else #if last pack on last search on last bot finished, then resets counters and starts over
@@ -457,16 +465,15 @@ sub ag_closedcc
$botcounter = 0;
$termcounter = 0;
$packcounter = 0;
- Irssi::print "AG | Waiting " . $exedelay . " minutes until next search";
+ $statusbarmessage = "Waiting $nexdelay seconds";
Irssi::timeout_add_once($exedelay * 1000 * 60, sub { &ag_run; } , []);
$runningflag = 0;
}
}
else #if not, retry transfer
{
- Irssi::print "AG | transfer failed";
- Irssi::print "AG | " . $dcrdelay . " seconds until retry";
- push(@totags, Irssi::timeout_add_once($dcrdelay * 1000, sub { Irssi::print "AG | retrying: " .$bots[$botcounter] . " " . $packs[$packcounter]; &ag_packrequest; }, []));
+ $statusbarmessage = "File incomplete. Retrying in $nexdelay seconds";
+ push(@totags, Irssi::timeout_add_once($dcrdelay * 1000, sub { &ag_packrequest; }, []));
}
}
}
@@ -483,6 +490,13 @@ sub ag_uniq #only returns unique entries
grep !$seen{$_}++, @_;
}
+sub ag_bar #prints the message to the statusbar
+{
+ my ($item, $get_size_only) = @_;
+ $item->default_handler($get_size_only, "{sb %_AG:%_ $statusbarmessage}", "", 1);
+}
+
+
sub ag_addfinished #save finished downloads
{
my $filename = $_[0];
@@ -496,70 +510,67 @@ sub ag_addfinished #save finished downloads
sub ag_parseadd #parses add arguments for storage
{
my ($file, @args) = @_;
- open(FILE, ">>", $file);
+ my @temp;
+ my @fil;
foreach my $arg (@args)
{
- print FILE $arg . "\n"; #print to file
+ push (@temp, $arg);
}
- close(FILE);
- copy($file, "/tmp/temp"); #copy to temp file so that duplicate lines [searches/bots] can be removed
+ open(FILE, "<", $file);
+ @fil = <FILE>;
+ chomp(@fil);
unlink "$file";
- open(TEMP, "<", "/tmp/temp");
open(FILE, ">", $file);
+ push(@fil, @temp);
my %hTmp;
- while (my $sLine = <TEMP>) #remove duplicate lines
+ foreach my $sLine (@fil) #remove duplicate lines
{
next if $sLine =~ m/^\s*$/; #remove empty lines. Without this, still destroys empty lines except for the first one.
$sLine=~s/^\s+//; #strip leading/trailing whitespace
$sLine=~s/\s+$//;
print FILE qq{$sLine\n} unless ($hTmp{$sLine}++);
}
- unlink "/tmp/temp";
close(FILE);
+ &ag_getbots;
+ &ag_getterms;
}
sub ag_parserem #parses remove arguments for deletion from file
{
my ($file, @args) = @_;
- open(TEMP, ">>", "/tmp/temp");
+ my @temp;
+ my @temp2;
+ my @fil;
foreach my $arg (@args)
{
- Irssi::print "AG | removing term: " . $arg;
- print TEMP $arg . "\n";
+ push (@temp, $arg);
}
- close(TEMP);
- open(TEMP2, ">", "/tmp/temp2");
open(FILE, "<", $file);
+ @fil = <FILE>;
+ chomp(@fil);
my %hTmp;
- while( my $fileLine = FILE->getline() ) #get each entry already stored
+ foreach my $fileLine (@fil) #get each entry already stored
{
- open(TEMP, "<", "/tmp/temp");
- while( my $tempLine = TEMP->getline() )
+ foreach my $tempLine (@temp)
{
if ($fileLine eq $tempLine) #if entry in file and arguments
{
$hTmp{$fileLine}++; #set flag to not copy
}
- print TEMP2 qq{$fileLine} unless $hTmp{$fileLine}; #copy other lines to other temp file
+ push(@temp2, qq{$fileLine}) unless $hTmp{$fileLine}; #copy other lines to other temp file
}
- close(TEMP);
}
- close(TEMP2);
- copy("/tmp/temp2", $file); #rewrite old file
- copy($file, "/tmp/temp");
+ @fil = @temp2; #rewrite old file
unlink "$file";
- open(TEMP, "<", "/tmp/temp");
- open(SEARCHES, ">", $file);
+ open(FILE, ">", $file);
%hTmp = ();
- while (my $sLine = <TEMP>) #remove duplicate lines
+ foreach my $sLine (@fil) #remove duplicate lines
{
next if $sLine =~ m/^\s*$/; #remove empty lines. Without this, still destroys empty lines except for the first one.
$sLine=~s/^\s+//; #strip leading/trailing whitespace
$sLine=~s/\s+$//;
print FILE qq{$sLine\n} unless ($hTmp{$sLine}++);
}
- unlink "/tmp/temp";
- unlink "/tmp/temp2";
close(FILE);
}
@@ -621,14 +632,14 @@ sub ag_run #main loop
if($runningflag == 0)
{
$runningflag = 1;
- if($#bots < 0 or $#terms < 0) {Irssi::print "AG | No bots or no search terms added. Halting"; &ag_stop;}
+ &ag_getbots;
+ &ag_getterms;
+ if($#bots < 0 or $#terms < 0) { $statusbarmessage = "No bots or no search terms."; push(@totags, Irssi::timeout_add_once(1000, sub { &ag_run; }, []));}
else
{
- Irssi::print "AG | Search and get cycle Initiated";
&ag_search;
}
}
- else {Irssi::print "AG | Another Instance is already running";}
}
sub ag_stop
@@ -647,7 +658,7 @@ sub ag_stop
if($runningflag == 1)
{
$runningflag = 0;
- Irssi::print "AG | Killed";
+ $statusbarmessage = "Inactive";
}
$msgflag = 1;
$termisepisodicflag = 0;
@@ -666,6 +677,33 @@ sub ag_stop
$episode = 1;
}
+sub ag_restart
+{
+ $statusbarmessage = "No connection";
+ Irssi::signal_remove("dcc get receive", "ag_opendcc");
+ Irssi::signal_remove("message irc notice", "ag_getmsg");
+
+ foreach my $to (@totags)
+ {
+ Irssi::timeout_remove($to);
+ }
+ @totags = ();
+
+ ag_message("msg $bots[$botcounter] $cancelprefix");
+
+ if($runningflag == 1)
+ {
+ $runningflag = 0;
+ }
+ $msgflag = 1;
+ $termisepisodicflag = 0;
+ $formatflag = 1;
+ $reqpackflag = 0;
+ $downloadflag = 0;
+ $newpackflag = 1;
+ $dccflag = 0;
+ Irssi::signal_add("server connected", "ag_initserver");
+}
sub ag_reset
{
Irssi::settings_set_int("ag_next_delay", 10);
@@ -704,9 +742,14 @@ close(SEARCHES);
open(FINISHED, ">>", $cachefilename);
close(FINISHED);
+Irssi::statusbar_item_register('ag_statusbar', 0, 'ag_bar');
+Irssi::timeout_add(100, sub { Irssi::statusbars_recreate_items(); Irssi::statusbar_items_redraw("ag_bar"); } , []);
+
&ag_init;
if ($initflag) {Irssi::signal_add("server connected", "ag_initserver");}
+Irssi::signal_add("server disconnected", "ag_restart");
+Irssi::signal_add("server lag disconnect", "ag_restart");
Irssi::signal_add("dcc closed", "ag_closedcc");
Irssi::signal_add("setup changed", "ag_setsettings");