add plugin system
[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 use xmlrtorrent;
15
16 my @talkers;
17 my $talker;
18 my $conf;
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');
22 my %torrentlist = ();
23 my $torrentindex = 1;
24 my $rtorrent;
25
26 my @outputstack = (undef);
27
28 my $PARAMS = {
29     'XMLURL' => 'http://localhost/RPC2',
30     'USERNAME' => '',
31     'PASSWORD' => '',
32     '_QUEUE' => {},
33 };
34
35 # activate debug here
36 my $debug = 1;
37
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);});
42
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);});
47
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);});
52
53 # For tab completion
54 signal_add_first('complete word', \&sig_complete);
55
56 my $xmlrtorrent_commands = {
57     'save' => sub {
58         cmd_save();
59     },
60
61     'set' => sub {
62         cmd_set(@_);
63     },
64     
65     'show' => sub {
66         cmd_show(@_);
67     },
68
69     'help' => sub {
70         cmd_help(@_);
71     },
72
73     'queue' => sub {
74         cmd_queue(@_);
75     },
76
77     'remote' => sub {
78         cmd_remote(@_);
79     },
80
81     'debug' => sub {
82         $debug = 1;
83         write_irssi('Enabled debugging');
84     },
85
86     'nodebug' => sub {
87         $debug = 0;
88         write_irssi('Disabled debugging');
89     },
90 };
91
92 sub write_irssi {
93     my @text = @_;
94     my $output = $outputstack[0];
95
96     $text[0] = '%%mxmlrtorrent: %%n' . $text[0];
97
98     if (defined($output) and ref($output)) {
99         $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
100     } else {
101         Irssi::print(sprintf(shift(@text), @text));
102     }
103
104 }
105
106 sub push_output {
107     unshift(@outputstack, shift);
108 }
109
110 sub pop_output {
111     shift(@outputstack);
112 }
113
114 sub write_debug {
115     if ($debug) {
116         write_irssi(@_);
117     }
118 }
119
120 # This is shamelessly stolen from pythons urlgrabber
121 sub format_number {
122     my $number = shift;
123     my $SI = shift || 0;
124     my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
125     my $step = $SI?1000:1024;
126     my $thresh = 999;
127     my $depth = 0;
128     my $max_depth = $#symbols;
129     my $format;
130
131     while (($number > $thresh) and ($depth < $max_depth)) {
132         $depth += 1;
133         $number /= $step;
134     }
135
136     if ($number =~ /^[+-]?\d+$/) {
137         # Integer.
138         $format = '%i%s';
139     } elsif ($number < 9.95) {
140         $format = '%.1f%s';
141     } else {
142         $format = '%.0f%s';
143     }
144     return sprintf($format, $number, $symbols[$depth]);
145 }
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 $m;
157     my $p;
158
159     my $witem;
160     if (defined $server) {
161         $witem = $server->window_item_find($target);
162     } else {
163         $witem = Irssi::window_item_find($target);
164     }
165
166     # Look if we should ignore this line
167     if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
168         return;
169     }
170
171     push_output($witem);
172
173     # Look if there is a torrent link in there
174     $message =~ m,(http://\S*\.(?:torrent|penis)),;
175     $m = $1;
176     while (defined($m)) {
177         write_debug('Torrent-URL: %s', $m);
178         $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $m};
179
180         # Remove the matched part from the message and try again (there may be
181         # more!)
182         $message =~ s/$m//;
183
184         $message =~ m|(http://.*\.torrent)|;
185         $m = $1;
186     }
187
188     pop_output();
189 }
190
191 # Handle the queue of unhandled torrents
192 sub cmd_queue {
193     my ($subcmd, $id, @params) = @_;
194
195     if ('remove' eq $subcmd) {
196         if (defined($id)) {
197             delete($torrentlist{$id});
198         }
199     } elsif ('clear' eq $subcmd) {
200         %torrentlist = ();
201     } elsif ('confirm' eq $subcmd) {
202         my $u;
203         return unless(defined($id) and exists($torrentlist{$id}));
204
205         $u = $torrentlist{$id}->{'URL'};
206
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());
210         } else {
211             write_irssi('%s enqueued', $u);
212             delete($torrentlist{$id});
213         }
214     } elsif ('add' eq $subcmd) {
215         unless(defined($id)) {
216             return;
217         }
218         $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
219     } elsif (('list' eq $subcmd) or !defined($subcmd))  {
220         my $l;
221         write_irssi('List of queued torrents');
222         if (0 == scalar(keys(%torrentlist))) {
223             write_irssi('  (no torrents in local queue)');
224         } else {
225             foreach (sort(keys(%torrentlist))) {
226                 write_irssi('  %3d: %s@%s: %s', $_,
227                         $torrentlist{$_}->{'NICK'},
228                         $torrentlist{$_}->{'CHANNEL'},
229                         $torrentlist{$_}->{'URL'});
230             }
231         }
232     } else {
233         write_irssi('Unknown subcommand: %s', $subcmd);
234     }
235 }
236
237 # Handle the remote rtorrent queue
238 sub cmd_remote {
239     my ($subcmd, $id, @params) = @_;
240     my $rqueue;
241
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());
245             return;
246         }
247
248         write_irssi('List of rempote torrents');
249         if (0 == scalar(@{$rqueue})) {
250             write_irssi('  (no torrents in remote queue)');
251         } else {
252             foreach (@{$rqueue}) {
253                 write_irssi('  %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
254                         $_->[6]?'*':' ',
255                         $_->[0],
256                         format_number($_->[2]),
257                         format_number($_->[1]),
258                         ($_->[2]*100)/$_->[1],
259                         format_number($_->[3]),
260                         format_number($_->[4]));
261             }
262         }
263     }
264 }
265
266
267 sub cmd_save {
268     
269     my %mappedqueue;
270
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);
274
275     eval {
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'});
279         close(CONF);
280     };
281     if ($@) {
282         write_irssi('Could not save config to %s: %s', ($conffile, $@));
283     } else {
284         write_irssi('configuration saved to %s', $conffile);
285     }
286 }
287
288 sub cmd_set {
289     my $target = shift;
290     my $key = shift;
291     my $val = shift;
292
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');
302                     return;
303                 }
304             }
305         } else {
306             write_irssi('Key %s does not exist', $key);
307         }
308     }
309 }
310
311
312 sub cmd_show {
313     my $target = shift;
314     my $p;
315     my $e;
316 }
317
318 sub cmd_help {
319     my $target = shift;
320     my $p;
321
322     write_irssi(<<'EOT');
323 Supported commands:
324  save: Save the current configuration
325  help: Display this help
326  debug: enable debugging messages
327  nodebug: disable debugging messages
328 EOT
329 }
330
331
332 # save on unload
333 sub sig_command_script_unload {
334     my $script = shift;
335     if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
336         cmd_save();
337     }
338 }
339
340 sub ploader {
341
342     my $dir = shift;
343     my $pattern = shift;
344     my $type = shift;
345     my @list;
346     my $p;
347     my $g;
348     my @g = ();
349
350     opendir(D, $dir) || return ();
351     @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
352     closedir(D);
353
354     foreach $p (@list) {
355         write_debug('Trying to load %s:', $p);
356         $p =~ s/\.pm$//;
357         eval qq{ require xmlrtorrent::$p; };
358         if ($@) {
359             write_irssi('Failed to load plugin: %s', "$@");
360             next;
361         }
362
363         $g = eval qq{ xmlrtorrent::$p->new(); };
364         if ($@) {
365             write_irssi('Failed to instanciate: %s', "$@");
366             delete($INC{$p});
367             next;
368         }
369
370         write_debug('found %s %s', $g->{'TYPE'}, $g->{'NAME'});
371         if ($type eq $g->{'TYPE'}) {
372             push(@g, $g);
373             $g->setio(sub {Irssi::print(shift)});
374         } else {
375             write_irssi('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
376             delete($INC{$p});
377         }
378     }
379
380     write_debug('Loaded %d plugins', $#g+1);
381     
382     return @g;
383 }
384
385 sub _load_modules($) {
386
387     my $path = shift;
388
389     foreach (keys(%INC)) {
390         if ($INC{$_} =~ m|^$path|) {
391             write_debug('Removing %s from $INC', $_);
392             delete($INC{$_});
393         }
394     }
395     @talkers = ploader($path, '.*Talker\.pm$', 'talker');
396 }
397
398 sub init_xmlrtorrent {
399
400     my $bindings = shift;
401     my $p;
402
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' => { }};
407     }
408     foreach (keys(%{$PARAMS})) {
409         unless (exists($conf->{'xmlrtorrent'}->{$_})) {
410             $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
411         }
412     }
413
414     _load_modules($plugindir);
415
416     unless (defined(@talkers)) {
417         write_irssi('No talkers found, can not proceed.');
418         return;
419     }
420
421     $talker = $talkers[0];
422     foreach $p (@talkers) {
423         if ($conf->{'xmlrtorrent'}->{'talker'} eq $p->{'NAME'}) {
424             $talker = $p;
425         }
426     }
427     write_debug(undef, 'Selected %s as talker', $talker->{'NAME'});
428     $conf->{'videosite'}->{'talker'} = $talker->{'NAME'};
429
430
431     # Restore the queue
432     %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
433     %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
434     $torrentindex = max(keys(%torrentlist)) + 1;
435
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');
441         return;
442     }
443
444     if ($bindings) {
445
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');
449
450
451         Irssi::command_bind('torrent' => \&cmdhandler);
452     }
453
454     write_irssi('xmlrtorrent initialized');
455 }
456
457 sub sig_complete {
458     my ($complist, $window, $word, $linestart, $want_space) = @_;
459     my @matches;
460
461     if ($linestart !~ m|^/torrent\b|) {
462         return;
463     }
464
465     ${$want_space} = 0;
466
467     Irssi::signal_stop();
468 }
469
470 sub cmdhandler {
471     my ($data, $server, $witem) = @_;
472     my ($cmd, @params) = split(/\s+/, $data);
473
474     push_output($witem);
475
476     if (exists($xmlrtorrent_commands->{$cmd})) {
477         $xmlrtorrent_commands->{$cmd}->(@params);
478     } else {
479         write_irssi('Unknown command: %s', $cmd);
480     }
481
482     pop_output();
483 }
484
485 unshift(@INC, $scriptdir);
486 init_xmlrtorrent(1);