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 = {
83 write_irssi('Enabled debugging');
88 write_irssi('Disabled debugging');
94 my $output = $outputstack[0];
96 $text[0] = '%%mxmlrtorrent: %%n' . $text[0];
98 if (defined($output) and ref($output)) {
99 $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
101 Irssi::print(sprintf(shift(@text), @text));
107 unshift(@outputstack, shift);
120 # This is shamelessly stolen from pythons urlgrabber
124 my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
125 my $step = $SI?1000:1024;
128 my $max_depth = $#symbols;
131 while (($number > $thresh) and ($depth < $max_depth)) {
136 if ($number =~ /^[+-]?\d+$/) {
139 } elsif ($number < 9.95) {
144 return sprintf($format, $number, $symbols[$depth]);
150 my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
151 my $server = $signal->[$paramserver];
152 my $target = $signal->[$paramchannel];
153 my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
154 my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick];
160 if (defined $server) {
161 $witem = $server->window_item_find($target);
163 $witem = Irssi::window_item_find($target);
166 # Look if we should ignore this line
167 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
173 # Look if there is a torrent link in there
174 $message =~ m,(http://\S*\.(?:torrent|penis)),;
176 while (defined($m)) {
177 write_debug('Torrent-URL: %s', $m);
178 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $m};
180 # Remove the matched part from the message and try again (there may be
184 $message =~ m|(http://.*\.torrent)|;
191 # Handle the queue of unhandled torrents
193 my ($subcmd, $id, @params) = @_;
195 if ('remove' eq $subcmd) {
197 delete($torrentlist{$id});
199 } elsif ('clear' eq $subcmd) {
201 } elsif ('confirm' eq $subcmd) {
203 return unless(defined($id) and exists($torrentlist{$id}));
205 $u = $torrentlist{$id}->{'URL'};
207 write_debug('Sending %s to rtorrent', $u);
208 unless(defined($rtorrent->load_start($u))) {
209 write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
211 write_irssi('%s enqueued', $u);
212 delete($torrentlist{$id});
214 } elsif ('add' eq $subcmd) {
215 unless(defined($id)) {
218 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
219 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
221 write_irssi('List of queued torrents');
222 if (0 == scalar(keys(%torrentlist))) {
223 write_irssi(' (no torrents in local queue)');
225 foreach (sort(keys(%torrentlist))) {
226 write_irssi(' %3d: %s@%s: %s', $_,
227 $torrentlist{$_}->{'NICK'},
228 $torrentlist{$_}->{'CHANNEL'},
229 $torrentlist{$_}->{'URL'});
233 write_irssi('Unknown subcommand: %s', $subcmd);
237 # Handle the remote rtorrent queue
239 my ($subcmd, $id, @params) = @_;
242 if (('list' eq $subcmd) or !defined($subcmd)) {
243 unless(defined($rqueue = $rtorrent->download_list())) {
244 write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
248 write_irssi('List of rempote torrents');
249 if (0 == scalar(@{$rqueue})) {
250 write_irssi(' (no torrents in remote queue)');
252 foreach (@{$rqueue}) {
253 write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
256 format_number($_->[2]),
257 format_number($_->[1]),
258 ($_->[2]*100)/$_->[1],
259 format_number($_->[3]),
260 format_number($_->[4]));
271 # XML::Simple has some problems with numbers as nodenames,
272 # so we have to modify our queue a bit.
273 %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
276 open(CONF, '>'.$conffile) or die 'Could not open config file';
277 $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
278 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
282 write_irssi('Could not save config to %s: %s', ($conffile, $@));
284 write_irssi('configuration saved to %s', $conffile);
293 if ('global' eq $target) {
294 if(exists($PARAMS->{$key})) {
295 $conf->{'xmlrtorrent'}->{$key} = $val;
296 if ('XMLURL' eq $key) {
297 unless(defined($rtorrent = xmlrtorrent->new(
298 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
299 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
300 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
301 write_irssi('Could not initialize XMLRPC instance');
306 write_irssi('Key %s does not exist', $key);
322 write_irssi(<<'EOT');
324 save: Save the current configuration
325 help: Display this help
326 debug: enable debugging messages
327 nodebug: disable debugging messages
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 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
438 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
439 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
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);