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