From: Paul Hänsch Date: Wed, 20 Nov 2019 20:27:48 +0000 (+0100) Subject: split up in multiple files X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=1cd692ed9af229a0118fb77b77bcbd889a0adcbf;p=httpchat split up in multiple files --- diff --git a/channel.sh b/channel.sh new file mode 100755 index 0000000..eab42fc --- /dev/null +++ b/channel.sh @@ -0,0 +1,70 @@ +#!/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)" +fi + +case $(POST action) in + create) + if [ ! -f "$chatfile" ]; then + { randomid; printf ' '; STRING "$nickname"; echo; } >"$chatfile" + fi + REDIRECT "$(URL "/$LOCATION")" + ;; + submit) + if [ -f "$chatfile" -a "$channelkey" = "$(POST channelkey)" ]; 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")" + ;; + *) 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 + 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 + ;; +esac diff --git a/index.cgi b/index.cgi index e84122c..2daabd8 100755 --- a/index.cgi +++ b/index.cgi @@ -7,110 +7,10 @@ _DATA=. . "$_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 <"$chatfile" - fi - REDIRECT "$(URL "/$LOCATION")" - ;; - channel\ submit) - if [ -f "$chatfile" -a "$channelkey" = "$(POST channelkey)" ]; then - printf "%s %s: %s\n" "$(date +%F_%T)" "$(STRING "$nickname")" "$(POST message |STRING)" >>"$chatfile" - fi - REDIRECT "$(URL "/$LOCATION")" - ;; - channel\ nick) - SET_COOKIE +1209600 "nick=$(POST nickname |URL)" - REDIRECT "$(URL "/$LOCATION")" - ;; - 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 - 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 diff --git a/webchat.css b/webchat.css new file mode 100644 index 0000000..4f36157 --- /dev/null +++ b/webchat.css @@ -0,0 +1,102 @@ +* { + box-sizing: border-box; + background-color: #FFF; + color: #000; + font: normal normal normal medium/1.25 Sans-Serif; + font: normal normal normal normal medium/1.25 Sans-Serif; + text-decoration: none; + margin: 0; padding: 0; + border: none; +} + +#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; +} + +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; +}