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 @text = grep { s/%/%%/g } @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'}?'*':' ',
236 # XML::Simple has some problems with numbers as nodenames,
237 # so we have to modify our queue a bit.
238 %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
241 open(CONF, '>'.$conffile) or die 'Could not open config file';
242 $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
243 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
247 write_irssi('Could not save config to %s: %s', ($conffile, $@));
249 write_irssi('configuration saved to %s', $conffile);
259 foreach $p (@talkers) {
260 if ($p->{'NAME'} eq $target) {
261 $p->setval($key, $val);
265 write_irssi(undef, 'No such module');
273 if (defined($target)) {
274 foreach $p (@talkers) {
275 if ($p->{'NAME'} eq $target) {
276 write_irssi($p->getconfstr());
280 write_irssi('No such module');
282 write_irssi('Loaded talkers:');
283 foreach $p (@talkers) {
284 write_irssi(' %s', $p->{'NAME'});
293 if (defined($target)) {
294 foreach $p (@talkers) {
295 if ($p->{'NAME'} eq $target) {
296 write_irssi($p->gethelpstr());
300 write_irssi('No such module');
302 write_irssi(<<'EOT');
304 save: save the current configuration
305 help [modulename]: display this help or module specific help
306 show [modulename]: show loaded modules or the current parameters of a module
307 talker [modulename]: display or set the talker to use
308 debug: enable debugging messages
309 nodebug: disable debugging messages
319 if (defined($target)) {
320 foreach $p (@talkers) {
321 if (($p->{'NAME'} eq $target) && ($p->{'TYPE'} eq 'talker')) {
323 $conf->{'videosite'}->{'talker'} = $target;
327 write_irssi('No such talker');
329 write_irssi('Current talker: %s', $conf->{'videosite'}->{'talker'});
336 sub sig_command_script_unload {
338 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
353 opendir(D, $dir) || return ();
354 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
358 write_debug('Trying to load %s:', $p);
360 eval qq{ require xmlrtorrent::$p; };
362 write_irssi('Failed to load plugin: %s', "$@");
366 $g = eval qq{ xmlrtorrent::$p->new(); };
368 write_irssi('Failed to instanciate: %s', "$@");
373 write_debug('found %s %s', $g->{'TYPE'}, $g->{'NAME'});
374 if ($type eq $g->{'TYPE'}) {
376 $g->setio(sub {Irssi::print(shift)});
378 write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
383 write_debug('Loaded %d plugins', $#g+1);
388 sub _load_modules($) {
392 foreach (keys(%INC)) {
393 if ($INC{$_} =~ m|^$path|) {
394 write_debug('Removing %s from $INC', $_);
398 @talkers = ploader($path, '.*Talker\.pm$', 'talker');
401 sub init_xmlrtorrent {
403 my $bindings = shift;
406 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
407 # No config, start with an empty one
408 write_debug('No config found, using defaults');
409 $conf = { 'xmlrtorrent' => { }};
411 foreach (keys(%{$PARAMS})) {
412 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
413 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
417 _load_modules($plugindir);
419 unless (defined(@talkers)) {
420 write_irssi('No talkers found, can not proceed.');
424 $talker = $talkers[0];
425 foreach $p (@talkers) {
426 if ($conf->{'xmlrtorrent'}->{'talker'} eq $p->{'NAME'}) {
430 write_debug(undef, 'Selected %s as talker', $talker->{'NAME'});
431 $conf->{'videosite'}->{'talker'} = $talker->{'NAME'};
435 %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
436 %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
437 $torrentindex = max(keys(%torrentlist)) + 1;
439 unless(defined($rtorrent = xmlrtorrent->new())) {
440 write_irssi('Could not initialize XMLRPC instance');
446 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
447 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
448 Irssi::signal_add('setup saved', 'cmd_save');
451 Irssi::command_bind('torrent' => \&cmdhandler);
454 write_irssi('xmlrtorrent initialized');
458 my ($complist, $window, $word, $linestart, $want_space) = @_;
461 if ($linestart !~ m|^/torrent\b|) {
467 Irssi::signal_stop();
471 my ($data, $server, $witem) = @_;
472 my ($cmd, @params) = split(/\s+/, $data);
476 if (exists($xmlrtorrent_commands->{$cmd})) {
477 $xmlrtorrent_commands->{$cmd}->(@params);
479 write_irssi('Unknown command: %s', $cmd);
485 unshift(@INC, $scriptdir);