-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellbot.sh
More file actions
executable file
·179 lines (153 loc) · 4.38 KB
/
shellbot.sh
File metadata and controls
executable file
·179 lines (153 loc) · 4.38 KB
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
#!/usr/bin/env bash
# shellbot.sh -- core for miyoko's shellbot
# setup config
config="etc/core_config.sh"
# config check
if test ! -z $config; then
# include our config or die
if test ! -e $config; then
echo "FATAL: config file \'${config}\' does not exist."; exit 1
else
. $config
fi
fi
# setup for arguments and case it
while getopts "S:P:C:N:H:f:hbB" flag
do
case "$flag" in
S) export server="$OPTARG";if test ! -e "tmp/${server}_input"; then export socket="tmp/${server}_input"; elif test -e "tmp/${server}_input"; then export socket="tmp/${server}_input_"; fi
;;
P) export port="$OPTARG"
;;
C) export channel="$OPTARG"
;;
N) export nick="$OPTARG"
;;
H) . include/libcompat.sh
pass_md5 $OPTARG; exit 0
;;
f) export cust_conf="yes";export config="$OPTARG"
;;
h) echo -en "`basename $0`: [-Sserver|-Pport|-Nnick|-Cchannel||-fconfigfile||-H password|-h]\x0aoptions do not have to be used in conjunction, you may use any option without the others.\x0aDO NOT SPECIFY -f with -S, -P, -C, OR -N.\x0a"; exit 0
;;
b) export backgrounded="yes"
;;
B) export running="yes"
;;
esac
done
# config stuff
if test "$cust_conf" == "no"; then
config="etc/core_config.sh"
elif test "$cust_conf" == "yes"; then
# include our config or die
if test ! -e $config; then
echo "FATAL: config file \'${config}\' does not exist."; exit 1
else
. $config
fi
fi
# setup the socket
socket="tmp/${server}.socket"
# setup runtime with whatever the config says
case "$core_debug" in
[Yy][Ee][Ss]) set -x
;;
[Nn][Oo]) echo "CORE: debugging off"
;;
esac
# see if we need to background and do so, if needed
case "$core_bck" in
[Yy][Ee][Ss]) if test "$backgrounded" == "yes"; then
echo "CORE: backgrounded"
elif test ! "$backgrounded" == "yes"; then
echo "CORE: backgrounding..."
{ nohup $0 $* -b 2>&1 1>>tmp/${server}.console & disown; exit 0; }; exit 0
fi
;;
[Nn][Oo]) if test "$running" == "yes"; then
echo "CORE: running"
elif test ! "$running" == "yes"; then
echo "CORE: not backgrounding."
{ tail -f tmp/${server}.console & nohup $0 $* -B 2>&1 1>>tmp/${server}.console; }
fi
;;
esac
# some tiny bit of setup
boottime=$(date +%s)
# empty out the socket
if [ -e $socket ] ; then
echo '' > $socket
else
touch $socket
fi
# setup the nohup log
if test -e tmp/${server}.console; then
echo '' >tmp/${server}.console
else
touch tmp/${server}.console
fi
# simple variable for kickrejoin
one=1
# include our parsing, help, compatibility and channel management libraries
. include/libparser.sh
. include/libcompat.sh
. include/libchannel.sh
. include/libhelp.sh
# include our require stuff
. include/required.sh
# prepare to generate help
export prefix=${prefix}
# generate help
help.generate
# dump registration info into core_input
echo "NICK $nick" >> $socket
echo "USER $(whoami) +iw $nick :$nick" >> $socket
# setup 'die' function
function die () {
state="halting"
kill -9 $$
}
# start up the connection and enter main loop
echo -en "CORE: entering main loop\x0a\x0a"
tail -f $socket | telnet $server $port | \
while true
do read LINE || break
# protect from the exploit that nukes bashIRC
LINE=${LINE//\*/\\x2a}
echo "$LINE"
# check for pings from the ircd
if [ $(echo "$LINE" | awk '{print $1}') == "PING" ] ; then
server_resp=$(echo "$LINE" | awk '{print $2}')
echo "PONG $server_resp" >> $socket
fi
# make sure there wasnt an ERROR: for disconnect sent
if [ "$(echo ${LINE} | awk '{print $1}')" == "ERROR" ] && [ ! "$state" == "halting" ] ; then
echo "CONNECTION: server error occurred."
if test "$core_reconn" == "yes"; then
echo "CORE: reconnecting"
{ $0 $*; }
else
echo "CONNECTION: abort"
exit 1
fi
fi
# check if the nick was already in use
if [ "$(echo "$LINE" | awk '{print $2}')" == "433" ] ; then
nick="$nick-"
echo "NICK $nick" >>$socket
fi
# check the perform to know when to join our channel
if [ $(echo "$LINE" | awk '{print $2}' | cut -b 1) == "4" ] || [ "$(echo $LINE | awk '{print $2}' | cut -b 1)" == "3" ] ; then
join $channel
fi
# check for a kick so we know to rejoin
if [ $(echo "$LINE" | awk '{print $2}') == "KICK" ] && [ "$(echo $LINE | awk '{print $4}')" == "${nick}" ] ; then
one=$((one++))
rejoin $(echo $LINE | awk '{print $3}')
fi
# parse each line in real time
parse $LINE
# log the line just for reference
echo "$LINE" >> etc/core_log
done