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];
157 if (defined $server) {
158 $witem = $server->window_item_find($target);
160 $witem = Irssi::window_item_find($target);
163 # Look if we should ignore this line
164 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
170 # Look if there is a torrent link in there
171 $message =~ m,(http://\S*\.(?:torrent|penis)),;
173 while (defined($m)) {
174 write_debug('Torrent-URL: %s', $m);
175 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $m};
177 # Remove the matched part from the message and try again (there may be
181 $message =~ m|(http://.*\.torrent)|;
188 # Handle the queue of unhandled torrents
190 my ($subcmd, $id, @params) = @_;
192 if ('remove' eq $subcmd) {
194 delete($torrentlist{$id});
196 } elsif ('clear' eq $subcmd) {
198 } elsif ('confirm' eq $subcmd) {
200 return unless(defined($id) and exists($torrentlist{$id}));
202 $u = $torrentlist{$id}->{'URL'};
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());
208 write_irssi('%s enqueued', $u);
209 delete($torrentlist{$id});
211 } elsif ('add' eq $subcmd) {
212 unless(defined($id)) {
215 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
216 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
218 write_irssi('List of queued torrents');
219 if (0 == scalar(keys(%torrentlist))) {
220 write_irssi(' (no torrents in local queue)');
222 foreach (sort(keys(%torrentlist))) {
223 write_irssi(' %3d: %s@%s: %s', $_,
224 $torrentlist{$_}->{'NICK'},
225 $torrentlist{$_}->{'CHANNEL'},
226 $torrentlist{$_}->{'URL'});
230 write_irssi('Unknown subcommand: %s', $subcmd);
234 # Handle the remote rtorrent queue
236 my ($subcmd, $id, @params) = @_;
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());
245 write_irssi('List of rempote torrents');
246 if (0 == scalar(@{$rqueue})) {
247 write_irssi(' (no torrents in remote queue)');
249 foreach (@{$rqueue}) {
250 write_irssi(' %s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
253 format_number($_->[2]),
254 format_number($_->[1]),
255 ($_->[2]*100)/$_->[1],
256 format_number($_->[3]),
257 format_number($_->[4]));
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);
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'});
279 write_irssi('Could not save config to %s: %s', ($conffile, $@));
281 write_irssi('configuration saved to %s', $conffile);
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');
303 write_irssi('Key %s does not exist', $key);
319 write_irssi(<<'EOT');
321 save: Save the current configuration
322 help: Display this help
323 debug: enable debugging messages
324 nodebug: disable debugging messages
330 sub sig_command_script_unload {
332 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
337 sub init_xmlrtorrent {
339 my $bindings = shift;
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' => { }};
347 foreach (keys(%{$PARAMS})) {
348 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
349 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
354 %torrentlist = %{$conf->{'xmlrtorrent'}->{'_QUEUE'}};
355 %torrentlist = map { my $a = substr($_, 1); ("$a" => $torrentlist{$_}) } keys(%torrentlist);
356 $torrentindex = max(keys(%torrentlist)) + 1;
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');
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');
373 Irssi::command_bind('torrent' => \&cmdhandler);
376 write_irssi('xmlrtorrent initialized');
380 my ($complist, $window, $word, $linestart, $want_space) = @_;
383 if ($linestart !~ m|^/torrent\b|) {
389 Irssi::signal_stop();
393 my ($data, $server, $witem) = @_;
394 my ($cmd, @params) = split(/\s+/, $data);
398 if (exists($xmlrtorrent_commands->{$cmd})) {
399 $xmlrtorrent_commands->{$cmd}->(@params);
401 write_irssi('Unknown command: %s', $cmd);
407 unshift(@INC, $scriptdir);