]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/rea.c
Don't lie "You have a new telegram" after read with inform off
[empserver] / src / lib / commands / rea.c
index 934b0dfba258e6012ad9daeb2de9d2be05166e7e..33fcde34ca4ac5128c465d77d5f1fce23a2c2cae 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  rea.c: Read telegrams
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare
  *     Doug Hay, 1998
  *     Steve McClure, 1998-2000
+ *     Ron Koenderink, 2005-2007
+ *     Markus Armbruster, 2009-2011
  */
 
 #include <config.h>
 
 #include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
 #include "commands.h"
 #include "match.h"
@@ -43,6 +45,8 @@
 #include "optlist.h"
 #include "tel.h"
 
+static int print_sink(char *, size_t, void *);
+
 int
 rea(void)
 {
@@ -57,23 +61,15 @@ rea(void)
     FILE *telfp;
     int teles;
     int size;
-    unsigned nbytes;
     char buf[1024];
-    char msgbuf[4096];         /* UTF-8 */
-    int lasttype;
-    int lastcnum;
-    time_t lastdate;
-    int header;
     int filelen;
     char *kind;
-    int n;
+    int n, res;
     int num = player->cnum;
     struct natstr *np = getnatp(player->cnum);
     time_t now;
     time_t then;
     time_t delta;
-    int first = 1;
-    int readit;
     int may_delete = 1; /* may messages be deleted? */
 
     now = time(NULL);
@@ -98,75 +94,54 @@ rea(void)
            may_delete = 0;
        }
        mbox = mailbox(mbox_buf, num);
-       clear_telegram_is_new(player->cnum);
     }
 
-    if ((telfp = fopen(mbox, "rb+")) == 0) {
+    if (!(telfp = fopen(mbox, "rb+"))) {
        logerror("telegram file %s", mbox);
        return RET_FAIL;
     }
     teles = 0;
-    fseek(telfp, 0L, SEEK_SET);
     size = fsize(fileno(telfp));
   more:
-    lastdate = 0;
-    lastcnum = -1;
-    lasttype = -1;
-    while (fread(&tgm, sizeof(tgm), 1, telfp) == 1) {
-       readit = 1;
-       if (tgm.tel_length < 0) {
-           logerror("bad telegram file header in %s", mbox);
-           break;
-       }
-       if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
-           pr("Bad telegram header.  Skipping telegram...\n");
-           readit = 0;
-           goto skip;
-       }
+    while ((res = tel_read_header(telfp, mbox, &tgm)) > 0) {
        if (*kind == 'a') {
-           if (!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO)) {
-               readit = 0;
-               goto skip;
-           }
-           if (tgm.tel_date < then) {
-               readit = 0;
-               goto skip;
+           if ((!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO))
+               || tgm.tel_date < then) {
+               res = tel_read_body(telfp, mbox, &tgm, NULL, NULL);
+               if (res < 0)
+                   break;
+               continue;
            }
        }
-       if (first && *kind == 'a') {
+       if (!teles && *kind == 'a')
            pr("\nAnnouncements since %s", ctime(&then));
-           first = 0;
-       }
-       header = 0;
-       if (tgm.tel_type != lasttype || tgm.tel_from != lastcnum)
-           header++;
-       if (abs((int)(tgm.tel_date - (long)lastdate)) > TEL_SECONDS)
-           header++;
-       if (header) {
+       if (!teles || !tgm.tel_cont) {
            pr("\n> ");
-           lastcnum = tgm.tel_from;
-           lasttype = tgm.tel_type;
-           pr("%s ", telnames[(int)tgm.tel_type]);
+           pr("%s ", telnames[tgm.tel_type]);
            if ((tgm.tel_type == TEL_NORM) ||
                (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;
        }
        teles++;
-      skip:
-       while (tgm.tel_length > 0) {
-           nbytes = tgm.tel_length;
-           if (nbytes > sizeof(msgbuf) - 1)
-               nbytes = sizeof(msgbuf) - 1;
-           fread(msgbuf, 1, nbytes, telfp);
-           msgbuf[nbytes] = 0;
-           if (readit)
-               uprnf(msgbuf);
-           tgm.tel_length -= nbytes;
+       res = tel_read_body(telfp, mbox, &tgm, print_sink, NULL);
+       if (res < 0)
+           break;
+    }
+    if (res < 0)
+       pr("\n> Mailbox corrupt, tell the deity.\n");
+
+    if (*kind == 'a')
+       np->nat_ann = 0;
+    else {
+       np->nat_tgms = 0;
+       if (np->nat_flags & NF_INFORM) {
+           pr_inform(player, "\n");
        }
     }
+    putnat(np);
+
     if (teles > 0 && player->cnum == num && may_delete) {
        pr("\n");
        if (teles == 1) {
@@ -209,10 +184,41 @@ rea(void)
            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");
-       np->nat_tgms = 0;
-       putnat(np);
-    }
     return RET_OK;
 }
+
+/*
+ * Print first telegram in file FNAME.
+ */
+int
+show_first_tel(char *fname)
+{
+    FILE *fp;
+    int ret;
+    struct telstr tgm;
+
+    if ((fp = fopen(fname, "rb")) == NULL) {
+       if (errno == ENOENT)
+           return 0;
+       else {
+           logerror("Could not open %s.\n", fname);
+           return -1;
+       }
+    }
+
+    ret = tel_read_header(fp, fname, &tgm);
+    if (ret < 0)
+       goto out;
+    ret = tel_read_body(fp, fname, &tgm, print_sink, NULL);
+
+out:
+    fclose(fp);
+    return ret;
+}
+
+static int
+print_sink(char *chunk, size_t sz, void *arg)
+{
+    uprnf(chunk);
+    return 0;
+}