summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 4f3b76db3dcb09850c607436632b1887af4f19c6 (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. my $commentopenid;
  138. if (defined $params{username}) {
  139. $commentuser = $params{username};
  140. my $oiduser = eval { IkiWiki::openiduser($commentuser) };
  141. if (defined $oiduser) {
  142. # looks like an OpenID
  143. $commentauthorurl = $commentuser;
  144. $commentauthor = $oiduser;
  145. $commentopenid = $commentuser;
  146. }
  147. else {
  148. $commentauthorurl = IkiWiki::cgiurl(
  149. do => 'commenter',
  150. page => (length $config{userdir}
  151. ? "$config{userdir}/$commentuser"
  152. : "$commentuser"));
  153. $commentauthor = $commentuser;
  154. }
  155. }
  156. else {
  157. if (defined $params{ip}) {
  158. $commentip = $params{ip};
  159. }
  160. $commentauthor = gettext("Anonymous");
  161. }
  162. $pagestate{$page}{comments}{commentuser} = $commentuser;
  163. $pagestate{$page}{comments}{commentopenid} = $commentopenid;
  164. $pagestate{$page}{comments}{commentip} = $commentip;
  165. $pagestate{$page}{comments}{commentauthor} = $commentauthor;
  166. $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
  167. if (! defined $pagestate{$page}{meta}{author}) {
  168. $pagestate{$page}{meta}{author} = $commentauthor;
  169. }
  170. if (! defined $pagestate{$page}{meta}{authorurl}) {
  171. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  172. }
  173. if ($config{comments_allowauthor}) {
  174. if (defined $params{claimedauthor}) {
  175. $pagestate{$page}{meta}{author} = $params{claimedauthor};
  176. }
  177. if (defined $params{url} and safeurl($params{url})) {
  178. $pagestate{$page}{meta}{authorurl} = $params{url};
  179. }
  180. }
  181. else {
  182. $pagestate{$page}{meta}{author} = $commentauthor;
  183. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  184. }
  185. if (defined $params{subject}) {
  186. $pagestate{$page}{meta}{title} = $params{subject};
  187. }
  188. if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
  189. $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
  190. "#".$params{page};
  191. }
  192. eval q{use Date::Parse};
  193. if (! $@) {
  194. my $time = str2time($params{date});
  195. $IkiWiki::pagectime{$page} = $time if defined $time;
  196. }
  197. return $content;
  198. }
  199. # This is exactly the same as recentchanges_link :-(
  200. sub linkcgi ($) {
  201. my $cgi=shift;
  202. if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
  203. my $page=decode_utf8($cgi->param("page"));
  204. if (! defined $page) {
  205. error("missing page parameter");
  206. }
  207. IkiWiki::loadindex();
  208. my $link=bestlink("", $page);
  209. if (! length $link) {
  210. print "Content-type: text/html\n\n";
  211. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  212. "<p>".
  213. sprintf(gettext("The page %s does not exist."),
  214. htmllink("", "", $page)).
  215. "</p>");
  216. }
  217. else {
  218. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  219. }
  220. exit;
  221. }
  222. }
  223. # Mostly cargo-culted from IkiWiki::plugin::editpage
  224. sub sessioncgi ($$) {
  225. my $cgi=shift;
  226. my $session=shift;
  227. my $do = $cgi->param('do');
  228. return unless $do eq 'comment';
  229. IkiWiki::decode_cgi_utf8($cgi);
  230. eval q{use CGI::FormBuilder};
  231. error($@) if $@;
  232. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  233. my $form = CGI::FormBuilder->new(
  234. fields => [qw{do sid page subject editcontent type author url}],
  235. charset => 'utf-8',
  236. method => 'POST',
  237. required => [qw{editcontent}],
  238. javascript => 0,
  239. params => $cgi,
  240. action => $config{cgiurl},
  241. header => 0,
  242. table => 0,
  243. template => scalar IkiWiki::template_params('editcomment.tmpl'),
  244. );
  245. IkiWiki::decode_form_utf8($form);
  246. IkiWiki::run_hooks(formbuilder_setup => sub {
  247. shift->(title => "comment", form => $form, cgi => $cgi,
  248. session => $session, buttons => \@buttons);
  249. });
  250. IkiWiki::decode_form_utf8($form);
  251. my $type = $form->param('type');
  252. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  253. $type = IkiWiki::possibly_foolish_untaint($type);
  254. }
  255. else {
  256. $type = $config{default_pageext};
  257. }
  258. my @page_types;
  259. if (exists $IkiWiki::hooks{htmlize}) {
  260. @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
  261. }
  262. $form->field(name => 'do', type => 'hidden');
  263. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  264. force => 1);
  265. $form->field(name => 'page', type => 'hidden');
  266. $form->field(name => 'subject', type => 'text', size => 72);
  267. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  268. $form->field(name => "type", value => $type, force => 1,
  269. type => 'select', options => \@page_types);
  270. $form->tmpl_param(username => $session->param('name'));
  271. if ($config{comments_allowauthor} and
  272. ! defined $session->param('name')) {
  273. $form->tmpl_param(allowauthor => 1);
  274. $form->field(name => 'author', type => 'text', size => '40');
  275. $form->field(name => 'url', type => 'text', size => '40');
  276. }
  277. else {
  278. $form->tmpl_param(allowauthor => 0);
  279. $form->field(name => 'author', type => 'hidden', value => '',
  280. force => 1);
  281. $form->field(name => 'url', type => 'hidden', value => '',
  282. force => 1);
  283. }
  284. # The untaint is OK (as in editpage) because we're about to pass
  285. # it to file_pruned anyway
  286. my $page = $form->field('page');
  287. $page = IkiWiki::possibly_foolish_untaint($page);
  288. if (! defined $page || ! length $page ||
  289. IkiWiki::file_pruned($page, $config{srcdir})) {
  290. error(gettext("bad page name"));
  291. }
  292. # FIXME: is this right? Or should we be using the candidate subpage
  293. # (whatever that might mean) as the base URL?
  294. my $baseurl = urlto($page, undef, 1);
  295. $form->title(sprintf(gettext("commenting on %s"),
  296. IkiWiki::pagetitle($page)));
  297. $form->tmpl_param('helponformattinglink',
  298. htmllink($page, $page, 'ikiwiki/formatting',
  299. noimageinline => 1,
  300. linktext => 'FormattingHelp'),
  301. allowdirectives => $config{allow_directives});
  302. if ($form->submitted eq CANCEL) {
  303. # bounce back to the page they wanted to comment on, and exit.
  304. # CANCEL need not be considered in future
  305. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  306. exit;
  307. }
  308. if (not exists $pagesources{$page}) {
  309. error(sprintf(gettext(
  310. "page '%s' doesn't exist, so you can't comment"),
  311. $page));
  312. }
  313. if (pagespec_match($page, $config{comments_closed_pagespec},
  314. location => $page)) {
  315. error(sprintf(gettext(
  316. "comments on page '%s' are closed"),
  317. $page));
  318. }
  319. # Set a flag to indicate that we're posting a comment,
  320. # so that postcomment() can tell it should match.
  321. $postcomment=1;
  322. IkiWiki::check_canedit($page, $cgi, $session);
  323. $postcomment=0;
  324. # FIXME: rather a simplistic way to make the comments...
  325. my $i = 0;
  326. my $file;
  327. my $location;
  328. do {
  329. $i++;
  330. $location = "$page/$config{comments_pagename}$i";
  331. } while (-e "$config{srcdir}/$location._comment");
  332. my $content = "[[!_comment format=$type\n";
  333. # FIXME: handling of double quotes probably wrong?
  334. if (defined $session->param('name')) {
  335. my $username = $session->param('name');
  336. $username =~ s/"/&quot;/g;
  337. $content .= " username=\"$username\"\n";
  338. }
  339. elsif (defined $ENV{REMOTE_ADDR}) {
  340. my $ip = $ENV{REMOTE_ADDR};
  341. if ($ip =~ m/^([.0-9]+)$/) {
  342. $content .= " ip=\"$1\"\n";
  343. }
  344. }
  345. if ($config{comments_allowauthor}) {
  346. my $author = $form->field('author');
  347. if (length $author) {
  348. $author =~ s/"/&quot;/g;
  349. $content .= " claimedauthor=\"$author\"\n";
  350. }
  351. my $url = $form->field('url');
  352. if (length $url) {
  353. $url =~ s/"/&quot;/g;
  354. $content .= " url=\"$url\"\n";
  355. }
  356. }
  357. my $subject = $form->field('subject');
  358. if (length $subject) {
  359. $subject =~ s/"/&quot;/g;
  360. $content .= " subject=\"$subject\"\n";
  361. }
  362. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  363. my $editcontent = $form->field('editcontent') || '';
  364. $editcontent =~ s/\r\n/\n/g;
  365. $editcontent =~ s/\r/\n/g;
  366. $editcontent =~ s/"/\\"/g;
  367. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  368. # This is essentially a simplified version of editpage:
  369. # - the user does not control the page that's created, only the parent
  370. # - it's always a create operation, never an edit
  371. # - this means that conflicts should never happen
  372. # - this means that if they do, rocks fall and everyone dies
  373. if ($form->submitted eq PREVIEW) {
  374. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  375. IkiWiki::linkify($page, $page,
  376. IkiWiki::preprocess($page, $page,
  377. IkiWiki::filter($location,
  378. $page, $content),
  379. 0, 1)));
  380. IkiWiki::run_hooks(format => sub {
  381. $preview = shift->(page => $page,
  382. content => $preview);
  383. });
  384. my $template = template("comment.tmpl");
  385. $template->param(content => $preview);
  386. $template->param(title => $form->field('subject'));
  387. $template->param(ctime => displaytime(time));
  388. $form->tmpl_param(page_preview => $template->output);
  389. }
  390. else {
  391. $form->tmpl_param(page_preview => "");
  392. }
  393. if ($form->submitted eq POST_COMMENT && $form->validate) {
  394. my $file = "$location._comment";
  395. IkiWiki::checksessionexpiry($cgi, $session);
  396. # FIXME: could probably do some sort of graceful retry
  397. # on error? Would require significant unwinding though
  398. writefile($file, $config{srcdir}, $content);
  399. my $conflict;
  400. if ($config{rcs} and $config{comments_commit}) {
  401. my $message = gettext("Added a comment");
  402. if (defined $form->field('subject') &&
  403. length $form->field('subject')) {
  404. $message = sprintf(
  405. gettext("Added a comment: %s"),
  406. $form->field('subject'));
  407. }
  408. IkiWiki::rcs_add($file);
  409. IkiWiki::disable_commit_hook();
  410. $conflict = IkiWiki::rcs_commit_staged($message,
  411. $session->param('name'), $ENV{REMOTE_ADDR});
  412. IkiWiki::enable_commit_hook();
  413. IkiWiki::rcs_update();
  414. }
  415. # Now we need a refresh
  416. require IkiWiki::Render;
  417. IkiWiki::refresh();
  418. IkiWiki::saveindex();
  419. # this should never happen, unless a committer deliberately
  420. # breaks it or something
  421. error($conflict) if defined $conflict;
  422. # Jump to the new comment on the page.
  423. IkiWiki::redirect($cgi, urlto($page, undef, 1)."#$location");
  424. }
  425. else {
  426. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  427. forcebaseurl => $baseurl);
  428. }
  429. exit;
  430. }
  431. sub commentsshown ($) {
  432. my $page=shift;
  433. return ! pagespec_match($page, "*/$config{comments_pagename}*",
  434. location => $page) &&
  435. pagespec_match($page, $config{comments_pagespec},
  436. location => $page);
  437. }
  438. sub commentsopen ($) {
  439. my $page = shift;
  440. return length $config{cgiurl} > 0 &&
  441. (! length $config{comments_closed_pagespec} ||
  442. ! pagespec_match($page, $config{comments_closed_pagespec},
  443. location => $page));
  444. }
  445. sub pagetemplate (@) {
  446. my %params = @_;
  447. my $page = $params{page};
  448. my $template = $params{template};
  449. my $shown = ($template->query(name => 'commentslink') ||
  450. $template->query(name => 'comments')) &&
  451. commentsshown($page);
  452. if ($template->query(name => 'comments')) {
  453. my $comments = undef;
  454. if ($shown) {
  455. $comments = IkiWiki::preprocess_inline(
  456. pages => "internal($page/$config{comments_pagename}*)",
  457. template => 'comment',
  458. show => 0,
  459. reverse => 'yes',
  460. page => $page,
  461. destpage => $params{destpage},
  462. feedfile => 'comments',
  463. emptyfeeds => 'no',
  464. );
  465. }
  466. if (defined $comments && length $comments) {
  467. $template->param(comments => $comments);
  468. }
  469. if ($shown && commentsopen($page)) {
  470. my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
  471. page => $page);
  472. $template->param(addcommenturl => $addcommenturl);
  473. }
  474. }
  475. if ($template->query(name => 'commentslink')) {
  476. # XXX Would be nice to say how many comments there are in
  477. # the link. But, to update the number, blog pages
  478. # would have to update whenever comments of any inlines
  479. # page are added, which is not currently done.
  480. if ($shown) {
  481. $template->param(commentslink =>
  482. htmllink($page, $params{destpage}, $page,
  483. linktext => gettext("Comments"),
  484. anchor => "comments",
  485. noimageinline => 1));
  486. }
  487. }
  488. if ($template->query(name => 'commentuser')) {
  489. $template->param(commentuser =>
  490. $pagestate{$page}{comments}{commentuser});
  491. }
  492. if ($template->query(name => 'commentopenid')) {
  493. $template->param(commentopenid =>
  494. $pagestate{$page}{comments}{commentopenid});
  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