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