]> git.plutz.net Git - httpchat/blobdiff - channel.sh
fix syntax error m)
[httpchat] / channel.sh
index eab42fc6d9b07f012735c384ef050b7037cbbe6a..f8c97a43ae1622f627f518d52a9df40c6d0e5f3f 100755 (executable)
@@ -1,11 +1,5 @@
 #!/bin/sh
 
-if [ "$(COOKIE nick)" ]; then
-  nickname="?$(COOKIE nick)"
-else
-  nickname='?Guest'
-fi
-  
 if [ -f "$chatfile" ]; then
   read -r channelkey x <"$chatfile"
   channelkey="$( printf '%s-%s' "$channelkey" "$SESSION_ID" |sha256sum)"
@@ -13,58 +7,95 @@ fi
 
 case $(POST action) in
   create)
-    if [ ! -f "$chatfile" ]; then
+    if mkdir "${_DATA}/${LOCATION}"; then
       { randomid; printf ' '; STRING "$nickname"; echo; } >"$chatfile"
     fi
     REDIRECT "$(URL "/$LOCATION")"
     ;;
   submit)
-    if [ -f "$chatfile" -a "$channelkey" = "$(POST channelkey)" ]; then
+    read lasttime x <<-EOFread
+       $(tail -n 50 "$chatfile" |grep -F " $(STRING "$nickname"): " |tail -n1)
+       EOFread
+    if [ "$lasttime" ]; then
+      lasttime="$(date -d "${lasttime%_*} ${lasttime#*_}" +%s)"
+    else
+      lasttime=0
+    fi
+    if [ -f "$chatfile" -a "$channelkey" = "$(POST channelkey)" -a "$(POST timenonce)" -ge "$lasttime" ]; then
       printf "%s %s: %s\n" "$(date +%F_%T)" "$(STRING "$nickname")" "$(POST message |STRING)" >>"$chatfile"
     fi
     REDIRECT "$(URL "/$LOCATION")"
     ;;
-  nick)
-    SET_COOKIE +1209600 "nick=$(POST nickname |URL)"
-    REDIRECT "$(URL "/$LOCATION")"
+esac
+
+nicklist(){
+  local nickfile="${chatfile%/channel}/nicks"
+  case $1 in
+    enter)
+      trap 'nicklist leave' INT QUIT
+      sed -i -E "/^${SESSION_KEY%%-*} /d" "$nickfile"
+      printf '%s %i %s\n' "${SESSION_KEY%%-*}" "$$" "$nickname" >>"$nickfile"
+    ;;
+    leave)
+      sed -i -E "/^${SESSION_KEY%%-*} $$ /d" "$nickfile"
     ;;
-  *) if [ ! -f "$chatfile" ]; then
-      yield_page create <<-EOF
-       [form #nonexist method=POST action="$(URL "/$LOCATION")"
-           There is no channel named $(HTML "$LOCATION")
-           [submit "action" "create" Create]
-        ]
+  esac
+
+  nicklist='NICKNAMES: '
+  while read -r s p nick; do
+    [ -d "/proc/$p" ] && nicklist="${nicklist}/$nick/" \
+    || sed -i -E "/^$s $p /d" "$nickfile"
+  done <"$nickfile"
+  if ! tail -n20 "$chatfile" |tac |grep -m1 '^NICKNAMES: ' |grep -qxF "$nicklist"; then
+    printf '%s\n' "$nicklist" >>"$chatfile"
+  fi
+}
+
+if [ ! -f "$chatfile" ]; then
+  yield_page create <<-EOF
+       [h1 No such Channel]
+       [form #nonexist method="POST"
+          There is no channel named $(HTML "$LOCATION")
+          [submit "action" "create" Set Up]
+       ]
        EOF
-    else
-      { printf '
-        [form #channel method=POST action="%s"
-          [submit "action" "submit" style="display: none;"]
-          [input type=hidden name=channelkey value="%s"]
-          %s [input name="message" autofocus=true][submit "action" "submit" Send!]
-        ]
-      ' "$(URL "/$LOCATION")" "$channelkey" "$(settings_menu)"
-      SHESCAPE='s;[]&<>#."[];\\&;g;'
+else
+  nicklist enter
 
-      printf '[div #chat'
-      # tail -n30 -f "$chatfile" | {
-      #   read x
-      #   while read -r date nick message; do
-      #     printf '[p .message [span .date %s] [span .nick [span .indicator %s]%s:] [span .message %s]]\n' \
-      #            "${date#*_}" "${nick%${nick#?}}" "$(UNSTRING "${nick#?}" |HTML)" "$(UNSTRING "$message" |HTML)"
-      #     done
-      #   }
-      tail -n50 -f "$chatfile" \
-      | sed -nuE '
-        /^[^ ]+ [^ ]+ [^ ]+$/{
-        h; s;^([^ ]+) ([^ ]+) ([^ ]+)$;\1;; s;.*_;;;         s;.+;[p .message [span .date &];p;
-        g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;a\2;; bESC; :A s;.;;; s;(.)(.+);[span .nick [span .indicator \1]\2];p;
-        g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;b\3;; bESC; :B s;.;;; s;.+;[span .message &]];p;
-        }
-        b; :ESC
-        '"$UNSTRING"' '"$SHESCAPE"'
-        /^a/bA; /^b/bB;
-        '
-      } |yield_page channel
-    fi 
-    ;;
-esac
+  printf '%s: %s\r\n' Refresh 1
+  { printf '
+    [form #channel method="POST"
+      [submit "action" "submit" style="display: none;"]
+      [hidden "session_key" "%s"][hidden "channelkey" "%s"][hidden "timenonce" "%s"]
+      [a .settings href="?settings#nick" Settings][input autocomplete="off" name="message" autofocus=true][submit "action" "submit" Send!]
+    ]
+  ' "$SESSION_KEY" "$channelkey" "$_DATE"
+  SHESCAPE='s;[]&<>#."[];\\&;g;'
+
+  while sleep 10; do printf '\n'; done &
+  printf '[div #chat'
+  tail --pid $$ -n50 -f "$chatfile" \
+  | sed -nuE '
+    /^[0-9-]{10}_[0-9:]{8}+ [^ ]+ [^ ]+$/{
+    h; s;^([^ ]+) ([^ ]+) ([^ ]+)$;\1;; s;.*_;;;         s;.+;[p .message [span .date &];p;
+    g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;a\2;; bESC; :A s;.;;; s;(.)(.+);[span .nick [span .indicator \1]\2];p;
+    g; s;^([^ ]+) ([^ ]+) ([^ ]+)$;b\3;; bESC; :B s;.;;; s;.+;[span .message &]];p;
+    b;
+    }
+    /^NICKNAMES: .*/{
+      s;^NICKNAMES: ;;; h; s;^.*$;[div .nicklist [h2 Nicknames];p; g;
+      :NICKLIST
+      h; s;^/([^/]+)/.*$;c\1;; bESC :C
+      s;^.([^?])(.*)$;[a .nick href="/~\1\2" \1\2];p;
+      s;^.(\?)(.*)$;[span .nick [span .indcator \1]\2];p;
+      g; s;/[^/]+/;;; /.+/bNICKLIST s;^.*$;];p;
+      b;
+    }
+    b; :ESC
+    '"$UNSTRING"' '"$SHESCAPE"'
+    /^a/bA; /^b/bB; /^c/bC;
+    '
+  } |yield_page channel
+
+  nicklist leave
+fi