]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / IkiWiki / Plugin / inline.pm
1 #!/usr/bin/perl
2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
4
5 use warnings;
6 use strict;
7 use Encode;
8 use IkiWiki 3.00;
9 use URI;
10
11 my %knownfeeds;
12 my %page_numfeeds;
13 my @inline;
14 my $nested=0;
15
16 sub import {
17         hook(type => "getopt", id => "inline", call => \&getopt);
18         hook(type => "getsetup", id => "inline", call => \&getsetup);
19         hook(type => "checkconfig", id => "inline", call => \&checkconfig);
20         hook(type => "sessioncgi", id => "inline", call => \&sessioncgi);
21         hook(type => "preprocess", id => "inline", 
22                 call => \&IkiWiki::preprocess_inline);
23         hook(type => "pagetemplate", id => "inline",
24                 call => \&IkiWiki::pagetemplate_inline);
25         hook(type => "format", id => "inline", call => \&format, first => 1);
26         # Hook to change to do pinging since it's called late.
27         # This ensures each page only pings once and prevents slow
28         # pings interrupting page builds.
29         hook(type => "change", id => "inline", call => \&IkiWiki::pingurl);
30 }
31
32 sub getopt () {
33         eval q{use Getopt::Long};
34         error($@) if $@;
35         Getopt::Long::Configure('pass_through');
36         GetOptions(
37                 "rss!" => \$config{rss},
38                 "atom!" => \$config{atom},
39                 "allowrss!" => \$config{allowrss},
40                 "allowatom!" => \$config{allowatom},
41                 "pingurl=s" => sub {
42                         push @{$config{pingurl}}, $_[1];
43                 },      
44         );
45 }
46
47 sub getsetup () {
48         return
49                 plugin => {
50                         safe => 1,
51                         rebuild => undef,
52                 },
53                 rss => {
54                         type => "boolean",
55                         example => 0,
56                         description => "enable rss feeds by default?",
57                         safe => 1,
58                         rebuild => 1,
59                 },
60                 atom => {
61                         type => "boolean",
62                         example => 0,
63                         description => "enable atom feeds by default?",
64                         safe => 1,
65                         rebuild => 1,
66                 },
67                 allowrss => {
68                         type => "boolean",
69                         example => 0,
70                         description => "allow rss feeds to be used?",
71                         safe => 1,
72                         rebuild => 1,
73                 },
74                 allowatom => {
75                         type => "boolean",
76                         example => 0,
77                         description => "allow atom feeds to be used?",
78                         safe => 1,
79                         rebuild => 1,
80                 },
81                 pingurl => {
82                         type => "string",
83                         example => "http://rpc.technorati.com/rpc/ping",
84                         description => "urls to ping (using XML-RPC) on feed update",
85                         safe => 1,
86                         rebuild => 0,
87                 },
88 }
89
90 sub checkconfig () {
91         if (($config{rss} || $config{atom}) && ! length $config{url}) {
92                 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
93         }
94         if ($config{rss}) {
95                 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
96         }
97         if ($config{atom}) {
98                 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
99         }
100         if (! exists $config{pingurl}) {
101                 $config{pingurl}=[];
102         }
103 }
104
105 sub format (@) {
106         my %params=@_;
107
108         # Fill in the inline content generated earlier. This is actually an
109         # optimisation.
110         $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
111                 delete @inline[$1,]
112         }eg;
113         return $params{content};
114 }
115
116 sub sessioncgi ($$) {
117         my $q=shift;
118         my $session=shift;
119
120         if ($q->param('do') eq 'blog') {
121                 my $page=titlepage(decode_utf8($q->param('title')));
122                 $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
123                 # if the page already exists, munge it to be unique
124                 my $from=$q->param('from');
125                 my $add="";
126                 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
127                         $add=1 unless length $add;
128                         $add++;
129                 }
130                 $q->param('page', $page.$add);
131                 # now go create the page
132                 $q->param('do', 'create');
133                 # make sure the editpage plugin in loaded
134                 if (IkiWiki->can("cgi_editpage")) {
135                         IkiWiki::cgi_editpage($q, $session);
136                 }
137                 else {
138                         error(gettext("page editing not allowed"));
139                 }
140                 exit;
141         }
142 }
143
144 # Back to ikiwiki namespace for the rest, this code is very much
145 # internal to ikiwiki even though it's separated into a plugin.
146 package IkiWiki;
147
148 my %toping;
149 my %feedlinks;
150
151 sub preprocess_inline (@) {
152         my %params=@_;
153         
154         if (! exists $params{pages} && ! exists $params{pagenames}) {
155                 error gettext("missing pages parameter");
156         }
157         my $raw=yesno($params{raw});
158         my $archive=yesno($params{archive});
159         my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
160         my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
161         my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
162         my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
163         my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
164         my $feedonly=yesno($params{feedonly});
165         if (! exists $params{show} && ! $archive) {
166                 $params{show}=10;
167         }
168         if (! exists $params{feedshow} && exists $params{show}) {
169                 $params{feedshow}=$params{show};
170         }
171         my $desc;
172         if (exists $params{description}) {
173                 $desc = $params{description} 
174         }
175         else {
176                 $desc = $config{wikiname};
177         }
178         my $actions=yesno($params{actions});
179         if (exists $params{template}) {
180                 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
181         }
182         else {
183                 $params{template} = $archive ? "archivepage" : "inlinepage";
184         }
185
186         my @list;
187
188         if (exists $params{pagenames}) {
189
190                 foreach my $p (qw(sort pages)) {
191                         if (exists $params{$p}) {
192                                 error sprintf(gettext("the %s and %s parameters cannot be used together"),
193                                         "pagenames", $p);
194                         }
195                 }
196
197                 @list = split ' ', $params{pagenames};
198                 my $_;
199                 @list = map { bestlink($params{page}, $_) } @list;
200
201                 $params{pages} = join(" or ", @list);
202         }
203         else {
204                 @list = pagespec_match_list(
205                         [ grep { $_ ne $params{page} } keys %pagesources ],
206                         $params{pages}, location => $params{page});
207
208                 if (exists $params{sort} && $params{sort} eq 'title') {
209                         @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
210                 }
211                 elsif (exists $params{sort} && $params{sort} eq 'title_natural') {
212                         eval q{use Sort::Naturally};
213                         if ($@) {
214                                 error(gettext("Sort::Naturally needed for title_natural sort"));
215                         }
216                         @list=sort { Sort::Naturally::ncmp(pagetitle(basename($a)), pagetitle(basename($b))) } @list;
217                 }
218                 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
219                         @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
220                 }
221                 elsif (! exists $params{sort} || $params{sort} eq 'age') {
222                         @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
223                 }
224                 else {
225                         error sprintf(gettext("unknown sort type %s"), $params{sort});
226                 }
227         }
228
229         if (yesno($params{reverse})) {
230                 @list=reverse(@list);
231         }
232
233         if (exists $params{skip}) {
234                 @list=@list[$params{skip} .. scalar @list - 1];
235         }
236         
237         my @feedlist;
238         if ($feeds) {
239                 if (exists $params{feedshow} &&
240                     $params{feedshow} && @list > $params{feedshow}) {
241                         @feedlist=@list[0..$params{feedshow} - 1];
242                 }
243                 else {
244                         @feedlist=@list;
245                 }
246         }
247         
248         if ($params{show} && @list > $params{show}) {
249                 @list=@list[0..$params{show} - 1];
250         }
251
252         add_depends($params{page}, $params{pages});
253         # Explicitly add all currently displayed pages as dependencies, so
254         # that if they are removed or otherwise changed, the inline will be
255         # sure to be updated.
256         add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
257         
258         if ($feeds && exists $params{feedpages}) {
259                 @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
260         }
261
262         my ($feedbase, $feednum);
263         if ($feeds) {
264                 # Ensure that multiple feeds on a page go to unique files.
265                 
266                 # Feedfile can lead to conflicts if usedirs is not enabled,
267                 # so avoid supporting it in that case.
268                 delete $params{feedfile} if ! $config{usedirs};
269                 # Tight limits on legal feedfiles, to avoid security issues
270                 # and conflicts.
271                 if (defined $params{feedfile}) {
272                         if ($params{feedfile} =~ /\// ||
273                             $params{feedfile} !~ /$config{wiki_file_regexp}/) {
274                                 error("illegal feedfile");
275                         }
276                         $params{feedfile}=possibly_foolish_untaint($params{feedfile});
277                 }
278                 $feedbase=targetpage($params{destpage}, "", $params{feedfile});
279
280                 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
281                 if (exists $knownfeeds{$feedid}) {
282                         $feednum=$knownfeeds{$feedid};
283                 }
284                 else {
285                         if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
286                                 if ($feeds) {
287                                         $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
288                                 }
289                         }
290                         else {
291                                 $feednum=$knownfeeds{$feedid}="";
292                                 if ($feeds) {
293                                         $page_numfeeds{$params{destpage}}{$feedbase}=1;
294                                 }
295                         }
296                 }
297         }
298
299         my $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $rss;
300         my $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $atom;
301
302         my $ret="";
303
304         if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
305             (exists $params{postform} && yesno($params{postform}))) &&
306             IkiWiki->can("cgi_editpage")) {
307                 # Add a blog post form, with feed buttons.
308                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
309                 $formtemplate->param(cgiurl => $config{cgiurl});
310                 my $rootpage;
311                 if (exists $params{rootpage}) {
312                         $rootpage=bestlink($params{page}, $params{rootpage});
313                         if (!length $rootpage) {
314                                 $rootpage=$params{rootpage};
315                         }
316                 }
317                 else {
318                         $rootpage=$params{page};
319                 }
320                 $formtemplate->param(rootpage => $rootpage);
321                 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
322                 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
323                 if (exists $params{postformtext}) {
324                         $formtemplate->param(postformtext =>
325                                 $params{postformtext});
326                 }
327                 else {
328                         $formtemplate->param(postformtext =>
329                                 gettext("Add a new post titled:"));
330                 }
331                 $ret.=$formtemplate->output;
332                 
333                 # The post form includes the feed buttons, so
334                 # emptyfeeds cannot be hidden.
335                 $emptyfeeds=1;
336         }
337         elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
338                 # Add feed buttons.
339                 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
340                 $linktemplate->param(rssurl => $rssurl) if $rss;
341                 $linktemplate->param(atomurl => $atomurl) if $atom;
342                 $ret.=$linktemplate->output;
343         }
344         
345         if (! $feedonly) {
346                 require HTML::Template;
347                 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
348                 if (! @params) {
349                         error sprintf(gettext("nonexistant template %s"), $params{template});
350                 }
351                 my $template=HTML::Template->new(@params) unless $raw;
352         
353                 foreach my $page (@list) {
354                         my $file = $pagesources{$page};
355                         my $type = pagetype($file);
356                         if (! $raw || ($raw && ! defined $type)) {
357                                 unless ($archive && $quick) {
358                                         # Get the content before populating the
359                                         # template, since getting the content uses
360                                         # the same template if inlines are nested.
361                                         my $content=get_inline_content($page, $params{destpage});
362                                         $template->param(content => $content);
363                                 }
364                                 $template->param(pageurl => urlto($page, $params{destpage}));
365                                 $template->param(inlinepage => $page);
366                                 $template->param(title => pagetitle(basename($page)));
367                                 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
368                                 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
369                                 $template->param(first => 1) if $page eq $list[0];
370                                 $template->param(last => 1) if $page eq $list[$#list];
371         
372                                 if ($actions) {
373                                         my $file = $pagesources{$page};
374                                         my $type = pagetype($file);
375                                         if ($config{discussion}) {
376                                                 my $discussionlink=lc(gettext("Discussion"));
377                                                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
378                                                     (length $config{cgiurl} ||
379                                                      exists $links{$page."/".$discussionlink})) {
380                                                         $template->param(have_actions => 1);
381                                                         $template->param(discussionlink =>
382                                                                 htmllink($page,
383                                                                         $params{destpage},
384                                                                         gettext("Discussion"),
385                                                                         noimageinline => 1,
386                                                                         forcesubpage => 1));
387                                                 }
388                                         }
389                                         if (length $config{cgiurl} && defined $type) {
390                                                 $template->param(have_actions => 1);
391                                                 $template->param(editurl => cgiurl(do => "edit", page => $page));
392                                         }
393                                 }
394         
395                                 run_hooks(pagetemplate => sub {
396                                         shift->(page => $page, destpage => $params{destpage},
397                                                 template => $template,);
398                                 });
399         
400                                 $ret.=$template->output;
401                                 $template->clear_params;
402                         }
403                         else {
404                                 if (defined $type) {
405                                         $ret.="\n".
406                                               linkify($page, $params{destpage},
407                                               preprocess($page, $params{destpage},
408                                               filter($page, $params{destpage},
409                                               readfile(srcfile($file)))));
410                                 }
411                         }
412                 }
413         }
414         
415         if ($feeds && ($emptyfeeds || @feedlist)) {
416                 if ($rss) {
417                         my $rssp=$feedbase."rss".$feednum;
418                         will_render($params{destpage}, $rssp);
419                         if (! $params{preview}) {
420                                 writefile($rssp, $config{destdir},
421                                         genfeed("rss",
422                                                 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
423                                 $toping{$params{destpage}}=1 unless $config{rebuild};
424                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
425                         }
426                 }
427                 if ($atom) {
428                         my $atomp=$feedbase."atom".$feednum;
429                         will_render($params{destpage}, $atomp);
430                         if (! $params{preview}) {
431                                 writefile($atomp, $config{destdir},
432                                         genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
433                                 $toping{$params{destpage}}=1 unless $config{rebuild};
434                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
435                         }
436                 }
437         }
438         
439         return $ret if $raw || $nested;
440         push @inline, $ret;
441         return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
442 }
443
444 sub pagetemplate_inline (@) {
445         my %params=@_;
446         my $page=$params{page};
447         my $template=$params{template};
448
449         $template->param(feedlinks => $feedlinks{$page})
450                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
451 }
452
453 sub get_inline_content ($$) {
454         my $page=shift;
455         my $destpage=shift;
456         
457         my $file=$pagesources{$page};
458         my $type=pagetype($file);
459         if (defined $type) {
460                 $nested++;
461                 my $ret=htmlize($page, $destpage, $type,
462                        linkify($page, $destpage,
463                        preprocess($page, $destpage,
464                        filter($page, $destpage,
465                        readfile(srcfile($file))))));
466                 $nested--;
467                 return $ret;
468         }
469         else {
470                 return "";
471         }
472 }
473
474 sub date_822 ($) {
475         my $time=shift;
476
477         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
478         POSIX::setlocale(&POSIX::LC_TIME, "C");
479         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
480         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
481         return $ret;
482 }
483
484 sub date_3339 ($) {
485         my $time=shift;
486
487         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
488         POSIX::setlocale(&POSIX::LC_TIME, "C");
489         my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
490         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
491         return $ret;
492 }
493
494 sub absolute_urls ($$) {
495         # sucky sub because rss sucks
496         my $content=shift;
497         my $baseurl=shift;
498
499         my $url=$baseurl;
500         $url=~s/[^\/]+$//;
501
502         # what is the non path part of the url?
503         my $top_uri = URI->new($url);
504         $top_uri->path_query(""); # reset the path
505         my $urltop = $top_uri->as_string;
506
507         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
508         # relative to another wiki page
509         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
510         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
511         # relative to the top of the site
512         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
513         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
514         return $content;
515 }
516
517 sub genfeed ($$$$$@) {
518         my $feedtype=shift;
519         my $feedurl=shift;
520         my $feeddesc=shift;
521         my $guid=shift;
522         my $page=shift;
523         my @pages=@_;
524         
525         my $url=URI->new(encode_utf8(urlto($page,"",1)));
526         
527         my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
528         my $content="";
529         my $lasttime = 0;
530         foreach my $p (@pages) {
531                 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
532                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
533
534                 $itemtemplate->param(
535                         title => pagetitle(basename($p)),
536                         url => $u,
537                         permalink => $u,
538                         cdate_822 => date_822($pagectime{$p}),
539                         mdate_822 => date_822($pagemtime{$p}),
540                         cdate_3339 => date_3339($pagectime{$p}),
541                         mdate_3339 => date_3339($pagemtime{$p}),
542                 );
543
544                 if (exists $pagestate{$p}) {
545                         if (exists $pagestate{$p}{meta}{guid}) {
546                                 $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
547                         }
548
549                         if (exists $pagestate{$p}{meta}{updated}) {
550                                 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
551                                 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
552                         }
553                 }
554
555                 if ($itemtemplate->query(name => "enclosure")) {
556                         my $file=$pagesources{$p};
557                         my $type=pagetype($file);
558                         if (defined $type) {
559                                 $itemtemplate->param(content => $pcontent);
560                         }
561                         else {
562                                 my $size=(srcfile_stat($file))[8];
563                                 my $mime="unknown";
564                                 eval q{use File::MimeInfo};
565                                 if (! $@) {
566                                         $mime = mimetype($file);
567                                 }
568                                 $itemtemplate->param(
569                                         enclosure => $u,
570                                         type => $mime,
571                                         length => $size,
572                                 );
573                         }
574                 }
575                 else {
576                         $itemtemplate->param(content => $pcontent);
577                 }
578
579                 run_hooks(pagetemplate => sub {
580                         shift->(page => $p, destpage => $page,
581                                 template => $itemtemplate);
582                 });
583
584                 $content.=$itemtemplate->output;
585                 $itemtemplate->clear_params;
586
587                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
588         }
589
590         my $template=template($feedtype."page.tmpl", blind_cache => 1);
591         $template->param(
592                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
593                 wikiname => $config{wikiname},
594                 pageurl => $url,
595                 content => $content,
596                 feeddesc => $feeddesc,
597                 guid => $guid,
598                 feeddate => date_3339($lasttime),
599                 feedurl => $feedurl,
600                 version => $IkiWiki::version,
601         );
602         run_hooks(pagetemplate => sub {
603                 shift->(page => $page, destpage => $page,
604                         template => $template);
605         });
606         
607         return $template->output;
608 }
609
610 sub pingurl (@) {
611         return unless @{$config{pingurl}} && %toping;
612
613         eval q{require RPC::XML::Client};
614         if ($@) {
615                 debug(gettext("RPC::XML::Client not found, not pinging"));
616                 return;
617         }
618
619         # daemonize here so slow pings don't slow down wiki updates
620         defined(my $pid = fork) or error("Can't fork: $!");
621         return if $pid;
622         chdir '/';
623         POSIX::setsid() or error("Can't start a new session: $!");
624         open STDIN, '/dev/null';
625         open STDOUT, '>/dev/null';
626         open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
627
628         # Don't need to keep a lock on the wiki as a daemon.
629         IkiWiki::unlockwiki();
630
631         foreach my $page (keys %toping) {
632                 my $title=pagetitle(basename($page), 0);
633                 my $url=urlto($page, "", 1);
634                 foreach my $pingurl (@{$config{pingurl}}) {
635                         debug("Pinging $pingurl for $page");
636                         eval {
637                                 my $client = RPC::XML::Client->new($pingurl);
638                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
639                                         $title, $url);
640                                 my $res = $client->send_request($req);
641                                 if (! ref $res) {
642                                         error("Did not receive response to ping");
643                                 }
644                                 my $r=$res->value;
645                                 if (! exists $r->{flerror} || $r->{flerror}) {
646                                         error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
647                                 }
648                         };
649                         if ($@) {
650                                 error "Ping failed: $@";
651                         }
652                 }
653         }
654
655         exit 0; # daemon done
656 }
657
658 1