/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
*
* ---
*
- * 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.
*
* ---
*
* rea.c: Read telegrams
- *
+ *
* Known contributors to this file:
* Dave Pare
* Doug Hay, 1998
#include <config.h>
-#include "misc.h"
-#include "player.h"
-#include "nat.h"
-#include "file.h"
-#include "tel.h"
+#include <ctype.h>
+#include <stdio.h>
#include "commands.h"
-#include "optlist.h"
#include "match.h"
+#include "misc.h"
+#include "optlist.h"
+#include "tel.h"
-#include <stdio.h>
-#include <fcntl.h>
-#if !defined(_WIN32)
-#include <sys/file.h>
-#endif
+static int print_sink(char *, size_t, void *);
int
rea(void)
{
- static s_char *telnames[] = {
+ static char *telnames[] = {
/* must follow TEL_ defines in tel.h */
"Telegram", "Announcement", "BULLETIN", "Production Report"
};
- register s_char *p;
- register s_char *mbox;
- s_char mbox_buf[256]; /* Maximum path length */
+ char *p;
+ char *mbox;
+ char mbox_buf[256]; /* Maximum path length */
struct telstr tgm;
FILE *telfp;
int teles;
int size;
- unsigned int nbytes;
char buf[1024];
- char msgbuf[4096]; /* UTF-8 */
int lasttype;
int lastcnum;
time_t lastdate;
int header;
int filelen;
- s_char kind[80];
- int n;
+ char *kind;
+ 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? */
- memset(kind, 0, sizeof(kind));
now = time(NULL);
if (*player->argp[0] == 'w') {
- strcpy(kind, "announcement");
+ kind = "announcement";
if (player->argp[1] && isdigit(*player->argp[1])) {
delta = days(atoi(player->argp[1]));
then = now - delta;
then = np->nat_annotim;
mbox = annfil;
} else {
- strcpy(kind, "telegram");
+ kind = "telegram";
if (player->god && player->argp[1] &&
(mineq(player->argp[1], "yes") == ME_MISMATCH) &&
(mineq(player->argp[1], "no") == ME_MISMATCH)) {
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') {
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))
lastdate = tgm.tel_date;
}
teles++;
- skip:
- while (tgm.tel_length > 0) {
- nbytes = tgm.tel_length;
- if (nbytes > sizeof(msgbuf) - 1)
- nbytes = sizeof(msgbuf) - 1;
- (void)fread(msgbuf, sizeof(s_char), 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;
}
- p = NULL;
+ if (res < 0)
+ pr("\n> Mailbox corrupt, tell the deity.\n");
+
if (teles > 0 && player->cnum == num && may_delete) {
pr("\n");
if (teles == 1) {
/* 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, "wb");
+ telfp = fopen(mbox, "wb");
}
}
}
}
return RET_OK;
}
+
+static int
+print_sink(char *chunk, size_t sz, void *arg)
+{
+ uprnf(chunk);
+ return 0;
+}