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