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