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 my $format = '%%mxmlrtorrent: %%n' . shift(@text);
99 # escape % in parameters from irssi
100 s/%/%%/g foreach @text;
102 if (defined($output) and ref($output)) {
103 $output->print(sprintf($format, @text), MSGLEVEL_CLIENTCRAP);
105 Irssi::print(sprintf($format, @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];
134 if (defined $server) {
135 $witem = $server->window_item_find($target);
137 $witem = Irssi::window_item_find($target);
140 # Look if we should ignore this line
141 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
147 # Look if there is a torrent link in there
149 while ($message =~ m,(http://\S*\.(?:torrent|penis)),g) {
150 write_debug('Torrent-URL: %s', $1);
151 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $1};
157 # Handle the queue of unhandled torrents
159 my ($subcmd, $id, @params) = @_;
161 if ('remove' eq $subcmd) {
163 delete($torrentlist{$id});
165 } elsif ('clear' eq $subcmd) {
167 } elsif ('confirm' eq $subcmd) {
169 return unless(defined($id) and exists($torrentlist{$id}));
171 $u = $torrentlist{$id}->{'URL'};
173 write_debug('Sending %s to rtorrent', $u);
174 unless(defined($rtorrent->load_start($talker, $u))) {
175 write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
177 write_irssi('%s enqueued', $u);
178 delete($torrentlist{$id});
180 } elsif ('add' eq $subcmd) {
181 unless(defined($id)) {
184 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
185 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
187 write_irssi('List of queued torrents');
188 if (0 == scalar(keys(%torrentlist))) {
189 write_irssi(' (no torrents in local queue)');
191 foreach (sort(keys(%torrentlist))) {
192 write_irssi(' %3d: %s@%s: %s', $_,
193 $torrentlist{$_}->{'NICK'},
194 $torrentlist{$_}->{'CHANNEL'},
195 $torrentlist{$_}->{'URL'});
199 write_irssi('Unknown subcommand: %s', $subcmd);
203 # Handle the remote rtorrent queue
205 my ($subcmd, $id, @params) = @_;
208 if (('list' eq $subcmd) or !defined($subcmd)) {
209 unless(defined($rqueue = $rtorrent->download_list($talker))) {
210 write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
214 write_irssi('List of remote torrents');
215 if (0 == scalar(@{$rqueue})) {
216 write_irssi(' (no torrents in remote queue)');
218 foreach (@{$rqueue}) {
219 write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
220 $_->{'ACTIVE'}?'*':' ',
224 $_->{'BYTES_DONE'}*100/$_->{'SIZE_BYTES'},
237 # XML::Simple has some problems with numbers as nodenames,
238 # so we have to modify our queue a bit.
239 %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
242 open(CONF, '>'.$conffile) or die 'Could not open config file';
243 $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
244 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
248 write_irssi('Could not save config to %s: %s', ($conffile, $@));
250 write_irssi('configuration saved to %s', $conffile);
260 foreach $p (@talkers) {
261 if ($p->{'NAME'} eq $target) {
262 $p->setval($key, $val);
266 write_irssi('No such module');
274 if (defined($target)) {
275 foreach $p (@talkers) {
276 if ($p->{'NAME'} eq $target) {
277 write_irssi($p->getconfstr());
281 write_irssi('No such module');
283 write_irssi('Loaded talkers:');
284 foreach $p (@talkers) {
285 write_irssi(' %s', $p->{'NAME'});
294 if (defined($target)) {
295 foreach $p (@talkers) {
296 if ($p->{'NAME'} eq $target) {
297 write_irssi($p->gethelpstr());
301 write_irssi('No such module');
303 write_irssi(<<'EOT');
305 save: save the current configuration
306 help [modulename]: display this help or module specific help
307 show [modulename]: show loaded modules or the current parameters of a module
308 talker [modulename]: display or set the talker to use
309 debug: enable debugging messages
310 nodebug: disable debugging messages
320 if (defined($target)) {
321 foreach $p (@talkers) {
322 if (($p->{'NAME'} eq $target) && ($p->{'TYPE'} eq 'talker')) {
324 $conf->{'xmlrtorrent'}->{'talker'} = $target;
328 write_irssi('No such talker');
330 write_irssi('Current talker: %s', $conf->{'xmlrtorrent'}->{'talker'});
337 sub sig_command_script_unload {
339 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
354 opendir(D, $dir) || return ();
355 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
359 write_debug('Trying to load %s:', $p);
361 eval qq{ require xmlrtorrent::$p; };
363 write_irssi('Failed to load plugin: %s', "$@");
367 $g = eval qq{ xmlrtorrent::$p->new(); };
369 write_irssi('Failed to instanciate: %s', "$@");
374 write_debug('found %s %s', $g->{'TYPE'}, $g->{'NAME'});
375 if ($type eq $g->{'TYPE'}) {
377 $g->setio(sub {Irssi::print(shift)});
379 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
384 write_debug('Loaded %d plugins', $#g+1);
389 sub _load_modules($) {
393 foreach (keys(%INC)) {
394 if ($INC{$_} =~ m|^$path|) {
395 write_debug('Removing %s from $INC', $_);
399 @talkers = ploader($path, '.*Talker\.pm$', 'talker');
402 sub init_xmlrtorrent {
404 my $bindings = shift;
407 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
408 # No config, start with an empty one
409 write_debug('No config found, using defaults');
410 $conf = { 'xmlrtorrent' => { }};
412 foreach (keys(%{$PARAMS})) {
413 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
414 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
418 _load_modules($plugindir);
420 unless (defined(@talkers)) {
421 write_irssi('No talkers found, can not proceed.');
425 $talker = $talkers[0];
426 foreach $p (@talkers) {
427 if ($conf->{'xmlrtorrent'}->{'talker'} eq $p->{'NAME'}) {
431 write_debug('Selected %s as talker', $talker->{'NAME'});
432 $conf->{'xmlrtorrent'}->{'talker'} = $talker->{'NAME'};
436 %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
437 %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
438 $torrentindex = max(keys(%torrentlist)) + 1;
440 unless(defined($rtorrent = xmlrtorrent->new())) {
441 write_irssi('Could not initialize XMLRPC instance');
447 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
448 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
449 Irssi::signal_add('setup saved', 'cmd_save');
452 Irssi::command_bind('torrent' => \&cmdhandler);
455 write_irssi('xmlrtorrent initialized');
459 my ($complist, $window, $word, $linestart, $want_space) = @_;
462 if ($linestart !~ m|^/torrent\b|) {
468 Irssi::signal_stop();
472 my ($data, $server, $witem) = @_;
473 my ($cmd, @params) = split(/\s+/, $data);
477 if (exists($xmlrtorrent_commands->{$cmd})) {
478 $xmlrtorrent_commands->{$cmd}->(@params);
480 write_irssi('Unknown command: %s', $cmd);
486 unshift(@INC, $scriptdir);