X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=xmlrtorrent.git;a=blobdiff_plain;f=xmlrtorrent.pl;h=fae32f70fa8507027a561e1d09aa3dafce711c8a;hp=3e13500abb79e3b3b27c9088dd02778c7c259bd5;hb=HEAD;hpb=bd6aedbd2131d60cd3c0291405f03b25cf8b3326 diff --git a/xmlrtorrent.pl b/xmlrtorrent.pl index 3e13500..fae32f7 100644 --- a/xmlrtorrent.pl +++ b/xmlrtorrent.pl @@ -11,7 +11,6 @@ use XML::Simple; use Data::Dumper; use File::Spec; use List::Util qw(max); -use xmlrtorrent; my @talkers; my $talker; @@ -21,19 +20,23 @@ my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts'); my $plugindir = File::Spec->catfile($scriptdir, 'xmlrtorrent'); my %torrentlist = (); my $torrentindex = 1; -my $rtorrent; my @outputstack = (undef); my $PARAMS = { - 'XMLURL' => 'http://localhost/RPC2', - 'USERNAME' => '', - 'PASSWORD' => '', '_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);}); @@ -91,35 +94,17 @@ my $xmlrtorrent_commands = { $debug = 0; write_irssi('Disabled debugging'); }, -}; - -sub write_irssi { - my @text = @_; - my $output = $outputstack[0]; - - $text[0] = '%%mxmlrtorrent: %%n' . $text[0]; - - if (defined($output) and ref($output)) { - $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP); - } else { - Irssi::print(sprintf(shift(@text), @text)); - } - -} -sub push_output { - unshift(@outputstack, shift); -} - -sub pop_output { - shift(@outputstack); -} + 'autosave' => sub { + $conf->{'xmlrtorrent'}->{'_AUTOSAVE'} = 1; + write_irssi('Autosave enabled'); + }, -sub write_debug { - if ($debug) { - write_irssi(@_); - } -} + 'noautosave' => sub { + $conf->{'xmlrtorrent'}->{'_AUTOSAVE'} = 0; + write_irssi('Autosave disabled'); + }, +}; # This is shamelessly stolen from pythons urlgrabber sub format_number { @@ -148,7 +133,36 @@ sub format_number { return sprintf($format, $number, $symbols[$depth]); } +sub write_irssi { + my @text = @_; + my $output = $outputstack[0]; + + my $format = '%%mxmlrtorrent: %%n' . shift(@text); + + # escape % in parameters from irssi + s/%/%%/g foreach @text; + + if (defined($output) and ref($output)) { + $output->print(sprintf($format, @text), MSGLEVEL_CLIENTCRAP); + } else { + Irssi::print(sprintf($format, @text)); + } +} + +sub push_output { + unshift(@outputstack, shift); +} + +sub pop_output { + shift(@outputstack); +} + +sub write_debug { + if ($debug) { + write_irssi(@_); + } +} sub check_for_link { my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_; @@ -157,7 +171,6 @@ sub check_for_link { my $message = ($parammessage == -1) ? '' : $signal->[$parammessage]; my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick]; my $g; - my $m; my $p; my $witem; @@ -166,27 +179,19 @@ sub check_for_link { } else { $witem = Irssi::window_item_find($target); } - + # Look if we should ignore this line if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) { return; } - + push_output($witem); # Look if there is a torrent link in there - $message =~ m,(http://\S*\.(?:torrent|penis)),; - $m = $1; - while (defined($m)) { - write_debug('Torrent-URL: %s', $m); - $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $m}; - - # Remove the matched part from the message and try again (there may be - # more!) - $message =~ s/$m//; - - $message =~ m|(http://.*\.torrent)|; - $m = $1; + + while ($message =~ m,(http://\S*\.(?:torrent|penis)),g) { + write_debug('Torrent-URL: %s', $1); + $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $1}; } pop_output(); @@ -209,8 +214,8 @@ sub cmd_queue { $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}); @@ -244,24 +249,24 @@ sub cmd_remote { 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; } - write_irssi('List of rempote torrents'); + write_irssi('List of remote 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])); + $_->{'ACTIVE'}?'*':' ', + $_->{'NAME'}, + format_number($_->{'BYTES_DONE'}), + format_number($_->{'SIZE_BYTES'}), + $_->{'BYTES_DONE'}*100/$_->{'SIZE_BYTES'}, + format_number($_->{'UP_RATE'}), + format_number($_->{'DOWN_RATE'})); } } } @@ -301,7 +306,7 @@ sub cmd_set { return; } } - write_irssi(undef, 'No such module'); + write_irssi('No such module'); } sub cmd_show { @@ -359,13 +364,13 @@ sub cmd_talker { 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'}); } } @@ -466,20 +471,19 @@ sub init_xmlrtorrent { $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');