From a0d7b7dea026944eb3671eebfbc078474598cbf0 Mon Sep 17 00:00:00 2001 From: Ralf Ertzinger Date: Sun, 21 Dec 2008 14:00:27 +0100 Subject: [PATCH] - print notice if local or remote queue is empty - save local queue across restarts --- xmlrtorrent.pl | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/xmlrtorrent.pl b/xmlrtorrent.pl index 3adbe27..d3e4f81 100644 --- a/xmlrtorrent.pl +++ b/xmlrtorrent.pl @@ -10,6 +10,7 @@ use vars qw($VERSION %IRSSI); use XML::Simple; use Data::Dumper; use File::Spec; +use List::Util qw(max); use xmlrtorrent; my $conf; @@ -25,6 +26,7 @@ my $PARAMS = { 'XMLURL' => 'http://localhost/RPC2', 'USERNAME' => '', 'PASSWORD' => '', + '_QUEUE' => {}, }; # activate debug here @@ -212,12 +214,17 @@ sub cmd_queue { } $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id}; } elsif (('list' eq $subcmd) or !defined($subcmd)) { + my $l; write_irssi('List of queued torrents'); - foreach (sort(keys(%torrentlist))) { - write_irssi(' %3d: %s@%s: %s', $_, - $torrentlist{$_}->{'NICK'}, - $torrentlist{$_}->{'CHANNEL'}, - $torrentlist{$_}->{'URL'}); + if (0 == scalar(keys(%torrentlist))) { + write_irssi(' (no torrents in local queue)'); + } else { + foreach (sort(keys(%torrentlist))) { + write_irssi(' %3d: %s@%s: %s', $_, + $torrentlist{$_}->{'NICK'}, + $torrentlist{$_}->{'CHANNEL'}, + $torrentlist{$_}->{'URL'}); + } } } else { write_irssi('Unknown subcommand: %s', $subcmd); @@ -236,24 +243,35 @@ sub cmd_remote { } write_irssi('List of rempote torrents'); - foreach (@{$rqueue}) { - write_irssi('%s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down', - $_->[6]?'*':' ', - $_->[0], - format_number($_->[2]), - format_number($_->[1]), - ($_->[2]*100)/$_->[1], - format_number($_->[3]), - format_number($_->[4])); + if (0 == scalar(@{$rqueue})) { + write_irssi(' (no torrents in remote queue)'); + } else { + foreach (@{$rqueue}) { + write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down', + $_->[6]?'*':' ', + $_->[0], + format_number($_->[2]), + format_number($_->[1]), + ($_->[2]*100)/$_->[1], + format_number($_->[3]), + format_number($_->[4])); + } } } } sub cmd_save { + + my %mappedqueue; + + # XML::Simple has some problems with numbers as nodenames, + # so we have to modify our queue a bit. + %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist); eval { open(CONF, '>'.$conffile) or die 'Could not open config file'; + $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue; print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}); close(CONF); }; @@ -332,6 +350,11 @@ sub init_xmlrtorrent { } } + # Restore the queue + %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}}; + %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist); + $torrentindex = max(keys(%torrentlist)) + 1; + unless(defined($rtorrent = xmlrtorrent->new( 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'}, 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'}, -- 1.8.3.1