-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.sh
120 lines (109 loc) · 2.83 KB
/
bot.sh
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
#!/usr/bin/env bash
# Based on kekbot by dom, Aatrox, and Hunner from the CAT @ Portland State University.
syntax='Usage: MCBE_Bot.sh [OPTION] ...'
input() {
# $USER, $HOSTNAME, and $fqdn are verified, name is clearly not
# $USER = `whoami` and is not set in cron
echo "USER $(whoami) $HOSTNAME $fqdn :The Mafia"
echo "NICK $nick"
echo "$join"
# Last 10 lines of $buffer as IRC appends to it
tail -f "$buffer"
}
ping_timeout() {
diff=0
# 15 minute timeout
# irc.cat.pdx.edu ping timeout is 4m20s
while [ "$diff" -lt 900 ]; do
sleep 1
# Seconds since epoch
thyme=$(date +%s)
# File modification time in seconds since epoch
mthyme=$(stat -c %Y "$ping_time")
diff=$((thyme - mthyme))
done
>&2 echo Ping timeout
# Kill session processes of $$ (script PID)
# exit doesn't exit script when backgrounded
pkill -s $$
exit 1
}
send() {
# Avoid filename expansion
echo "$*" >> "$buffer"
}
args=$(getopt -l help,instance: -o hi: -- "$@")
eval set -- "$args"
while [ "$1" != -- ]; do
case $1 in
--help|-h)
echo "$syntax"
echo Minecraft Bedrock Edition server IRC bot
echo
echo Mandatory arguments to long options are mandatory for short options too.
echo '-i, --instance=INSTANCE use configuration file ~/.MCBE_Bot/{INSTANCE}Join.txt. defaults to MCBE_Bot.'
echo
echo 'See README.md for format of ~/.MCBE_Bot/{INSTANCE}Join.txt'
exit
;;
--instance|-i)
instance=$2
shift 2
;;
esac
done
shift
if [ "$#" -gt 0 ]; then
>&2 echo Too much arguments
>&2 echo "$syntax"
exit 1
fi
# If $instance doesn't exist
if [ -z "$instance" ]; then
instance=MCBE_Bot
fi
# Make directory and parents quietly
mkdir -p /bedrock-server/MCBE_Bot
buffer=/bedrock-server/MCBE_Bot/${instance}Buffer
# Kill all doppelgangers
# Duplicate bots exit if $buffer is removed
rm -f "$buffer"
mkfifo "$buffer"
chmod 600 "$buffer"
ping_time=/bedrock-server/MCBE_Bot/${instance}Ping
touch "$ping_time"
join_file=/bedrock-server/MCBE_Bot/${instance}Join.txt
chmod 600 "$join_file"
join=$(grep -Ev '^[^ ]+:[0-9]+$' "$join_file")
if ! server=$(grep -E '^[^ ]+:[0-9]+$' "$join_file"); then
echo "No server in $join_file"
exit 1
fi
if echo "$join" | grep -q '^NICK '; then
nick=$(echo "$join" | grep '^NICK ' | cut -d ' ' -f 2 -s)
join=$(echo "$join" | grep -v '^NICK ')
else
nick=$instance
fi
# DNS check
# Trim off $server after first :
if ! stdout=$(host "${server%%:*}"); then
>&2 echo "$stdout"
exit 1
fi
fqdn=$(host "$HOSTNAME" | head -n 1 | cut -d ' ' -f 1)
ping_timeout &
input | openssl s_client -connect "$server" 2>&1 | while read -r irc; do
# If disconnected MCBE_Bot reads an empty string
if [ -n "$irc" ]; then
# Reset timeout
touch "$ping_time"
echo "$irc"
if [ "$(echo "$irc" | cut -d ' ' -f 1)" = PING ]; then
send PONG
elif [[ "$(echo "$irc" | cut -d ' ' -f 1)" =~ connect:errno=[0-9]+ ]]; then
pkill -s $$
exit 1
fi
fi
done