1 # control an rTorrent client via XMLRPC,
2 # and collect rtorrent files from IRC for later download
4 # (c) 2007-2008 by Ralf Ertzinger <ralf@camperquake.de>
5 # licensed under GNU GPL v2
8 use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last);
9 use vars qw($VERSION %IRSSI);
13 use List::Util qw(max);
17 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'xmlrtorrent.xml');
18 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
23 my @outputstack = (undef);
26 'XMLURL' => 'http://localhost/RPC2',
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);});
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);});
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);});
51 signal_add_first('complete word', \&sig_complete);
53 my $xmlrtorrent_commands = {
80 write_irssi('Enabled debugging');
85 write_irssi('Disabled debugging');
91 my $output = $outputstack[0];
93 my $format = '%%mxmlrtorrent: %%n' . shift(@text);
95 # escape % in parameters from irssi
96 s/%/%%/g foreach @text;
98 if (defined($output) and ref($output)) {
99 $output->print(sprintf($format, @text), MSGLEVEL_CLIENTCRAP);
101 Irssi::print(sprintf($format, @text));
107 unshift(@outputstack, shift);
120 # This is shamelessly stolen from pythons urlgrabber
124 my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
125 my $step = $SI?1000:1024;
128 my $max_depth = $#symbols;
131 while (($number > $thresh) and ($depth < $max_depth)) {
136 if ($number =~ /^[+-]?\d+$/) {
139 } elsif ($number < 9.95) {
144 return sprintf($format, $number, $symbols[$depth]);
150 my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
151 my $server = $signal->[$paramserver];
152 my $target = $signal->[$paramchannel];
153 my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
154 my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick];
159 if (defined $server) {
160 $witem = $server->window_item_find($target);
162 $witem = Irssi::window_item_find($target);
165 # Look if we should ignore this line
166 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
172 # Look if there is a torrent link in there
174 while ($message =~ m,(http://\S*\.(?:torrent|penis)),g) {
175 write_debug('Torrent-URL: %s', $1);
176 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $1};
182 # Handle the queue of unhandled torrents
184 my ($subcmd, $id, @params) = @_;
186 if ('remove' eq $subcmd) {
188 delete($torrentlist{$id});
190 } elsif ('clear' eq $subcmd) {
192 } elsif ('confirm' eq $subcmd) {
194 return unless(defined($id) and exists($torrentlist{$id}));
196 $u = $torrentlist{$id}->{'URL'};
198 write_debug('Sending %s to rtorrent', $u);
199 unless(defined($rtorrent->load_start($u))) {
200 write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
202 write_irssi('%s enqueued', $u);
203 delete($torrentlist{$id});
205 } elsif ('add' eq $subcmd) {
206 unless(defined($id)) {
209 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
210 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
212 write_irssi('List of queued torrents');
213 if (0 == scalar(keys(%torrentlist))) {
214 write_irssi(' (no torrents in local queue)');
216 foreach (sort(keys(%torrentlist))) {
217 write_irssi(' %3d: %s@%s: %s', $_,
218 $torrentlist{$_}->{'NICK'},
219 $torrentlist{$_}->{'CHANNEL'},
220 $torrentlist{$_}->{'URL'});
224 write_irssi('Unknown subcommand: %s', $subcmd);
228 # Handle the remote rtorrent queue
230 my ($subcmd, $id, @params) = @_;
233 if (('list' eq $subcmd) or !defined($subcmd)) {
234 unless(defined($rqueue = $rtorrent->download_list())) {
235 write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
239 write_irssi('List of rempote torrents');
240 if (0 == scalar(@{$rqueue})) {
241 write_irssi(' (no torrents in remote queue)');
243 foreach (@{$rqueue}) {
244 write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
247 format_number($_->[2]),
248 format_number($_->[1]),
249 ($_->[2]*100)/$_->[1],
250 format_number($_->[3]),
251 format_number($_->[4]));
262 # XML::Simple has some problems with numbers as nodenames,
263 # so we have to modify our queue a bit.
264 %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
267 open(CONF, '>'.$conffile) or die 'Could not open config file';
268 $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
269 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
273 write_irssi('Could not save config to %s: %s', ($conffile, $@));
275 write_irssi('configuration saved to %s', $conffile);
284 if ('global' eq $target) {
285 if(exists($PARAMS->{$key})) {
286 $conf->{'xmlrtorrent'}->{$key} = $val;
287 if ('XMLURL' eq $key) {
288 unless(defined($rtorrent = xmlrtorrent->new(
289 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
290 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
291 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
292 write_irssi('Could not initialize XMLRPC instance');
297 write_irssi('Key %s does not exist', $key);
313 write_irssi(<<'EOT');
315 save: Save the current configuration
316 help: Display this help
317 debug: enable debugging messages
318 nodebug: disable debugging messages
324 sub sig_command_script_unload {
326 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
331 sub init_xmlrtorrent {
333 my $bindings = shift;
336 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
337 # No config, start with an empty one
338 write_debug('No config found, using defaults');
339 $conf = { 'xmlrtorrent' => { }};
341 foreach (keys(%{$PARAMS})) {
342 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
343 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
348 %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
349 %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
350 $torrentindex = max(keys(%torrentlist)) + 1;
352 unless(defined($rtorrent = xmlrtorrent->new(
353 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
354 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
355 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
356 write_irssi('Could not initialize XMLRPC instance');
362 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
363 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
364 Irssi::signal_add('setup saved', 'cmd_save');
367 Irssi::command_bind('torrent' => \&cmdhandler);
370 write_irssi('xmlrtorrent initialized');
374 my ($complist, $window, $word, $linestart, $want_space) = @_;
377 if ($linestart !~ m|^/torrent\b|) {
383 Irssi::signal_stop();
387 my ($data, $server, $witem) = @_;
388 my ($cmd, @params) = split(/\s+/, $data);
392 if (exists($xmlrtorrent_commands->{$cmd})) {
393 $xmlrtorrent_commands->{$cmd}->(@params);
395 write_irssi('Unknown command: %s', $cmd);
401 unshift(@INC, $scriptdir);