shithub: choc

Download patch

ref: 5037bbfa38f1946e465f9d4d0823e2aaa4229b35
parent: 25ae4973fab0cfffe47fbc8373dae8a8715786d7
author: Jonathan Dowland <jon@dow.land>
date: Wed Feb 27 15:39:49 EST 2019

Generate a random "pet" player username

Instead of using environment variables like USER etc which might
leak private information accidentally, generate a random player
name from a pool of nouns and adjectives (e.g. "Feisty Cacodemon").

Inspired by Dustin Kirkland's "petname" library.

Fixes #1141.

--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -79,6 +79,7 @@
     net_sdl.c           net_sdl.h
     net_server.c        net_server.h
     net_structrw.c      net_structrw.h
+    petname.c           petname.h
     sha1.c              sha1.h
     memio.c             memio.h
     tables.c            tables.h
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -45,6 +45,7 @@
 net_query.c          net_query.h           \
 net_server.c         net_server.h          \
 net_structrw.c       net_structrw.h        \
+petname.c            petname.h             \
 z_native.c           z_zone.h
 
 @PROGRAM_PREFIX@server_SOURCES=$(COMMON_SOURCE_FILES) $(DEDSERV_FILES)
@@ -98,6 +99,7 @@
 net_sdl.c            net_sdl.h             \
 net_server.c         net_server.h          \
 net_structrw.c       net_structrw.h        \
+petname.c            petname.h             \
 sha1.c               sha1.h                \
 memio.c              memio.h               \
 tables.c             tables.h              \
@@ -198,6 +200,7 @@
 net_sdl.c            net_sdl.h             \
 net_query.c          net_query.h           \
 net_structrw.c       net_structrw.h        \
+petname.c            petname.h             \
 z_native.c           z_zone.h
 
 if HAVE_WINDRES
--- a/src/net_client.c
+++ b/src/net_client.c
@@ -38,6 +38,7 @@
 #include "net_query.h"
 #include "net_server.h"
 #include "net_structrw.h"
+#include "petname.h"
 #include "w_checksum.h"
 #include "w_wad.h"
 
@@ -1211,20 +1212,10 @@
     // Try to set from the USER and USERNAME environment variables
     // Otherwise, fallback to "Player"
 
-    if (net_player_name == NULL) 
-        net_player_name = getenv("USER");
     if (net_player_name == NULL)
-        net_player_name = getenv("USERNAME");
-
-    // On Windows, environment variables are in OEM codepage
-    // encoding, so convert to UTF8:
-
-#ifdef _WIN32
-    if (net_player_name != NULL)
     {
-        net_player_name = M_OEMToUTF8(net_player_name);
+        net_player_name = getRandomPetName();
     }
-#endif
 
     if (net_player_name == NULL)
         net_player_name = "Player";
--- /dev/null
+++ b/src/petname.c
@@ -1,0 +1,130 @@
+//
+// Copyright(C) 2019 Jonathan Dowland
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// DESCRIPTION:
+//     Generate a randomized, private, memorable name for a Player
+//
+
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+
+static char *adjectives[] = {
+    "Grumpy",
+    "Ecstatic",
+    "Surly",
+    "Prepared",
+    "Crafty",
+    "Alert",
+    "Sluggish",
+    "Testy",
+    "Reluctant",
+    "Languid",
+    "Passive",
+    "Pacifist",
+    "Aggressive",
+    "Hostile",
+    "Diseased",
+    "Bubbly",
+    "Giggly",
+    "Laughing",
+    "Crying",
+    "Frowning",
+    "Flatulent",
+    "Torpid",
+    "Lethargic",
+    "Manic",
+    "Patient",
+    "Protective",
+    "Philosophical",
+    "Enquiring",
+    "Debating",
+    "Furious",
+    "Laid-Back",
+    "Easy-Going",
+    "Cromulent",
+    "Excitable",
+    "Tired",
+    "Exhausted",
+    "Ruminating",
+    "Redundant",
+    "Sporty",
+    "Ginger",
+    "Scary",
+    "Posh",
+    "Baby",
+};
+#define NUM_ADJECTIVES (sizeof adjectives / sizeof (char *))
+
+
+static char *nouns[] = {
+    // Doom
+    "Cacodemon",
+    "Arch-Vile",
+    "Cyberdemon",
+    "Imp",
+    "Demon",
+    "Mancubus",
+    "Arachnotron",
+    "Baron",
+    "Knight",
+    "Revenant",
+    // Hexen
+    "Ettin",
+    "Maulotaur",
+    "Centaur",
+    "Afrit",
+    "Serpent",
+    // Heretic
+    "Disciple",
+    "Gargoyle",
+    "Golem",
+    "Lich",
+    // Strife
+    "Sentinel",
+    "Acolyte",
+    "Templar",
+    "Reaver",
+    "Crusader",
+};
+#define NUM_NOUNS (sizeof nouns / sizeof (char *))
+
+/*
+ * ideally we would export this and the caller would invoke it during
+ * their setup routine. But, the two callers only invoke getRandomPetName
+ * once, so the initialisation might as well occur then.
+ */
+static void initPetName()
+{
+    srandom((unsigned int)time(NULL));
+}
+
+char *getRandomPetName()
+{
+    char *a, *n, *r;
+
+    initPetName();
+
+    a = adjectives[random() % NUM_ADJECTIVES];
+    n = nouns[random() % NUM_NOUNS];
+    r = (char *)malloc(strlen(a) + (sizeof ' ') +
+                       strlen(n) + (sizeof '\0'));
+    if(r)
+    {
+        strcpy(r,a);
+        r[strlen(a)] = ' ';
+        strcpy(r + strlen(a) + 1, n);
+    }
+
+    return r;
+}
--- /dev/null
+++ b/src/petname.h
@@ -1,0 +1,18 @@
+//
+// Copyright(C) 2019 Jonathan Dowland
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// DESCRIPTION:
+//     Generate a randomized, private, memorable name for a Player
+//
+
+char *getRandomPetName();
--- a/src/setup/multiplayer.c
+++ b/src/setup/multiplayer.c
@@ -33,6 +33,8 @@
 #include "net_io.h"
 #include "net_query.h"
 
+#include "petname.h"
+
 #define MULTI_START_HELP_URL "https://www.chocolate-doom.org/setup-multi-start"
 #define MULTI_JOIN_HELP_URL "https://www.chocolate-doom.org/setup-multi-join"
 #define MULTI_CONFIG_HELP_URL "https://www.chocolate-doom.org/setup-multi-config"
@@ -1096,29 +1098,13 @@
 {
     if (net_player_name == NULL)
     {
-        net_player_name = getenv("USER");
+        net_player_name = getRandomPetName();
     }
 
     if (net_player_name == NULL)
     {
-        net_player_name = getenv("USERNAME");
+        net_player_name = M_StringDuplicate("player");
     }
-
-    if (net_player_name == NULL)
-    {
-        net_player_name = "player";
-    }
-
-    // Now strdup() the string so that it's in a mutable form
-    // that can be freed when the value changes.
-
-#ifdef _WIN32
-    // On Windows, environment variables are in OEM codepage
-    // encoding, so convert to UTF8:
-    net_player_name = M_OEMToUTF8(net_player_name);
-#else
-    net_player_name = M_StringDuplicate(net_player_name);
-#endif
 }
 
 void MultiplayerConfig(TXT_UNCAST_ARG(widget), void *user_data)