]> git.plutz.net Git - httpchat/blobdiff - index.cgi
fix syntax error m)
[httpchat] / index.cgi
index e84122c467e0cf93e4c75a77db9f2d1f3ce7eeaa..23139976acc4fdd5dd0e135dd4421193dd1e5100 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
 #!/bin/sh
 
-_EXEC=.
-_DATA=.
+_EXEC="${_EXEC:-.}"
+_DATA="${_DATA:-.}"
+SESSION_TIMEOUT=43200
 . "$_EXEC/cgilite/logging.sh"
 . "$_EXEC/cgilite/cgilite.sh"
 . "$_EXEC/cgilite/session.sh"
 . "$_EXEC/cgilite/storage.sh"
 
-
 LOCATION="$(PATH "$PATH_INFO")"
 LOCATION="${LOCATION#/}"
 LOCATION="${LOCATION%%/*}"
 
-yield_css(){
-  printf 'Content-Type: text/css; charset=utf-8\r\n\r\n'
-  cat <<ENDCSS
-* { box-sizing: border-box;}
-#chat {
-  position: fixed;
-  bottom: 2.5em;
-  left: 0; right: 0;
-  border: 1px solid #08b;
-  padding: 1ex;
-  margin: .5ex;
-  z-index: -1;
-}
-form#channel {
-  position: fixed;
-  bottom: 0;
-  left: 0; right: 0;
-}
-
-#check_settings { display: none; }
-#check_settings + label {
-  display: inline-block;
-}
-#check_settings + label:before {
-  content: '\2699';
-  padding: .5ex;
-  margin-left: .5ex;
-  margin-right: 2em;
-}
-#check_settings + label + #settings { display: none; }
-#check_settings:checked + label + #settings {
-  display: block;
-  position: fixed;
-  min-width: 20%; max-width: 90%;
-  width: 30em;
-  top: 3em;
-  left: 50%; transform: translate(-50%);
-  background-color: #FFF;
-  border: 1px solid;
-  border-radius: 1ex 1ex .5ex .5ex;
-}
-#settings h1 {
-  background-color: #08b;
-  margin: 0;
-  padding: 0 1ex;
-  font-size: 1em;
-  font-weight: bold;
-  border-bottom: 1px solid;
-  border-radius: 1ex 1ex 0 0;
-}
-#settings label[for=check_settings] {
-  position: absolute;
-  top: 0; right: 1px;
-  background-color: #F88;
-  border-left: 1px solid;
-  border-radius: 0 1ex 0 0;
-  width: 3ex;
-  overflow: hidden;
-}
-#settings label[for=check_settings]:before {
-  content: "x";
-  padding: 0 1ex;
-}
-#settings input[type=radio] { display: none; }
-#settings input[type=radio] + label + * { display: none; }
-#settings input[type=radio]:checked + label + * { display: block; }
-#settings input[type=radio] + label {
-  display: block;
-  font-weight: bold;
-  text-decoration: underline;
-  margin: -1px 1px; padding: 0 1ex;
-  border-top: 1px solid;
-  background-color: #EEE;
-}
-#settings input[type=radio] + label + * {
-  padding: 1ex 1ex .5ex 1ex;
-}
+NICK_REGISTRATION="${NICK_REGISTRATION:-on}"
+CHANNEL_LIST="${CHANNEL_LIST:-off}"
 
-form#channel input[name=message] {
-  display: inline-block;
-  position: absolute;
-  left: 4.5ex;
-  width: calc(100% - 5ex - 1px);
-}
-form#channel button[value=submit] { display: none; }
-#chat .message .date {
-  color: #888;
-  font-size: .75em;
-}
-#chat .message .nick {
-  font-weight: bold;
-}
-#chat .message .nick .indicator {
-  color: #888;
-}
-
-ENDCSS
-}
+# ToDo:
+# COOKIE_NICK_EXPIRE=$((86400 * 14))
+# REGEISTERED_NICK_EXPIRE=$((86400 * 365))
 
 yield_page(){
   page="$1"
-  printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
+  printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
+                  "Content-Security-Policy: script-src 'none'" \
+                  ''
   { printf '[html
     [head
       [meta name="viewport" content="width=device-width"]
@@ -121,116 +31,92 @@ yield_page(){
       [title Webchat]
     ] [body class="%s"
   ' "$page"
+  [ "$QUERY_STRING" = settings ] && settings_menu
   cat
   printf '] ]'
   } |"$_EXEC/cgilite/html-sh.sed" -u
 }
 
 settings_menu(){
+  local nick="$(HTML "${nickname#\?}")"
   printf '
-    [input #check_settings type="checkbox"][label for=check_settings Settings]
-    [div #settings
-      [h1 Settings][label for=check_settings Close]
-      [input #set_nick type=radio name="setting" value="nick" selected][label for=set_nick Nickname]
-      [div [input name="nickname" value="%s"][submit "action" "nick" Set Cookie]]
-    ]
-  ' "$(HTML "${nickname#\?}")"
+    [form #settings method="POST" action="?settings"
+      [hidden "session_key" "%s"]
+      [h1 Settings][a .settings href="?" Close]
+  ' "$SESSION_KEY"
+  if [ "$ERROR" ]; then
+    printf '[p .error %s %s]' "${ERROR%% *}" "$(HTML "${ERROR#.* }")"
+    unset ERROR
+  fi
+  if [ "$NICK_REGISTRATION" != on -o "$nickname" = '?Guest' ]; then
+    printf '
+      [a .section href="#nick" Nickname]
+      [div #nick .open [input name="nickname" placeholder="%s"][submit "action" "nick" Set Cookie]]
+    ' "$nick"
+  else
+    printf '
+      [a .section href="#nick" Nickname]
+      [div #nick [input name="nickname" placeholder="%s"][submit "action" "nick" Set Cookie]]
+      [a .section href="#register" Register Nickname]
+      [div #register
+        [p Registration will set a permanent Cookie in your Browser.
+           Registration requires neither a password, nor an email address.]
+        [input name="regnick" value="%s"][submit "action" "register" Register]
+      ]' "$nick" "$nick"
+  fi
+  printf ']'
 }
 
-case ${LOCATION} in
-  \&?*) chatfile="$_DATA/${LOCATION}"
-       page=channel
-   ;;
-  @?*) if [ -d "$_DATA/${LOCATION}" ]; then
-        chatfile="$_DATA/${LOCATION}/?${SESSION_ID}"
-        page=channel
-      else
-        REDIRECT /
-      fi
-   ;;
-  ~?*) if [ -d "$_DATA/@${LOCATION#~}" ]; then
-        pubinfo="$_DATA/@${LOCATION#~}/pubinfo"
-        page=pubinfo
-      else 
-        REDIRECT /
-      fi
-   ;;
-  webchat.css) yield_css; exit 0;;
-  '') page=front;;
-  *) REDIRECT /;;
-esac
-
-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)"
-fi
+. "$_EXEC/usernick.sh"
 
-case "$page $(POST action)" in
-  channel\ create)
-    if [ ! -f "$chatfile" ]; then
-      { randomid; printf ' '; STRING "$nickname"; echo; } >"$chatfile"
-    fi
-    REDIRECT "$(URL "/$LOCATION")"
+case ${LOCATION} in
+  webchat.css)
+    . "$_EXEC/cgilite/file.sh"
+    FILE "$_EXEC/${LOCATION}"
+    return 0
+    ;;
+  \&?*)
+    [ "$(COOKIE nick)" -o "$QUERY_STRING" = settings ] || REDIRECT "/$LOCATION?settings#nick"
+    chatfile="$_DATA/${LOCATION}/channel"
+    . "$_EXEC/channel.sh"
+    exit 0
     ;;
-  channel\ submit)
-    if [ -f "$chatfile" -a "$channelkey" = "$(POST channelkey)" ]; then
-      printf "%s %s: %s\n" "$(date +%F_%T)" "$(STRING "$nickname")" "$(POST message |STRING)" >>"$chatfile"
+  @?*)
+    if [ -d "$_DATA/${LOCATION}" ]; then
+      chatfile="$_DATA/${LOCATION}/?${SESSION_ID}"
+      . "$_EXEC/channel.sh"
+    else
+      REDIRECT /
     fi
-    REDIRECT "$(URL "/$LOCATION")"
+    exit 0
     ;;
-  channel\ nick)
-    SET_COOKIE +1209600 "nick=$(POST nickname |URL)"
-    REDIRECT "$(URL "/$LOCATION")"
+  ~?*)
+    if [ -d "$_DATA/@${LOCATION#~}" ]; then
+      pubinfo="$_DATA/@${LOCATION#~}/pubinfo"
+    else 
+      # ToDo Edit / Display of public user information
+      REDIRECT /
+    fi
     ;;
-  channel\ *)
-    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]
-        ]
-       EOF
+  '')
+    if [ "$(POST action)" = join ]; then
+      REDIRECT "./&$(POST channel |URL)"
     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;'
-
-      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 
-    ;;
-  pubinfo\ *);;
-  front\ *) yield_page front <<-EOF
-       Front
+      yield_page front <<-EOF
+       [h1 Webchat]
+       [form method=POST
+         [label Join Channel: [input name=channel value='' placeholder="Name of Channel"][button type=submit name=action value=join Join]]
+       ]
+       $(if [ "$CHANNEL_LIST" = on ]; then
+         printf '[h2 Existing channels][div #channels'
+         for chan in "$_DATA/&"*; do
+           printf '[a href="./%s" . %s]\n' "$(HTML "${chan##*/}")" "$(HTML "${chan##*/}")"
+         done
+         printf ']'
+       fi)
        EOF
+    fi
+    ;;
+  *) REDIRECT /
     ;;
 esac