forked from DamianZaremba/davinci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cb_functions.php
184 lines (142 loc) · 3.58 KB
/
cb_functions.php
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
173
174
175
176
177
178
179
180
181
182
183
184
<?php
// vim: noet
require "libirc.php";
### Misc utilities
function strescape($str) {
return addcslashes($str, "\x00..\x1F\x7F..\xFF\\");
}
### Logging
function _log_open() {
global $log_fh;
if (!isset($log_fh))
$log_fh = fopen("points.log", "a");
return $log_fh;
}
function log_str($str) {
$time = time();
$fh = _log_open();
fprintf($fh, "%d %s\n", $time, $str);
printf("%s %s\n", date("c", $time), $str);
}
function log_strv(/*$fmt, @args*/) {
$args = func_get_args();
$fmt = array_shift($args);
foreach ($args as &$v)
if ($v === null)
$v = "-";
return log_str(vsprintf($fmt, $args));
}
### User database (high-level)
function user_is_admin($nick) {
global $users;
$nick = nicktolower($nick);
return (bool) @$users[$nick]['admin'];
}
function user_make_admin($caller, $nick) {
global $users;
$nick = nicktolower($nick);
@$users[$nick]['admin'] = true;
save_db();
log_strv("%s grant:admin %s", $caller, $nick);
}
function user_is_ignored($nick) {
global $users;
$nick = nicktolower($nick);
return (bool) @$users[$nick]['ignore'];
}
function user_set_ignored($caller, $nick, $ignore) {
global $users;
$nick = nicktolower($nick);
@$users[$nick]["ignore"] = $ignore;
log_strv("%s ignore %s ignored=%s points=%d",
$caller, $nick, $ignore ? "y" : "n", @$users[$nick]["points"]);
}
function user_get_stats($nick) {
global $users;
$nick = nicktolower($nick);
if (is_array(@$users[$nick]["log"])) {
$stats = "";
foreach (@$users[$nick]["log"] as $reason => $count)
$stats .= "$reason: $count. ";
return rtrim($stats);
} else {
return "No stats.";
}
}
function user_get_points($nick) {
global $users;
$nick = nicktolower($nick);
return (int) @$users[$nick]["points"];
}
function user_adj_points($nick, $delta, $reason) {
return user_adj_points_by(null, $nick, $delta, $reason);
}
function user_adj_points_by($caller, $nick, $delta, $reason) {
global $users;
$nick = nicktolower($nick);
if (@$users[$nick]["ignore"])
return;
@$users[$nick]["points"] += $delta;
@$users[$nick]["log"][$reason]++;
save_db();
log_strv("%s change %s %s%d \"%s\"",
@$caller, $nick,
($delta < 0 ? "" : "+"), $delta,
strescape($reason));
if ($caller !== null) {
$do_log = true;
$reason .= " by $caller";
} elseif ($delta >= 0) {
$do_log = @$users[$nick]["verbose"]
&& !@$users[$nick]["vdedo"];
} else {
$do_log = @$users[$nick]["verbose"];
}
if ($do_log)
send("NOTICE", $nick, "$reason ($delta points)");
return $delta;
}
function user_reset_points($caller, $nick) {
global $users;
$nick = nicktolower($nick);
$old = @$users[$nick];
unset($users[$nick]);
save_db();
log_strv("%s reset %s oldpoints=%d",
$caller, $nick, @$old["points"]);
}
function user_merge($caller, $old_user, $new_user) {
global $users;
$old_user = nicktolower($old_user);
$new_user = nicktolower($new_user);
$old_points = user_get_points($old_user);
user_adj_points_by($caller, $new_user, $old_points, "Administratively changed");
user_reset_points($caller, $old_user);
save_db();
log_strv("%s merge %s %s %s", $caller, $old_user, $new_user, $old_points);
}
function gettop($bottom=false) {
global $users;
foreach ($users as $nick => $data) {
if (isset($data["points"]))
$tmp[$nick] = $data["points"];
}
arsort($tmp);
if ($bottom)
return array_slice($tmp, -3);
else
return array_slice($tmp, 0, 3);
}
### Database operations
const DB_FILE = 'users.db';
function get_db() {
$ret = unserialize(file_get_contents(DB_FILE));
return $ret;
}
function save_db() {
global $users;
global $locked;
if($locked) { return; }
file_put_contents(DB_FILE,serialize($users));
}
?>