From 21d38a6bfc3bcfa8c115df26847b7c2b4d986565 Mon Sep 17 00:00:00 2001
From: remoitnane <remoit(DOT)nane(AT)gmail(DOT)com>
Date: Sun, 3 Oct 2010 03:17:37 -0700
Subject: Do not allow empty/whitespace-only party/guild names

Also makes minor style adjustments and cleans up comments.
---
 src/map/clif.c  | 40 ++++++++++++++++++++++++++++++++++------
 src/map/guild.c | 43 ++++++++++++++++++++++++++++++++-----------
 src/map/party.c | 31 ++++++++++++++++++++++++-------
 src/map/tmw.c   | 24 ++++++++++++++++++++++++
 src/map/tmw.h   |  6 +++++-
 5 files changed, 119 insertions(+), 25 deletions(-)

(limited to 'src')

diff --git a/src/map/clif.c b/src/map/clif.c
index 205c222..3cd01ee 100644
--- a/src/map/clif.c
+++ b/src/map/clif.c
@@ -5209,7 +5209,14 @@ int clif_cart_equiplist (struct map_session_data *sd)
 }
 
 /*==========================================
- * �p�[�e�B�쐬����
+ * Relay the result of party creation.
+ *
+ * (R 00fa <flag>.B)
+ *
+ * flag:
+ *  0 The party was created.
+ *  1 The party name is invalid/taken.
+ *  2 The character is already in a party.
  *------------------------------------------
  */
 int clif_party_created (struct map_session_data *sd, int flag)
@@ -5269,7 +5276,7 @@ int clif_party_info (struct party *p, int fd)
 /*==========================================
  * Relay a party invitation.
  *
- * (S 00fe <sender_ID>.l <party_name>.24B)
+ * (R 00fe <sender_ID>.l <party_name>.24B)
  *------------------------------------------
  */
 int clif_party_invite (struct map_session_data *sd,
@@ -5738,7 +5745,15 @@ int clif_mvp_exp (struct map_session_data *sd, int exp)
 }
 
 /*==========================================
- * �M���h�쐬�”ےʒm
+ * Relay the result of guild creation.
+ *
+ * (R 0167 <flag>.B)
+ *
+ * flag:
+ *  0 The guild was created.
+ *  1 The character is already in a guild.
+ *  2 The guild name is invalid/taken.
+ *  3 The Emperium item is required.
  *------------------------------------------
  */
 int clif_guild_created (struct map_session_data *sd, int flag)
@@ -8423,7 +8438,9 @@ void clif_parse_CloseKafra (int fd, struct map_session_data *sd)
 }
 
 /*==========================================
- * �p�[�e�B������
+ * Process request to create a party.
+ *
+ * (S 00f9 <party_name>.24B)
  *------------------------------------------
  */
 void clif_parse_CreateParty (int fd, struct map_session_data *sd)
@@ -8438,7 +8455,14 @@ void clif_parse_CreateParty (int fd, struct map_session_data *sd)
 }
 
 /*==========================================
- * �p�[�e�B������
+ * Process request to create a party.
+ *
+ * (S 01e8 <party_name>.24B <exp>.B <itm>.B)
+ *
+ * Note: Upstream eAthena uses this to
+ *       specify experience/item sharing,
+ *       respectively, but it was left
+ *       incomplete here.
  *------------------------------------------
  */
 void clif_parse_CreateParty2 (int fd, struct map_session_data *sd)
@@ -8590,7 +8614,11 @@ void clif_parse_GM_Monster_Item (int fd, struct map_session_data *sd)
 }
 
 /*==========================================
- * �M���h������
+ * Process request to create a guild.
+ *
+ * (S 0165 <account_ID>.l <guild_name>.24B)
+ *
+ * Note: The account ID seems to be ignored.
  *------------------------------------------
  */
 void clif_parse_CreateGuild (int fd, struct map_session_data *sd)
diff --git a/src/map/guild.c b/src/map/guild.c
index 63c94bf..afa942d 100644
--- a/src/map/guild.c
+++ b/src/map/guild.c
@@ -17,6 +17,7 @@
 #include "mob.h"
 #include "intif.h"
 #include "clif.h"
+#include "tmw.h"
 
 #ifdef MEMWATCH
 #include "memwatch.h"
@@ -324,55 +325,75 @@ int guild_payexp_timer (int tid, unsigned int tick, int id, int data)
 
 //------------------------------------------------------------------------
 
-// �쐬�v��
+/* Process a guild creation request. */
 int guild_create (struct map_session_data *sd, char *name)
 {
+    char pname[24];
+
     nullpo_retr (0, sd);
 
+    strncpy (pname, name, 24);
+    pname[23] = '\0';
+    tmw_TrimStr (pname);
+
+    /* The guild name is empty/invalid. */
+    if (!*pname)
+        clif_guild_created (sd, 2);
+
+    /* Make sure the character isn't already in a guild. */
     if (sd->status.guild_id == 0)
     {
+        /*
+         * Require the "Emperium" (ID 714) item. Note that this is
+         * inadvertently a Snake Egg for us.
+         */
         if (!battle_config.guild_emperium_check
             || pc_search_inventory (sd, 714) >= 0)
         {
             struct guild_member m;
             guild_makemember (&m, sd);
             m.position = 0;
-            intif_guild_create (name, &m);
+            intif_guild_create (pname, &m);
         }
         else
-            clif_guild_created (sd, 3); // �G���y���E�������Ȃ�
+            clif_guild_created (sd, 3);
     }
     else
-        clif_guild_created (sd, 1); // ���łɏ������Ă���
+        clif_guild_created (sd, 1);
 
     return 0;
 }
 
-// �쐬�”�
+/* Relay the result of a guild creation request. */
 int guild_created (int account_id, int guild_id)
 {
     struct map_session_data *sd = map_id2sd (account_id);
 
-    if (sd == NULL)
+    if (!sd)
         return 0;
+
+    /* The guild name is valid and not already taken. */
     if (guild_id > 0)
     {
         struct guild *g;
         sd->status.guild_id = guild_id;
         sd->guild_sended = 0;
+
         if ((g = numdb_search (guild_db, guild_id)) != NULL)
         {
-            printf ("guild: id already exists!\n");
+            printf ("guild_created(): ID already exists!\n");
             exit (1);
         }
+
+        /* The guild was created successfully. */
         clif_guild_created (sd, 0);
+        /* The Emperium item is required. See the note in guild_create(). */
         if (battle_config.guild_emperium_check)
-            pc_delitem (sd, pc_search_inventory (sd, 714), 1, 0);   // �G���y���E������
+            pc_delitem (sd, pc_search_inventory (sd, 714), 1, 0);
     }
     else
-    {
-        clif_guild_created (sd, 2); // �쐬���s�i�����M���h���݁j
-    }
+        clif_guild_created (sd, 2);
+
     return 0;
 }
 
diff --git a/src/map/party.c b/src/map/party.c
index a6e9f24..3277edf 100644
--- a/src/map/party.c
+++ b/src/map/party.c
@@ -15,6 +15,7 @@
 #include "intif.h"
 #include "clif.h"
 #include "skill.h"
+#include "tmw.h"
 
 #ifdef MEMWATCH
 #include "memwatch.h"
@@ -76,19 +77,30 @@ struct party *party_searchname (char *str)
     return p;
 }
 
-// �쐬�v��
+/* Process a party creation request. */
 int party_create (struct map_session_data *sd, char *name)
 {
+    char pname[24];
     nullpo_retr (0, sd);
 
+    strncpy (pname, name, 24);
+    pname[23] = '\0';
+    tmw_TrimStr (pname);
+
+    /* The party name is empty/invalid. */
+    if (!*pname)
+        clif_party_created (sd, 1);
+
+    /* Make sure the character isn't already in a party. */
     if (sd->status.party_id == 0)
-        intif_create_party (sd, name);
+        intif_create_party (sd, pname);
     else
         clif_party_created (sd, 2);
+
     return 0;
 }
 
-// �쐬�”�
+/* Relay the result of a party creation request. */
 int party_created (int account_id, int fail, int party_id, char *name)
 {
     struct map_session_data *sd;
@@ -96,25 +108,30 @@ int party_created (int account_id, int fail, int party_id, char *name)
 
     nullpo_retr (0, sd);
 
-    if (fail == 0)
+    /* The party name is valid and not already taken. */
+    if (!fail)
     {
         struct party *p;
         sd->status.party_id = party_id;
+
         if ((p = numdb_search (party_db, party_id)) != NULL)
         {
-            printf ("party: id already exists!\n");
+            printf ("party_created(): ID already exists!\n");
             exit (1);
         }
+
         p = (struct party *) aCalloc (1, sizeof (struct party));
         p->party_id = party_id;
         memcpy (p->name, name, 24);
         numdb_insert (party_db, party_id, p);
+
+        /* The party was created successfully. */
         clif_party_created (sd, 0);
     }
+
     else
-    {
         clif_party_created (sd, 1);
-    }
+
     return 0;
 }
 
diff --git a/src/map/tmw.c b/src/map/tmw.c
index 783875a..a71c93c 100644
--- a/src/map/tmw.c
+++ b/src/map/tmw.c
@@ -162,3 +162,27 @@ void tmw_GmHackMsg (const char *fmt, ...)
                              battle_config.hack_info_GM_level, outbuf,
                              strlen (outbuf) + 1);
 }
+
+/* Remove leading and trailing spaces from a string, modifying in place. */
+void tmw_TrimStr (char *str)
+{
+    char *l;
+    char *a;
+    char *e;
+
+    if (!*str)
+        return;
+
+    e = str + strlen (str) - 1;
+
+    /* Skip all leading spaces. */
+    for (l = str; *l && isspace (*l); ++l)
+        ;
+
+    /* Find the end of the string, or the start of trailing spaces. */
+    for (a = e; *a && a > l && isspace (*a); --a)
+        ;
+
+    memmove (str, l, a - l + 1);
+    str[a - l + 1] = '\0';
+}
diff --git a/src/map/tmw.h b/src/map/tmw.h
index 394998d..5141159 100644
--- a/src/map/tmw.h
+++ b/src/map/tmw.h
@@ -1,4 +1,6 @@
 //
+#ifndef _TMW_H_
+#define _TMW_H_
 
 #include "map.h"
 
@@ -6,5 +8,7 @@ int  tmw_CheckChatSpam (struct map_session_data *sd, char *message);
 int  tmw_ShorterStrlen (char *s1, char *s2);
 int  tmw_CheckChatLameness (struct map_session_data *sd, char *message);
 void tmw_GmHackMsg (const char *fmt, ...);
-void tmw_AutoBan(struct map_session_data *sd, char *reason, int length);
+void tmw_AutoBan (struct map_session_data *sd, char *reason, int length);
+void tmw_TrimStr (char *str);
 
+#endif /* _TMW_H_ */
-- 
cgit v1.2.3-70-g09d2