-
Notifications
You must be signed in to change notification settings - Fork 6
/
plugins_debug
executable file
·168 lines (146 loc) · 4.78 KB
/
plugins_debug
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
#!/bin/bash
trap 'quit' 2 3 15
b=$(tput bold)
n=$(tput sgr0)
[ -z "$1" ] && {
PLUGINS_CONF=./plugins.json
function log {
local call_stack="$(tr ' ' '/' <<< "${FUNCNAME[@]:1}")"
echo "$b[$call_stack]$n $1" >&2
}
function curl {
log "curl invoked, param: $*"
read -p "what to do with this request? [(P)ass/(R)eject/(C)ustom] " _act </dev/tty
case "$_act" in
p*|P*)
log "passing curl request."
resp="$($CURL_BIN "$@")"
read -p "we got respond, what to do? [(P)ass/(E)dit] " __act </dev/tty
case $__act in
p*|P*) log "passing respond."; echo $resp;;
e*|E*)
tmpf="$(mktemp)"
[ -z "$tmpf" ] && log "error making temp file. " && return 1
echo "$resp" > $tmpf
vim $tmpf </dev/tty 1>&2 >/dev/tty
log "passing edited respond."
cat $tmpf </dev/tty
;;
*) log "unknow action $__act, passing respond."; echo $resp;;
esac
;;
r*|R*) log "rejecting curl request.";return 1 ;;
c*|C*) log "type the respond, end with EOF."; cat </dev/tty ;;
*) log "unknow operation: $_act, rejecting request."; return 1 ;;
esac
}
function find_handler {
jq -cr 'to_entries [] | select(.value | index("'$1'")) | .key' "$PLUGINS_CONF"
}
function add_msg_inspector {
log "adding message inspector: $*"
MSG_ISPC="$MSG_ISPC $*"
}
function msg_send {
[[ ! $c_type == "private" && -z "$NO_REPLY" ]] && msg_send_OPTIONS="reply_to_message_id=$mid $msg_send_OPTIONS"
log "msg_send invoked, options: $msg_send_OPTIONS"
[ -z "$1" ] && {
log "empty message."
}
send=$(iconv -f utf-8 -t utf-8//IGNORE <<< "$1")
log "outgoing message: cid $cid|\"$send\""
}
function msg_recv {
[ ! -z "$1" ] && text="$1"
[ -z "$text" ] && log "empty message? from $uid@$cid." && return 0
log "incoming message: uid $uid|cid $cid|mid $mid|\"$text\""
for ispc in $MSG_ISPC; do {
log "executing inspector: $ispc"
$ispc
}; done
[[ ! "$text" == '/'* ]] && log "not a command, skipping command processing logic." && return 0
cmd="$(cut -d' ' -f1 <<< "$text")"
[[ "$cmd" == *"@"* ]] && {
to_bot="$(cut -d'@' -f2 <<< "$cmd")"
[[ ! "$to_bot" == "$BOT_USERNAME" ]] && log "not commanding me, abort." && return 0
}
[ -z "$cmd_body" ] && cmd_body="$(awk -F"$cmd " '{print $2}' <<< "$text")"
cmd="$(sed -e 's/@'$BOT_USERNAME'//; s/\///;' <<< "$cmd")"
handler_cmd="$(find_handler "$cmd")"
[ -z $handler_cmd ] && {
log "no handler for $cmd, abort"
return 0
}
log "procressing command: $cmd|body \"$cmd_body\"|handler $handler_cmd."
. $handler_cmd
cmd_body=''
}
function quit {
read -p "save the session [y/N] " _act
case $_act in
y|Y) read -p "filename [$SESSION_NAME]: " _name; save "${_name:-$SESSION_NAME}";;
*) exit 0
esac
exit 0
}
function eval_ {
shift 1
eval "$*"
}
function handler_exec {
. $1
}
function save {
[ -z "$1" ] && log "no name supplied, using 'debug_session'"
local name="${1:-debug_session}"
set > "$name"
log "session saved as $name. load with $0 '$name'"
}
echo "mahonato-ng plugin debugger"
echo
echo "debugger startup configuration, some mandatory ENV needed:"
read -p "CURL_BIN [/usr/bin/curl]: " CURL_BIN
read -p "LC_ALL [zh_CN.utf-8]: " LC_ALL
read -p "TZ [Asia/Shanghai]: " TZ
read -p "BOT_USERNAME [mahonato_bot]: " BOT_USERNAME
read -p 'mid [$RANDOM]: ' mid
read -p 'uid [$RANDOM]: ' uid
read -p 'uid [-$RANDOM]: ' cid
read -p 'u_fn [Maho]: ' u_fn
read -p 'u_ln [Morichika]: ' u_ln
read -p 'c_title [Nato Lab]: ' c_title
read -p 'c_type [group]: ' c_type
mid="${mid:-$RANDOM}"; uid="${uid:-$RANDOM}"; cid="${cid:--$RANDOM}"
u_fn="${u_fn:-Maho}"; u_ln="${u_ln:-Morichika}";
u_un="${u_un:-magicnat}"; c_title="${c_title:-Nato Lab}"; c_type="${c_type:-group}"
LC_ALL="${LC_ALL:-zh_CN.utf-8}"
TZ="${TZ:-Asia/Shanghai}"
BOT_USERNAME="${BOT_USERNAME:-mahonato_bot}"
CURL_BIN="${CURL_BIN:-/usr/bin/curl}"
for INSP in inspectors/*; do {
. $INSP
}; done
}
[ ! -z "$1" ] && {
[ ! -e "$1" ] && echo "session $1 not found, exiting." && exit 1
. $1 1>&2 2>/dev/null
log "loaded session from $1"
SESSION_NAME=$1
}
while IFS="" read -r -e -d $'\n' -p "$b?>$n " expr_; do
[ -z "$expr_" ] && continue
history -s -- "$expr_"
IFS=' '
case "$expr_" in
eval*) $expr_ ;;
lscmd*) cat $PLUGINS_CONF | jq -cr '. [][]';;
echo*|save*) eval "$expr_" ;;
+*|-*) log "set: $expr_"; set $expr_;;
*)
msg_recv "$expr_" &
wait
let mid++
;; # put in subshell so exit() in plugin won't exit debug shell.
esac
done
quit