]> git.plutz.net Git - httpchat/blob - index.cgi
ac9a110149f9e8b79a992b946b4cd6321c2df85c
[httpchat] / index.cgi
1 #!/bin/sh
2
3 _EXEC="${_EXEC:-.}"
4 _DATA="${_DATA:-.}"
5 SESSION_TIMEOUT=43200
6 . "$_EXEC/cgilite/logging.sh"
7 . "$_EXEC/cgilite/cgilite.sh"
8 . "$_EXEC/cgilite/session.sh"
9 . "$_EXEC/cgilite/storage.sh"
10
11 LOCATION="$(PATH "$PATH_INFO")"
12 LOCATION="${LOCATION#/}"
13 LOCATION="${LOCATION%%/*}"
14
15 NICK_REGISTRATION="${NICK_REGISTRATION:-on}"
16
17 # ToDo:
18 # COOKIE_NICK_EXPIRE=$((86400 * 14))
19 # REGEISTERED_NICK_EXPIRE=$((86400 * 365))
20
21 yield_page(){
22   page="$1"
23   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
24                   "Content-Security-Policy: script-src 'none'" \
25                   ''
26   { printf '[html
27     [head
28       [meta name="viewport" content="width=device-width"]
29       [link rel="stylesheet" type="text/css" href="/webchat.css"]
30       [title Webchat]
31     ] [body class="%s"
32   ' "$page"
33   [ "$QUERY_STRING" = settings ] && settings_menu
34   cat
35   printf '] ]'
36   } |"$_EXEC/cgilite/html-sh.sed" -u
37 }
38
39 settings_menu(){
40   printf '
41     [form #settings method="POST" action="?settings"
42       [hidden "session_key" "%s"]
43       [h1 Settings][a .settings href="?" Close]
44   ' "$SESSION_KEY"
45   if [ "$ERROR" ]; then
46     printf '[p .error %s %s]' "${ERROR%% *}" "$(HTML "${ERROR#.* }")"
47     unset ERROR
48   fi
49   printf '
50       [a .section href="#nick" Nickname]
51       [div #nick [input name="nickname" placeholder="%s"][submit "action" "nick" Set Cookie]]
52   ' "$(HTML "${nickname#\?}")"
53   [ "$NICK_REGISTRATION" = on -a "$nickname" != '?Guest' ] && printf '
54       [a .section href="#register" Register Nickname]
55       [div #register
56         [p Registration will set a permanent Cookie in your Browser.
57            Registration requires neither a password, nor an email address.]
58         [input name="regnick" value="%s"][submit "action" "register" Register]
59       ]' "$(HTML "${nickname#\?}")"
60   printf ']'
61 }
62
63 . "$_EXEC/usernick.sh"
64
65 case ${LOCATION} in
66   webchat.css)
67     . "$_EXEC/cgilite/file.sh"
68     FILE "$_EXEC/${LOCATION}"
69     return 0
70     ;;
71   \&?*)
72     [ "$(COOKIE nick)" -o "$QUERY_STRING" = settings ] || REDIRECT "/$LOCATION?settings#nick"
73     chatfile="$_DATA/${LOCATION}/channel"
74     . "$_EXEC/channel.sh"
75     exit 0
76     ;;
77   @?*)
78     if [ -d "$_DATA/${LOCATION}" ]; then
79       chatfile="$_DATA/${LOCATION}/?${SESSION_ID}"
80       . "$_EXEC/channel.sh"
81     else
82       REDIRECT /
83     fi
84     exit 0
85     ;;
86   ~?*)
87     if [ -d "$_DATA/@${LOCATION#~}" ]; then
88       pubinfo="$_DATA/@${LOCATION#~}/pubinfo"
89     else 
90       # ToDo Edit / Display of public user information
91       REDIRECT /
92     fi
93     ;;
94   '') yield_page front <<-EOF
95         Front
96         EOF
97     ;;
98   *) REDIRECT /
99     ;;
100 esac