summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/smcvpostcomment.pm
blob: 22958c84aa900f40c28e99da4901c2e0c095ac49 (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::smcvpostcomment;
  7. use warnings;
  8. use strict;
  9. use IkiWiki 2.00;
  10. use constant PLUGIN => "smcvpostcomment";
  11. use constant PREVIEW => "Preview";
  12. use constant POST_COMMENT => "Post comment";
  13. use constant CANCEL => "Cancel";
  14. sub import { #{{{
  15. hook(type => "getsetup", id => PLUGIN, call => \&getsetup);
  16. hook(type => "preprocess", id => PLUGIN, call => \&preprocess);
  17. hook(type => "sessioncgi", id => PLUGIN, call => \&sessioncgi);
  18. hook(type => "htmlize", id => "_".PLUGIN,
  19. call => \&IkiWiki::Plugin::mdwn::htmlize);
  20. IkiWiki::loadplugin("inline");
  21. IkiWiki::loadplugin("mdwn");
  22. } # }}}
  23. sub htmlize { # {{{
  24. eval { use IkiWiki::Plugin::mdwn; };
  25. error($@) if ($@);
  26. return IkiWiki::Plugin::mdwn::htmlize(@_)
  27. } # }}}
  28. sub getsetup () { #{{{
  29. return
  30. plugin => {
  31. safe => 1,
  32. rebuild => undef,
  33. },
  34. } #}}}
  35. # Somewhat based on IkiWiki::Plugin::inline blog posting support
  36. sub preprocess (@) { #{{{
  37. my %params=@_;
  38. unless (length $config{cgiurl}) {
  39. error(sprintf (gettext("[[!%s plugin requires CGI enabled]]"),
  40. PLUGIN));
  41. }
  42. my $page = $params{page};
  43. $pagestate{$page}{PLUGIN()}{comments} = defined $params{closed}
  44. ? (not IkiWiki::yesno($params{closed}))
  45. : 1;
  46. $pagestate{$page}{PLUGIN()}{allowhtml} = IkiWiki::yesno($params{allowhtml});
  47. $pagestate{$page}{PLUGIN()}{allowdirectives} = IkiWiki::yesno($params{allowdirectives});
  48. $pagestate{$page}{PLUGIN()}{commit} = defined $params{commit}
  49. ? IkiWiki::yesno($params{commit})
  50. : 1;
  51. my $formtemplate = IkiWiki::template(PLUGIN . "_embed.tmpl",
  52. blind_cache => 1);
  53. $formtemplate->param(cgiurl => $config{cgiurl});
  54. $formtemplate->param(page => $params{page});
  55. if (not $pagestate{$page}{PLUGIN()}{comments}) {
  56. $formtemplate->param("disabled" =>
  57. gettext('comments are closed'));
  58. }
  59. elsif ($params{preview}) {
  60. $formtemplate->param("disabled" =>
  61. gettext('not available during Preview'));
  62. }
  63. debug("page $params{page} => destpage $params{destpage}");
  64. my $posts = '';
  65. unless (defined $params{inline} && !IkiWiki::yesno($params{inline})) {
  66. eval { use IkiWiki::Plugin::inline; };
  67. error($@) if ($@);
  68. my @args = (
  69. pages => "internal($params{page}/_comment_*)",
  70. template => PLUGIN . "_display",
  71. show => 0,
  72. reverse => "yes",
  73. # special stuff passed through
  74. page => $params{page},
  75. destpage => $params{destpage},
  76. preview => $params{preview},
  77. );
  78. push @args, atom => $params{atom} if defined $params{atom};
  79. push @args, rss => $params{rss} if defined $params{rss};
  80. push @args, feeds => $params{feeds} if defined $params{feeds};
  81. push @args, feedshow => $params{feedshow} if defined $params{feedshow};
  82. push @args, timeformat => $params{timeformat} if defined $params{timeformat};
  83. push @args, feedonly => $params{feedonly} if defined $params{feedonly};
  84. $posts = "\n" . IkiWiki::preprocess_inline(@args);
  85. }
  86. return $formtemplate->output . $posts;
  87. } # }}}
  88. # FIXME: logic taken from editpage, should be common code?
  89. sub getcgiuser ($) { # {{{
  90. my $session = shift;
  91. my $user = $session->param('name');
  92. $user = $ENV{REMOTE_ADDR} unless defined $user;
  93. debug("getcgiuser() -> $user");
  94. return $user;
  95. } # }}}
  96. # FIXME: logic adapted from recentchanges, should be common code?
  97. sub linkuser ($) { # {{{
  98. my $user = shift;
  99. my $oiduser = eval { IkiWiki::openiduser($user) };
  100. if (defined $oiduser) {
  101. return ($user, $oiduser);
  102. }
  103. else {
  104. my $page = bestlink('', (length $config{userdir}
  105. ? "$config{userdir}/"
  106. : "").$user);
  107. return (urlto($page, undef, 1), $user);
  108. }
  109. } # }}}
  110. # FIXME: taken from IkiWiki::Plugin::editpage, should be common?
  111. sub checksessionexpiry ($$) { # {{{
  112. my $session = shift;
  113. my $sid = shift;
  114. if (defined $session->param("name")) {
  115. if (! defined $sid || $sid ne $session->id) {
  116. error(gettext("Your login session has expired."));
  117. }
  118. }
  119. } # }}}
  120. # Mostly cargo-culted from IkiWiki::plugin::editpage
  121. sub sessioncgi ($$) { #{{{
  122. my $cgi=shift;
  123. my $session=shift;
  124. my $do = $cgi->param('do');
  125. return unless $do eq PLUGIN;
  126. IkiWiki::decode_cgi_utf8($cgi);
  127. eval q{use CGI::FormBuilder};
  128. error($@) if $@;
  129. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  130. my $form = CGI::FormBuilder->new(
  131. fields => [qw{do sid page subject body}],
  132. charset => 'utf-8',
  133. method => 'POST',
  134. required => [qw{body}],
  135. javascript => 0,
  136. params => $cgi,
  137. action => $config{cgiurl},
  138. header => 0,
  139. table => 0,
  140. template => scalar IkiWiki::template_params(PLUGIN . '_form.tmpl'),
  141. # wtf does this do in editpage?
  142. wikiname => $config{wikiname},
  143. );
  144. IkiWiki::decode_form_utf8($form);
  145. IkiWiki::run_hooks(formbuilder_setup => sub {
  146. shift->(title => PLUGIN, form => $form, cgi => $cgi,
  147. session => $session, buttons => \@buttons);
  148. });
  149. IkiWiki::decode_form_utf8($form);
  150. $form->field(name => 'do', type => 'hidden');
  151. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  152. force => 1);
  153. $form->field(name => 'page', type => 'hidden');
  154. $form->field(name => 'subject', type => 'text', size => 72);
  155. $form->field(name => 'body', type => 'textarea', rows => 5,
  156. cols => 80);
  157. # The untaint is OK (as in editpage) because we're about to pass
  158. # it to file_pruned anyway
  159. my $page = $form->field('page');
  160. $page = IkiWiki::possibly_foolish_untaint($page);
  161. if (!defined $page || !length $page ||
  162. IkiWiki::file_pruned($page, $config{srcdir})) {
  163. error(gettext("bad page name"));
  164. }
  165. my $allow_directives = $pagestate{$page}{PLUGIN()}{allowdirectives};
  166. my $allow_html = $pagestate{$page}{PLUGIN()}{allowdirectives};
  167. my $commit_comments = defined $pagestate{$page}{PLUGIN()}{commit}
  168. ? $pagestate{$page}{PLUGIN()}{commit}
  169. : 1;
  170. # FIXME: is this right? Or should we be using the candidate subpage
  171. # (whatever that might mean) as the base URL?
  172. my $baseurl = urlto($page, undef, 1);
  173. $form->title(sprintf(gettext("commenting on %s"),
  174. IkiWiki::pagetitle($page)));
  175. $form->tmpl_param('helponformattinglink',
  176. htmllink($page, $page, 'ikiwiki/formatting',
  177. noimageinline => 1,
  178. linktext => 'FormattingHelp'),
  179. allowhtml => $allow_html,
  180. allowdirectives => $allow_directives);
  181. if (not exists $pagesources{$page}) {
  182. error(sprintf(gettext(
  183. "page '%s' doesn't exist, so you can't comment"),
  184. $page));
  185. }
  186. if (not $pagestate{$page}{PLUGIN()}{comments}) {
  187. error(sprintf(gettext(
  188. "comments are not enabled on page '%s'"),
  189. $page));
  190. }
  191. if ($form->submitted eq CANCEL) {
  192. # bounce back to the page they wanted to comment on, and exit.
  193. # CANCEL need not be considered in future
  194. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  195. exit;
  196. }
  197. IkiWiki::check_canedit($page . "[" . PLUGIN . "]", $cgi, $session);
  198. my ($authorurl, $author) = linkuser(getcgiuser($session));
  199. my $body = $form->field('body') || '';
  200. $body =~ s/\r\n/\n/g;
  201. $body =~ s/\r/\n/g;
  202. $body = "\n" if $body !~ /\n$/;
  203. unless ($allow_directives) {
  204. # don't allow new-style directives at all
  205. $body =~ s/(^|[^\\])\[\[!/$1\\[[!/g;
  206. # don't allow [[ unless it begins an old-style
  207. # wikilink, if prefix_directives is off
  208. $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1\\[[!/g
  209. unless $config{prefix_directives};
  210. }
  211. unless ($allow_html) {
  212. $body =~ s/&(\w|#)/&amp;$1/g;
  213. $body =~ s/</&lt;/g;
  214. $body =~ s/>/&gt;/g;
  215. }
  216. # In this template, the [[!meta]] directives should stay at the end,
  217. # so that they will override anything the user specifies. (For
  218. # instance, [[!meta author="I can fake the author"]]...)
  219. my $content_tmpl = template(PLUGIN . '_comment.tmpl');
  220. $content_tmpl->param(author => $author);
  221. $content_tmpl->param(authorurl => $authorurl);
  222. $content_tmpl->param(subject => $form->field('subject'));
  223. $content_tmpl->param(body => $body);
  224. my $content = $content_tmpl->output;
  225. # This is essentially a simplified version of editpage:
  226. # - the user does not control the page that's created, only the parent
  227. # - it's always a create operation, never an edit
  228. # - this means that conflicts should never happen
  229. # - this means that if they do, rocks fall and everyone dies
  230. if ($form->submitted eq PREVIEW) {
  231. # $fake is a location that has the same number of slashes
  232. # as the eventual location of this comment.
  233. my $fake = "$page/_" . PLUGIN . "hypothetical";
  234. my $preview = IkiWiki::htmlize($fake, $page, 'mdwn',
  235. IkiWiki::linkify($page, $page,
  236. IkiWiki::preprocess($page, $page,
  237. IkiWiki::filter($fake, $page,
  238. $content),
  239. 0, 1)));
  240. IkiWiki::run_hooks(format => sub {
  241. $preview = shift->(page => $page,
  242. content => $preview);
  243. });
  244. my $template = template(PLUGIN . "_display.tmpl");
  245. $template->param(content => $preview);
  246. $template->param(title => $form->field('subject'));
  247. $template->param(ctime => displaytime(time));
  248. $template->param(author => $author);
  249. $template->param(authorurl => $authorurl);
  250. $form->tmpl_param(page_preview => $template->output);
  251. }
  252. else {
  253. $form->tmpl_param(page_preview => "");
  254. }
  255. if ($form->submitted eq POST_COMMENT && $form->validate) {
  256. # Let's get posting. We don't check_canedit here because
  257. # that somewhat defeats the point of this plugin.
  258. checksessionexpiry($session, $cgi->param('sid'));
  259. # FIXME: check that the wiki is locked right now, because
  260. # if it's not, there are mad race conditions!
  261. # FIXME: rather a simplistic way to make the comments...
  262. my $i = 0;
  263. my $file;
  264. do {
  265. $i++;
  266. $file = "$page/_comment_${i}._" . PLUGIN;
  267. } while (-e "$config{srcdir}/$file");
  268. # FIXME: could probably do some sort of graceful retry
  269. # if I could be bothered
  270. writefile($file, $config{srcdir}, $content);
  271. my $conflict;
  272. if ($config{rcs} and $commit_comments) {
  273. my $message = gettext("Added a comment");
  274. if (defined $form->field('subject') &&
  275. length $form->field('subject')) {
  276. $message .= ": ".$form->field('subject');
  277. }
  278. IkiWiki::rcs_add($file);
  279. IkiWiki::disable_commit_hook();
  280. $conflict = IkiWiki::rcs_commit_staged($message,
  281. $session->param('name'), $ENV{REMOTE_ADDR});
  282. IkiWiki::enable_commit_hook();
  283. IkiWiki::rcs_update();
  284. }
  285. # Now we need a refresh
  286. require IkiWiki::Render;
  287. IkiWiki::refresh();
  288. IkiWiki::saveindex();
  289. # this should never happen, unless a committer deliberately
  290. # breaks it or something
  291. error($conflict) if defined $conflict;
  292. # Bounce back to where we were, but defeat broken caches
  293. my $anticache = "?updated=$page/_comment_$i";
  294. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  295. }
  296. else {
  297. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  298. forcebaseurl => $baseurl);
  299. }
  300. exit;
  301. } #}}}
  302. package IkiWiki::PageSpec;
  303. sub match_smcvpostcomment ($$;@) {
  304. my $page = shift;
  305. my $glob = shift;
  306. unless ($page =~ s/\[smcvpostcomment\]$//) {
  307. return IkiWiki::FailReason->new("not posting a comment");
  308. }
  309. return match_glob($page, $glob);
  310. }
  311. 1