Skip to content

Commit 53b6d6e

Browse files
feat(bin): add lsext script to list currently loaded extensions
Signed-off-by: Ar Rakin <[email protected]>
1 parent af9386e commit 53b6d6e

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

bin/lsext

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#!/bin/sh
2+
#
3+
# lsext - list all extensions
4+
# Copyright (C) 2021, 2022, 2023, 2024, 2025 OSN Developers Team.
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
#
17+
18+
# shellcheck disable=SC3043
19+
20+
dir="$(dirname "$0")"
21+
env_file="$(dirname "$dir")/.env"
22+
me="$(basename "$0")"
23+
24+
socket_file="/run/sudobot.sock"
25+
26+
if ! command -v nc >/dev/null 2>&1; then
27+
echo "$me: netcat is not installed" >&2
28+
exit 1
29+
fi
30+
31+
if ! command -v jq >/dev/null 2>&1; then
32+
echo "$me: jq is not installed" >&2
33+
exit 1
34+
fi
35+
36+
if ! command -v bc >/dev/null 2>&1; then
37+
echo "$me: bc is not installed" >&2
38+
exit 1
39+
fi
40+
41+
if ! command -v date >/dev/null 2>&1; then
42+
echo "$me: date is not installed" >&2
43+
exit 1
44+
fi
45+
46+
while [ "$#" -gt 0 ]; do
47+
case "$1" in
48+
-h|--help)
49+
echo "Usage: $me [options]"
50+
echo ""
51+
echo "List all extensions loaded in SudoBot. This requires you to set the"
52+
echo "environment variable SOCKET_FILE to the path of the socket file."
53+
echo ""
54+
echo "Options:"
55+
echo " -h, --help Show this help message"
56+
exit 0
57+
;;
58+
*)
59+
echo "$me: invalid option: $1" >&2
60+
exit 1
61+
;;
62+
esac
63+
done
64+
65+
load_env_socket_file_name() {
66+
if [ -f "$env_file" ]; then
67+
local contents=""
68+
69+
contents="$(cat "$env_file")"
70+
71+
if [ $? -ne 0 ]; then
72+
echo "$me: unable to read $env_file" >&2
73+
return 1
74+
fi
75+
76+
local socket_file_value="$(echo "$contents" | grep "SOCKET_FILE" | cut -d'=' -f2- | tr -d '\r\n')"
77+
local prefix_value="$(echo "$contents" | grep "SUDO_PREFIX" | cut -d'=' -f2- | tr -d '\r\n')"
78+
79+
if [ -z "$socket_file_value" ]; then
80+
echo "$prefix_value/sudobot.sock"
81+
return 0
82+
fi
83+
84+
echo "$socket_file_value"
85+
fi
86+
87+
return 0
88+
}
89+
90+
list_extensions() {
91+
socket_file_env="$(load_env_socket_file_name)"
92+
93+
if [ $? -ne 0 ]; then
94+
echo "$me: unable to load environment variables" >&2
95+
exit 1
96+
fi
97+
98+
if [ -n "$socket_file_env" ]; then
99+
socket_file="$socket_file_env"
100+
fi
101+
102+
if [ ! -r "$socket_file" ]; then
103+
echo "$me: unable to read socket: $socket_file" >&2
104+
exit 1
105+
fi
106+
107+
if [ ! -S "$socket_file" ]; then
108+
echo "$me: socket file is not a socket: $socket_file" >&2
109+
exit 1
110+
fi
111+
112+
magic="$(head -c 16 /dev/urandom | base64 | tr -d '\n' | cut -c 1-8)"
113+
114+
response="$(echo $magic'{"type":"ListExtensions"}'$magic | nc -U "$socket_file")"
115+
116+
if [ $? -ne 0 ]; then
117+
echo "$me: unable to connect to socket: $socket_file" >&2
118+
exit 1
119+
fi
120+
121+
if [ -z "$response" ]; then
122+
echo "$me: empty response from socket: $socket_file" >&2
123+
exit 1
124+
fi
125+
126+
if ! echo "$response" | jq -e . >/dev/null 2>&1; then
127+
echo "$me: invalid response from socket: $socket_file" >&2
128+
exit 1
129+
fi
130+
131+
if [ "$(echo "$response" | jq -r .type)" != "ListExtensions" ]; then
132+
echo "$me: invalid type in response from socket: $socket_file" >&2
133+
exit 1
134+
fi
135+
136+
printf "\033[1m%-20s %-10s %-10s %-20s %s\033[0m\n" "Name" "Version" "Author" "Loaded At" "Description"
137+
138+
for extension in $(echo "$response" | jq -r '.extensions[] | @base64'); do
139+
_jq() {
140+
echo "${extension}" | base64 --decode | jq -r "${1}"
141+
}
142+
143+
name="$(_jq '.name')"
144+
version="$(_jq '.version')"
145+
author="$(_jq '.author')"
146+
loaded_at="$(_jq '.loadedAt')"
147+
description="$(_jq '.description')"
148+
149+
if [ "$name" = "null" ]; then
150+
name="N/A"
151+
fi
152+
153+
if [ "$version" = "null" ]; then
154+
version="N/A"
155+
fi
156+
157+
if [ "$author" = "null" ]; then
158+
author="N/A"
159+
fi
160+
161+
if [ "$loaded_at" = "null" ] || [ -z "$loaded_at" ]; then
162+
loaded_at="N/A"
163+
else
164+
loaded_at="$(echo "scale=0; $loaded_at/1000" | bc)"
165+
loaded_at="$(date --date=@"$loaded_at" +'%Y-%m-%d %H:%M:%S')"
166+
fi
167+
168+
if [ "$description" = "null" ]; then
169+
description="N/A"
170+
fi
171+
172+
printf "%-20s %-10s %-10s %-20s %s\n" "$name" "$version" "$author" "$loaded_at" "$description"
173+
done
174+
}

0 commit comments

Comments
 (0)