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