]> git.pond.sub.org Git - empserver/blobdiff - src/lib/player/login.c
Update known contributors comments
[empserver] / src / lib / player / login.c
index 677d6b4798c1c52af7c7288560f9832bb774617a..28b5311c09839bbdcdbe82097cdfa196c03bb59d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *  ---
  *
  *  login.c: Allow the player to login
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1994
  *     Steve McClure, 2000
- *     Markus Armbruster, 2004-2005
+ *     Markus Armbruster, 2004-2008
+ *     Ron Koenderink, 2005-2009
  */
 
 #include <config.h>
 
-#include <string.h>
-#include "prototypes.h"
-#include "misc.h"
-#include "proto.h"
-#include "player.h"
 #include "com.h"
-#include "empthread.h"
 #include "empio.h"
+#include "empthread.h"
+#include "file.h"
 #include "match.h"
-#include "nsc.h"
+#include "misc.h"
 #include "nat.h"
+#include "nsc.h"
 #include "optlist.h"
-#include "file.h"
-#include "subs.h"
-#include "common.h"
-#include "gen.h"
-
-#if !defined(_WIN32)
-#include <netinet/in.h>
-#endif
-#include <errno.h>
+#include "player.h"
+#include "proto.h"
+#include "prototypes.h"
 
-static struct cmndstr login_coms[];
+static int client_cmd(void);
+static int coun_cmd(void);
+static int kill_cmd(void);
+static int options_cmd(void);
+static int pass_cmd(void);
+static int play_cmd(void);
+static int quit_cmd(void);
+static int sanc_cmd(void);
+static int user_cmd(void);
 
+static struct cmndstr login_coms[] = {
+    {"client client-id...", 0, client_cmd, 0, 0},
+    {"coun country", 0, coun_cmd, 0, 0},
+    {"kill", 0, kill_cmd, 0, 0},
+    {"options option=value...", 0, options_cmd, 0, 0},
+    {"pass password", 0, pass_cmd, 0, 0},
+    {"play [user [country [password]]]", 0, play_cmd, 0, 0},
+    {"quit", 0, quit_cmd, 0, 0},
+    {"sanc", 0, sanc_cmd, 0, 0},
+    {"user name", 0, user_cmd, 0, 0},
+    {0, 0, 0, 0, 0}
+};
 
 /*ARGSUSED*/
 void
@@ -68,18 +80,24 @@ player_login(void *ud)
     char space[128];
     int ac;
     int cmd;
+    int res;
 
     player->proc = empth_self();
 
     pr_id(player, C_INIT, "Empire server ready\n");
 
-    while (!io_eof(player->iop) && !io_error(player->iop)) {
+    while (player->state != PS_SHUTDOWN) {
        io_output(player->iop, IO_WAIT);
        if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
-           io_input(player->iop, IO_WAIT);
+           res = io_input(player->iop, IO_WAIT);
+           if (res <= 0) {
+               if (res == 0 && !io_eof(player->iop))
+                   pr_id(player, C_DATA, "idle connection terminated\n");
+               break;
+           }
            continue;
        }
-       ac = parse(buf, player->argp, NULL, space, NULL);
+       ac = parse(buf, space, player->argp, NULL, NULL, NULL);
        cmd = comtch(player->argp[0], login_coms, 0);
        if (cmd < 0) {
            pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
@@ -96,8 +114,6 @@ player_login(void *ud)
        default:
            break;
        }
-       if (player->state >= PS_SHUTDOWN)
-           break;
     }
     player->state = PS_SHUTDOWN;
     if (!io_eof(player->iop)) {
@@ -113,17 +129,23 @@ player_login(void *ud)
 static int
 client_cmd(void)
 {
-    int i;
+    int i, sz;
+    char *p, *end;
 
     if (!player->argp[1])
        return RET_SYN;
 
+    p = player->client;
+    end = player->client + sizeof(player->client) - 1;
     for (i = 1; player->argp[i]; ++i) {
        if (i > 1)
-           strncat(player->client, " ", sizeof(player->client) - 1);
-       strncat(player->client, player->argp[i], sizeof(player->client) - 1);
+           *p++ = ' ';
+       sz = strlen(player->argp[i]);
+       sz = MIN(sz, end - p);
+       memcpy(p, player->argp[i], sz);
+       p += sz;
     }
-    player->client[sizeof(player->client) - 1] = '\0';
+    *p = 0;
     pr_id(player, C_CMDOK, "talking to %s\n", player->client);
     return RET_OK;
 }
@@ -191,7 +213,7 @@ pass_cmd(void)
 {
     if (!player->argp[1])
        return RET_SYN;
-    if (player->cnum == 255) {
+    if (player->cnum == NATID_BAD) {
        pr_id(player, C_CMDERR, "need country first\n");
        return RET_FAIL;
     }
@@ -220,7 +242,8 @@ options_cmd(void)
        int val;
     };
     static struct logoptstr login_opts[] = {
-       { "utf-8", PF_UTF8 }
+       { "utf-8", PF_UTF8 },
+       { NULL, 0 }
     };
     char **ap;
     char *p;
@@ -228,7 +251,7 @@ options_cmd(void)
     unsigned i;
 
     if (!player->argp[1]) {
-       for (i = 0; i < sizeof(login_opts) / sizeof(*login_opts); ++i)
+       for (i = 0; login_opts[i].name; ++i)
            pr_id(player, C_DATA, "%s=%d\n",
                  login_opts[i].name,
                  (player->flags & login_opts[i].val) != 0);
@@ -263,7 +286,7 @@ may_play(void)
 {
     struct natstr *np;
 
-    if (player->cnum == 255 || !player->authenticated) {
+    if (player->cnum == NATID_BAD || !player->authenticated) {
        pr_id(player, C_CMDERR, "need country and password\n");
        return 0;
     }
@@ -290,6 +313,7 @@ play_cmd(void)
     natid cnum;
     struct natstr *natp;
     char **ap;
+    char buf[128];
 
     ap = player->argp;
     if (*++ap) {
@@ -325,6 +349,8 @@ play_cmd(void)
        }
        return RET_FAIL;
     }
+    snprintf(buf, sizeof(buf), "Play#%d", player->cnum);
+    empth_set_name(empth_self(), buf);
     logerror("%s logged in as country #%d", praddr(player), player->cnum);
     pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
     player_main(player);
@@ -358,16 +384,3 @@ quit_cmd(void)
     io_shutdown(player->iop, IO_READ);
     return RET_OK;
 }
-
-static struct cmndstr login_coms[] = {
-    {"client client-id...", 0, client_cmd, 0, 0},
-    {"coun country", 0, coun_cmd, 0, 0},
-    {"kill", 0, kill_cmd, 0, 0},
-    {"options option=value...", 0, options_cmd, 0, 0},
-    {"pass password", 0, pass_cmd, 0, 0},
-    {"play [user [country [password]]]", 0, play_cmd, 0, 0},
-    {"quit", 0, quit_cmd, 0, 0},
-    {"sanc", 0, sanc_cmd, 0, 0},
-    {"user name", 0, user_cmd, 0, 0},
-    {0, 0, 0, 0, 0}
-};