From: Ralf Ertzinger Date: Fri, 26 Dec 2008 20:46:02 +0000 (+0100) Subject: Merge branch 'master' of ssh://sun@ryoko:22003/home/sun/GIT/xmlrtorrent X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=xmlrtorrent.git;a=commitdiff_plain;h=3b9c53fd7fe3fc1c66906ca54eb1704107956968;hp=e77002cd5b75ee1eabf1c6adcf1613d653516d16 Merge branch 'master' of ssh://sun@ryoko:22003/home/sun/GIT/xmlrtorrent --- diff --git a/xmlrtorrent.pl b/xmlrtorrent.pl index 2089ce8..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; @@ -23,6 +24,9 @@ my @outputstack = (undef); my $PARAMS = { 'XMLURL' => 'http://localhost/RPC2', + 'USERNAME' => '', + 'PASSWORD' => '', + '_QUEUE' => {}, }; # activate debug here @@ -86,7 +90,7 @@ sub write_irssi { my @text = @_; my $output = $outputstack[0]; - $text[0] = 'xmlrtorrent: ' . $text[0]; + $text[0] = '%%mxmlrtorrent: %%n' . $text[0]; if (defined($output) and ref($output)) { $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP); @@ -164,7 +168,7 @@ sub check_for_link { push_output($witem); # Look if there is a torrent link in there - $message =~ m|(http://\S*\.torrent)|; + $message =~ m,(http://\S*\.(?:torrent|penis)),; $m = $1; while (defined($m)) { write_debug('Torrent-URL: %s', $m); @@ -199,18 +203,31 @@ sub cmd_queue { write_debug('Sending %s to rtorrent', $u); unless(defined($rtorrent->load_start($u))) { - write_irssi('Error sending URL %s: %s', $u, $rtorrent->errstr()); + write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr()); } else { + write_irssi('%s enqueued', $u); delete($torrentlist{$id}); } + } elsif ('add' eq $subcmd) { + unless(defined($id)) { + return; + } + $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(' %d: %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); } } @@ -219,29 +236,42 @@ sub cmd_remote { my ($subcmd, $id, @params) = @_; my $rqueue; - if ('queue' eq $subcmd) { + if (('list' eq $subcmd) or !defined($subcmd)) { unless(defined($rqueue = $rtorrent->download_list())) { write_irssi('Error getting list of downloads: %s', $rtorrent->errstr()); return; } - foreach (@{$rqueue}) { - write_irssi('%s%s: %sB/%sB done, %sb/s up, %sb/s down', - $_->[6]?'*':' ', - $_->[0], - format_number($_->[2]), - format_number($_->[1]), - format_number($_->[3]), - format_number($_->[4])); + write_irssi('List of rempote torrents'); + 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); }; @@ -261,7 +291,10 @@ sub cmd_set { if(exists($PARAMS->{$key})) { $conf->{'xmlrtorrent'}->{$key} = $val; if ('XMLURL' eq $key) { - unless(defined($rtorrent = xmlrtorrent->new('XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'}))) { + unless(defined($rtorrent = xmlrtorrent->new( + 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'}, + 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'}, + 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) { write_irssi('Could not initialize XMLRPC instance'); return; } @@ -317,7 +350,15 @@ sub init_xmlrtorrent { } } - unless(defined($rtorrent = xmlrtorrent->new('XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'}))) { + # 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'}, + 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) { write_irssi('Could not initialize XMLRPC instance'); return; } @@ -356,6 +397,8 @@ sub cmdhandler { if (exists($xmlrtorrent_commands->{$cmd})) { $xmlrtorrent_commands->{$cmd}->(@params); + } else { + write_irssi('Unknown command: %s', $cmd); } pop_output(); diff --git a/xmlrtorrent.pm b/xmlrtorrent.pm index 92cd1aa..b1c41b9 100644 --- a/xmlrtorrent.pm +++ b/xmlrtorrent.pm @@ -9,11 +9,15 @@ sub new { my $class = shift; my $self = {@_}; - unless(exists($self->{'XMLURL'}) && defined($self->{'XMLURL'})) { + unless(exists($self->{'XMLURL'}) and defined($self->{'XMLURL'})) { return undef; } $self->{'__RPCClient'} = RPC::XML::Client->new($self->{'XMLURL'}); + if ((exists($self->{'USERNAME'}) and exists($self->{'PASSWORD'})) and + ($self->{'USERNAME'} ne '')) { + $self->{'__RPCClient'}->credentials('', $self->{'USERNAME'}, $self->{'PASSWORD'}); + } return bless($self, $class); }