summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 50c048fee2d757aaf3cebe09c3a8a037e15a9c6f (plain)
  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. use warnings;
  8. use strict;
  9. use IkiWiki 2.00;
  10. use Encode;
  11. use POSIX qw(strftime);
  12. use constant PREVIEW => "Preview";
  13. use constant POST_COMMENT => "Post comment";
  14. use constant CANCEL => "Cancel";
  15. my $postcomment;
  16. sub import {
  17. hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
  18. hook(type => "getsetup", id => 'comments', call => \&getsetup);
  19. hook(type => "preprocess", id => '_comment', call => \&preprocess);
  20. hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
  21. hook(type => "htmlize", id => "_comment", call => \&htmlize);
  22. hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
  23. hook(type => "cgi", id => "comments", call => \&linkcgi);
  24. IkiWiki::loadplugin("inline");
  25. }
  26. sub getsetup () {
  27. return
  28. plugin => {
  29. safe => 1,
  30. rebuild => 1,
  31. },
  32. comments_pagespec => {
  33. type => 'pagespec',
  34. example => 'blog/* and !*/Discussion',
  35. description => 'PageSpec of pages where comments are allowed',
  36. link => 'ikiwiki/PageSpec',
  37. safe => 1,
  38. rebuild => 1,
  39. },
  40. comments_closed_pagespec => {
  41. type => 'pagespec',
  42. example => 'blog/controversial or blog/flamewar',
  43. description => 'PageSpec of pages where posting new comments is not allowed',
  44. link => 'ikiwiki/PageSpec',
  45. safe => 1,
  46. rebuild => 1,
  47. },
  48. comments_pagename => {
  49. type => 'string',
  50. default => 'comment_',
  51. description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
  52. safe => 0, # manual page moving required
  53. rebuild => undef,
  54. },
  55. comments_allowdirectives => {
  56. type => 'boolean',
  57. example => 0,
  58. description => 'Interpret directives in comments?',
  59. safe => 1,
  60. rebuild => 0,
  61. },
  62. comments_allowauthor => {
  63. type => 'boolean',
  64. example => 0,
  65. description => 'Allow anonymous commenters to set an author name?',
  66. safe => 1,
  67. rebuild => 0,
  68. },
  69. comments_commit => {
  70. type => 'boolean',
  71. example => 1,
  72. description => 'commit comments to the VCS',
  73. # old uncommitted comments are likely to cause
  74. # confusion if this is changed
  75. safe => 0,
  76. rebuild => 0,
  77. },
  78. }
  79. sub checkconfig () {
  80. $config{comments_commit} = 1
  81. unless defined $config{comments_commit};
  82. $config{comments_pagespec} = ''
  83. unless defined $config{comments_pagespec};
  84. $config{comments_closed_pagespec} = ''
  85. unless defined $config{comments_closed_pagespec};
  86. $config{comments_pagename} = 'comment_'
  87. unless defined $config{comments_pagename};
  88. }
  89. sub htmlize {
  90. my %params = @_;
  91. return $params{content};
  92. }
  93. # FIXME: copied verbatim from meta
  94. sub safeurl ($) {
  95. my $url=shift;
  96. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  97. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  98. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  99. }
  100. else {
  101. return 1;
  102. }
  103. }
  104. sub preprocess {
  105. my %params = @_;
  106. my $page = $params{page};
  107. my $format = $params{format};
  108. if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
  109. error(sprintf(gettext("unsupported page format %s"), $format));
  110. }
  111. my $content = $params{content};
  112. if (! defined $content) {
  113. error(gettext("comment must have content"));
  114. }
  115. $content =~ s/\\"/"/g;
  116. $content = IkiWiki::filter($page, $params{destpage}, $content);
  117. if ($config{comments_allowdirectives}) {
  118. $content = IkiWiki::preprocess($page, $params{destpage},
  119. $content);
  120. }
  121. # no need to bother with htmlize if it's just HTML
  122. $content = IkiWiki::htmlize($page, $params{destpage}, $format,
  123. $content) if defined $format;
  124. IkiWiki::run_hooks(sanitize => sub {
  125. $content = shift->(
  126. page => $page,
  127. destpage => $params{destpage},
  128. content => $content,
  129. );
  130. });
  131. # set metadata, possibly overriding [[!meta]] directives from the
  132. # comment itself
  133. my $commentuser;
  134. my $commentip;
  135. my $commentauthor;
  136. my $commentauthorurl;
  137. if (defined $params{username}) {
  138. $commentuser = $params{username};
  139. ($commentauthorurl, $commentauthor) =
  140. linkuser($params{username});
  141. }
  142. else {
  143. if (defined $params{ip}) {
  144. $commentip = $params{ip};
  145. }
  146. $commentauthor = gettext("Anonymous");
  147. }
  148. $pagestate{$page}{comments}{commentuser} = $commentuser;
  149. $pagestate{$page}{comments}{commentip} = $commentip;
  150. $pagestate{$page}{comments}{commentauthor} = $commentauthor;
  151. $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
  152. if (! defined $pagestate{$page}{meta}{author}) {
  153. $pagestate{$page}{meta}{author} = $commentauthor;
  154. }
  155. if (! defined $pagestate{$page}{meta}{authorurl}) {
  156. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  157. }
  158. if ($config{comments_allowauthor}) {
  159. if (defined $params{claimedauthor}) {
  160. $pagestate{$page}{meta}{author} = $params{claimedauthor};
  161. }
  162. if (defined $params{url} and safeurl($params{url})) {
  163. $pagestate{$page}{meta}{authorurl} = $params{url};
  164. }
  165. }
  166. else {
  167. $pagestate{$page}{meta}{author} = $commentauthor;
  168. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  169. }
  170. if (defined $params{subject}) {
  171. $pagestate{$page}{meta}{title} = $params{subject};
  172. }
  173. if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
  174. $pagestate{$page}{meta}{permalink} = urlto($params{destpage}, undef, 1).
  175. "#".$params{page};
  176. }
  177. eval q{use Date::Parse};
  178. if (! $@) {
  179. my $time = str2time($params{date});
  180. $IkiWiki::pagectime{$page} = $time if defined $time;
  181. }
  182. return $content;
  183. }
  184. # This is exactly the same as recentchanges_link :-(
  185. sub linkcgi ($) {
  186. my $cgi=shift;
  187. if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
  188. my $page=decode_utf8($cgi->param("page"));
  189. if (! defined $page) {
  190. error("missing page parameter");
  191. }
  192. IkiWiki::loadindex();
  193. my $link=bestlink("", $page);
  194. if (! length $link) {
  195. print "Content-type: text/html\n\n";
  196. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  197. "<p>".
  198. sprintf(gettext("The page %s does not exist."),
  199. htmllink("", "", $page)).
  200. "</p>");
  201. }
  202. else {
  203. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  204. }
  205. exit;
  206. }
  207. }
  208. # FIXME: basically the same logic as recentchanges
  209. # returns (author URL, pretty-printed version)
  210. sub linkuser ($) {
  211. my $user = shift;
  212. my $oiduser = eval { IkiWiki::openiduser($user) };
  213. if (defined $oiduser) {
  214. return ($user, $oiduser);
  215. }
  216. # FIXME: it'd be good to avoid having such a link for anonymous
  217. # posts
  218. else {
  219. return (IkiWiki::cgiurl(
  220. do => 'commenter',
  221. page => (length $config{userdir}
  222. ? "$config{userdir}/$user"
  223. : "$user")
  224. ), $user);
  225. }
  226. }
  227. # Mostly cargo-culted from IkiWiki::plugin::editpage
  228. sub sessioncgi ($$) {
  229. my $cgi=shift;
  230. my $session=shift;
  231. my $do = $cgi->param('do');
  232. return unless $do eq 'comment';
  233. IkiWiki::decode_cgi_utf8($cgi);
  234. eval q{use CGI::FormBuilder};
  235. error($@) if $@;
  236. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  237. my $form = CGI::FormBuilder->new(
  238. fields => [qw{do sid page subject editcontent type author url}],
  239. charset => 'utf-8',
  240. method => 'POST',
  241. required => [qw{editcontent}],
  242. javascript => 0,
  243. params => $cgi,
  244. action => $config{cgiurl},
  245. header => 0,
  246. table => 0,
  247. template => scalar IkiWiki::template_params('editcomment.tmpl'),
  248. );
  249. IkiWiki::decode_form_utf8($form);
  250. IkiWiki::run_hooks(formbuilder_setup => sub {
  251. shift->(title => "comment", form => $form, cgi => $cgi,
  252. session => $session, buttons => \@buttons);
  253. });
  254. IkiWiki::decode_form_utf8($form);
  255. my $type = $form->param('type');
  256. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  257. $type = IkiWiki::possibly_foolish_untaint($type);
  258. }
  259. else {
  260. $type = $config{default_pageext};
  261. }
  262. my @page_types;
  263. if (exists $IkiWiki::hooks{htmlize}) {
  264. @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
  265. }
  266. $form->field(name => 'do', type => 'hidden');
  267. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  268. force => 1);
  269. $form->field(name => 'page', type => 'hidden');
  270. $form->field(name => 'subject', type => 'text', size => 72);
  271. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  272. $form->field(name => "type", value => $type, force => 1,
  273. type => 'select', options => \@page_types);
  274. $form->tmpl_param(username => $session->param('name'));
  275. if ($config{comments_allowauthor} and
  276. ! defined $session->param('name')) {
  277. $form->tmpl_param(allowauthor => 1);
  278. $form->field(name => 'author', type => 'text', size => '40');
  279. $form->field(name => 'url', type => 'text', size => '40');
  280. }
  281. else {
  282. $form->tmpl_param(allowauthor => 0);
  283. $form->field(name => 'author', type => 'hidden', value => '',
  284. force => 1);
  285. $form->field(name => 'url', type => 'hidden', value => '',
  286. force => 1);
  287. }
  288. # The untaint is OK (as in editpage) because we're about to pass
  289. # it to file_pruned anyway
  290. my $page = $form->field('page');
  291. $page = IkiWiki::possibly_foolish_untaint($page);
  292. if (! defined $page || ! length $page ||
  293. IkiWiki::file_pruned($page, $config{srcdir})) {
  294. error(gettext("bad page name"));
  295. }
  296. # FIXME: is this right? Or should we be using the candidate subpage
  297. # (whatever that might mean) as the base URL?
  298. my $baseurl = urlto($page, undef, 1);
  299. $form->title(sprintf(gettext("commenting on %s"),
  300. IkiWiki::pagetitle($page)));
  301. $form->tmpl_param('helponformattinglink',
  302. htmllink($page, $page, 'ikiwiki/formatting',
  303. noimageinline => 1,
  304. linktext => 'FormattingHelp'),
  305. allowdirectives => $config{allow_directives});
  306. if ($form->submitted eq CANCEL) {
  307. # bounce back to the page they wanted to comment on, and exit.
  308. # CANCEL need not be considered in future
  309. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  310. exit;
  311. }
  312. if (not exists $pagesources{$page}) {
  313. error(sprintf(gettext(
  314. "page '%s' doesn't exist, so you can't comment"),
  315. $page));
  316. }
  317. if (pagespec_match($page, $config{comments_closed_pagespec},
  318. location => $page)) {
  319. error(sprintf(gettext(
  320. "comments on page '%s' are closed"),
  321. $page));
  322. }
  323. # Set a flag to indicate that we're posting a comment,
  324. # so that postcomment() can tell it should match.
  325. $postcomment=1;
  326. IkiWiki::check_canedit($page, $cgi, $session);
  327. $postcomment=0;
  328. # FIXME: rather a simplistic way to make the comments...
  329. my $i = 0;
  330. my $file;
  331. my $location;
  332. do {
  333. $i++;
  334. $location = "$page/$config{comments_pagename}$i";
  335. } while (-e "$config{srcdir}/$location._comment");
  336. my $content = "[[!_comment format=$type\n";
  337. # FIXME: handling of double quotes probably wrong?
  338. if (defined $session->param('name')) {
  339. my $username = $session->param('name');
  340. $username =~ s/"/&quot;/g;
  341. $content .= " username=\"$username\"\n";
  342. }
  343. elsif (defined $ENV{REMOTE_ADDR}) {
  344. my $ip = $ENV{REMOTE_ADDR};
  345. if ($ip =~ m/^([.0-9]+)$/) {
  346. $content .= " ip=\"$1\"\n";
  347. }
  348. }
  349. if ($config{comments_allowauthor}) {
  350. my $author = $form->field('author');
  351. if (length $author) {
  352. $author =~ s/"/&quot;/g;
  353. $content .= " claimedauthor=\"$author\"\n";
  354. }
  355. my $url = $form->field('url');
  356. if (length $url) {
  357. $url =~ s/"/&quot;/g;
  358. $content .= " url=\"$url\"\n";
  359. }
  360. }
  361. my $subject = $form->field('subject');
  362. if (length $subject) {
  363. $subject =~ s/"/&quot;/g;
  364. $content .= " subject=\"$subject\"\n";
  365. }
  366. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  367. my $editcontent = $form->field('editcontent') || '';
  368. $editcontent =~ s/\r\n/\n/g;
  369. $editcontent =~ s/\r/\n/g;
  370. $editcontent =~ s/"/\\"/g;
  371. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  372. # This is essentially a simplified version of editpage:
  373. # - the user does not control the page that's created, only the parent
  374. # - it's always a create operation, never an edit
  375. # - this means that conflicts should never happen
  376. # - this means that if they do, rocks fall and everyone dies
  377. if ($form->submitted eq PREVIEW) {
  378. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  379. IkiWiki::linkify($page, $page,
  380. IkiWiki::preprocess($page, $page,
  381. IkiWiki::filter($location,
  382. $page, $content),
  383. 0, 1)));
  384. IkiWiki::run_hooks(format => sub {
  385. $preview = shift->(page => $page,
  386. content => $preview);
  387. });
  388. my $template = template("comment.tmpl");
  389. $template->param(content => $preview);
  390. $template->param(title => $form->field('subject'));
  391. $template->param(ctime => displaytime(time));
  392. $form->tmpl_param(page_preview => $template->output);
  393. }
  394. else {
  395. $form->tmpl_param(page_preview => "");
  396. }
  397. if ($form->submitted eq POST_COMMENT && $form->validate) {
  398. my $file = "$location._comment";
  399. IkiWiki::checksessionexpiry($cgi, $session);
  400. # FIXME: could probably do some sort of graceful retry
  401. # on error? Would require significant unwinding though
  402. writefile($file, $config{srcdir}, $content);
  403. my $conflict;
  404. if ($config{rcs} and $config{comments_commit}) {
  405. my $message = gettext("Added a comment");
  406. if (defined $form->field('subject') &&
  407. length $form->field('subject')) {
  408. $message = sprintf(
  409. gettext("Added a comment: %s"),
  410. $form->field('subject'));
  411. }
  412. IkiWiki::rcs_add($file);
  413. IkiWiki::disable_commit_hook();
  414. $conflict = IkiWiki::rcs_commit_staged($message,
  415. $session->param('name'), $ENV{REMOTE_ADDR});
  416. IkiWiki::enable_commit_hook();
  417. IkiWiki::rcs_update();
  418. }
  419. # Now we need a refresh
  420. require IkiWiki::Render;
  421. IkiWiki::refresh();
  422. IkiWiki::saveindex();
  423. # this should never happen, unless a committer deliberately
  424. # breaks it or something
  425. error($conflict) if defined $conflict;
  426. # Jump to the new comment on the page.
  427. IkiWiki::redirect($cgi, urlto($page, undef, 1)."#$location");
  428. }
  429. else {
  430. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  431. forcebaseurl => $baseurl);
  432. }
  433. exit;
  434. }
  435. sub commentsshown ($) {
  436. my $page=shift;
  437. return ! pagespec_match($page, "*/$config{comments_pagename}*",
  438. location => $page) &&
  439. pagespec_match($page, $config{comments_pagespec},
  440. location => $page);
  441. }
  442. sub commentsopen ($) {
  443. my $page = shift;
  444. return length $config{cgiurl} > 0 &&
  445. (! length $config{comments_closed_pagespec} ||
  446. ! pagespec_match($page, $config{comments_closed_pagespec},
  447. location => $page));
  448. }
  449. sub pagetemplate (@) {
  450. my %params = @_;
  451. my $page = $params{page};
  452. my $template = $params{template};
  453. my $shown = ($template->query(name => 'commentslink') ||
  454. $template->query(name => 'comments')) &&
  455. commentsshown($page);
  456. if ($template->query(name => 'comments')) {
  457. my $comments = undef;
  458. if ($shown) {
  459. $comments = IkiWiki::preprocess_inline(
  460. pages => "internal($page/$config{comments_pagename}*)",
  461. template => 'comment',
  462. show => 0,
  463. reverse => 'yes',
  464. page => $page,
  465. destpage => $params{destpage},
  466. feedfile => 'comments',
  467. emptyfeeds => 'no',
  468. );
  469. }
  470. if (defined $comments && length $comments) {
  471. $template->param(comments => $comments);
  472. }
  473. if ($shown && commentsopen($page)) {
  474. my $commenturl = IkiWiki::cgiurl(do => 'comment',
  475. page => $page);
  476. $template->param(commenturl => $commenturl);
  477. }
  478. }
  479. if ($template->query(name => 'commentslink')) {
  480. # XXX Would be nice to say how many comments there are in
  481. # the link. But, to update the number, blog pages
  482. # would have to update whenever comments of any inlines
  483. # page are added, which is not currently done.
  484. if ($shown) {
  485. $template->param(commentslink =>
  486. htmllink($page, $params{destpage}, $page,
  487. linktext => gettext("Comments"),
  488. anchor => "comments",
  489. noimageinline => 1));
  490. }
  491. }
  492. if ($template->query(name => 'commentuser')) {
  493. $template->param(commentuser =>
  494. $pagestate{$page}{comments}{commentuser});
  495. }
  496. if ($template->query(name => 'commentip')) {
  497. $template->param(commentip =>
  498. $pagestate{$page}{comments}{commentip});
  499. }
  500. if ($template->query(name => 'commentauthor')) {
  501. $template->param(commentauthor =>
  502. $pagestate{$page}{comments}{commentauthor});
  503. }
  504. if ($template->query(name => 'commentauthorurl')) {
  505. $template->param(commentauthorurl =>
  506. $pagestate{$page}{comments}{commentauthorurl});
  507. }
  508. }
  509. package IkiWiki::PageSpec;
  510. sub match_postcomment ($$;@) {
  511. my $page = shift;
  512. my $glob = shift;
  513. if (! $postcomment) {
  514. return IkiWiki::FailReason->new("not posting a comment");
  515. }
  516. return match_glob($page, $glob);
  517. }
  518. 1