]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/rea.c
commands: Rename the command functions
[empserver] / src / lib / commands / rea.c
index 288cdecb7a6e85b916d9480995ba9a2e5ad842a4..fea82ce5966dcfce91843ea79d6763d872a7afff 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -31,7 +31,7 @@
  *     Doug Hay, 1998
  *     Steve McClure, 1998-2000
  *     Ron Koenderink, 2005-2007
- *     Markus Armbruster, 2009
+ *     Markus Armbruster, 2009-2016
  */
 
 #include <config.h>
@@ -39,6 +39,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
+#include "chance.h"
 #include "commands.h"
 #include "match.h"
 #include "misc.h"
@@ -48,7 +49,7 @@
 static int print_sink(char *, size_t, void *);
 
 int
-rea(void)
+c_read(void)
 {
     static char *telnames[] = {
        /* must follow TEL_ defines in tel.h */
@@ -59,14 +60,8 @@ rea(void)
     char mbox_buf[256];                /* Maximum path length */
     struct telstr tgm;
     FILE *telfp;
-    int teles;
-    int size;
+    int teles, need_hdr;
     char buf[1024];
-    int lasttype;
-    int lastcnum;
-    time_t lastdate;
-    int header;
-    int filelen;
     char *kind;
     int n, res;
     int num = player->cnum;
@@ -74,8 +69,7 @@ rea(void)
     time_t now;
     time_t then;
     time_t delta;
-    int first = 1;
-    int may_delete = 1; /* may messages be deleted? */
+    int may_delete = 1;
 
     now = time(NULL);
 
@@ -99,22 +93,21 @@ rea(void)
            may_delete = 0;
        }
        mbox = mailbox(mbox_buf, num);
-       clear_telegram_is_new(player->cnum);
     }
 
-    if (!(telfp = fopen(mbox, "rb+"))) {
+    if (!(telfp = fopen(mbox, "rb"))) {
        logerror("telegram file %s", mbox);
        return RET_FAIL;
     }
     teles = 0;
-    size = fsize(fileno(telfp));
-  more:
-    lastdate = 0;
-    lastcnum = -1;
-    lasttype = -1;
-    while ((res = tel_read_header(telfp, mbox, &tgm)) > 0) {
+    need_hdr = 1;
+    for (;;) {
+       res = tel_read_header(telfp, mbox, &tgm);
+    more:
+       if (res <= 0)
+           break;
        if (*kind == 'a') {
-           if ((!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO))
+           if ((!nat_accepts(player->cnum, tgm.tel_from, REJ_ANNO))
                || tgm.tel_date < then) {
                res = tel_read_body(telfp, mbox, &tgm, NULL, NULL);
                if (res < 0)
@@ -122,34 +115,37 @@ rea(void)
                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 (need_hdr || !tgm.tel_cont) {
            pr("\n> ");
-           lastcnum = tgm.tel_from;
-           lasttype = 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++;
+       need_hdr = 0;
        res = tel_read_body(telfp, mbox, &tgm, print_sink, NULL);
        if (res < 0)
            break;
     }
-    if (res < 0)
+    if (res < 0) {
        pr("\n> Mailbox corrupt, tell the deity.\n");
+       may_delete = 0;
+    }
+
+    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");
@@ -166,19 +162,19 @@ rea(void)
        }
        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);
-               /* force stdio to re-read tel file */
-               (void)fflush(telfp);
-               (void)fseek(telfp, (long)size, SEEK_SET);
-               size = filelen;
-               now = time(NULL);
-               goto more;
-           }
            if (*kind == 'a') {
                np->nat_annotim = now;
                putnat(np);
            } else {
+               /* force stdio to re-read tel file */
+               fflush(telfp);
+               fseek(telfp, 0, SEEK_CUR);
+               res = tel_read_header(telfp, mbox, &tgm);
+               if (res != 0) {
+                   pr("Wait a sec!  A new %s has arrived...\n", kind);
+                   need_hdr = 1;
+                   goto more;
+               }
                /* Here, we just re-open the file for "w" only,
                   and that will wipe the file clean automatically */
                (void)fclose(telfp);
@@ -193,16 +189,11 @@ 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.
+ * Print first telegram in file @fname.
  */
 int
 show_first_tel(char *fname)