]> git.plutz.net Git - httpchat/commitdiff
improved nick handling, allow disabling registration
authorPaul Hänsch <paul@plutz.net>
Wed, 18 Nov 2020 21:52:24 +0000 (22:52 +0100)
committerPaul Hänsch <paul@plutz.net>
Wed, 18 Nov 2020 21:52:24 +0000 (22:52 +0100)
index.cgi
usernick.sh

index ff9e563f356689f95115ddb1aaac6f4f4df42bbe..ac9a110149f9e8b79a992b946b4cd6321c2df85c 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -12,6 +12,12 @@ LOCATION="$(PATH "$PATH_INFO")"
 LOCATION="${LOCATION#/}"
 LOCATION="${LOCATION%%/*}"
 
+NICK_REGISTRATION="${NICK_REGISTRATION:-on}"
+
+# ToDo:
+# COOKIE_NICK_EXPIRE=$((86400 * 14))
+# REGEISTERED_NICK_EXPIRE=$((86400 * 365))
+
 yield_page(){
   page="$1"
   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
@@ -42,9 +48,9 @@ settings_menu(){
   fi
   printf '
       [a .section href="#nick" Nickname]
-      [div #nick [input name="nickname" value="%s"][submit "action" "nick" Set Cookie]]
+      [div #nick [input name="nickname" placeholder="%s"][submit "action" "nick" Set Cookie]]
   ' "$(HTML "${nickname#\?}")"
-  printf '
+  [ "$NICK_REGISTRATION" = on -a "$nickname" != '?Guest' ] && printf '
       [a .section href="#register" Register Nickname]
       [div #register
         [p Registration will set a permanent Cookie in your Browser.
index 3880fbe61972c6331b038ac3211b8ffbfa8ed925..13759eee48fb57169a599e8b2a1bc2ed339cac81 100755 (executable)
@@ -36,6 +36,8 @@ else
     SET_COOKIE +"$((86400 * 365))" "user_client=${clientid}" HttpOnly
     SET_COOKIE +"$((86400 * 365))" "nick=$(URL "${nickname}")"
   else
+    export ERROR=".nick Your current nickname has been registered elsewhere."
+    QUERY_STRING=settings
     nickname='?Guest'
   fi
 fi
@@ -43,17 +45,30 @@ fi
 case $(POST action) in
   nick)
     nick="$(POST nickname |username)"
-    if [ ! -d "$_DATA/@$nick" ]; then
-      SET_COOKIE +1209600 "nick=$(POST nickname |URL)"
+    if [ -d "$_DATA/@$nick" ]; then
+      export ERROR=".nick This nickname has already been registered elsewhere."
+    elif [ -z "$(POST nickname)" ]; then
+      SET_COOKIE +1209600 "nick=$(URL "${nickname#\?}")"
       REDIRECT "$(URL "/$LOCATION")"
+    elif [ -z "$nick" ]; then
+      export ERROR='.nick Nicknames must be between 3 and 24 characters long. They may not start with "&", "#", "?", "@", or "+" and they must not contain a "/".'
     else
-      export ERROR=".nick This nickname has already been registered elsewhere"
+      SET_COOKIE +1209600 "nick=$(URL $nick)"
+      REDIRECT "$(URL "/$LOCATION")"
     fi
     ;;
   register)
     regnick="$(POST regnick |username)"
     userdir="$_DATA/@${regnick}"
-    if [ "$regnick" ] && mkdir "$userdir"; then
+    if [ "$NICK_REGISTRATION" != on ]; then
+      export ERROR='.register Nickname registration is disabled on this server.'
+    elif [ "$regnick" = Guest ]; then
+      export ERROR='.register The name "Guest" may not be registered as a permanent nickname.'
+    elif [ -z "$regnick" ]; then
+      export ERROR='.register Nicknames must be between 3 and 24 characters long. They may not start with "&", "#", "?", "@", or "+" and they must not contain a "/".'
+    elif [ -d "$userdir" ]; then
+      export ERROR=".register This nickname has already been registered elsewhere."
+    elif mkdir "$userdir"; then
       secuid="$(randomid)"; clientid="$(randomid)"
       printf %s\\n "$secuid" >"${userdir}/secuid"
       clientid="${clientid}-$(printf '%s%s' "${clientid}" "${secuid}" |sha256sum |cut -d\  -f1)"
@@ -61,7 +76,7 @@ case $(POST action) in
       SET_COOKIE +"$((86400 * 365))" "nick=$(URL "${regnick}")"
       REDIRECT "$(URL "/$LOCATION")"
     else
-      export ERROR=".register This nickname has already been registered elsewhere"
+      export ERROR=".register Registration failed, possibly due to faulty server configuration."
     fi
     ;;
 esac