use XML::Simple;
use Data::Dumper;
use File::Spec;
+use List::Util qw(max);
use xmlrtorrent;
my $conf;
my $PARAMS = {
'XMLURL' => 'http://localhost/RPC2',
+ 'USERNAME' => '',
+ 'PASSWORD' => '',
+ '_QUEUE' => {},
};
# activate debug here
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);
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);
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);
}
}
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);
};
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;
}
}
}
- 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;
}
if (exists($xmlrtorrent_commands->{$cmd})) {
$xmlrtorrent_commands->{$cmd}->(@params);
+ } else {
+ write_irssi('Unknown command: %s', $cmd);
}
pop_output();