use Data::Dumper;
use File::Spec;
use List::Util qw(max);
-use xmlrtorrent;
my @talkers;
my $talker;
my $plugindir = File::Spec->catfile($scriptdir, 'xmlrtorrent');
my %torrentlist = ();
my $torrentindex = 1;
-my $rtorrent;
my @outputstack = (undef);
my $PARAMS = {
'_QUEUE' => {},
+ '_AUTOSAVE' => 1,
};
+# Handle module unload/irssi shutdown
+sub UNLOAD {
+ if ($conf->{'xmlrtorrent'}->{'_AUTOSAVE'}) {
+ cmd_save();
+ }
+}
+
# activate debug here
-my $debug = 1;
+my $debug = 0;
# "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);});
$debug = 0;
write_irssi('Disabled debugging');
},
+
+ 'autosave' => sub {
+ $conf->{'xmlrtorrent'}->{'_AUTOSAVE'} = 1;
+ write_irssi('Autosave enabled');
+ },
+
+ 'noautosave' => sub {
+ $conf->{'xmlrtorrent'}->{'_AUTOSAVE'} = 0;
+ write_irssi('Autosave disabled');
+ },
};
+# This is shamelessly stolen from pythons urlgrabber
+sub format_number {
+ my $number = shift;
+ my $SI = shift || 0;
+ my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
+ my $step = $SI?1000:1024;
+ my $thresh = 999;
+ my $depth = 0;
+ my $max_depth = $#symbols;
+ my $format;
+
+ while (($number > $thresh) and ($depth < $max_depth)) {
+ $depth += 1;
+ $number /= $step;
+ }
+
+ if ($number =~ /^[+-]?\d+$/) {
+ # Integer.
+ $format = '%i%s';
+ } elsif ($number < 9.95) {
+ $format = '%.1f%s';
+ } else {
+ $format = '%.0f%s';
+ }
+ return sprintf($format, $number, $symbols[$depth]);
+}
+
sub write_irssi {
my @text = @_;
my $output = $outputstack[0];
$u = $torrentlist{$id}->{'URL'};
write_debug('Sending %s to rtorrent', $u);
- unless(defined($rtorrent->load_start($talker, $u))) {
- write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
+ unless(defined($talker->load_start($u))) {
+ write_irssi('%%RError sending URL %s: %s', $u, $talker->errstr());
} else {
write_irssi('%s enqueued', $u);
delete($torrentlist{$id});
my $rqueue;
if (('list' eq $subcmd) or !defined($subcmd)) {
- unless(defined($rqueue = $rtorrent->download_list($talker))) {
- write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
+ unless(defined($rqueue = $talker->download_list())) {
+ write_irssi('Error getting list of downloads: %s', $talker->errstr());
return;
}
} else {
foreach (@{$rqueue}) {
write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
- $_->{'ACTIVE'}?'*':' ',
- $_->{'NAME'},
- $_->{'BYTES_DONE'},
- $_->{'SIZE_BYTES'},
- $_->{'UP_RATE'},
- $_->{'DOWN_RATE'});
+ $_->{'ACTIVE'}?'*':' ',
+ $_->{'NAME'},
+ format_number($_->{'BYTES_DONE'}),
+ format_number($_->{'SIZE_BYTES'}),
+ $_->{'BYTES_DONE'}*100/$_->{'SIZE_BYTES'},
+ format_number($_->{'UP_RATE'}),
+ format_number($_->{'DOWN_RATE'}));
}
}
}
return;
}
}
- write_irssi(undef, 'No such module');
+ write_irssi('No such module');
}
sub cmd_show {
foreach $p (@talkers) {
if (($p->{'NAME'} eq $target) && ($p->{'TYPE'} eq 'talker')) {
$talker = $p;
- $conf->{'videosite'}->{'talker'} = $target;
+ $conf->{'xmlrtorrent'}->{'talker'} = $target;
return;
}
}
write_irssi('No such talker');
} else {
- write_irssi('Current talker: %s', $conf->{'videosite'}->{'talker'});
+ write_irssi('Current talker: %s', $conf->{'xmlrtorrent'}->{'talker'});
}
}
$talker = $p;
}
}
- write_debug(undef, 'Selected %s as talker', $talker->{'NAME'});
- $conf->{'videosite'}->{'talker'} = $talker->{'NAME'};
+ write_debug('Selected %s as talker', $talker->{'NAME'});
+ $conf->{'xmlrtorrent'}->{'talker'} = $talker->{'NAME'};
+ # Loop through all plugins and load the config
+ foreach $p (@talkers) {
+ $conf->{'xmlrtorrent'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'xmlrtorrent'}->{'config'}->{$p->{'NAME'}});
+ }
# 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())) {
- write_irssi('Could not initialize XMLRPC instance');
- return;
- }
-
if ($bindings) {
Irssi::signal_add_first('command script load', 'sig_command_script_unload');