]> git.plutz.net Git - httpchat/blob - index.cgi
fix syntax error m)
[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 CHANNEL_LIST="${CHANNEL_LIST:-off}"
17
18 # ToDo:
19 # COOKIE_NICK_EXPIRE=$((86400 * 14))
20 # REGEISTERED_NICK_EXPIRE=$((86400 * 365))
21
22 yield_page(){
23   page="$1"
24   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
25                   "Content-Security-Policy: script-src 'none'" \
26                   ''
27   { printf '[html
28     [head
29       [meta name="viewport" content="width=device-width"]
30       [link rel="stylesheet" type="text/css" href="/webchat.css"]
31       [title Webchat]
32     ] [body class="%s"
33   ' "$page"
34   [ "$QUERY_STRING" = settings ] && settings_menu
35   cat
36   printf '] ]'
37   } |"$_EXEC/cgilite/html-sh.sed" -u
38 }
39
40 settings_menu(){
41   local nick="$(HTML "${nickname#\?}")"
42   printf '
43     [form #settings method="POST" action="?settings"
44       [hidden "session_key" "%s"]
45       [h1 Settings][a .settings href="?" Close]
46   ' "$SESSION_KEY"
47   if [ "$ERROR" ]; then
48     printf '[p .error %s %s]' "${ERROR%% *}" "$(HTML "${ERROR#.* }")"
49     unset ERROR
50   fi
51   if [ "$NICK_REGISTRATION" != on -o "$nickname" = '?Guest' ]; then
52     printf '
53       [a .section href="#nick" Nickname]
54       [div #nick .open [input name="nickname" placeholder="%s"][submit "action" "nick" Set Cookie]]
55     ' "$nick"
56   else
57     printf '
58       [a .section href="#nick" Nickname]
59       [div #nick [input name="nickname" placeholder="%s"][submit "action" "nick" Set Cookie]]
60       [a .section href="#register" Register Nickname]
61       [div #register
62         [p Registration will set a permanent Cookie in your Browser.
63            Registration requires neither a password, nor an email address.]
64         [input name="regnick" value="%s"][submit "action" "register" Register]
65       ]' "$nick" "$nick"
66   fi
67   printf ']'
68 }
69
70 . "$_EXEC/usernick.sh"
71
72 case ${LOCATION} in
73   webchat.css)
74     . "$_EXEC/cgilite/file.sh"
75     FILE "$_EXEC/${LOCATION}"
76     return 0
77     ;;
78   \&?*)
79     [ "$(COOKIE nick)" -o "$QUERY_STRING" = settings ] || REDIRECT "/$LOCATION?settings#nick"
80     chatfile="$_DATA/${LOCATION}/channel"
81     . "$_EXEC/channel.sh"
82     exit 0
83     ;;
84   @?*)
85     if [ -d "$_DATA/${LOCATION}" ]; then
86       chatfile="$_DATA/${LOCATION}/?${SESSION_ID}"
87       . "$_EXEC/channel.sh"
88     else
89       REDIRECT /
90     fi
91     exit 0
92     ;;
93   ~?*)
94     if [ -d "$_DATA/@${LOCATION#~}" ]; then
95       pubinfo="$_DATA/@${LOCATION#~}/pubinfo"
96     else 
97       # ToDo Edit / Display of public user information
98       REDIRECT /
99     fi
100     ;;
101   '')
102     if [ "$(POST action)" = join ]; then
103       REDIRECT "./&$(POST channel |URL)"
104     else
105       yield_page front <<-EOF
106         [h1 Webchat]
107         [form method=POST
108           [label Join Channel: [input name=channel value='' placeholder="Name of Channel"][button type=submit name=action value=join Join]]
109         ]
110         $(if [ "$CHANNEL_LIST" = on ]; then
111           printf '[h2 Existing channels][div #channels'
112           for chan in "$_DATA/&"*; do
113             printf '[a href="./%s" . %s]\n' "$(HTML "${chan##*/}")" "$(HTML "${chan##*/}")"
114           done
115           printf ']'
116         fi)
117         EOF
118     fi
119     ;;
120   *) REDIRECT /
121     ;;
122 esac