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',
33 # "message public", SERVER_REC, char *msg, char *nick, char *address, char *target
34 signal_add_last("message public" => sub {check_for_link(\@_,1,4,2,0);});
35 # "message own_public", SERVER_REC, char *msg, char *target
36 signal_add_last("message own_public" => sub {check_for_link(\@_,1,2,-1,0);});
38 # "message private", SERVER_REC, char *msg, char *nick, char *address
39 signal_add_last("message private" => sub {check_for_link(\@_,1,-1,2,0);});
40 # "message own_private", SERVER_REC, char *msg, char *target, char *orig_target
41 signal_add_last("message own_private" => sub {check_for_link(\@_,1,2,-1,0);});
43 # "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target
44 signal_add_last("message irc action" => sub {check_for_link(\@_,1,4,2,0);});
45 # "message irc own_action", SERVER_REC, char *msg, char *target
46 signal_add_last("message irc own_action" => sub {check_for_link(\@_,1,2,-1,0);});
49 signal_add_first('complete word', \&sig_complete);
51 my $xmlrtorrent_commands = {
78 write_irssi('Enabled debugging');
83 write_irssi('Disabled debugging');
89 my $output = $outputstack[0];
91 $text[0] = '%%mxmlrtorrent: %%n' . $text[0];
93 if (defined($output) and ref($output)) {
94 $output->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP);
96 Irssi::print(sprintf(shift(@text), @text));
102 unshift(@outputstack, shift);
115 # This is shamelessly stolen from pythons urlgrabber
119 my @symbols = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
120 my $step = $SI?1000:1024;
123 my $max_depth = $#symbols;
126 while (($number > $thresh) and ($depth < $max_depth)) {
131 if ($number =~ /^[+-]?\d+$/) {
134 } elsif ($number < 9.95) {
139 return sprintf($format, $number, $symbols[$depth]);
145 my ($signal,$parammessage,$paramchannel,$paramnick,$paramserver) = @_;
146 my $server = $signal->[$paramserver];
147 my $target = $signal->[$paramchannel];
148 my $message = ($parammessage == -1) ? '' : $signal->[$parammessage];
149 my $nick = ($paramnick == -1)?defined($server)?$server->{'nick'}:'':$signal->[$paramnick];
155 if (defined $server) {
156 $witem = $server->window_item_find($target);
158 $witem = Irssi::window_item_find($target);
161 # Look if we should ignore this line
162 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
168 # Look if there is a torrent link in there
169 $message =~ m,(http://\S*\.(?:torrent|penis)),;
171 while (defined($m)) {
172 write_debug('Torrent-URL: %s', $m);
173 $torrentlist{$torrentindex++} = {'CHANNEL' => $target, 'NICK' => $nick, 'URL' => $m};
175 # Remove the matched part from the message and try again (there may be
179 $message =~ m|(http://.*\.torrent)|;
186 # Handle the queue of unhandled torrents
188 my ($subcmd, $id, @params) = @_;
190 if ('remove' eq $subcmd) {
192 delete($torrentlist{$id});
194 } elsif ('clear' eq $subcmd) {
196 } elsif ('confirm' eq $subcmd) {
198 return unless(defined($id) and exists($torrentlist{$id}));
200 $u = $torrentlist{$id}->{'URL'};
202 write_debug('Sending %s to rtorrent', $u);
203 unless(defined($rtorrent->load_start($u))) {
204 write_irssi('%%RError sending URL %s: %s', $u, $rtorrent->errstr());
206 write_irssi('%s enqueued', $u);
207 delete($torrentlist{$id});
209 } elsif ('add' eq $subcmd) {
210 unless(defined($id)) {
213 $torrentlist{$torrentindex++} = {'CHANNEL' => '', 'NICK' => '', 'URL' => $id};
214 } elsif (('list' eq $subcmd) or !defined($subcmd)) {
215 write_irssi('List of queued torrents');
216 foreach (sort(keys(%torrentlist))) {
217 write_irssi(' %3d: %s@%s: %s', $_,
218 $torrentlist{$_}->{'NICK'},
219 $torrentlist{$_}->{'CHANNEL'},
220 $torrentlist{$_}->{'URL'});
223 write_irssi('Unknown subcommand: %s', $subcmd);
227 # Handle the remote rtorrent queue
229 my ($subcmd, $id, @params) = @_;
232 if (('list' eq $subcmd) or !defined($subcmd)) {
233 unless(defined($rqueue = $rtorrent->download_list())) {
234 write_irssi('Error getting list of downloads: %s', $rtorrent->errstr());
238 write_irssi('List of rempote torrents');
239 foreach (@{$rqueue}) {
240 write_irssi('%s%s: %sB/%sB done (%d%%), %sB/s up, %sB/s down',
243 format_number($_->[2]),
244 format_number($_->[1]),
245 ($_->[2]*100)/$_->[1],
246 format_number($_->[3]),
247 format_number($_->[4]));
256 open(CONF, '>'.$conffile) or die 'Could not open config file';
257 print CONF XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'});
261 write_irssi('Could not save config to %s: %s', ($conffile, $@));
263 write_irssi('configuration saved to %s', $conffile);
272 if ('global' eq $target) {
273 if(exists($PARAMS->{$key})) {
274 $conf->{'xmlrtorrent'}->{$key} = $val;
275 if ('XMLURL' eq $key) {
276 unless(defined($rtorrent = xmlrtorrent->new(
277 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
278 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
279 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
280 write_irssi('Could not initialize XMLRPC instance');
285 write_irssi('Key %s does not exist', $key);
301 write_irssi(<<'EOT');
303 save: Save the current configuration
304 help: Display this help
305 debug: enable debugging messages
306 nodebug: disable debugging messages
312 sub sig_command_script_unload {
314 if ($script =~ /(.*\/)?xmlrtorrent(\.pl)?$/) {
319 sub init_xmlrtorrent {
321 my $bindings = shift;
324 unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option'], KeepRoot => 1, KeyAttr => {'config' => 'module', 'option' => 'key'}))) {
325 # No config, start with an empty one
326 write_debug('No config found, using defaults');
327 $conf = { 'xmlrtorrent' => { }};
329 foreach (keys(%{$PARAMS})) {
330 unless (exists($conf->{'xmlrtorrent'}->{$_})) {
331 $conf->{'xmlrtorrent'}->{$_} = $PARAMS->{$_};
335 unless(defined($rtorrent = xmlrtorrent->new(
336 'XMLURL' => $conf->{'xmlrtorrent'}->{'XMLURL'},
337 'USERNAME' => $conf->{'xmlrtorrent'}->{'USERNAME'},
338 'USERNAME' => $conf->{'xmlrtorrent'}->{'PASSWORD'}))) {
339 write_irssi('Could not initialize XMLRPC instance');
345 Irssi::signal_add_first('command script load', 'sig_command_script_unload');
346 Irssi::signal_add_first('command script unload', 'sig_command_script_unload');
347 Irssi::signal_add('setup saved', 'cmd_save');
350 Irssi::command_bind('torrent' => \&cmdhandler);
353 write_irssi('xmlrtorrent initialized');
357 my ($complist, $window, $word, $linestart, $want_space) = @_;
360 if ($linestart !~ m|^/torrent\b|) {
366 Irssi::signal_stop();
370 my ($data, $server, $witem) = @_;
371 my ($cmd, @params) = split(/\s+/, $data);
375 if (exists($xmlrtorrent_commands->{$cmd})) {
376 $xmlrtorrent_commands->{$cmd}->(@params);
378 write_irssi('Unknown command: %s', $cmd);
384 unshift(@INC, $scriptdir);