-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.sh
executable file
·71 lines (66 loc) · 1.36 KB
/
socket.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
#! /bin/sh
username="anonymous"
game=''
watch_pid=''
count_cmd=$(PATH=$PWD:$PATH which counter.sh)
validate='
/^register \S+$/p;
/^list$/p;
/^create$/p;
/^join \w{4}$/p;
/^leave$/p;
/^start$/p;
/^submit [a-fA-F0-9]{6}$/p;
'
echo "hello"
while read line
do
cmd=$(echo "$line" | sed -rn "$validate" | cut -d ' ' -f 1)
case $cmd in
"")
echo "`date -Ins` $username bad-message $line"
continue
;;
"register")
username=$(echo $line | cut -d ' ' -f 2)
echo "`date -Ins` $username registered"
continue
;;
"list")
find "$GAME_FOLDER" -type f -cmin -15 -printf\
"%CY-%Cm-%CdT%CT $username gameitem %f\n"\
| sed "s/\.log//g"\
| sort
game=''
continue
;;
"create")
game=$(mktemp "$GAME_FOLDER/XXXX.log")
line="$line $(basename -s .log $game)"
echo "`date -Ins` $username $line"
continue
;;
"join")
if [ -w $game ]; then
kill $watch_pid 2>/dev/null
hash=$(echo $line | cut -d ' ' -f 2)
game="$GAME_FOLDER/$hash.log"
tail -f -n 0 --pid=$$ $game & watch_pid=$!
cat $game
sleep 0.5
else
cat $game
continue
fi
;;
"leave")
kill $watch_pid 2>/dev/null
game=''
;;
"start")
line="$line `head -c 100 /dev/urandom | sha1sum -b | head -c 6`"
`$count_cmd $game` &
;;
esac
echo "`date -Ins` $username $line" >> $game
done