]> git.plutz.net Git - httpchat/blob - channel.sh
split up in multiple files
[httpchat] / channel.sh
1 #!/bin/sh
2
3 if [ "$(COOKIE nick)" ]; then
4   nickname="?$(COOKIE nick)"
5 else
6   nickname='?Guest'
7 fi
8   
9 if [ -f "$chatfile" ]; then
10   read -r channelkey x <"$chatfile"
11   channelkey="$( printf '%s-%s' "$channelkey" "$SESSION_ID" |sha256sum)"
12 fi
13
14 case $(POST action) in
15   create)
16     if [ ! -f "$chatfile" ]; then
17       { randomid; printf ' '; STRING "$nickname"; echo; } >"$chatfile"
18     fi
19     REDIRECT "$(URL "/$LOCATION")"
20     ;;
21   submit)
22     if [ -f "$chatfile" -a "$channelkey" = "$(POST channelkey)" ]; then
23       printf "%s %s: %s\n" "$(date +%F_%T)" "$(STRING "$nickname")" "$(POST message |STRING)" >>"$chatfile"
24     fi
25     REDIRECT "$(URL "/$LOCATION")"
26     ;;
27   nick)
28     SET_COOKIE +1209600 "nick=$(POST nickname |URL)"
29     REDIRECT "$(URL "/$LOCATION")"
30     ;;
31   *) if [ ! -f "$chatfile" ]; then
32       yield_page create <<-EOF
33         [form #nonexist method=POST action="$(URL "/$LOCATION")"
34            There is no channel named $(HTML "$LOCATION")
35            [submit "action" "create" Create]
36         ]
37         EOF
38     else
39       { printf '
40         [form #channel method=POST action="%s"
41           [submit "action" "submit" style="display: none;"]
42           [input type=hidden name=channelkey value="%s"]
43           %s [input name="message" autofocus=true][submit "action" "submit" Send!]
44         ]
45       ' "$(URL "/$LOCATION")" "$channelkey" "$(settings_menu)"
46       SHESCAPE='s;[]&<>#."[];\\&;g;'
47
48       printf '[div #chat'
49       # tail -n30 -f "$chatfile" | {
50       #   read x
51       #   while read -r date nick message; do
52       #     printf '[p .message [span .date %s] [span .nick [span .indicator %s]%s:] [span .message %s]]\n' \
53       #            "${date#*_}" "${nick%${nick#?}}" "$(UNSTRING "${nick#?}" |HTML)" "$(UNSTRING "$message" |HTML)"
54       #     done
55       #   }
56       tail -n50 -f "$chatfile" \
57       | sed -nuE '
58         /^[^ ]+ [^ ]+ [^ ]+$/{
59         h; s;^([^ ]+) ([^ ]+) ([^ ]+)$;\1;; s;.*_;;;         s;.+;[p .message [span .date &];p;
60         g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;a\2;; bESC; :A s;.;;; s;(.)(.+);[span .nick [span .indicator \1]\2];p;
61         g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;b\3;; bESC; :B s;.;;; s;.+;[span .message &]];p;
62         }
63         b; :ESC
64         '"$UNSTRING"' '"$SHESCAPE"'
65         /^a/bA; /^b/bB;
66         '
67       } |yield_page channel
68     fi 
69     ;;
70 esac