]> git.plutz.net Git - httpchat/blob - channel.sh
hold session key in POST data
[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 if [ ! -f "$chatfile" ]; then
32   yield_page create <<-EOF
33         [form #nonexist method="POST"
34            There is no channel named $(HTML "$LOCATION")
35            [submit "action" "create" Create]
36         ]
37         EOF
38 else
39   printf '%s: %s\r\n' Refresh 1
40   { printf '
41     [form #channel method="POST"
42       [submit "action" "submit" style="display: none;"]
43       [hidden "session_key" "%s"][hidden "channelkey" "%s"][hidden "timenonce" "%s"]
44       [a .settings href="?settings#nick" Settings][input autocomplete="off" name="message" autofocus=true][submit "action" "submit" Send!]
45     ]
46   ' "$SESSION_KEY" "$channelkey" "$_DATE"
47   SHESCAPE='s;[]&<>#."[];\\&;g;'
48
49   while sleep 10; do printf '\n'; done &
50   printf '[div #chat'
51   tail --pid $$ -n50 -f "$chatfile" \
52   | sed -nuE '
53     /^[^ ]+ [^ ]+ [^ ]+$/{
54     h; s;^([^ ]+) ([^ ]+) ([^ ]+)$;\1;; s;.*_;;;         s;.+;[p .message [span .date &];p;
55     g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;a\2;; bESC; :A s;.;;; s;(.)(.+);[span .nick [span .indicator \1]\2];p;
56     g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;b\3;; bESC; :B s;.;;; s;.+;[span .message &]];p;
57     }
58     b; :ESC
59     '"$UNSTRING"' '"$SHESCAPE"'
60     /^a/bA; /^b/bB;
61     '
62   } |yield_page channel
63 fi