aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/moderatedcomments.pm
blob: 5957833fc2de44dc20484f616f8813ff0306400b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl
package IkiWiki::Plugin::moderatedcomments;

use warnings;
use strict;
use IkiWiki 3.00;

sub import {
	hook(type => "getsetup", id => "moderatedcomments",  call => \&getsetup);
	hook(type => "checkcontent", id => "moderatedcomments", call => \&checkcontent);
}

sub getsetup () {
	return
		plugin => {
			safe => 1,
			rebuild => 0,
			section => "auth",
		},
		moderate_pagespec => {
			type => 'pagespec',
			example => '*',
			description => 'PageSpec matching users or comment locations to moderate',
			link => 'ikiwiki/PageSpec',
			safe => 1,
			rebuild => 0,
		},
}

sub checkcontent (@) {
	my %params=@_;
	
	# only handle comments	
	return undef unless pagespec_match($params{page}, "postcomment(*)",
	                	location => $params{page});
	
	# backwards compatability
	if (exists $config{moderate_users} &&
	    ! exists $config{moderate_pagespec}) {
		$config{moderate_pagespec} = $config{moderate_users}
			? "!admin()"
			: "!user(*)";
	}

	# default is to moderate all except admins
	if (! exists $config{moderate_pagespec}) {
		$config{moderate_pagespec}="!admin()";
	}

	my $session=$params{session};
	my $user=$session->param("name");
	if (pagespec_match($params{page}, $config{moderate_pagespec},
			location => $params{page},
			(defined $user ? (user => $user) : ()),
			(defined $session->remote_addr() ? (ip => $session->remote_addr()) : ()),
	)) {
		return gettext("comment needs moderation");
	}
	else {
		return undef;
	}
}

1