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);
35 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
36 signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);});
37 # "message own_public", SERVER_REC, char *msg, char *target
38 signal_add_last("message own_public" => sub {check_for_link(\@_,1,2,-1,0);});
40 # "message private", SERVER_REC, char *msg, char *nick, char *address
41 signal_add_last("message private" => sub {check_for_link(\@_,1,-1,2,0);});
42 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
43 signal_add_last("message own_private" => sub {check_for_link(\@_,1,2,-1,0);});
45 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
46 signal_add_last("message irc action" => sub {check_for_link(\@_,1,4,2,0);});
47 # "message irc own_action", SERVER_REC, char *msg, char *target
48 signal_add_last("message irc own_action" => sub {check_for_link(\@_,1,2,-1,0);});
51 signal_add_first('complete word', \&sig_complete);
53 my $xmlrtorrent_commands = {
84 write_irssi('Enabled debugging');
89 write_irssi('Disabled debugging');
95 my $output = $outputstack[0];
97 $text[0] = '%%mxmlrtorrent: %%n' . $text[0];
99 if (defined($output) and ref($output)) {
100 $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
102 Irssi::print(sprintf(shift(@text), @text));
108 unshift(@outputstack, shift);
122 my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
123 my $server = $signal->[$paramserver];
124 my $target = $signal->[$paramchannel];
125 my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
126 my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick];
132 if (defined $server) {
133 $witem = $server->window_item_find($target);
135 $witem = Irssi::window_item_find($target);
138 # Look if we should ignore this line
139 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
145 # Look if there is a torrent link in there
146 $message =~ m,(http://\S*\.(?:torrent|penis)),;
148 while (defined($m)) {
149 write_debug('Torrent-URL: %s', $m);
150 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $m};
152 # Remove the matched part from the message and try again (there may be
156 $message =~ m|(http://.*\.torrent)|;
163 # Handle the queue of unhandled torrents
165 my ($subcmd, $id, @params) = @_;
167 if ('remove' eq $subcmd) {
169 delete($torrentlist{$id});
171 } elsif ('clear' eq $subcmd) {
173 } elsif ('confirm' eq $subcmd) {
175 return unless(defined($id) and exists($torrentlist{$id}));
177 $u = $torrentlist{$id}->{'URL'};
179 write_debug('Sending %s to rtorrent', $u);
180 unless(defined($rtorrent->load_start($talker, $u))) {
181 write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
183 write_irssi('%s enqueued', $u);
184 delete($torrentlist{$id});
186 } elsif ('add' eq $subcmd) {
187 unless(defined($id)) {
190 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
191 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
193 write_irssi('List of queued torrents');
194 if (0 == scalar(keys(%torrentlist))) {
195 write_irssi(' (no torrents in local queue)');
197 foreach (sort(keys(%torrentlist))) {
198 write_irssi(' %3d: %s@%s: %s', $_,
199 $torrentlist{$_}->{'NICK'},
200 $torrentlist{$_}->{'CHANNEL'},
201 $torrentlist{$_}->{'URL'});
205 write_irssi('Unknown subcommand: %s', $subcmd);
209 # Handle the remote rtorrent queue
211 my ($subcmd, $id, @params) = @_;
214 if (('list' eq $subcmd) or !defined($subcmd)) {
215 unless(defined($rqueue = $rtorrent->download_list($talker))) {
216 write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
220 write_irssi('List of remote torrents');
221 if (0 == scalar(@{$rqueue})) {
222 write_irssi(' (no torrents in remote queue)');
224 foreach (@{$rqueue}) {
225 write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
226 $_->{'ACTIVE'}?'*':' ',
242 # XML::Simple has some problems with numbers as nodenames,
243 # so we have to modify our queue a bit.
244 %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
247 open(CONF, '>'.$conffile) or die 'Could not open config file';
248 $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
249 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
253 write_irssi('Could not save config to %s: %s', ($conffile, $@));
255 write_irssi('configuration saved to %s', $conffile);
265 foreach $p (@talkers) {
266 if ($p->{'NAME'} eq $target) {
267 $p->setval($key, $val);
271 write_irssi(undef, 'No such module');
279 if (defined($target)) {
280 foreach $p (@talkers) {
281 if ($p->{'NAME'} eq $target) {
282 write_irssi($p->getconfstr());
286 write_irssi('No such module');
288 write_irssi('Loaded talkers:');
289 foreach $p (@talkers) {
290 write_irssi(' %s', $p->{'NAME'});
299 if (defined($target)) {
300 foreach $p (@talkers) {
301 if ($p->{'NAME'} eq $target) {
302 write_irssi($p->gethelpstr());
306 write_irssi('No such module');
308 write_irssi(<<'EOT');
310 save: save the current configuration
311 help [modulename]: display this help or module specific help
312 show [modulename]: show loaded modules or the current parameters of a module
313 talker [modulename]: display or set the talker to use
314 debug: enable debugging messages
315 nodebug: disable debugging messages
325 if (defined($target)) {
326 foreach $p (@talkers) {
327 if (($p->{'NAME'} eq $target) && ($p->{'TYPE'} eq 'talker')) {
329 $conf->{'videosite'}->{'talker'} = $target;
333 write_irssi('No such talker');
335 write_irssi('Current talker: %s', $conf->{'videosite'}->{'talker'});
342 sub sig_command_script_unload {
344 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
359 opendir(D, $dir) || return ();
360 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
364 write_debug('Trying to load %s:', $p);
366 eval qq{ require xmlrtorrent::$p; };
368 write_irssi('Failed to load plugin: %s', "$@");
372 $g = eval qq{ xmlrtorrent::$p->new(); };
374 write_irssi('Failed to instanciate: %s', "$@");
379 write_debug('found %s %s', $g->{'TYPE'}, $g->{'NAME'});
380 if ($type eq $g->{'TYPE'}) {
382 $g->setio(sub {Irssi::print(shift)});
384 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
389 write_debug('Loaded %d plugins', $#g+1);
394 sub _load_modules($) {
398 foreach (keys(%INC)) {
399 if ($INC{$_} =~ m|^$path|) {
400 write_debug('Removing %s from $INC', $_);
404 @talkers = ploader($path, '.*Talker\.pm$', 'talker');
407 sub init_xmlrtorrent {
409 my $bindings = shift;
412 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
413 # No config, start with an empty one
414 write_debug('No config found, using defaults');
415 $conf = { 'xmlrtorrent' => { }};
417 foreach (keys(%{$PARAMS})) {
418 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
419 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
423 _load_modules($plugindir);
425 unless (defined(@talkers)) {
426 write_irssi('No talkers found, can not proceed.');
430 $talker = $talkers[0];
431 foreach $p (@talkers) {
432 if ($conf->{'xmlrtorrent'}->{'talker'} eq $p->{'NAME'}) {
436 write_debug(undef, 'Selected %s as talker', $talker->{'NAME'});
437 $conf->{'videosite'}->{'talker'} = $talker->{'NAME'};
441 %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
442 %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
443 $torrentindex = max(keys(%torrentlist)) + 1;
445 unless(defined($rtorrent = xmlrtorrent->new())) {
446 write_irssi('Could not initialize XMLRPC instance');
452 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
453 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
454 Irssi::signal_add('setup saved', 'cmd_save');
457 Irssi::command_bind('torrent' => \&cmdhandler);
460 write_irssi('xmlrtorrent initialized');
464 my ($complist, $window, $word, $linestart, $want_space) = @_;
467 if ($linestart !~ m|^/torrent\b|) {
473 Irssi::signal_stop();
477 my ($data, $server, $witem) = @_;
478 my ($cmd, @params) = split(/\s+/, $data);
482 if (exists($xmlrtorrent_commands->{$cmd})) {
483 $xmlrtorrent_commands->{$cmd}->(@params);
485 write_irssi('Unknown command: %s', $cmd);
491 unshift(@INC, $scriptdir);