1 # control an rTorrent client via XMLRPC,
2 # and collect rtorrent files from IRC for later download
4 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
5 # licensed under GNU GPL v2
8 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
9 use vars qw($VERSION %IRSSI);
13 use List::Util qw(max);
18 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'xmlrtorrent.xml');
19 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
20 my $plugindir = File::Spec->catfile($scriptdir, 'xmlrtorrent');
24 my @outputstack = (undef);
31 # Handle module unload/irssi shutdown
33 if ($conf->{'xmlrtorrent'}->{'_AUTOSAVE'}) {
41 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
42 signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);});
43 # "message own_public", SERVER_REC, char *msg, char *target
44 signal_add_last("message own_public" => sub {check_for_link(\@_,1,2,-1,0);});
46 # "message private", SERVER_REC, char *msg, char *nick, char *address
47 signal_add_last("message private" => sub {check_for_link(\@_,1,-1,2,0);});
48 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
49 signal_add_last("message own_private" => sub {check_for_link(\@_,1,2,-1,0);});
51 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
52 signal_add_last("message irc action" => sub {check_for_link(\@_,1,4,2,0);});
53 # "message irc own_action", SERVER_REC, char *msg, char *target
54 signal_add_last("message irc own_action" => sub {check_for_link(\@_,1,2,-1,0);});
57 signal_add_first('complete word', \&sig_complete);
59 my $xmlrtorrent_commands = {
90 write_irssi('Enabled debugging');
95 write_irssi('Disabled debugging');
99 $conf->{'xmlrtorrent'}->{'_AUTOSAVE'} = 1;
100 write_irssi('Autosave enabled');
103 'noautosave' => sub {
104 $conf->{'xmlrtorrent'}->{'_AUTOSAVE'} = 0;
105 write_irssi('Autosave disabled');
109 # This is shamelessly stolen from pythons urlgrabber
113 my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
114 my $step = $SI?1000:1024;
117 my $max_depth = $#symbols;
120 while (($number > $thresh) and ($depth < $max_depth)) {
125 if ($number =~ /^[+-]?\d+$/) {
128 } elsif ($number < 9.95) {
133 return sprintf($format, $number, $symbols[$depth]);
138 my $output = $outputstack[0];
140 my $format = '%%mxmlrtorrent: %%n' . shift(@text);
142 # escape % in parameters from irssi
143 s/%/%%/g foreach @text;
145 if (defined($output) and ref($output)) {
146 $output->print(sprintf($format, @text), MSGLEVEL_CLIENTCRAP);
148 Irssi::print(sprintf($format, @text));
154 unshift(@outputstack, shift);
168 my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
169 my $server = $signal->[$paramserver];
170 my $target = $signal->[$paramchannel];
171 my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
172 my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick];
177 if (defined $server) {
178 $witem = $server->window_item_find($target);
180 $witem = Irssi::window_item_find($target);
183 # Look if we should ignore this line
184 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
190 # Look if there is a torrent link in there
192 while ($message =~ m,(http://\S*\.(?:torrent|penis)),g) {
193 write_debug('Torrent-URL: %s', $1);
194 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $1};
200 # Handle the queue of unhandled torrents
202 my ($subcmd, $id, @params) = @_;
204 if ('remove' eq $subcmd) {
206 delete($torrentlist{$id});
208 } elsif ('clear' eq $subcmd) {
210 } elsif ('confirm' eq $subcmd) {
212 return unless(defined($id) and exists($torrentlist{$id}));
214 $u = $torrentlist{$id}->{'URL'};
216 write_debug('Sending %s to rtorrent', $u);
217 unless(defined($talker->load_start($u))) {
218 write_irssi('%%RError sending URL %s: %s', $u, $talker->errstr());
220 write_irssi('%s enqueued', $u);
221 delete($torrentlist{$id});
223 } elsif ('add' eq $subcmd) {
224 unless(defined($id)) {
227 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
228 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
230 write_irssi('List of queued torrents');
231 if (0 == scalar(keys(%torrentlist))) {
232 write_irssi(' (no torrents in local queue)');
234 foreach (sort(keys(%torrentlist))) {
235 write_irssi(' %3d: %s@%s: %s', $_,
236 $torrentlist{$_}->{'NICK'},
237 $torrentlist{$_}->{'CHANNEL'},
238 $torrentlist{$_}->{'URL'});
242 write_irssi('Unknown subcommand: %s', $subcmd);
246 # Handle the remote rtorrent queue
248 my ($subcmd, $id, @params) = @_;
251 if (('list' eq $subcmd) or !defined($subcmd)) {
252 unless(defined($rqueue = $talker->download_list())) {
253 write_irssi('Error getting list of downloads: %s', $talker->errstr());
257 write_irssi('List of remote torrents');
258 if (0 == scalar(@{$rqueue})) {
259 write_irssi(' (no torrents in remote queue)');
261 foreach (@{$rqueue}) {
262 write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
263 $_->{'ACTIVE'}?'*':' ',
265 format_number($_->{'BYTES_DONE'}),
266 format_number($_->{'SIZE_BYTES'}),
267 $_->{'BYTES_DONE'}*100/$_->{'SIZE_BYTES'},
268 format_number($_->{'UP_RATE'}),
269 format_number($_->{'DOWN_RATE'}));
280 # XML::Simple has some problems with numbers as nodenames,
281 # so we have to modify our queue a bit.
282 %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
285 open(CONF, '>'.$conffile) or die 'Could not open config file';
286 $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
287 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
291 write_irssi('Could not save config to %s: %s', ($conffile, $@));
293 write_irssi('configuration saved to %s', $conffile);
303 foreach $p (@talkers) {
304 if ($p->{'NAME'} eq $target) {
305 $p->setval($key, $val);
309 write_irssi('No such module');
317 if (defined($target)) {
318 foreach $p (@talkers) {
319 if ($p->{'NAME'} eq $target) {
320 write_irssi($p->getconfstr());
324 write_irssi('No such module');
326 write_irssi('Loaded talkers:');
327 foreach $p (@talkers) {
328 write_irssi(' %s', $p->{'NAME'});
337 if (defined($target)) {
338 foreach $p (@talkers) {
339 if ($p->{'NAME'} eq $target) {
340 write_irssi($p->gethelpstr());
344 write_irssi('No such module');
346 write_irssi(<<'EOT');
348 save: save the current configuration
349 help [modulename]: display this help or module specific help
350 show [modulename]: show loaded modules or the current parameters of a module
351 talker [modulename]: display or set the talker to use
352 debug: enable debugging messages
353 nodebug: disable debugging messages
363 if (defined($target)) {
364 foreach $p (@talkers) {
365 if (($p->{'NAME'} eq $target) && ($p->{'TYPE'} eq 'talker')) {
367 $conf->{'xmlrtorrent'}->{'talker'} = $target;
371 write_irssi('No such talker');
373 write_irssi('Current talker: %s', $conf->{'xmlrtorrent'}->{'talker'});
380 sub sig_command_script_unload {
382 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
397 opendir(D, $dir) || return ();
398 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
402 write_debug('Trying to load %s:', $p);
404 eval qq{ require xmlrtorrent::$p; };
406 write_irssi('Failed to load plugin: %s', "$@");
410 $g = eval qq{ xmlrtorrent::$p->new(); };
412 write_irssi('Failed to instanciate: %s', "$@");
417 write_debug('found %s %s', $g->{'TYPE'}, $g->{'NAME'});
418 if ($type eq $g->{'TYPE'}) {
420 $g->setio(sub {Irssi::print(shift)});
422 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
427 write_debug('Loaded %d plugins', $#g+1);
432 sub _load_modules($) {
436 foreach (keys(%INC)) {
437 if ($INC{$_} =~ m|^$path|) {
438 write_debug('Removing %s from $INC', $_);
442 @talkers = ploader($path, '.*Talker\.pm$', 'talker');
445 sub init_xmlrtorrent {
447 my $bindings = shift;
450 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
451 # No config, start with an empty one
452 write_debug('No config found, using defaults');
453 $conf = { 'xmlrtorrent' => { }};
455 foreach (keys(%{$PARAMS})) {
456 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
457 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
461 _load_modules($plugindir);
463 unless (defined(@talkers)) {
464 write_irssi('No talkers found, can not proceed.');
468 $talker = $talkers[0];
469 foreach $p (@talkers) {
470 if ($conf->{'xmlrtorrent'}->{'talker'} eq $p->{'NAME'}) {
474 write_debug('Selected %s as talker', $talker->{'NAME'});
475 $conf->{'xmlrtorrent'}->{'talker'} = $talker->{'NAME'};
477 # Loop through all plugins and load the config
478 foreach $p (@talkers) {
479 $conf->{'xmlrtorrent'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'xmlrtorrent'}->{'config'}->{$p->{'NAME'}});
483 %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
484 %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
485 $torrentindex = max(keys(%torrentlist)) + 1;
489 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
490 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
491 Irssi::signal_add('setup saved', 'cmd_save');
494 Irssi::command_bind('torrent' => \&cmdhandler);
497 write_irssi('xmlrtorrent initialized');
501 my ($complist, $window, $word, $linestart, $want_space) = @_;
504 if ($linestart !~ m|^/torrent\b|) {
510 Irssi::signal_stop();
514 my ($data, $server, $witem) = @_;
515 my ($cmd, @params) = split(/\s+/, $data);
519 if (exists($xmlrtorrent_commands->{$cmd})) {
520 $xmlrtorrent_commands->{$cmd}->(@params);
522 write_irssi('Unknown command: %s', $cmd);
528 unshift(@INC, $scriptdir);