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);
16 my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'xmlrtorrent.xml');
17 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
22 my @outputstack = (undef);
25 'XMLURL' => 'http://localhost/RPC2',
31 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
32 signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);});
33 # "message own_public", SERVER_REC, char *msg, char *target
34 signal_add_last("message own_public" => sub {check_for_link(\@_,1,2,-1,0);});
36 # "message private", SERVER_REC, char *msg, char *nick, char *address
37 signal_add_last("message private" => sub {check_for_link(\@_,1,-1,2,0);});
38 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
39 signal_add_last("message own_private" => sub {check_for_link(\@_,1,2,-1,0);});
41 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
42 signal_add_last("message irc action" => sub {check_for_link(\@_,1,4,2,0);});
43 # "message irc own_action", SERVER_REC, char *msg, char *target
44 signal_add_last("message irc own_action" => sub {check_for_link(\@_,1,2,-1,0);});
47 signal_add_first('complete word', \&sig_complete);
49 my $xmlrtorrent_commands = {
76 write_irssi('Enabled debugging');
81 write_irssi('Disabled debugging');
87 my $output = $outputstack[0];
89 $text[0] = 'xmlrtorrent: ' . $text[0];
91 if (defined($output) and ref($output)) {
92 $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
94 Irssi::print(sprintf(shift(@text), @text));
100 unshift(@outputstack, shift);
113 # This is shamelessly stolen from pythons urlgrabber
117 my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
118 my $step = $SI?1000:1024;
121 my $max_depth = $#symbols;
124 while (($number > $thresh) and ($depth < $max_depth)) {
129 if ($number =~ /^[+-]?\d+$/) {
132 } elsif ($number < 9.95) {
137 return sprintf($format, $number, $symbols[$depth]);
143 my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
144 my $server = $signal->[$paramserver];
145 my $target = $signal->[$paramchannel];
146 my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
147 my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick];
153 if (defined $server) {
154 $witem = $server->window_item_find($target);
156 $witem = Irssi::window_item_find($target);
159 # Look if we should ignore this line
160 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
166 # Look if there is a torrent link in there
167 $message =~ m|(http://\S*\.torrent)|;
169 while (defined($m)) {
170 write_debug('Torrent-URL: %s', $m);
171 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $m};
173 # Remove the matched part from the message and try again (there may be
177 $message =~ m|(http://.*\.torrent)|;
184 # Handle the queue of unhandled torrents
186 my ($subcmd, $id, @params) = @_;
188 if ('remove' eq $subcmd) {
190 delete($torrentlist{$id});
192 } elsif ('clear' eq $subcmd) {
194 } elsif ('confirm' eq $subcmd) {
196 return unless(defined($id) and exists($torrentlist{$id}));
198 $u = $torrentlist{$id}->{'URL'};
200 write_debug('Sending %s to rtorrent', $u);
201 unless(defined($rtorrent->load_start($u))) {
202 write_irssi('Error sending URL %s: %s', $u, $rtorrent->errstr());
204 delete($torrentlist{$id});
206 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
207 write_irssi('List of queued torrents');
208 foreach (sort(keys(%torrentlist))) {
209 write_irssi(' %d: %s@%s: %s', $_,
210 $torrentlist{$_}->{'NICK'},
211 $torrentlist{$_}->{'CHANNEL'},
212 $torrentlist{$_}->{'URL'});
217 # Handle the remote rtorrent queue
219 my ($subcmd, $id, @params) = @_;
222 if ('queue' eq $subcmd) {
223 unless(defined($rqueue = $rtorrent->download_list())) {
224 write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
228 foreach (@{$rqueue}) {
229 write_irssi('%s%s: %sB/%sB done, %sb/s up, %sb/s down',
232 format_number($_->[2]),
233 format_number($_->[1]),
234 format_number($_->[3]),
235 format_number($_->[4]));
244 open(CONF, '>'.$conffile) or die 'Could not open config file';
245 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
249 write_irssi('Could not save config to %s: %s', ($conffile, $@));
251 write_irssi('configuration saved to %s', $conffile);
260 if ('global' eq $target) {
261 if(exists($PARAMS->{$key})) {
262 $conf->{'xmlrtorrent'}->{$key} = $val;
263 if ('XMLURL' eq $key) {
264 unless(defined($rtorrent = xmlrtorrent->new('XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'}))) {
265 write_irssi('Could not initialize XMLRPC instance');
270 write_irssi('Key %s does not exist', $key);
286 write_irssi(<<'EOT');
288 save: Save the current configuration
289 help: Display this help
290 debug: enable debugging messages
291 nodebug: disable debugging messages
297 sub sig_command_script_unload {
299 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
304 sub init_xmlrtorrent {
306 my $bindings = shift;
309 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
310 # No config, start with an empty one
311 write_debug('No config found, using defaults');
312 $conf = { 'xmlrtorrent' => { }};
314 foreach (keys(%{$PARAMS})) {
315 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
316 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
320 unless(defined($rtorrent = xmlrtorrent->new('XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'}))) {
321 write_irssi('Could not initialize XMLRPC instance');
327 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
328 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
329 Irssi::signal_add('setup saved', 'cmd_save');
332 Irssi::command_bind('torrent' => \&cmdhandler);
335 write_irssi('xmlrtorrent initialized');
339 my ($complist, $window, $word, $linestart, $want_space) = @_;
342 if ($linestart !~ m|^/torrent\b|) {
348 Irssi::signal_stop();
352 my ($data, $server, $witem) = @_;
353 my ($cmd, @params) = split(/\s+/, $data);
357 if (exists($xmlrtorrent_commands->{$cmd})) {
358 $xmlrtorrent_commands->{$cmd}->(@params);
364 unshift(@INC, $scriptdir);