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];
131 if (defined $server) {
132 $witem = $server->window_item_find($target);
134 $witem = Irssi::window_item_find($target);
137 # Look if we should ignore this line
138 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
144 # Look if there is a torrent link in there
146 while ($message =~ m,(http://\S*\.(?:torrent|penis)),g) {
147 write_debug('Torrent-URL: %s', $1);
148 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $1};
154 # Handle the queue of unhandled torrents
156 my ($subcmd, $id, @params) = @_;
158 if ('remove' eq $subcmd) {
160 delete($torrentlist{$id});
162 } elsif ('clear' eq $subcmd) {
164 } elsif ('confirm' eq $subcmd) {
166 return unless(defined($id) and exists($torrentlist{$id}));
168 $u = $torrentlist{$id}->{'URL'};
170 write_debug('Sending %s to rtorrent', $u);
171 unless(defined($rtorrent->load_start($talker, $u))) {
172 write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
174 write_irssi('%s enqueued', $u);
175 delete($torrentlist{$id});
177 } elsif ('add' eq $subcmd) {
178 unless(defined($id)) {
181 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
182 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
184 write_irssi('List of queued torrents');
185 if (0 == scalar(keys(%torrentlist))) {
186 write_irssi(' (no torrents in local queue)');
188 foreach (sort(keys(%torrentlist))) {
189 write_irssi(' %3d: %s@%s: %s', $_,
190 $torrentlist{$_}->{'NICK'},
191 $torrentlist{$_}->{'CHANNEL'},
192 $torrentlist{$_}->{'URL'});
196 write_irssi('Unknown subcommand: %s', $subcmd);
200 # Handle the remote rtorrent queue
202 my ($subcmd, $id, @params) = @_;
205 if (('list' eq $subcmd) or !defined($subcmd)) {
206 unless(defined($rqueue = $rtorrent->download_list($talker))) {
207 write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
211 write_irssi('List of remote torrents');
212 if (0 == scalar(@{$rqueue})) {
213 write_irssi(' (no torrents in remote queue)');
215 foreach (@{$rqueue}) {
216 write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
217 $_->{'ACTIVE'}?'*':' ',
233 # XML::Simple has some problems with numbers as nodenames,
234 # so we have to modify our queue a bit.
235 %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
238 open(CONF, '>'.$conffile) or die 'Could not open config file';
239 $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
240 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
244 write_irssi('Could not save config to %s: %s', ($conffile, $@));
246 write_irssi('configuration saved to %s', $conffile);
256 foreach $p (@talkers) {
257 if ($p->{'NAME'} eq $target) {
258 $p->setval($key, $val);
262 write_irssi(undef, 'No such module');
270 if (defined($target)) {
271 foreach $p (@talkers) {
272 if ($p->{'NAME'} eq $target) {
273 write_irssi($p->getconfstr());
277 write_irssi('No such module');
279 write_irssi('Loaded talkers:');
280 foreach $p (@talkers) {
281 write_irssi(' %s', $p->{'NAME'});
290 if (defined($target)) {
291 foreach $p (@talkers) {
292 if ($p->{'NAME'} eq $target) {
293 write_irssi($p->gethelpstr());
297 write_irssi('No such module');
299 write_irssi(<<'EOT');
301 save: save the current configuration
302 help [modulename]: display this help or module specific help
303 show [modulename]: show loaded modules or the current parameters of a module
304 talker [modulename]: display or set the talker to use
305 debug: enable debugging messages
306 nodebug: disable debugging messages
316 if (defined($target)) {
317 foreach $p (@talkers) {
318 if (($p->{'NAME'} eq $target) && ($p->{'TYPE'} eq 'talker')) {
320 $conf->{'videosite'}->{'talker'} = $target;
324 write_irssi('No such talker');
326 write_irssi('Current talker: %s', $conf->{'videosite'}->{'talker'});
333 sub sig_command_script_unload {
335 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
350 opendir(D, $dir) || return ();
351 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
355 write_debug('Trying to load %s:', $p);
357 eval qq{ require xmlrtorrent::$p; };
359 write_irssi('Failed to load plugin: %s', "$@");
363 $g = eval qq{ xmlrtorrent::$p->new(); };
365 write_irssi('Failed to instanciate: %s', "$@");
370 write_debug('found %s %s', $g->{'TYPE'}, $g->{'NAME'});
371 if ($type eq $g->{'TYPE'}) {
373 $g->setio(sub {Irssi::print(shift)});
375 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
380 write_debug('Loaded %d plugins', $#g+1);
385 sub _load_modules($) {
389 foreach (keys(%INC)) {
390 if ($INC{$_} =~ m|^$path|) {
391 write_debug('Removing %s from $INC', $_);
395 @talkers = ploader($path, '.*Talker\.pm$', 'talker');
398 sub init_xmlrtorrent {
400 my $bindings = shift;
403 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
404 # No config, start with an empty one
405 write_debug('No config found, using defaults');
406 $conf = { 'xmlrtorrent' => { }};
408 foreach (keys(%{$PARAMS})) {
409 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
410 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
414 _load_modules($plugindir);
416 unless (defined(@talkers)) {
417 write_irssi('No talkers found, can not proceed.');
421 $talker = $talkers[0];
422 foreach $p (@talkers) {
423 if ($conf->{'xmlrtorrent'}->{'talker'} eq $p->{'NAME'}) {
427 write_debug(undef, 'Selected %s as talker', $talker->{'NAME'});
428 $conf->{'videosite'}->{'talker'} = $talker->{'NAME'};
432 %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
433 %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
434 $torrentindex = max(keys(%torrentlist)) + 1;
436 unless(defined($rtorrent = xmlrtorrent->new())) {
437 write_irssi('Could not initialize XMLRPC instance');
443 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
444 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
445 Irssi::signal_add('setup saved', 'cmd_save');
448 Irssi::command_bind('torrent' => \&cmdhandler);
451 write_irssi('xmlrtorrent initialized');
455 my ($complist, $window, $word, $linestart, $want_space) = @_;
458 if ($linestart !~ m|^/torrent\b|) {
464 Irssi::signal_stop();
468 my ($data, $server, $witem) = @_;
469 my ($cmd, @params) = split(/\s+/, $data);
473 if (exists($xmlrtorrent_commands->{$cmd})) {
474 $xmlrtorrent_commands->{$cmd}->(@params);
476 write_irssi('Unknown command: %s', $cmd);
482 unshift(@INC, $scriptdir);