-
Notifications
You must be signed in to change notification settings - Fork 8
/
moderatord
executable file
·172 lines (146 loc) · 4.7 KB
/
moderatord
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/perl -w
###############################################################################
# moderatord - the moderation "daemon". Calculates moderation information and
# assigns results to users
#
# Copyright (C) 1997 Rob "CmdrTaco" Malda
# malda@slashdot.org
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
# $Id$
###############################################################################
use strict;
use vars '%I';
use Slash;
*I = getSlashConf();
sub moderatordLog {
local *LOG;
my $dir = "$I{datadir}/logs";
open LOG, ">>$dir/moderatord.log"
or die "Can't append to $dir/moderatord.log: $!";
print LOG localtime() . "\t", join("\t", @_), "\n";
close LOG;
}
sub tokens2points {
my $c = sqlSelectMany("uid,tokens", "users_info", "tokens >= $I{maxtokens}");
$I{dbh}->do("LOCK TABLES users READ,
users_info WRITE,
users_comments WRITE");
while (my($uid, $tokens) = $c->fetchrow) {
moderatordLog("Giving $I{maxtokens}/$I{tokensperpoint} " .
($I{maxtokens}/$I{tokensperpoint}) . " to $uid");
sqlUpdate("users_info", {
-lastgranted => 'now()',
-tokens => "tokens - $I{maxtokens}"
}, "uid=$uid");
sqlUpdate("users_comments", {
-points => "points +" . ($I{maxtokens} / $I{tokensperpoint})
}, "uid=$uid");
}
$c->finish;
$c = sqlSelectMany("users.uid as uid", "users,users_comments,users_info",
"karma > -1 AND
points > 5 AND
seclev < 100 AND
users.uid=users_comments.uid AND
users.uid=users_info.uid");
$I{dbh}->do("UNLOCK TABLES");
$I{dbh}->do("LOCK TABLES users_comments WRITE");
while (my($uid) = $c->fetchrow) {
sqlUpdate("users_comments", { points => 5 } ,"uid=$uid");
}
$I{dbh}->do("UNLOCK TABLES");
}
sub stirpool {
my $c = sqlSelectMany("points,users.uid as uid",
"users,users_comments,users_info",
"users.uid=users_comments.uid AND
users.uid=users_info.uid AND
seclev = 0
AND points > 0
AND to_days(now())-to_days(lastgranted) > $I{stir}");
my $revoked = 0;
$I{dbh}->do("LOCK TABLES users_comments WRITE");
while (my($p, $u) = $c->fetchrow) {
moderatordLog("Taking $p points from $u");
$revoked += $p;
sqlUpdate("users_comments", { points => '0' }, "uid=$u");
}
$I{dbh}->do("UNLOCK TABLES");
$c->finish;
return 0;
# return $revoked;
}
sub comments2tokens {
my $comments = shift;
my $tokenpool = $comments * $I{tokenspercomment};
moderatordLog("$comments becomes $tokenpool tokens.");
my $stirredpoints = stirpool();
$tokenpool += $stirredpoints * $I{tokensperpoint};
moderatordLog(
($stirredpoints ? "Stirred out $stirredpoints points, " : '')
. "So the total token Pool=$tokenpool");
my($totalusers) = sqlSelect("max(uid)", "users_info");
my $eligibleusers = int($totalusers / 5) * 4;
moderatordLog("First $eligibleusers accounts available");
my $c=sqlSelectMany("users_info.uid,count(*) as c",
"users_info,users_prefs, accesslog",
"users_info.uid < $eligibleusers
AND users_info.uid=accesslog.uid
AND users_info.uid=users_prefs.uid
AND (op='article' or op='comments')
AND willing=1
AND karma >= 0
GROUP BY users_info.uid
ORDER BY c");
my $eligible = $c->rows;
my($uid, $cnt);
while ((($uid,$cnt) = $c->fetchrow) && ($cnt < 4)) {
$eligible--;
}
my($st, $fi) = (int($eligible / 6), int($eligible / 8) * 7);
my $x;
moderatordLog("Start at $st end at $fi. $eligible left. First score is $cnt");
my @eligibles;
while (($x++ < $fi) && (($uid, $cnt) = $c->fetchrow)) {
next if $x < $st;
push @eligibles, $uid;
# moderatordLog("$uid is eligible");
}
$c->finish;
my @scores;
for (my $x = 0; $x < $tokenpool; $x++) {
$scores[$eligibles[rand @eligibles]]++;
}
$I{dbh}->do("LOCK TABLES users_info WRITE");
foreach $uid (@eligibles) {
next unless $scores[$uid];
# moderatordLog("$uid: ".$scores[$uid].", ");
sqlUpdate("users_info",{
-tokens => "tokens+" . $scores[$uid]
}, "uid=$uid");
}
$I{dbh}->do("UNLOCK TABLES");
}
my($tc, $lc) = getvars("totalComments", "lastComments");
my $newcomments = $tc - $lc;
#$newcomments+=1000;
if ($newcomments > 0) {
setvar("lastComments", $tc);
comments2tokens($newcomments);
tokens2points();
}