d3e4f81463b700dcd724673382b592090f5f96de
[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 $conf;
17 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'xmlrtorrent.xml');
18 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
19 my %torrentlist = ();
20 my $torrentindex = 1;
21 my $rtorrent;
22
23 my @outputstack = (undef);
24
25 my $PARAMS = {
26     'XMLURL' => 'http://localhost/RPC2',
27     'USERNAME' => '',
28     'PASSWORD' => '',
29     '_QUEUE' => {},
30 };
31
32 # activate debug here
33 my $debug = 0;
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     'debug' => sub {
79         $debug = 1;
80         write_irssi('Enabled debugging');
81     },
82
83     'nodebug' => sub {
84         $debug = 0;
85         write_irssi('Disabled debugging');
86     },
87 };
88
89 sub write_irssi {
90     my @text = @_;
91     my $output = $outputstack[0];
92
93     $text[0] = '%%mxmlrtorrent: %%n' . $text[0];
94
95     if (defined($output) and ref($output)) {
96         $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
97     } else {
98         Irssi::print(sprintf(shift(@text), @text));
99     }
100
101 }
102
103 sub push_output {
104     unshift(@outputstack, shift);
105 }
106
107 sub pop_output {
108     shift(@outputstack);
109 }
110
111 sub write_debug {
112     if ($debug) {
113         write_irssi(@_);
114     }
115 }
116
117 # This is shamelessly stolen from pythons urlgrabber
118 sub format_number {
119     my $number = shift;
120     my $SI = shift || 0;
121     my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
122     my $step = $SI?1000:1024;
123     my $thresh = 999;
124     my $depth = 0;
125     my $max_depth = $#symbols;
126     my $format;
127
128     while (($number > $thresh) and ($depth < $max_depth)) {
129         $depth += 1;
130         $number /= $step;
131     }
132
133     if ($number =~ /^[+-]?\d+$/) {
134         # Integer.
135         $format = '%i%s';
136     } elsif ($number < 9.95) {
137         $format = '%.1f%s';
138     } else {
139         $format = '%.0f%s';
140     }
141     return sprintf($format, $number, $symbols[$depth]);
142 }
143
144
145
146 sub check_for_link {
147     my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
148     my $server = $signal->[$paramserver];
149     my $target = $signal->[$paramchannel];
150     my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
151     my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick];
152     my $g;
153     my $m;
154     my $p;
155
156     my $witem;
157     if (defined $server) {
158         $witem = $server->window_item_find($target);
159     } else {
160         $witem = Irssi::window_item_find($target);
161     }
162
163     # Look if we should ignore this line
164     if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
165         return;
166     }
167
168     push_output($witem);
169
170     # Look if there is a torrent link in there
171     $message =~ m,(http://\S*\.(?:torrent|penis)),;
172     $m = $1;
173     while (defined($m)) {
174         write_debug('Torrent-URL: %s', $m);
175         $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $m};
176
177         # Remove the matched part from the message and try again (there may be
178         # more!)
179         $message =~ s/$m//;
180
181         $message =~ m|(http://.*\.torrent)|;
182         $m = $1;
183     }
184
185     pop_output();
186 }
187
188 # Handle the queue of unhandled torrents
189 sub cmd_queue {
190     my ($subcmd, $id, @params) = @_;
191
192     if ('remove' eq $subcmd) {
193         if (defined($id)) {
194             delete($torrentlist{$id});
195         }
196     } elsif ('clear' eq $subcmd) {
197         %torrentlist = ();
198     } elsif ('confirm' eq $subcmd) {
199         my $u;
200         return unless(defined($id) and exists($torrentlist{$id}));
201
202         $u = $torrentlist{$id}->{'URL'};
203
204         write_debug('Sending %s to rtorrent', $u);
205         unless(defined($rtorrent->load_start($u))) {
206             write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
207         } else {
208             write_irssi('%s enqueued', $u);
209             delete($torrentlist{$id});
210         }
211     } elsif ('add' eq $subcmd) {
212         unless(defined($id)) {
213             return;
214         }
215         $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
216     } elsif (('list' eq $subcmd) or !defined($subcmd))  {
217         my $l;
218         write_irssi('List of queued torrents');
219         if (0 == scalar(keys(%torrentlist))) {
220             write_irssi('  (no torrents in local queue)');
221         } else {
222             foreach (sort(keys(%torrentlist))) {
223                 write_irssi('  %3d: %s@%s: %s', $_,
224                         $torrentlist{$_}->{'NICK'},
225                         $torrentlist{$_}->{'CHANNEL'},
226                         $torrentlist{$_}->{'URL'});
227             }
228         }
229     } else {
230         write_irssi('Unknown subcommand: %s', $subcmd);
231     }
232 }
233
234 # Handle the remote rtorrent queue
235 sub cmd_remote {
236     my ($subcmd, $id, @params) = @_;
237     my $rqueue;
238
239     if (('list' eq $subcmd) or !defined($subcmd)) {
240         unless(defined($rqueue = $rtorrent->download_list())) {
241             write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
242             return;
243         }
244
245         write_irssi('List of rempote torrents');
246         if (0 == scalar(@{$rqueue})) {
247             write_irssi('  (no torrents in remote queue)');
248         } else {
249             foreach (@{$rqueue}) {
250                 write_irssi('  %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
251                         $_->[6]?'*':' ',
252                         $_->[0],
253                         format_number($_->[2]),
254                         format_number($_->[1]),
255                         ($_->[2]*100)/$_->[1],
256                         format_number($_->[3]),
257                         format_number($_->[4]));
258             }
259         }
260     }
261 }
262
263
264 sub cmd_save {
265     
266     my %mappedqueue;
267
268     # XML::Simple has some problems with numbers as nodenames,
269     # so we have to modify our queue a bit.
270     %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
271
272     eval {
273         open(CONF, '>'.$conffile) or die 'Could not open config file';
274         $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
275         print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
276         close(CONF);
277     };
278     if ($@) {
279         write_irssi('Could not save config to %s: %s', ($conffile, $@));
280     } else {
281         write_irssi('configuration saved to %s', $conffile);
282     }
283 }
284
285 sub cmd_set {
286     my $target = shift;
287     my $key = shift;
288     my $val = shift;
289
290     if ('global' eq $target) {
291         if(exists($PARAMS->{$key})) {
292             $conf->{'xmlrtorrent'}->{$key} = $val;
293             if ('XMLURL' eq $key) {
294                 unless(defined($rtorrent = xmlrtorrent->new(
295                         'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
296                         'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
297                         'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
298                     write_irssi('Could not initialize XMLRPC instance');
299                     return;
300                 }
301             }
302         } else {
303             write_irssi('Key %s does not exist', $key);
304         }
305     }
306 }
307
308
309 sub cmd_show {
310     my $target = shift;
311     my $p;
312     my $e;
313 }
314
315 sub cmd_help {
316     my $target = shift;
317     my $p;
318
319     write_irssi(<<'EOT');
320 Supported commands:
321  save: Save the current configuration
322  help: Display this help
323  debug: enable debugging messages
324  nodebug: disable debugging messages
325 EOT
326 }
327
328
329 # save on unload
330 sub sig_command_script_unload {
331     my $script = shift;
332     if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
333         cmd_save();
334     }
335 }
336
337 sub init_xmlrtorrent {
338
339     my $bindings = shift;
340     my $p;
341
342     unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
343         # No config, start with an empty one
344         write_debug('No config found, using defaults');
345         $conf = { 'xmlrtorrent' => { }};
346     }
347     foreach (keys(%{$PARAMS})) {
348         unless (exists($conf->{'xmlrtorrent'}->{$_})) {
349             $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
350         }
351     }
352
353     # Restore the queue
354     %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
355     %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
356     $torrentindex = max(keys(%torrentlist)) + 1;
357
358     unless(defined($rtorrent = xmlrtorrent->new(
359             'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
360             'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
361             'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
362         write_irssi('Could not initialize XMLRPC instance');
363         return;
364     }
365
366     if ($bindings) {
367
368         Irssi::signal_add_first('command script load', 'sig_command_script_unload');
369         Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
370         Irssi::signal_add('setup saved', 'cmd_save');
371
372
373         Irssi::command_bind('torrent' => \&cmdhandler);
374     }
375
376     write_irssi('xmlrtorrent initialized');
377 }
378
379 sub sig_complete {
380     my ($complist, $window, $word, $linestart, $want_space) = @_;
381     my @matches;
382
383     if ($linestart !~ m|^/torrent\b|) {
384         return;
385     }
386
387     ${$want_space} = 0;
388
389     Irssi::signal_stop();
390 }
391
392 sub cmdhandler {
393     my ($data, $server, $witem) = @_;
394     my ($cmd, @params) = split(/\s+/, $data);
395
396     push_output($witem);
397
398     if (exists($xmlrtorrent_commands->{$cmd})) {
399         $xmlrtorrent_commands->{$cmd}->(@params);
400     } else {
401         write_irssi('Unknown command: %s', $cmd);
402     }
403
404     pop_output();
405 }
406
407 unshift(@INC, $scriptdir);
408 init_xmlrtorrent(1);