]> 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 df9a4f8046f84ebfb6bae322adc9a0bdba1f92a3..f604e606be77a316c9e08074c4b9ec5a3bdefbb1 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, 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,41 +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));
     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 ((telfp = fopen(mbox, "r+")) == 0) {
+    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);
@@ -157,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))
@@ -180,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);
@@ -201,15 +207,16 @@ rea(void)
                /* Here, we just re-open the file for "w" only,
                   and that will wipe the file clean automatically */
                (void)fclose(telfp);
-               telfp = fopen((char *)mbox, "w");
+               telfp = fopen((char *)mbox, "wb");
            }
        }
     }
-    if (teles <= 0)
+    if (teles <= 0) {
        if (player->cnum == num)
            pr("No %ss for you at the moment...\n", kind);
        else
            pr("No %ss for %s at the moment...\n", kind, cname(num));
+    }
     (void)fclose(telfp);
     if (np->nat_flags & NF_INFORM) {
        pr_inform(player, "\n");