]> git.plutz.net Git - httpchat/blob - channel.sh
introduce nickname registration
[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 [ ! -f "$chatfile" ]; then
11       { randomid; printf ' '; STRING "$nickname"; echo; } >"$chatfile"
12     fi
13     REDIRECT "$(URL "/$LOCATION")"
14     ;;
15   submit)
16     if [ -f "$chatfile" -a "$channelkey" = "$(POST channelkey)" ]; then
17       printf "%s %s: %s\n" "$(date +%F_%T)" "$(STRING "$nickname")" "$(POST message |STRING)" >>"$chatfile"
18     fi
19     REDIRECT "$(URL "/$LOCATION")"
20     ;;
21 esac
22
23 if [ ! -f "$chatfile" ]; then
24   yield_page create <<-EOF
25         [form #nonexist method="POST"
26            There is no channel named $(HTML "$LOCATION")
27            [submit "action" "create" Create]
28         ]
29         EOF
30 else
31   printf '%s: %s\r\n' Refresh 1
32   { printf '
33     [form #channel method="POST"
34       [submit "action" "submit" style="display: none;"]
35       [input type=hidden name=channelkey value="%s"]
36       [a .settings href="?settings#nick" Settings][input autocomplete="off" name="message" autofocus=true][submit "action" "submit" Send!]
37     ]
38   ' "$channelkey"
39   SHESCAPE='s;[]&<>#."[];\\&;g;'
40
41   while sleep 10; do printf '\n'; done &
42   printf '[div #chat'
43   tail --pid $$ -n50 -f "$chatfile" \
44   | sed -nuE '
45     /^[^ ]+ [^ ]+ [^ ]+$/{
46     h; s;^([^ ]+) ([^ ]+) ([^ ]+)$;\1;; s;.*_;;;         s;.+;[p .message [span .date &];p;
47     g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;a\2;; bESC; :A s;.;;; s;(.)(.+);[span .nick [span .indicator \1]\2];p;
48     g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;b\3;; bESC; :B s;.;;; s;.+;[span .message &]];p;
49     }
50     b; :ESC
51     '"$UNSTRING"' '"$SHESCAPE"'
52     /^a/bA; /^b/bB;
53     '
54   } |yield_page channel
55 fi