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