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);
19 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'xmlrtorrent.xml');
20 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
21 my $plugindir = File::Spec->catfile($scriptdir, 'xmlrtorrent');
26 my @outputstack = (undef);
29 'XMLURL' => 'http://localhost/RPC2',
38 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
39 signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);});
40 # "message own_public", SERVER_REC, char *msg, char *target
41 signal_add_last("message own_public" => sub {check_for_link(\@_,1,2,-1,0);});
43 # "message private", SERVER_REC, char *msg, char *nick, char *address
44 signal_add_last("message private" => sub {check_for_link(\@_,1,-1,2,0);});
45 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
46 signal_add_last("message own_private" => sub {check_for_link(\@_,1,2,-1,0);});
48 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
49 signal_add_last("message irc action" => sub {check_for_link(\@_,1,4,2,0);});
50 # "message irc own_action", SERVER_REC, char *msg, char *target
51 signal_add_last("message irc own_action" => sub {check_for_link(\@_,1,2,-1,0);});
54 signal_add_first('complete word', \&sig_complete);
56 my $xmlrtorrent_commands = {
87 write_irssi('Enabled debugging');
92 write_irssi('Disabled debugging');
98 my $output = $outputstack[0];
100 $text[0] = '%%mxmlrtorrent: %%n' . $text[0];
102 if (defined($output) and ref($output)) {
103 $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
105 Irssi::print(sprintf(shift(@text), @text));
111 unshift(@outputstack, shift);
125 my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
126 my $server = $signal->[$paramserver];
127 my $target = $signal->[$paramchannel];
128 my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
129 my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick];
135 if (defined $server) {
136 $witem = $server->window_item_find($target);
138 $witem = Irssi::window_item_find($target);
141 # Look if we should ignore this line
142 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
148 # Look if there is a torrent link in there
149 $message =~ m,(http://\S*\.(?:torrent|penis)),;
151 while (defined($m)) {
152 write_debug('Torrent-URL: %s', $m);
153 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $m};
155 # Remove the matched part from the message and try again (there may be
159 $message =~ m|(http://.*\.torrent)|;
166 # Handle the queue of unhandled torrents
168 my ($subcmd, $id, @params) = @_;
170 if ('remove' eq $subcmd) {
172 delete($torrentlist{$id});
174 } elsif ('clear' eq $subcmd) {
176 } elsif ('confirm' eq $subcmd) {
178 return unless(defined($id) and exists($torrentlist{$id}));
180 $u = $torrentlist{$id}->{'URL'};
182 write_debug('Sending %s to rtorrent', $u);
183 unless(defined($rtorrent->load_start($talker, $u))) {
184 write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
186 write_irssi('%s enqueued', $u);
187 delete($torrentlist{$id});
189 } elsif ('add' eq $subcmd) {
190 unless(defined($id)) {
193 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
194 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
196 write_irssi('List of queued torrents');
197 if (0 == scalar(keys(%torrentlist))) {
198 write_irssi(' (no torrents in local queue)');
200 foreach (sort(keys(%torrentlist))) {
201 write_irssi(' %3d: %s@%s: %s', $_,
202 $torrentlist{$_}->{'NICK'},
203 $torrentlist{$_}->{'CHANNEL'},
204 $torrentlist{$_}->{'URL'});
208 write_irssi('Unknown subcommand: %s', $subcmd);
212 # Handle the remote rtorrent queue
214 my ($subcmd, $id, @params) = @_;
217 if (('list' eq $subcmd) or !defined($subcmd)) {
218 unless(defined($rqueue = $rtorrent->download_list($talker))) {
219 write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
223 write_irssi('List of remote torrents');
224 if (0 == scalar(@{$rqueue})) {
225 write_irssi(' (no torrents in remote queue)');
227 foreach (@{$rqueue}) {
228 write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
229 $_->{'ACTIVE'}?'*':' ',
245 # XML::Simple has some problems with numbers as nodenames,
246 # so we have to modify our queue a bit.
247 %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
250 open(CONF, '>'.$conffile) or die 'Could not open config file';
251 $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
252 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
256 write_irssi('Could not save config to %s: %s', ($conffile, $@));
258 write_irssi('configuration saved to %s', $conffile);
268 foreach $p (@talkers) {
269 if ($p->{'NAME'} eq $target) {
270 $p->setval($key, $val);
274 write_irssi(undef, 'No such module');
282 if (defined($target)) {
283 foreach $p (@talkers) {
284 if ($p->{'NAME'} eq $target) {
285 write_irssi($p->getconfstr());
289 write_irssi('No such module');
291 write_irssi('Loaded talkers:');
292 foreach $p (@talkers) {
293 write_irssi(' %s', $p->{'NAME'});
302 if (defined($target)) {
303 foreach $p (@talkers) {
304 if ($p->{'NAME'} eq $target) {
305 write_irssi($p->gethelpstr());
309 write_irssi('No such module');
311 write_irssi(<<'EOT');
313 save: save the current configuration
314 help [modulename]: display this help or module specific help
315 show [modulename]: show loaded modules or the current parameters of a module
316 talker [modulename]: display or set the talker to use
317 debug: enable debugging messages
318 nodebug: disable debugging messages
328 if (defined($target)) {
329 foreach $p (@talkers) {
330 if (($p->{'NAME'} eq $target) && ($p->{'TYPE'} eq 'talker')) {
332 $conf->{'videosite'}->{'talker'} = $target;
336 write_irssi('No such talker');
338 write_irssi('Current talker: %s', $conf->{'videosite'}->{'talker'});
345 sub sig_command_script_unload {
347 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
362 opendir(D, $dir) || return ();
363 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
367 write_debug('Trying to load %s:', $p);
369 eval qq{ require xmlrtorrent::$p; };
371 write_irssi('Failed to load plugin: %s', "$@");
375 $g = eval qq{ xmlrtorrent::$p->new(); };
377 write_irssi('Failed to instanciate: %s', "$@");
382 write_debug('found %s %s', $g->{'TYPE'}, $g->{'NAME'});
383 if ($type eq $g->{'TYPE'}) {
385 $g->setio(sub {Irssi::print(shift)});
387 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
392 write_debug('Loaded %d plugins', $#g+1);
397 sub _load_modules($) {
401 foreach (keys(%INC)) {
402 if ($INC{$_} =~ m|^$path|) {
403 write_debug('Removing %s from $INC', $_);
407 @talkers = ploader($path, '.*Talker\.pm$', 'talker');
410 sub init_xmlrtorrent {
412 my $bindings = shift;
415 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
416 # No config, start with an empty one
417 write_debug('No config found, using defaults');
418 $conf = { 'xmlrtorrent' => { }};
420 foreach (keys(%{$PARAMS})) {
421 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
422 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
426 _load_modules($plugindir);
428 unless (defined(@talkers)) {
429 write_irssi('No talkers found, can not proceed.');
433 $talker = $talkers[0];
434 foreach $p (@talkers) {
435 if ($conf->{'xmlrtorrent'}->{'talker'} eq $p->{'NAME'}) {
439 write_debug(undef, 'Selected %s as talker', $talker->{'NAME'});
440 $conf->{'videosite'}->{'talker'} = $talker->{'NAME'};
444 %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
445 %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
446 $torrentindex = max(keys(%torrentlist)) + 1;
448 unless(defined($rtorrent = xmlrtorrent->new())) {
449 write_irssi('Could not initialize XMLRPC instance');
455 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
456 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
457 Irssi::signal_add('setup saved', 'cmd_save');
460 Irssi::command_bind('torrent' => \&cmdhandler);
463 write_irssi('xmlrtorrent initialized');
467 my ($complist, $window, $word, $linestart, $want_space) = @_;
470 if ($linestart !~ m|^/torrent\b|) {
476 Irssi::signal_stop();
480 my ($data, $server, $witem) = @_;
481 my ($cmd, @params) = split(/\s+/, $data);
485 if (exists($xmlrtorrent_commands->{$cmd})) {
486 $xmlrtorrent_commands->{$cmd}->(@params);
488 write_irssi('Unknown command: %s', $cmd);
494 unshift(@INC, $scriptdir);