- Handle autosave on module unload/irssi exit
[xmlrtorrent.git] / xmlrtorrent.pl
1 # control an rTorrent client via XMLRPC,
2 # and collect rtorrent files from IRC for later download
3 #
4 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
5 # licensed under GNU GPL v2
6
7 use strict;
8 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
9 use vars qw($VERSION %IRSSI);
10 use XML::Simple;
11 use Data::Dumper;
12 use File::Spec;
13 use List::Util qw(max);
14
15 my @talkers;
16 my $talker;
17 my $conf;
18 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'xmlrtorrent.xml');
19 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
20 my $plugindir = File::Spec->catfile($scriptdir, 'xmlrtorrent');
21 my %torrentlist = ();
22 my $torrentindex = 1;
23
24 my @outputstack = (undef);
25
26 my $PARAMS = {
27     '_QUEUE' => {},
28     '_AUTOSAVE' => 1,
29 };
30
31 # Handle module unload/irssi shutdown
32 sub UNLOAD {
33     if ($conf->{'xmlrtorrent'}->{'_AUTOSAVE'}) {
34         cmd_save();
35     }
36 }
37
38 # activate debug here
39 my $debug = 0;
40
41 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
42 signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);});
43 # "message own_public", SERVER_REC, char *msg, char *target
44 signal_add_last("message own_public" => sub {check_for_link(\@_,1,2,-1,0);});
45
46 # "message private", SERVER_REC, char *msg, char *nick, char *address
47 signal_add_last("message private" => sub {check_for_link(\@_,1,-1,2,0);});
48 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
49 signal_add_last("message own_private" => sub {check_for_link(\@_,1,2,-1,0);});
50
51 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
52 signal_add_last("message irc action" => sub {check_for_link(\@_,1,4,2,0);});
53 # "message irc own_action", SERVER_REC, char *msg, char *target
54 signal_add_last("message irc own_action" => sub {check_for_link(\@_,1,2,-1,0);});
55
56 # For tab completion
57 signal_add_first('complete word', \&sig_complete);
58
59 my $xmlrtorrent_commands = {
60     'save' => sub {
61         cmd_save();
62     },
63
64     'set' => sub {
65         cmd_set(@_);
66     },
67     
68     'show' => sub {
69         cmd_show(@_);
70     },
71
72     'help' => sub {
73         cmd_help(@_);
74     },
75
76     'queue' => sub {
77         cmd_queue(@_);
78     },
79
80     'remote' => sub {
81         cmd_remote(@_);
82     },
83
84     'talker' => sub {
85         cmd_talker(@_);
86     },
87
88     'debug' => sub {
89         $debug = 1;
90         write_irssi('Enabled debugging');
91     },
92
93     'nodebug' => sub {
94         $debug = 0;
95         write_irssi('Disabled debugging');
96     },
97
98     'autosave' => sub {
99         $conf->{'xmlrtorrent'}->{'_AUTOSAVE'} = 1;
100         write_irssi('Autosave enabled');
101     },
102
103     'noautosave' => sub {
104         $conf->{'xmlrtorrent'}->{'_AUTOSAVE'} = 0;
105         write_irssi('Autosave disabled');
106     },
107 };
108
109 # This is shamelessly stolen from pythons urlgrabber
110 sub format_number {
111     my $number = shift;
112     my $SI = shift || 0;
113     my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
114     my $step = $SI?1000:1024;
115     my $thresh = 999;
116     my $depth = 0;
117     my $max_depth = $#symbols;
118     my $format;
119
120     while (($number > $thresh) and ($depth < $max_depth)) {
121         $depth += 1;
122         $number /= $step;
123     }
124
125     if ($number =~ /^[+-]?\d+$/) {
126         # Integer.
127         $format = '%i%s';
128     } elsif ($number < 9.95) {
129         $format = '%.1f%s';
130     } else {
131         $format = '%.0f%s';
132     }
133     return sprintf($format, $number, $symbols[$depth]);
134 }
135
136 sub write_irssi {
137     my @text = @_;
138     my $output = $outputstack[0];
139
140     my $format = '%%mxmlrtorrent: %%n' . shift(@text);
141
142     # escape % in parameters from irssi
143     s/%/%%/g foreach @text;
144
145     if (defined($output) and ref($output)) {
146         $output->print(sprintf($format, @text), MSGLEVEL_CLIENTCRAP);
147     } else {
148         Irssi::print(sprintf($format, @text));
149     }
150
151 }
152
153 sub push_output {
154     unshift(@outputstack, shift);
155 }
156
157 sub pop_output {
158     shift(@outputstack);
159 }
160
161 sub write_debug {
162     if ($debug) {
163         write_irssi(@_);
164     }
165 }
166
167 sub check_for_link {
168     my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
169     my $server = $signal->[$paramserver];
170     my $target = $signal->[$paramchannel];
171     my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
172     my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick];
173     my $g;
174     my $p;
175
176     my $witem;
177     if (defined $server) {
178         $witem = $server->window_item_find($target);
179     } else {
180         $witem = Irssi::window_item_find($target);
181     }
182     
183     # Look if we should ignore this line
184     if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
185         return;
186     }
187         
188     push_output($witem);
189
190     # Look if there is a torrent link in there
191         
192     while ($message =~ m,(http://\S*\.(?:torrent|penis)),g) {
193         write_debug('Torrent-URL: %s', $1);
194         $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $1};
195     }
196
197     pop_output();
198 }
199
200 # Handle the queue of unhandled torrents
201 sub cmd_queue {
202     my ($subcmd, $id, @params) = @_;
203
204     if ('remove' eq $subcmd) {
205         if (defined($id)) {
206             delete($torrentlist{$id});
207         }
208     } elsif ('clear' eq $subcmd) {
209         %torrentlist = ();
210     } elsif ('confirm' eq $subcmd) {
211         my $u;
212         return unless(defined($id) and exists($torrentlist{$id}));
213
214         $u = $torrentlist{$id}->{'URL'};
215
216         write_debug('Sending %s to rtorrent', $u);
217         unless(defined($talker->load_start($u))) {
218             write_irssi('%%RError sending URL %s: %s', $u, $talker->errstr());
219         } else {
220             write_irssi('%s enqueued', $u);
221             delete($torrentlist{$id});
222         }
223     } elsif ('add' eq $subcmd) {
224         unless(defined($id)) {
225             return;
226         }
227         $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
228     } elsif (('list' eq $subcmd) or !defined($subcmd))  {
229         my $l;
230         write_irssi('List of queued torrents');
231         if (0 == scalar(keys(%torrentlist))) {
232             write_irssi('  (no torrents in local queue)');
233         } else {
234             foreach (sort(keys(%torrentlist))) {
235                 write_irssi('  %3d: %s@%s: %s', $_,
236                         $torrentlist{$_}->{'NICK'},
237                         $torrentlist{$_}->{'CHANNEL'},
238                         $torrentlist{$_}->{'URL'});
239             }
240         }
241     } else {
242         write_irssi('Unknown subcommand: %s', $subcmd);
243     }
244 }
245
246 # Handle the remote rtorrent queue
247 sub cmd_remote {
248     my ($subcmd, $id, @params) = @_;
249     my $rqueue;
250
251     if (('list' eq $subcmd) or !defined($subcmd)) {
252         unless(defined($rqueue = $talker->download_list())) {
253             write_irssi('Error getting list of downloads: %s', $talker->errstr());
254             return;
255         }
256
257         write_irssi('List of remote torrents');
258         if (0 == scalar(@{$rqueue})) {
259             write_irssi('  (no torrents in remote queue)');
260         } else {
261             foreach (@{$rqueue}) {
262                 write_irssi('  %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
263                             $_->{'ACTIVE'}?'*':' ',
264                             $_->{'NAME'},
265                             format_number($_->{'BYTES_DONE'}),
266                             format_number($_->{'SIZE_BYTES'}),
267                             $_->{'BYTES_DONE'}*100/$_->{'SIZE_BYTES'},
268                             format_number($_->{'UP_RATE'}),
269                             format_number($_->{'DOWN_RATE'}));
270             }
271         }
272     }
273 }
274
275
276 sub cmd_save {
277     
278     my %mappedqueue;
279
280     # XML::Simple has some problems with numbers as nodenames,
281     # so we have to modify our queue a bit.
282     %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
283
284     eval {
285         open(CONF, '>'.$conffile) or die 'Could not open config file';
286         $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
287         print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
288         close(CONF);
289     };
290     if ($@) {
291         write_irssi('Could not save config to %s: %s', ($conffile, $@));
292     } else {
293         write_irssi('configuration saved to %s', $conffile);
294     }
295 }
296
297 sub cmd_set {
298     my $target = shift;
299     my $key = shift;
300     my $val = shift;
301     my $p;
302
303     foreach $p (@talkers) {
304         if ($p->{'NAME'} eq $target) {
305             $p->setval($key, $val);
306             return;
307         }
308     }
309     write_irssi('No such module');
310 }
311
312 sub cmd_show {
313     my $target = shift;
314     my $p;
315     my $e;
316
317     if (defined($target)) {
318         foreach $p (@talkers) {
319             if ($p->{'NAME'} eq $target) {
320                 write_irssi($p->getconfstr());
321                 return;
322             }
323         }
324         write_irssi('No such module');
325     } else {
326         write_irssi('Loaded talkers:');
327         foreach $p (@talkers) {
328             write_irssi(' %s', $p->{'NAME'});
329         };
330     }
331 }
332
333 sub cmd_help {
334     my $target = shift;
335     my $p;
336
337     if (defined($target)) {
338         foreach $p (@talkers) {
339             if ($p->{'NAME'} eq $target) {
340                 write_irssi($p->gethelpstr());
341                 return;
342             }
343         }
344         write_irssi('No such module');
345     } else {
346         write_irssi(<<'EOT');
347 Supported commands:
348  save: save the current configuration
349  help [modulename]: display this help or module specific help
350  show [modulename]: show loaded modules or the current parameters of a module
351  talker [modulename]: display or set the talker to use
352  debug: enable debugging messages
353  nodebug: disable debugging messages
354 EOT
355 ;
356     }
357 }
358
359 sub cmd_talker {
360     my $target = shift;
361     my $p;
362
363     if (defined($target)) {
364         foreach $p (@talkers) {
365             if (($p->{'NAME'} eq $target) && ($p->{'TYPE'} eq 'talker')) {
366                 $talker = $p;
367                 $conf->{'xmlrtorrent'}->{'talker'} = $target;
368                 return;
369             }
370         }
371         write_irssi('No such talker');
372     } else {
373         write_irssi('Current talker: %s', $conf->{'xmlrtorrent'}->{'talker'});
374     }
375 }
376
377
378
379 # save on unload
380 sub sig_command_script_unload {
381     my $script = shift;
382     if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
383         cmd_save();
384     }
385 }
386
387 sub ploader {
388
389     my $dir = shift;
390     my $pattern = shift;
391     my $type = shift;
392     my @list;
393     my $p;
394     my $g;
395     my @g = ();
396
397     opendir(D, $dir) || return ();
398     @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
399     closedir(D);
400
401     foreach $p (@list) {
402         write_debug('Trying to load %s:', $p);
403         $p =~ s/\.pm$//;
404         eval qq{ require xmlrtorrent::$p; };
405         if ($@) {
406             write_irssi('Failed to load plugin: %s', "$@");
407             next;
408         }
409
410         $g = eval qq{ xmlrtorrent::$p->new(); };
411         if ($@) {
412             write_irssi('Failed to instanciate: %s', "$@");
413             delete($INC{$p});
414             next;
415         }
416
417         write_debug('found %s %s', $g->{'TYPE'}, $g->{'NAME'});
418         if ($type eq $g->{'TYPE'}) {
419             push(@g, $g);
420             $g->setio(sub {Irssi::print(shift)});
421         } else {
422             write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
423             delete($INC{$p});
424         }
425     }
426
427     write_debug('Loaded %d plugins', $#g+1);
428     
429     return @g;
430 }
431
432 sub _load_modules($) {
433
434     my $path = shift;
435
436     foreach (keys(%INC)) {
437         if ($INC{$_} =~ m|^$path|) {
438             write_debug('Removing %s from $INC', $_);
439             delete($INC{$_});
440         }
441     }
442     @talkers = ploader($path, '.*Talker\.pm$', 'talker');
443 }
444
445 sub init_xmlrtorrent {
446
447     my $bindings = shift;
448     my $p;
449
450     unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
451         # No config, start with an empty one
452         write_debug('No config found, using defaults');
453         $conf = { 'xmlrtorrent' => { }};
454     }
455     foreach (keys(%{$PARAMS})) {
456         unless (exists($conf->{'xmlrtorrent'}->{$_})) {
457             $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
458         }
459     }
460
461     _load_modules($plugindir);
462
463     unless (defined(@talkers)) {
464         write_irssi('No talkers found, can not proceed.');
465         return;
466     }
467
468     $talker = $talkers[0];
469     foreach $p (@talkers) {
470         if ($conf->{'xmlrtorrent'}->{'talker'} eq $p->{'NAME'}) {
471             $talker = $p;
472         }
473     }
474     write_debug('Selected %s as talker', $talker->{'NAME'});
475     $conf->{'xmlrtorrent'}->{'talker'} = $talker->{'NAME'};
476
477     # Loop through all plugins and load the config
478     foreach $p (@talkers) {
479         $conf->{'xmlrtorrent'}->{'config'}->{$p->{'NAME'}} = $p->mergeconfig($conf->{'xmlrtorrent'}->{'config'}->{$p->{'NAME'}});
480     }
481
482     # Restore the queue
483     %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
484     %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
485     $torrentindex = max(keys(%torrentlist)) + 1;
486
487     if ($bindings) {
488
489         Irssi::signal_add_first('command script load', 'sig_command_script_unload');
490         Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
491         Irssi::signal_add('setup saved', 'cmd_save');
492
493
494         Irssi::command_bind('torrent' => \&cmdhandler);
495     }
496
497     write_irssi('xmlrtorrent initialized');
498 }
499
500 sub sig_complete {
501     my ($complist, $window, $word, $linestart, $want_space) = @_;
502     my @matches;
503
504     if ($linestart !~ m|^/torrent\b|) {
505         return;
506     }
507
508     ${$want_space} = 0;
509
510     Irssi::signal_stop();
511 }
512
513 sub cmdhandler {
514     my ($data, $server, $witem) = @_;
515     my ($cmd, @params) = split(/\s+/, $data);
516
517     push_output($witem);
518
519     if (exists($xmlrtorrent_commands->{$cmd})) {
520         $xmlrtorrent_commands->{$cmd}->(@params);
521     } else {
522         write_irssi('Unknown command: %s', $cmd);
523     }
524
525     pop_output();
526 }
527
528 unshift(@INC, $scriptdir);
529 init_xmlrtorrent(1);