]> git.pond.sub.org Git - empserver/blobdiff - src/lib/player/player.c
(execute): Use player->comtail[1] instead of player->argp[1]. This
[empserver] / src / lib / player / player.c
index 8175c5082f338c4915f25ff016e48f291a4e10e7..89f63a7d0e033d2d606292ab50b297b3147546ae 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  * 
  *  Known contributors to this file:
  *     Steve McClure, 2000
- *     
  */
 
-#include "prototypes.h"
-#include <string.h>
-#include "gamesdef.h"
+#include <config.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include "com.h"
+#include "empio.h"
+#include "empthread.h"
+#include "file.h"
+#include "journal.h"
 #include "misc.h"
+#include "nat.h"
+#include "optlist.h"
 #include "player.h"
 #include "proto.h"
-#include "var.h"
-#include "com.h"
-#include "nat.h"
+#include "prototypes.h"
 #include "sect.h"
-#include "file.h"
-#include "proto.h"
-#include "empio.h"
-#include "empthread.h"
 #include "tel.h"
-#include "gen.h"
-#include "subs.h"
-#include "common.h"
-#include "optlist.h"
 
-#if !defined(_WIN32)
-#include <unistd.h>
-#include <sys/time.h>
-#endif
-#include <stdio.h>
-#include <fcntl.h>
+
+static int command(void);
+static int status(void);
 
 struct player *player;
 
@@ -66,25 +60,21 @@ player_main(struct player *p)
 {
     struct natstr *natp;
     int secs;
-    s_char buf[128];
+    char buf[128];
 
     p->state = PS_PLAYING;
     player = p;
     time(&player->lasttime);
-    (void)time(&player->curup);
-    showvers(CLIENTPROTO);
+    time(&player->curup);
     show_motd();
-    if (init_nats() < 0)
-       return;
-    natp = getnatp(player->cnum);
-    if (player->god && !match_user(authfil, player)) {
-       logerror("NON-AUTHed Login attempted by %s", praddr(player));
-       pr("You're not a deity!\n");
+    if (init_nats() < 0) {
+       pr("Server confused, try again later\n");
        return;
     }
+    natp = getnatp(player->cnum);
     if (!gamehours(player->curup)) {
        pr("Empire hours restriction in force\n");
-       if ((natp->nat_stat & STAT_GOD) == 0)
+       if (natp->nat_stat != STAT_GOD)
            return;
     }
     daychange(player->curup);
@@ -92,38 +82,25 @@ player_main(struct player *p)
        pr("Time exceeded today\n");
        return;
     }
-    if ((*natp->nat_hostaddr &&
-        *player->hostaddr &&
-        strcmp(natp->nat_hostaddr, player->hostaddr)) ||
-       (*natp->nat_userid &&
-        *player->userid && strcmp(natp->nat_userid, player->userid))) {
-       if (natp->nat_stat != VIS) {
-           pr("Last connection from: %s", ctime(&natp->nat_last_login));
-           pr("                  to: %s", natp->nat_last_login <
-              natp->nat_last_logout ? ctime(&natp->
-                                            nat_last_logout) : "?");
-           pr("                  by: %s@%s\n",
-              *natp->nat_userid ? natp->nat_userid : (s_char *)"nobody",
-              *natp->nat_hostname ? natp->nat_hostname : *natp->
-              nat_hostaddr ? natp->nat_hostaddr : (s_char *)"nowhere");
-       }
+    if (natp->nat_stat != STAT_VIS
+       && natp->nat_last_login
+       && (strcmp(natp->nat_hostaddr, player->hostaddr)
+           || strcmp(natp->nat_userid, player->userid))) {
+       pr("Last connection from: %s", ctime(&natp->nat_last_login));
+       pr("                  to: %s",
+          natp->nat_last_login <= natp->nat_last_logout
+          ? ctime(&natp->nat_last_logout) : "?");
+       pr("                  by: %s@%s\n",
+          natp->nat_userid,
+          *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
     }
-    if (*player->userid)
-       strcpy(natp->nat_userid, player->userid);
-    else
-       strcpy(natp->nat_userid, "nobody");
-
-    if (*player->hostname)
-       strcpy(natp->nat_hostname, player->hostname);
-    else
-       strcpy(natp->nat_hostname, "nowhere");
-
-    if (*player->hostaddr)
-       strcpy(natp->nat_hostaddr, player->hostaddr);
+    strcpy(natp->nat_userid, player->userid);
+    strcpy(natp->nat_hostname, player->hostname);
+    strcpy(natp->nat_hostaddr, player->hostaddr);
 
     time(&natp->nat_last_login);
-    natp->nat_connected = 1;
     putnat(natp);
+    journal_login();
     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
        if (natp->nat_tgms == 1)
            pr("You have a new telegram waiting ...\n");
@@ -137,6 +114,7 @@ player_main(struct player *p)
        if (command() == 0 && !player->aborted)
            break;
        player->aborted = 0;
+       empth_yield();
     }
     /* #*# I put the following line in to prevent server crash -KHS */
     natp = getnatp(player->cnum);
@@ -145,51 +123,41 @@ player_main(struct player *p)
      * charging at least 15 seconds.
      */
     time(&natp->nat_last_logout);
-    secs = max(natp->nat_last_logout - player->lasttime, 15);
+    secs = MAX(natp->nat_last_logout - player->lasttime, 15);
     natp->nat_minused += secs / 60;
     secs = secs % 60;
     if (chance(secs / 60.0))
        natp->nat_minused += 1;
-    natp->nat_connected = 0;
     putnat(natp);
     pr("Bye-bye\n");
+    journal_logout();
 }
 
-int
+static int
 command(void)
 {
-    register unsigned int x;
-    s_char *redir;
-    s_char scanspace[1024];
+    char *redir;               /* UTF-8 */
+    char scanspace[1024];
 
     if (getcommand(player->combuf) < 0)
        return 0;
-    if (parse(player->combuf, player->argp, &player->condarg,
-             scanspace, &redir) < 0) {
+    if (parse(player->combuf, scanspace, player->argp, player->comtail,
+             &player->condarg, &redir) < 0) {
        pr("See \"info Syntax\"?\n");
     } else {
-       /* XXX don't use alarm; use a scavenger thread */
-       /* DONT USE IT!!!! alarm and sleep may and dont work
-          together -- Sasha */
-       /* alarm((unsigned int)60*60); 1 hour */
-       if (player->condarg != (s_char *)0)
-           for (x = 0; x < strlen(player->condarg); x++)
-               if (isupper(*(player->condarg + x)))
-                   *(player->condarg + x) =
-                       tolower(*(player->condarg + x));
        if (dispatch(player->combuf, redir) < 0)
            pr("Try \"list of commands\" or \"info\"\n");
     }
     return 1;
 }
 
-int
+static int
 status(void)
 {
     struct natstr *natp;
     int minute;
     struct sctstr sect;
-    s_char buf[128];
+    char buf[128];
 
     if (player->state == PS_SHUTDOWN)
        return 0;
@@ -198,7 +166,6 @@ status(void)
        putnat(natp);
        return 0;
     }
-    player->visitor = (natp->nat_stat & (STAT_NORM | STAT_GOD)) == 0;
     if (player->dolcost != 0.0) {
        if (player->dolcost > 100.0)
            pr("That just cost you $%.2f\n", player->dolcost);
@@ -228,16 +195,14 @@ status(void)
        }
     }
     getsect(natp->nat_xcap, natp->nat_ycap, &sect);
-    if ((sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT ||
-        sect.sct_type == SCT_SANCT) && sect.sct_own == player->cnum)
-       player->nstat |= CAP;
-    else
+    if (influx(natp))
        player->nstat &= ~CAP;
-    /* Ok, has the country owner reset his capital yet after it was sacked? */
-    if (natp->nat_flags & NF_SACKED)
-       player->nstat &= ~CAP;  /* No capital yet */
+    else
+       player->nstat |= CAP;
     player->ncomstat = player->nstat;
-    (void)time(&player->curup);
+    if (player->god)
+       player->ncomstat |= CAP | MONEY;
+    time(&player->curup);
     minute = (player->curup - player->lasttime) / 60;
     if (minute > 0) {
        player->minleft -= minute;
@@ -249,7 +214,7 @@ status(void)
            daychange(player->curup);
            if (!gamehours(player->curup)) {
                pr("Empire hours restriction in force\n");
-               if ((natp->nat_stat & STAT_GOD) == 0) {
+               if (natp->nat_stat != STAT_GOD) {
                    putnat(natp);
                    return 0;
                }
@@ -259,7 +224,7 @@ status(void)
        player->lasttime += minute * 60;
        natp->nat_minused += minute;
     }
-    if ((player->nstat & NORM) && natp->nat_minused > m_m_p_d) {
+    if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
        pr("Max minutes per day limit exceeded.\n");
        player->ncomstat = VIS;
     }
@@ -285,7 +250,7 @@ status(void)
               numstr(buf, natp->nat_ann));
        natp->nat_ann = 0;
     }
-    if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
+    if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
        pr("You lost your capital... better designate one\n");
     putnat(natp);
     if (gamedown() && !player->god) {
@@ -304,30 +269,32 @@ status(void)
 int
 execute(void)
 {
-    s_char buf[512];
+    char buf[1024];
     int failed;
-    s_char *p;
-    s_char *redir;
-    s_char scanspace[1024];
+    char *p;
+    char *redir;               /* UTF-8 */
+    char scanspace[1024];
 
     failed = 0;
-    redir = 0;
-
-    p = getstarg(player->argp[1], "File? ", buf);
+    redir = NULL;
 
-    if (p == (s_char *)0 || *p == '\0')
+    if (player->comtail[1])
+       p = player->comtail[1];
+    else
+       p = getstring("File? ", buf);
+    if (p == NULL || *p == '\0')
        return RET_SYN;
+    prexec(p);
 
-    prexec(player->argp[1]);
     while (!failed && status()) {
        if (recvclient(buf, sizeof(buf)) < 0)
            break;
-       if (parse(buf, player->argp, &player->condarg,
-                 scanspace, &redir) < 0) {
+       if (parse(buf, scanspace, player->argp, player->comtail,
+                 &player->condarg, &redir) < 0) {
            failed = 1;
            continue;
        }
-       if (redir == 0)
+       if (redir == NULL)
            pr("\nExecute : %s\n", buf);
        if (dispatch(buf, redir) < 0)
            failed = 1;
@@ -335,73 +302,49 @@ execute(void)
     if (failed) {
        while (recvclient(buf, sizeof(buf)) >= 0) ;
     }
-    if (redir == 0)
+    if (redir == NULL)
        pr("Execute : %s\n", failed ? "aborted" : "terminated");
+    player->eof = 0;
     return RET_OK;
 }
 
 int
 show_motd(void)
 {
-    int upf;
+    FILE *motd_fp;
     struct telstr tgm;
-    s_char buf[MAXTELSIZE];
+    char buf[MAXTELSIZE + 1];  /* UTF-8 */
 
-#if !defined(_WIN32)
-    if ((upf = open(upfil, O_RDONLY, 0)) < 0)
-#else
-    if ((upf = open(upfil, O_RDONLY | O_BINARY, 0)) < 0)
-#endif
+    if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
+       if (errno == ENOENT)
+           return RET_OK;
+       else {
+           pr ("Could not open motd.\n");
+           logerror("Could not open motd (%s).\n", motdfil);
+           return RET_SYS;
+       }
+    }
+    if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
+       logerror("bad header on login message (motdfil)");
+       fclose(motd_fp);
        return RET_FAIL;
-    if (read(upf, (s_char *)&tgm, sizeof(tgm)) != sizeof(tgm)) {
-       logerror("bad header on login message (upfil)");
-       close(upf);
+    }
+    if (tgm.tel_length >= (long)sizeof(buf)) {
+       logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
+       fclose(motd_fp);
        return RET_FAIL;
     }
-    if (read(upf, buf, tgm.tel_length) != tgm.tel_length) {
+    if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
        logerror("bad length %ld on login message", tgm.tel_length);
-       close(upf);
+       fclose(motd_fp);
        return RET_FAIL;
     }
-    if (tgm.tel_length >= (long)sizeof(buf))
-       tgm.tel_length = sizeof(buf) - 1;
     buf[tgm.tel_length] = 0;
-    prnf(buf);
-    (void)close(upf);
+    uprnf(buf);
+    fclose(motd_fp);
     return RET_OK;
 }
 
-int
-match_user(char *file, struct player *p)
-{
-    FILE *fp;
-    int match = 0;
-    s_char host[256];
-    s_char user[256];
-
-    if ((fp = fopen(file, "r")) == NULL) {
-       /*logerror("Cannot find file %s", file); */
-       return 0;
-    }
-    match = 0;
-    while (!feof(fp) && !match) {
-       if (fgets(host, sizeof(host) - 1, fp) == NULL)
-           break;
-       if (host[0] == '#')
-           continue;
-       if (fgets(user, sizeof(user) - 1, fp) == NULL)
-           break;
-       host[strlen(host) - 1] = '\0';
-       user[strlen(user) - 1] = '\0';
-       if (strstr(p->userid, user) &&
-           (strstr(p->hostaddr, host) ||
-            strstr(p->hostname, host)))
-           ++match;
-    }
-    fclose(fp);
-    return match;
-}
-
 int
 quit(void)
 {
@@ -409,7 +352,7 @@ quit(void)
     return RET_OK;
 }
 
-s_char *
+char *
 praddr(struct player *p)
 {
     return prbuf("%s@%s", p->userid,