]> git.plutz.net Git - httpchat/blob - channel.sh
Merge commit 'b4afadf46825c28910365f4e6dce5ec20f6e1341'
[httpchat] / channel.sh
1 #!/bin/sh
2
3 if [ -f "$chatfile" ]; then
4   read -r channelkey x <"$chatfile"
5   channelkey="$( printf '%s-%s' "$channelkey" "$SESSION_ID" |sha256sum)"
6 fi
7
8 case $(POST action) in
9   create)
10     if mkdir "${_DATA}/${LOCATION}"; then
11       { randomid; printf ' '; STRING "$nickname"; echo; } >"$chatfile"
12     fi
13     REDIRECT "$(URL "/$LOCATION")"
14     ;;
15   submit)
16     read lasttime x <<-EOFread
17         $(tail -n 50 "$chatfile" |grep -F " $(STRING "$nickname"): " |tail -n1)
18         EOFread
19     if [ "$lasttime" ]; then
20       lasttime="$(date -d "${lasttime%_*} ${lasttime#*_}" +%s)"
21     else
22       lasttime=0
23     fi
24     if [ -f "$chatfile" -a "$channelkey" = "$(POST channelkey)" -a "$(POST timenonce)" -ge "$lasttime" ]; then
25       printf "%s %s: %s\n" "$(date +%F_%T)" "$(STRING "$nickname")" "$(POST message |STRING)" >>"$chatfile"
26     fi
27     REDIRECT "$(URL "/$LOCATION")"
28     ;;
29 esac
30
31 nicklist(){
32   local nickfile="${chatfile%/channel}/nicks"
33   case $1 in
34     enter)
35       trap 'sed -i -E "/^${SESSION_KEY%%-*} $$ /d" "$nickfile"' INT QUIT
36       sed -i -E "/^${SESSION_KEY%%-*} /d" "$nickfile"
37       printf '%s %i %s\n' "${SESSION_KEY%%-*}" "$$" "$nickname" >>"$nickfile"
38     ;;
39     leave)
40       sed -i -E "/^${SESSION_KEY%%-*} $$ /d" "$nickfile"
41     ;;
42   esac
43
44   nicklist='NICKNAMES: '
45   while read -r s p nick; do
46     nicklist="${nicklist}/$nick/"
47   done <"$nickfile"
48   if ! tail -n20 "$chatfile" |grep -qxF "$nicklist"; then
49     printf '%s\n' "$nicklist" >>"$chatfile"
50   fi
51 }
52
53 if [ ! -f "$chatfile" ]; then
54   yield_page create <<-EOF
55         [form #nonexist method="POST"
56            There is no channel named $(HTML "$LOCATION")
57            [submit "action" "create" Create]
58         ]
59         EOF
60 else
61   nicklist enter
62
63   printf '%s: %s\r\n' Refresh 1
64   { printf '
65     [form #channel method="POST"
66       [submit "action" "submit" style="display: none;"]
67       [hidden "session_key" "%s"][hidden "channelkey" "%s"][hidden "timenonce" "%s"]
68       [a .settings href="?settings#nick" Settings][input autocomplete="off" name="message" autofocus=true][submit "action" "submit" Send!]
69     ]
70   ' "$SESSION_KEY" "$channelkey" "$_DATE"
71   SHESCAPE='s;[]&<>#."[];\\&;g;'
72
73   while sleep 10; do printf '\n'; done &
74   printf '[div #chat'
75   tail --pid $$ -n50 -f "$chatfile" \
76   | sed -nuE '
77     /^[0-9-]{10}_[0-9:]{8}+ [^ ]+ [^ ]+$/{
78     h; s;^([^ ]+) ([^ ]+) ([^ ]+)$;\1;; s;.*_;;;         s;.+;[p .message [span .date &];p;
79     g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;a\2;; bESC; :A s;.;;; s;(.)(.+);[span .nick [span .indicator \1]\2];p;
80     g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;b\3;; bESC; :B s;.;;; s;.+;[span .message &]];p;
81     b;
82     }
83     /^NICKNAMES: .*/{
84       s;^NICKNAMES: ;;; h; s;^.*$;[div .nicklist [h2 Nicknames];p; g;
85       :NICKLIST
86       h; s;^/([^/]+)/.*$;c\1;; bESC :C
87       s;^.([^?])(.*)$;[a .nick href="/~\1\2" \1\2];p;
88       s;^.(\?)(.*)$;[span .nick [span .indcator \1]\2];p;
89       g; s;/[^/]+/;;; /.+/bNICKLIST s;^.*$;];p;
90       b;
91     }
92     b; :ESC
93     '"$UNSTRING"' '"$SHESCAPE"'
94     /^a/bA; /^b/bB; /^c/bC;
95     '
96   } |yield_page channel
97
98   nicklist leave
99 fi