]> git.plutz.net Git - httpchat/blob - usernick.sh
fix syntax error m)
[httpchat] / usernick.sh
1 #!/bin/sh
2
3 UNAME_VALID='
4   # Remove trailing CR, which may have been added by browser
5   s;\r$;;;
6   # Collapse white spaces
7   s;[\r\t\n ]+; ;g;
8   # Remove starting and trailing white spaces
9   s;^ ;;; s; $;;;
10   # Usernames starting with & # ? @ + will be invalid
11   /^[&#?@+]/d;
12   # Usernames containing a / will be invalid
13   /\//d;
14   # Usernames must be between 3 and 24 characters
15   /...+/!d; /.{25}/d;
16   # Usernames may not span multiple lines
17   q;'
18 username(){
19   { [ $# -eq 0 ] && cat || printf %s "$*"; } \
20   | sed -E ':X; $!{N;bX;}'"$UNAME_VALID"
21 }
22
23 nickname="$(COOKIE nick |username)"
24 if [ ! "$nickname" ]; then
25   nickname='?Guest'
26 elif [ ! -d "$_DATA/@$nickname" ]; then
27   nickname="?$nickname"
28 else
29   userclient="$(COOKIE user_client)"
30   secuid="$(cat "$_DATA/@$nickname/secuid")"
31   clientid="${userclient%%-*}"
32   clientid="${clientid}-$(printf '%s%s' "${clientid}" "${secuid}" |sha256sum)"
33   clientid="${clientid%% *}"
34   if [ "$clientid" = "$userclient" ]; then
35     nickname=" $nickname"
36     SET_COOKIE +"$((86400 * 365))" "user_client=${clientid}" HttpOnly
37     SET_COOKIE +"$((86400 * 365))" "nick=$(URL "${nickname}")"
38   else
39     export ERROR=".nick Your current nickname has been registered elsewhere."
40     QUERY_STRING=settings
41     nickname='?Guest'
42   fi
43 fi
44
45 case $(POST action) in
46   nick)
47     nick="$(POST nickname |username)"
48     if [ -d "$_DATA/@$nick" ]; then
49       export ERROR=".nick This nickname has already been registered elsewhere."
50     elif [ -z "$(POST nickname)" ]; then
51       SET_COOKIE +1209600 "nick=$(URL "${nickname#\?}")"
52       REDIRECT "$(URL "/$LOCATION")"
53     elif [ -z "$nick" ]; then
54       export ERROR='.nick Nicknames must be between 3 and 24 characters long. They may not start with "&", "#", "?", "@", or "+" and they must not contain a "/".'
55     else
56       SET_COOKIE +1209600 "nick=$(URL $nick)"
57       REDIRECT "$(URL "/$LOCATION")"
58     fi
59     ;;
60   register)
61     regnick="$(POST regnick |username)"
62     userdir="$_DATA/@${regnick}"
63     if [ "$NICK_REGISTRATION" != on ]; then
64       export ERROR='.register Nickname registration is disabled on this server.'
65     elif [ "$regnick" = Guest ]; then
66       export ERROR='.register The name "Guest" may not be registered as a permanent nickname.'
67     elif [ -z "$regnick" ]; then
68       export ERROR='.register Nicknames must be between 3 and 24 characters long. They may not start with "&", "#", "?", "@", or "+" and they must not contain a "/".'
69     elif [ -d "$userdir" ]; then
70       export ERROR=".register This nickname has already been registered elsewhere."
71     elif mkdir "$userdir"; then
72       secuid="$(randomid)"; clientid="$(randomid)"
73       printf %s\\n "$secuid" >"${userdir}/secuid"
74       clientid="${clientid}-$(printf '%s%s' "${clientid}" "${secuid}" |sha256sum |cut -d\  -f1)"
75       SET_COOKIE +"$((86400 * 365))" "user_client=${clientid}" HttpOnly
76       SET_COOKIE +"$((86400 * 365))" "nick=$(URL "${regnick}")"
77       REDIRECT "$(URL "/$LOCATION")"
78     else
79       export ERROR=".register Registration failed, possibly due to faulty server configuration."
80     fi
81     ;;
82 esac