checkcontent: New hook, can be used to implement arbitrary content filters, including...
[ikiwiki] / IkiWiki / Plugin / comments.pm
1 #!/usr/bin/perl
2 # Copyright © 2006-2008 Joey Hess <joey@ikiwiki.info>
3 # Copyright © 2008 Simon McVittie <http://smcv.pseudorandom.co.uk/>
4 # Licensed under the GNU GPL, version 2, or any later version published by the
5 # Free Software Foundation
6 package IkiWiki::Plugin::comments;
7
8 use warnings;
9 use strict;
10 use IkiWiki 3.00;
11 use Encode;
12 use POSIX qw(strftime);
13
14 use constant PREVIEW => "Preview";
15 use constant POST_COMMENT => "Post comment";
16 use constant CANCEL => "Cancel";
17
18 my $postcomment;
19 my %commentstate;
20
21 sub import {
22         hook(type => "checkconfig", id => 'comments',  call => \&checkconfig);
23         hook(type => "getsetup", id => 'comments',  call => \&getsetup);
24         hook(type => "preprocess", id => '_comment', call => \&preprocess);
25         hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
26         hook(type => "htmlize", id => "_comment", call => \&htmlize);
27         hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
28         hook(type => "cgi", id => "comments", call => \&linkcgi);
29         IkiWiki::loadplugin("inline");
30 }
31
32 sub getsetup () {
33         return
34                 plugin => {
35                         safe => 1,
36                         rebuild => 1,
37                 },
38                 comments_pagespec => {
39                         type => 'pagespec',
40                         example => 'blog/* and !*/Discussion',
41                         description => 'PageSpec of pages where comments are allowed',
42                         link => 'ikiwiki/PageSpec',
43                         safe => 1,
44                         rebuild => 1,
45                 },
46                 comments_closed_pagespec => {
47                         type => 'pagespec',
48                         example => 'blog/controversial or blog/flamewar',
49                         description => 'PageSpec of pages where posting new comments is not allowed',
50                         link => 'ikiwiki/PageSpec',
51                         safe => 1,
52                         rebuild => 1,
53                 },
54                 comments_pagename => {
55                         type => 'string',
56                         default => 'comment_',
57                         description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
58                         safe => 0, # manual page moving required
59                         rebuild => undef,
60                 },
61                 comments_allowdirectives => {
62                         type => 'boolean',
63                         example => 0,
64                         description => 'Interpret directives in comments?',
65                         safe => 1,
66                         rebuild => 0,
67                 },
68                 comments_allowauthor => {
69                         type => 'boolean',
70                         example => 0,
71                         description => 'Allow anonymous commenters to set an author name?',
72                         safe => 1,
73                         rebuild => 0,
74                 },
75                 comments_commit => {
76                         type => 'boolean',
77                         example => 1,
78                         description => 'commit comments to the VCS',
79                         # old uncommitted comments are likely to cause
80                         # confusion if this is changed
81                         safe => 0,
82                         rebuild => 0,
83                 },
84 }
85
86 sub checkconfig () {
87         $config{comments_commit} = 1
88                 unless defined $config{comments_commit};
89         $config{comments_pagespec} = ''
90                 unless defined $config{comments_pagespec};
91         $config{comments_closed_pagespec} = ''
92                 unless defined $config{comments_closed_pagespec};
93         $config{comments_pagename} = 'comment_'
94                 unless defined $config{comments_pagename};
95 }
96
97 sub htmlize {
98         my %params = @_;
99         return $params{content};
100 }
101
102 # FIXME: copied verbatim from meta
103 sub safeurl ($) {
104         my $url=shift;
105         if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
106             defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
107                 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
108         }
109         else {
110                 return 1;
111         }
112 }
113
114 sub preprocess {
115         my %params = @_;
116         my $page = $params{page};
117
118         my $format = $params{format};
119         if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
120                 error(sprintf(gettext("unsupported page format %s"), $format));
121         }
122
123         my $content = $params{content};
124         if (! defined $content) {
125                 error(gettext("comment must have content"));
126         }
127         $content =~ s/\\"/"/g;
128
129         $content = IkiWiki::filter($page, $params{destpage}, $content);
130
131         if ($config{comments_allowdirectives}) {
132                 $content = IkiWiki::preprocess($page, $params{destpage},
133                         $content);
134         }
135
136         # no need to bother with htmlize if it's just HTML
137         $content = IkiWiki::htmlize($page, $params{destpage}, $format,
138                 $content) if defined $format;
139
140         IkiWiki::run_hooks(sanitize => sub {
141                 $content = shift->(
142                         page => $page,
143                         destpage => $params{destpage},
144                         content => $content,
145                 );
146         });
147
148         # set metadata, possibly overriding [[!meta]] directives from the
149         # comment itself
150
151         my $commentuser;
152         my $commentip;
153         my $commentauthor;
154         my $commentauthorurl;
155         my $commentopenid;
156         if (defined $params{username}) {
157                 $commentuser = $params{username};
158
159                 my $oiduser = eval { IkiWiki::openiduser($commentuser) };
160
161                 if (defined $oiduser) {
162                         # looks like an OpenID
163                         $commentauthorurl = $commentuser;
164                         $commentauthor = $oiduser;
165                         $commentopenid = $commentuser;
166                 }
167                 else {
168                         $commentauthorurl = IkiWiki::cgiurl(
169                                 do => 'commenter',
170                                 page => (length $config{userdir}
171                                         ? "$config{userdir}/$commentuser"
172                                         : "$commentuser"));
173
174                         $commentauthor = $commentuser;
175                 }
176         }
177         else {
178                 if (defined $params{ip}) {
179                         $commentip = $params{ip};
180                 }
181                 $commentauthor = gettext("Anonymous");
182         }
183
184         $commentstate{$page}{commentuser} = $commentuser;
185         $commentstate{$page}{commentopenid} = $commentopenid;
186         $commentstate{$page}{commentip} = $commentip;
187         $commentstate{$page}{commentauthor} = $commentauthor;
188         $commentstate{$page}{commentauthorurl} = $commentauthorurl;
189         if (! defined $pagestate{$page}{meta}{author}) {
190                 $pagestate{$page}{meta}{author} = $commentauthor;
191         }
192         if (! defined $pagestate{$page}{meta}{authorurl}) {
193                 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
194         }
195
196         if ($config{comments_allowauthor}) {
197                 if (defined $params{claimedauthor}) {
198                         $pagestate{$page}{meta}{author} = $params{claimedauthor};
199                 }
200
201                 if (defined $params{url}) {
202                         my $url=$params{url};
203
204                         eval q{use URI::Heuristic}; 
205                         if (! $@) {
206                                 $url=URI::Heuristic::uf_uristr($url);
207                         }
208
209                         if (safeurl($url)) {
210                                 $pagestate{$page}{meta}{authorurl} = $url;
211                         }
212                 }
213         }
214         else {
215                 $pagestate{$page}{meta}{author} = $commentauthor;
216                 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
217         }
218
219         if (defined $params{subject}) {
220                 $pagestate{$page}{meta}{title} = $params{subject};
221         }
222
223         if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
224                 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
225                         "#".$params{page};
226         }
227
228         eval q{use Date::Parse};
229         if (! $@) {
230                 my $time = str2time($params{date});
231                 $IkiWiki::pagectime{$page} = $time if defined $time;
232         }
233
234         return $content;
235 }
236
237 # This is exactly the same as recentchanges_link :-(
238 sub linkcgi ($) {
239         my $cgi=shift;
240         if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
241
242                 my $page=decode_utf8($cgi->param("page"));
243                 if (! defined $page) {
244                         error("missing page parameter");
245                 }
246
247                 IkiWiki::loadindex();
248
249                 my $link=bestlink("", $page);
250                 if (! length $link) {
251                         print "Content-type: text/html\n\n";
252                         print IkiWiki::misctemplate(gettext(gettext("missing page")),
253                                 "<p>".
254                                 sprintf(gettext("The page %s does not exist."),
255                                         htmllink("", "", $page)).
256                                 "</p>");
257                 }
258                 else {
259                         IkiWiki::redirect($cgi, urlto($link, undef, 1));
260                 }
261
262                 exit;
263         }
264 }
265
266 # Mostly cargo-culted from IkiWiki::plugin::editpage
267 sub sessioncgi ($$) {
268         my $cgi=shift;
269         my $session=shift;
270
271         my $do = $cgi->param('do');
272         return unless $do eq 'comment';
273
274         IkiWiki::decode_cgi_utf8($cgi);
275
276         eval q{use CGI::FormBuilder};
277         error($@) if $@;
278
279         my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
280         my $form = CGI::FormBuilder->new(
281                 fields => [qw{do sid page subject editcontent type author url}],
282                 charset => 'utf-8',
283                 method => 'POST',
284                 required => [qw{editcontent}],
285                 javascript => 0,
286                 params => $cgi,
287                 action => $config{cgiurl},
288                 header => 0,
289                 table => 0,
290                 template => scalar IkiWiki::template_params('editcomment.tmpl'),
291         );
292
293         IkiWiki::decode_form_utf8($form);
294         IkiWiki::run_hooks(formbuilder_setup => sub {
295                         shift->(title => "comment", form => $form, cgi => $cgi,
296                                 session => $session, buttons => \@buttons);
297                 });
298         IkiWiki::decode_form_utf8($form);
299
300         my $type = $form->param('type');
301         if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
302                 $type = IkiWiki::possibly_foolish_untaint($type);
303         }
304         else {
305                 $type = $config{default_pageext};
306         }
307         my @page_types;
308         if (exists $IkiWiki::hooks{htmlize}) {
309                 @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
310         }
311
312         $form->field(name => 'do', type => 'hidden');
313         $form->field(name => 'sid', type => 'hidden', value => $session->id,
314                 force => 1);
315         $form->field(name => 'page', type => 'hidden');
316         $form->field(name => 'subject', type => 'text', size => 72);
317         $form->field(name => 'editcontent', type => 'textarea', rows => 10);
318         $form->field(name => "type", value => $type, force => 1,
319                 type => 'select', options => \@page_types);
320
321         $form->tmpl_param(username => $session->param('name'));
322
323         if ($config{comments_allowauthor} and
324             ! defined $session->param('name')) {
325                 $form->tmpl_param(allowauthor => 1);
326                 $form->field(name => 'author', type => 'text', size => '40');
327                 $form->field(name => 'url', type => 'text', size => '40');
328         }
329         else {
330                 $form->tmpl_param(allowauthor => 0);
331                 $form->field(name => 'author', type => 'hidden', value => '',
332                         force => 1);
333                 $form->field(name => 'url', type => 'hidden', value => '',
334                         force => 1);
335         }
336
337         # The untaint is OK (as in editpage) because we're about to pass
338         # it to file_pruned anyway
339         my $page = $form->field('page');
340         $page = IkiWiki::possibly_foolish_untaint($page);
341         if (! defined $page || ! length $page ||
342                 IkiWiki::file_pruned($page, $config{srcdir})) {
343                 error(gettext("bad page name"));
344         }
345
346         my $baseurl = urlto($page, undef, 1);
347
348         $form->title(sprintf(gettext("commenting on %s"),
349                         IkiWiki::pagetitle($page)));
350
351         $form->tmpl_param('helponformattinglink',
352                 htmllink($page, $page, 'ikiwiki/formatting',
353                         noimageinline => 1,
354                         linktext => 'FormattingHelp'),
355                         allowdirectives => $config{allow_directives});
356
357         if ($form->submitted eq CANCEL) {
358                 # bounce back to the page they wanted to comment on, and exit.
359                 # CANCEL need not be considered in future
360                 IkiWiki::redirect($cgi, urlto($page, undef, 1));
361                 exit;
362         }
363
364         if (not exists $pagesources{$page}) {
365                 error(sprintf(gettext(
366                         "page '%s' doesn't exist, so you can't comment"),
367                         $page));
368         }
369
370         if (pagespec_match($page, $config{comments_closed_pagespec},
371                 location => $page)) {
372                 error(sprintf(gettext(
373                         "comments on page '%s' are closed"),
374                         $page));
375         }
376
377         # Set a flag to indicate that we're posting a comment,
378         # so that postcomment() can tell it should match.
379         $postcomment=1;
380         IkiWiki::check_canedit($page, $cgi, $session);
381         $postcomment=0;
382
383         # FIXME: rather a simplistic way to make the comments...
384         my $i = 0;
385         my $file;
386         my $location;
387         do {
388                 $i++;
389                 $location = "$page/$config{comments_pagename}$i";
390         } while (-e "$config{srcdir}/$location._comment");
391
392         my $content = "[[!_comment format=$type\n";
393
394         # FIXME: handling of double quotes probably wrong?
395         if (defined $session->param('name')) {
396                 my $username = $session->param('name');
397                 $username =~ s/"/&quot;/g;
398                 $content .= " username=\"$username\"\n";
399         }
400         elsif (defined $ENV{REMOTE_ADDR}) {
401                 my $ip = $ENV{REMOTE_ADDR};
402                 if ($ip =~ m/^([.0-9]+)$/) {
403                         $content .= " ip=\"$1\"\n";
404                 }
405         }
406
407         if ($config{comments_allowauthor}) {
408                 my $author = $form->field('author');
409                 if (length $author) {
410                         $author =~ s/"/&quot;/g;
411                         $content .= " claimedauthor=\"$author\"\n";
412                 }
413                 my $url = $form->field('url');
414                 if (length $url) {
415                         $url =~ s/"/&quot;/g;
416                         $content .= " url=\"$url\"\n";
417                 }
418         }
419
420         my $subject = $form->field('subject');
421         if (length $subject) {
422                 $subject =~ s/"/&quot;/g;
423                 $content .= " subject=\"$subject\"\n";
424         }
425
426         $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
427
428         my $editcontent = $form->field('editcontent') || '';
429         $editcontent =~ s/\r\n/\n/g;
430         $editcontent =~ s/\r/\n/g;
431         $editcontent =~ s/"/\\"/g;
432         $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
433
434         # This is essentially a simplified version of editpage:
435         # - the user does not control the page that's created, only the parent
436         # - it's always a create operation, never an edit
437         # - this means that conflicts should never happen
438         # - this means that if they do, rocks fall and everyone dies
439
440         if ($form->submitted eq PREVIEW) {
441                 my $preview = IkiWiki::htmlize($location, $page, '_comment',
442                                 IkiWiki::linkify($location, $page,
443                                         IkiWiki::preprocess($location, $page,
444                                                 IkiWiki::filter($location,
445                                                         $page, $content),
446                                                 0, 1)));
447                 IkiWiki::run_hooks(format => sub {
448                                 $preview = shift->(page => $page,
449                                         content => $preview);
450                         });
451
452                 my $template = template("comment.tmpl");
453                 $template->param(content => $preview);
454                 $template->param(title => $form->field('subject'));
455                 $template->param(ctime => displaytime(time));
456
457                 IkiWiki::run_hooks(pagetemplate => sub {
458                         shift->(page => $location,
459                                 destpage => $page,
460                                 template => $template);
461                 });
462
463                 $form->tmpl_param(page_preview => $template->output);
464         }
465         else {
466                 $form->tmpl_param(page_preview => "");
467         }
468
469         if ($form->submitted eq POST_COMMENT && $form->validate) {
470                 IkiWiki::checksessionexpiry($cgi, $session);
471                 
472                 IkiWiki::check_content(content => $form->field('editcontent'),
473                         subject => $form->field('subject'),
474                         $config{comments_allowauthor} ? (
475                                 author => $form->field('author'),
476                                 url => $form->field('url'),
477                         ) : (),
478                         page => $location,
479                         cgi => $cgi, session => $session);
480                 
481                 my $file = "$location._comment";
482
483                 # FIXME: could probably do some sort of graceful retry
484                 # on error? Would require significant unwinding though
485                 writefile($file, $config{srcdir}, $content);
486
487                 my $conflict;
488
489                 if ($config{rcs} and $config{comments_commit}) {
490                         my $message = gettext("Added a comment");
491                         if (defined $form->field('subject') &&
492                                 length $form->field('subject')) {
493                                 $message = sprintf(
494                                         gettext("Added a comment: %s"),
495                                         $form->field('subject'));
496                         }
497
498                         IkiWiki::rcs_add($file);
499                         IkiWiki::disable_commit_hook();
500                         $conflict = IkiWiki::rcs_commit_staged($message,
501                                 $session->param('name'), $ENV{REMOTE_ADDR});
502                         IkiWiki::enable_commit_hook();
503                         IkiWiki::rcs_update();
504                 }
505
506                 # Now we need a refresh
507                 require IkiWiki::Render;
508                 IkiWiki::refresh();
509                 IkiWiki::saveindex();
510
511                 # this should never happen, unless a committer deliberately
512                 # breaks it or something
513                 error($conflict) if defined $conflict;
514
515                 # Jump to the new comment on the page.
516                 # The trailing question mark tries to avoid broken
517                 # caches and get the most recent version of the page.
518                 IkiWiki::redirect($cgi, urlto($page, undef, 1)."?updated#$location");
519
520         }
521         else {
522                 IkiWiki::showform ($form, \@buttons, $session, $cgi,
523                         forcebaseurl => $baseurl);
524         }
525
526         exit;
527 }
528
529 sub commentsshown ($) {
530         my $page=shift;
531
532         return ! pagespec_match($page, "*/$config{comments_pagename}*",
533                                 location => $page) &&
534                pagespec_match($page, $config{comments_pagespec},
535                               location => $page);
536 }
537
538 sub commentsopen ($) {
539         my $page = shift;
540
541         return length $config{cgiurl} > 0 &&
542                (! length $config{comments_closed_pagespec} ||
543                 ! pagespec_match($page, $config{comments_closed_pagespec},
544                                  location => $page));
545 }
546
547 sub pagetemplate (@) {
548         my %params = @_;
549
550         my $page = $params{page};
551         my $template = $params{template};
552         my $shown = ($template->query(name => 'commentslink') ||
553                      $template->query(name => 'commentsurl') ||
554                      $template->query(name => 'atomcommentsurl') ||
555                      $template->query(name => 'comments')) &&
556                     commentsshown($page);
557
558         if ($template->query(name => 'comments')) {
559                 my $comments = undef;
560                 if ($shown) {
561                         $comments = IkiWiki::preprocess_inline(
562                                 pages => "internal($page/$config{comments_pagename}*)",
563                                 template => 'comment',
564                                 show => 0,
565                                 reverse => 'yes',
566                                 page => $page,
567                                 destpage => $params{destpage},
568                                 feedfile => 'comments',
569                                 emptyfeeds => 'no',
570                         );
571                 }
572
573                 if (defined $comments && length $comments) {
574                         $template->param(comments => $comments);
575                 }
576
577                 if ($shown && commentsopen($page)) {
578                         my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
579                                 page => $page);
580                         $template->param(addcommenturl => $addcommenturl);
581                 }
582         }
583
584         if ($template->query(name => 'commentsurl')) {
585                 if ($shown) {
586                         $template->param(commentsurl =>
587                                 urlto($page, undef, 1).'#comments');
588                 }
589         }
590
591         if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
592                 if ($shown) {
593                         # This will 404 until there are some comments, but I
594                         # think that's probably OK...
595                         $template->param(atomcommentsurl =>
596                                 urlto($page, undef, 1).'comments.atom');
597                 }
598         }
599
600         if ($template->query(name => 'commentslink')) {
601                 # XXX Would be nice to say how many comments there are in
602                 # the link. But, to update the number, blog pages
603                 # would have to update whenever comments of any inlines
604                 # page are added, which is not currently done.
605                 if ($shown) {
606                         $template->param(commentslink =>
607                                 htmllink($page, $params{destpage}, $page,
608                                         linktext => gettext("Comments"),
609                                         anchor => "comments",
610                                         noimageinline => 1));
611                 }
612         }
613
614         # everything below this point is only relevant to the comments
615         # themselves
616         if (!exists $commentstate{$page}) {
617                 return;
618         }
619
620         if ($template->query(name => 'commentuser')) {
621                 $template->param(commentuser =>
622                         $commentstate{$page}{commentuser});
623         }
624
625         if ($template->query(name => 'commentopenid')) {
626                 $template->param(commentopenid =>
627                         $commentstate{$page}{commentopenid});
628         }
629
630         if ($template->query(name => 'commentip')) {
631                 $template->param(commentip =>
632                         $commentstate{$page}{commentip});
633         }
634
635         if ($template->query(name => 'commentauthor')) {
636                 $template->param(commentauthor =>
637                         $commentstate{$page}{commentauthor});
638         }
639
640         if ($template->query(name => 'commentauthorurl')) {
641                 $template->param(commentauthorurl =>
642                         $commentstate{$page}{commentauthorurl});
643         }
644
645         if ($template->query(name => 'removeurl') &&
646             IkiWiki::Plugin::remove->can("check_canremove") &&
647             length $config{cgiurl}) {
648                 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
649                         page => $page));
650                 $template->param(have_actions => 1);
651         }
652 }
653
654 package IkiWiki::PageSpec;
655
656 sub match_postcomment ($$;@) {
657         my $page = shift;
658         my $glob = shift;
659
660         if (! $postcomment) {
661                 return IkiWiki::FailReason->new("not posting a comment");
662         }
663         return match_glob($page, $glob);
664 }
665
666 1