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 $text[0] = '%%mxmlrtorrent: %%n' . $text[0];
95 if (defined($output) and ref($output)) {
96 $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
98 Irssi::print(sprintf(shift(@text), @text));
104 unshift(@outputstack, shift);
117 # This is shamelessly stolen from pythons urlgrabber
121 my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
122 my $step = $SI?1000:1024;
125 my $max_depth = $#symbols;
128 while (($number > $thresh) and ($depth < $max_depth)) {
133 if ($number =~ /^[+-]?\d+$/) {
136 } elsif ($number < 9.95) {
141 return sprintf($format, $number, $symbols[$depth]);
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];
156 if (defined $server) {
157 $witem = $server->window_item_find($target);
159 $witem = Irssi::window_item_find($target);
162 # Look if we should ignore this line
163 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
169 # Look if there is a torrent link in there
171 while ($message =~ m,(http://\S*\.(?:torrent|penis)),g) {
172 write_debug('Torrent-URL: %s', $1);
173 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $1};
179 # Handle the queue of unhandled torrents
181 my ($subcmd, $id, @params) = @_;
183 if ('remove' eq $subcmd) {
185 delete($torrentlist{$id});
187 } elsif ('clear' eq $subcmd) {
189 } elsif ('confirm' eq $subcmd) {
191 return unless(defined($id) and exists($torrentlist{$id}));
193 $u = $torrentlist{$id}->{'URL'};
195 write_debug('Sending %s to rtorrent', $u);
196 unless(defined($rtorrent->load_start($u))) {
197 write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
199 write_irssi('%s enqueued', $u);
200 delete($torrentlist{$id});
202 } elsif ('add' eq $subcmd) {
203 unless(defined($id)) {
206 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
207 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
209 write_irssi('List of queued torrents');
210 if (0 == scalar(keys(%torrentlist))) {
211 write_irssi(' (no torrents in local queue)');
213 foreach (sort(keys(%torrentlist))) {
214 write_irssi(' %3d: %s@%s: %s', $_,
215 $torrentlist{$_}->{'NICK'},
216 $torrentlist{$_}->{'CHANNEL'},
217 $torrentlist{$_}->{'URL'});
221 write_irssi('Unknown subcommand: %s', $subcmd);
225 # Handle the remote rtorrent queue
227 my ($subcmd, $id, @params) = @_;
230 if (('list' eq $subcmd) or !defined($subcmd)) {
231 unless(defined($rqueue = $rtorrent->download_list())) {
232 write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
236 write_irssi('List of rempote torrents');
237 if (0 == scalar(@{$rqueue})) {
238 write_irssi(' (no torrents in remote queue)');
240 foreach (@{$rqueue}) {
241 write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
244 format_number($_->[2]),
245 format_number($_->[1]),
246 ($_->[2]*100)/$_->[1],
247 format_number($_->[3]),
248 format_number($_->[4]));
259 # XML::Simple has some problems with numbers as nodenames,
260 # so we have to modify our queue a bit.
261 %mappedqueue = map {("_$_" => $torrentlist{$_})} keys(%torrentlist);
264 open(CONF, '>'.$conffile) or die 'Could not open config file';
265 $conf->{'xmlrtorrent'}->{'_QUEUE'} = \%mappedqueue;
266 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
270 write_irssi('Could not save config to %s: %s', ($conffile, $@));
272 write_irssi('configuration saved to %s', $conffile);
281 if ('global' eq $target) {
282 if(exists($PARAMS->{$key})) {
283 $conf->{'xmlrtorrent'}->{$key} = $val;
284 if ('XMLURL' eq $key) {
285 unless(defined($rtorrent = xmlrtorrent->new(
286 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
287 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
288 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
289 write_irssi('Could not initialize XMLRPC instance');
294 write_irssi('Key %s does not exist', $key);
310 write_irssi(<<'EOT');
312 save: Save the current configuration
313 help: Display this help
314 debug: enable debugging messages
315 nodebug: disable debugging messages
321 sub sig_command_script_unload {
323 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
328 sub init_xmlrtorrent {
330 my $bindings = shift;
333 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
334 # No config, start with an empty one
335 write_debug('No config found, using defaults');
336 $conf = { 'xmlrtorrent' => { }};
338 foreach (keys(%{$PARAMS})) {
339 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
340 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
345 %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
346 %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
347 $torrentindex = max(keys(%torrentlist)) + 1;
349 unless(defined($rtorrent = xmlrtorrent->new(
350 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
351 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
352 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
353 write_irssi('Could not initialize XMLRPC instance');
359 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
360 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
361 Irssi::signal_add('setup saved', 'cmd_save');
364 Irssi::command_bind('torrent' => \&cmdhandler);
367 write_irssi('xmlrtorrent initialized');
371 my ($complist, $window, $word, $linestart, $want_space) = @_;
374 if ($linestart !~ m|^/torrent\b|) {
380 Irssi::signal_stop();
384 my ($data, $server, $witem) = @_;
385 my ($cmd, @params) = split(/\s+/, $data);
389 if (exists($xmlrtorrent_commands->{$cmd})) {
390 $xmlrtorrent_commands->{$cmd}->(@params);
392 write_irssi('Unknown command: %s', $cmd);
398 unshift(@INC, $scriptdir);