-
Notifications
You must be signed in to change notification settings - Fork 3
/
admin.bmod
160 lines (152 loc) · 3.37 KB
/
admin.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
#!/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 2012 - Øyvind 'bolt' Hvidsten <bolt@dhampir.no>
if ${GHOST:-false}; then
function admin_has {
return 1
}
return 0
fi
adminfile="$CONFIG/admin.dat"
if ! sf_goodfile "$adminfile" && ! sf_mkfile -f "$adminfile"; then
sf_error "$MOD: Unable to create admin file!"
exit 1
fi
function admin_has
{
local user=${1,,} line
grep -q "^${user}$" "$adminfile" && return 0 || return 1
}
function admin_add
{
local user=${1,,}
if admin_has "$user"; then
sf_error "$MOD: $FUNCNAME: Attempting to add existing admin $user"
return 1
fi
echo "$user" >>"$adminfile"
}
function admin_remove
{
local user=${1,,} file
if ! admin_has "$user"; then
sf_error "$MOD: $FUNCNAME: Attempting to remove unknown admin $user"
return 1
fi
sf_mkfile -qo file
grep -v "^${user}$" "$adminfile" >"$file"
cat "$file" >"$adminfile"
rm "$file"
sf_rm_abort "$file"
}
function admin_verbosehas
{
local user=$1
if admin_has "$user"; then
return 0
fi
reply "$user is not a known admin of this bot."
return 1
}
function admin_process
{
if (( data_count < 2 )); then
reply "Usage: admin <command>"
return
fi
if ! admin_verbosehas "$(src_host)"; then
return
fi
case "${data_larray[1]}" in
add)
if (( data_count < 3 )); then
reply "Usage: admin add <mask>"
return
fi
if admin_add "${data_array[2]}"; then
reply "New admin: ${data_array[2]}"
else
reply "Unable to add ${data_array[2]}. Already exists?"
fi
;;
remove)
if (( data_count < 3 )); then
reply "Usage: admin remove <mask>"
return
fi
if [[ "${data_array[2]}" = "${OWNER:-}" ]]; then
reply "$OWNER is currently set as the owner of this bot and can not be removed from the admin list."
return
fi
if admin_remove "${data_array[2]}"; then
reply "Admin removed: ${data_array[2]}"
else
reply "Unable to remove ${data_array[2]}. No such admin?"
fi
;;
owner)
reply "My owner is \"$OWNER\""
;;
raw)
if [[ "$(src_host)" != "${OWNER:-}" ]]; then
reply "Only the owner can do that."
return
fi
if (( data_count < 3 )); then
reply "Raw what?"
return
fi
local raw
raw="$(dfromi 2)"
reply "$raw"
tx "$(echo -e "$raw")"
;;
list)
local a
while read -r a; do
[[ -n "$a" ]] || continue
reply "$a"
sleep 0.5
done <"$adminfile"
;;
test)
reply "Success."
;;
testkill)
reply "Killing connection."
sleep 1
kill -TERM "$catpid"
;;
die|shutdown)
action_all "dies..."
clean_exit
;;
*)
reply "Unknown or malformed admin command."
;;
esac
}
if [[ -n "$reply" ]]; then
case "$data" in
admin|admin\ *)
admin_process "$data"
;;
esac
fi
if
[[ -n "${OWNER:-}" ]] &&
! admin_has "${OWNER}"
then
sf_stdout "$MOD: Adding owner: $OWNER"
admin_add "$OWNER"
fi