]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/rea.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / rea.c
index cfcaddb3f1519980f1d5ef157cf2cfe97b450953..f604e606be77a316c9e08074c4b9ec5a3bdefbb1 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-2006, 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.
  *
  *  ---
  *
@@ -33,6 +33,8 @@
  *     Steve McClure, 1998-2000
  */
 
+#include <config.h>
+
 #include "misc.h"
 #include "player.h"
 #include "nat.h"
@@ -40,6 +42,7 @@
 #include "tel.h"
 #include "commands.h"
 #include "optlist.h"
+#include "match.h"
 
 #include <stdio.h>
 #include <fcntl.h>
@@ -62,7 +65,8 @@ rea(void)
     int teles;
     int size;
     unsigned int nbytes;
-    s_char buf[4096];
+    char buf[1024];
+    char msgbuf[4096];         /* UTF-8 */
     int lasttype;
     int lastcnum;
     time_t lastdate;
@@ -77,45 +81,46 @@ rea(void)
     time_t delta;
     int first = 1;
     int readit;
+    int may_delete = 1; /* may messages be deleted? */
 
     memset(kind, 0, sizeof(kind));
-    (void)time(&now);
+    now = time(NULL);
 
     if (*player->argp[0] == 'w') {
-       sprintf(kind, "announcement");
+       strcpy(kind, "announcement");
        if (player->argp[1] && isdigit(*player->argp[1])) {
            delta = days(atoi(player->argp[1]));
            then = now - delta;
+           may_delete = 0;
        } else
            then = np->nat_annotim;
        mbox = annfil;
     } else {
-       sprintf(kind, "telegram");
-       if (player->god && player->argp[1] != 0) {
-           if ((n = natarg(player->argp[1], "")) < 0)
+       strcpy(kind, "telegram");
+       if (player->god && player->argp[1] &&
+           (mineq(player->argp[1], "yes") == ME_MISMATCH) &&
+           (mineq(player->argp[1], "no") == ME_MISMATCH)) {
+           if ((n = natarg(player->argp[1], NULL)) < 0)
                return RET_SYN;
            num = n;
+           may_delete = 0;
        }
        mbox = mailbox(mbox_buf, num);
        clear_telegram_is_new(player->cnum);
     }
 
-#if !defined(_WIN32)
-    if ((telfp = fopen(mbox, "r+")) == 0) {
-#else
-    if ((telfp = fopen(mbox, "r+b")) == 0) {
-#endif
+    if ((telfp = fopen(mbox, "rb+")) == 0) {
        logerror("telegram file %s", mbox);
        return RET_FAIL;
     }
     teles = 0;
-    fseek(telfp, 0L, 0);
+    fseek(telfp, 0L, SEEK_SET);
     size = fsize(fileno(telfp));
   more:
     lastdate = 0;
     lastcnum = -1;
     lasttype = -1;
-    while (fread((s_char *)&tgm, sizeof(tgm), 1, telfp) == 1) {
+    while (fread(&tgm, sizeof(tgm), 1, telfp) == 1) {
        readit = 1;
        if (tgm.tel_length < 0) {
            logerror("bad telegram file header in %s", mbox);
@@ -151,12 +156,9 @@ rea(void)
            lasttype = tgm.tel_type;
            pr("%s ", telnames[(int)tgm.tel_type]);
            if ((tgm.tel_type == TEL_NORM) ||
-               (tgm.tel_type == TEL_ANNOUNCE)) {
-               pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
-           }
-           if (tgm.tel_type == TEL_BULLETIN) {
+               (tgm.tel_type == TEL_ANNOUNCE) ||
+               (tgm.tel_type == TEL_BULLETIN))
                pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
-           }
            pr("  dated %s", ctime(&tgm.tel_date));
            lastdate = tgm.tel_date;
        }
@@ -164,17 +166,17 @@ rea(void)
       skip:
        while (tgm.tel_length > 0) {
            nbytes = tgm.tel_length;
-           if (nbytes > sizeof(buf) - 1)
-               nbytes = sizeof(buf) - 1;
-           (void)fread(buf, sizeof(s_char), nbytes, telfp);
-           buf[nbytes] = 0;
+           if (nbytes > sizeof(msgbuf) - 1)
+               nbytes = sizeof(msgbuf) - 1;
+           (void)fread(msgbuf, sizeof(s_char), nbytes, telfp);
+           msgbuf[nbytes] = 0;
            if (readit)
-               prnf(buf);
+               uprnf(msgbuf);
            tgm.tel_length -= nbytes;
        }
     }
     p = NULL;
-    if (teles > 0 && player->cnum == num) {    /* } */
+    if (teles > 0 && player->cnum == num && may_delete) {
        pr("\n");
        if (teles == 1) {
            if (chance(0.25))
@@ -187,10 +189,7 @@ rea(void)
            else
                p = "Can I throw away these old love letters? ";
        }
-       if (player->god && *kind == 't')
-           p = getstarg(player->argp[2], p, buf);
-       else
-           p = getstarg(player->argp[1], p, buf);
+       p = getstarg(player->argp[1], p, buf);
        if (p && *p == 'y') {
            if ((filelen = fsize(fileno(telfp))) > size) {
                pr("Wait a sec!  A new %s has arrived...\n", kind);
@@ -198,7 +197,7 @@ rea(void)
                (void)fflush(telfp);
                (void)fseek(telfp, (long)size, SEEK_SET);
                size = filelen;
-               (void)time(&now);
+               now = time(NULL);
                goto more;
            }
            if (*kind == 'a') {
@@ -208,11 +207,7 @@ rea(void)
                /* Here, we just re-open the file for "w" only,
                   and that will wipe the file clean automatically */
                (void)fclose(telfp);
-#if !defined(_WIN32)
-               telfp = fopen((char *)mbox, "w");
-#else
                telfp = fopen((char *)mbox, "wb");
-#endif
            }
        }
     }