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