]> git.plutz.net Git - httpchat/blob - channel.sh
fix syntax error m)
[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 'nicklist leave' 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     [ -d "/proc/$p" ] && nicklist="${nicklist}/$nick/" \
47     || sed -i -E "/^$s $p /d" "$nickfile"
48   done <"$nickfile"
49   if ! tail -n20 "$chatfile" |tac |grep -m1 '^NICKNAMES: ' |grep -qxF "$nicklist"; then
50     printf '%s\n' "$nicklist" >>"$chatfile"
51   fi
52 }
53
54 if [ ! -f "$chatfile" ]; then
55   yield_page create <<-EOF
56         [h1 No such Channel]
57         [form #nonexist method="POST"
58            There is no channel named $(HTML "$LOCATION")
59            [submit "action" "create" Set Up]
60         ]
61         EOF
62 else
63   nicklist enter
64
65   printf '%s: %s\r\n' Refresh 1
66   { printf '
67     [form #channel method="POST"
68       [submit "action" "submit" style="display: none;"]
69       [hidden "session_key" "%s"][hidden "channelkey" "%s"][hidden "timenonce" "%s"]
70       [a .settings href="?settings#nick" Settings][input autocomplete="off" name="message" autofocus=true][submit "action" "submit" Send!]
71     ]
72   ' "$SESSION_KEY" "$channelkey" "$_DATE"
73   SHESCAPE='s;[]&<>#."[];\\&;g;'
74
75   while sleep 10; do printf '\n'; done &
76   printf '[div #chat'
77   tail --pid $$ -n50 -f "$chatfile" \
78   | sed -nuE '
79     /^[0-9-]{10}_[0-9:]{8}+ [^ ]+ [^ ]+$/{
80     h; s;^([^ ]+) ([^ ]+) ([^ ]+)$;\1;; s;.*_;;;         s;.+;[p .message [span .date &];p;
81     g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;a\2;; bESC; :A s;.;;; s;(.)(.+);[span .nick [span .indicator \1]\2];p;
82     g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;b\3;; bESC; :B s;.;;; s;.+;[span .message &]];p;
83     b;
84     }
85     /^NICKNAMES: .*/{
86       s;^NICKNAMES: ;;; h; s;^.*$;[div .nicklist [h2 Nicknames];p; g;
87       :NICKLIST
88       h; s;^/([^/]+)/.*$;c\1;; bESC :C
89       s;^.([^?])(.*)$;[a .nick href="/~\1\2" \1\2];p;
90       s;^.(\?)(.*)$;[span .nick [span .indcator \1]\2];p;
91       g; s;/[^/]+/;;; /.+/bNICKLIST s;^.*$;];p;
92       b;
93     }
94     b; :ESC
95     '"$UNSTRING"' '"$SHESCAPE"'
96     /^a/bA; /^b/bB; /^c/bC;
97     '
98   } |yield_page channel
99
100   nicklist leave
101 fi