-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·91 lines (77 loc) · 1.64 KB
/
run
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
#!/bin/sh
# A discord "bot" using dcfs
read -r TOKEN <data/.token
read -r SERVER <data/.server
read -r CHAN <data/.chan
read -r MNT <data/.mnt
read -r OWNER <data/.owner
BPREFIX='-'
# Previous csv string
p=''
die() {
echo dying, bye...
fusermount -uz "${MNT}"
read pid <data/pid
kill ${pid}
rm -f data/pid
exit 0
}
init() {
trap die INT
_TAGDB="data/tagdb"
_OWNDB="data/tagowndb"
for i in ${MNT} ${_TAGDB} ${_OWNDB}; do
[ -d ${i} ] || mkdir -p ${i}
done
TOKEN="${TOKEN}" dcfs "${MNT}" \
'{{.Author.Username}}' '{{.Content}}' '{{time .Timestamp "3:04PM"}}' \
>data/dcfs.log 2>data/dcfs.log &
echo ${!} >data/pid
for i in lib/*.sh; do
. "${i}"
done
}
type dcfs >/dev/null || {
echo error: dcfs not found
exit 1
}
init
while [ ! -f "${MNT}/${SERVER}/${CHAN}" ]; do
sleep 0.1
done
log starting to read ${CHAN}
# tail -f in a fuse fs doesn't really work in a proper way. When the
# file changes, tail -f -n1 doesn't output the last line. But when the
# file does change, GNU tail outputs a single line to stderr which
# could be captured to listen listen for commands but it is not
# consistent. I cannot use other inotify-based tools either.
while :; do
l="$(tail -n1 "${MNT}/${SERVER}/${CHAN}")"
[ "${l}" = "${p}" ] && continue
msg_cmd "${l}" || {
p="${l}"
rm -fr ${MSG}
continue
}
cmd=${content#?}
case ${cmd%% *} in
tags)
tags_parse "${content}" "${author}"
;;
choose)
choose "${content#${BPREFIX}choose }"
;;
shitpost)
shitpost "${content#${BPREFIX}shitpost }"
;;
test)
printf '%s\n' test
;;
help)
printf '%s\n' no
;;
esac >${MNT}/${SERVER}/${CHAN}
rm -fr ${MSG}
p="${l}"
sleep 1
done