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