-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkarma.bmod
167 lines (149 loc) · 3.35 KB
/
karma.bmod
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
#!/usr/bin/env bash
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, June 1991.
#
# 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, see <https://www.gnu.org/licenses/gpl-2.0.html>,
# or in pdf format at <http://www.dhampir.no/stuff/private/gpl-2.0.pdf>
# Copyright 2016 - Øyvind 'bolt' Hvidsten <[email protected]>
! ${GHOST:-false} || return 0
karma_file="$CONFIG/karma.dat"
function karma_save
{
local t n
sf_stdoutv "$MOD: Writing $karma_file"
sf_mkfile -qo t
for n in "${!karma[@]}"; do
echo "$n"$'\t'"${karma["$n"]}" >>"$t"
done
cat "$t" >"$karma_file"
rm "$t"
sf_rm_abort "$t"
}
function karma_load
{
local user points
if [[ -e "$karma_file" ]]; then
while IFS=$'\t' read -r user points; do
karma["$user"]="$points"
done <"$karma_file"
fi
}
function karma_seenow
{
local nick="$1" i j list
for i in "${!channel_names[@]}"; do
IFS=" " read -ra list <<<"${channel_names["$i"]}"
for j in "${!list[@]}"; do
if [[ "${list[$j],,}" = "$nick" ]]; then
return 0
fi
done
done
return 1
}
function karma_process
{
local user="$data"
user="${user%++}"
user="${user%--}"
user="${user#++}"
user="${user#--}"
user="${user%%=*}"
[[ "$user" != "$data" ]] || return 0
local low="${user,,}"
local setto=""
if [[ "$data" = *"="* ]]; then
setto="${data#*=}"
if ! sf_integer -n -- "$setto"; then
return
fi
if ! admin_verbosehas "$(src_host)"; then
return
fi
fi
if [[ "$target" != "#"* ]] && ! admin_has "$(src_host)"; then
reply "This command can only be used in a channel"
return
fi
if ! karma_seenow "$low"; then
reply "Sorry, but I don't see $user :("
return
fi
local src_nick
src_nick="$(src_nick)"
if [[ "$low" = "${src_nick,,}" ]] && ! admin_has "$(src_host)"; then
reply "Don't modify your own karma, you cheater!"
return
fi
local value
if [[ -n "$setto" ]]; then
value="$setto"
else
local value="${karma["$low"]:-0}"
case "$data" in
++*|*++)
(( value++ )) || :
;;
--*|*--)
(( value-- )) || :
;;
esac
fi
# if (( value )); then
karma["$low"]="$value"
# else
# unset 'karma[$low]'
# fi
reply "${user} now has a karma of $value"
karma_save
}
function karma_get
{
local nick="$1" low="${1,,}"
local value="${karma["$low"]:-0}"
if (( value )); then
local add=""
if (( value >= 10 )); then
add='\o/'
elif (( value >= 5 )); then
add=':D'
elif (( value >= 0 )); then
add=':)'
else
add=':('
fi
reply "${nick} has a karma of ${value} ${add}"
else
reply "No recorded karma for ${nick}"
fi
}
function karma_help
{
reply "Usage: karma <nick>"
reply "You can add and remove karma with <nick>++ and <nick>--, respectively."
}
if [[ -z "${karma[*]:-}" ]]; then
declare -A karma=()
karma_load
fi
if [[ -n "$reply" ]]; then
if (( data_count == 1 )); then
if [[ "${data_larray[0]}" = "karma" ]]; then
karma_help
else
karma_process
fi
elif [[ "${data_larray[0]}" = "karma" ]]; then
if (( data_count == 2 )); then
karma_get "${data_array[1]}"
else
karma_help
fi
fi
fi