2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * rea.c: Read telegrams
29 * Known contributors to this file:
32 * Steve McClure, 1998-2000
33 * Ron Koenderink, 2005-2007
34 * Markus Armbruster, 2009-2011
48 static int print_sink(char *, size_t, void *);
53 static char *telnames[] = {
54 /* must follow TEL_ defines in tel.h */
55 "Telegram", "Announcement", "BULLETIN", "Production Report"
59 char mbox_buf[256]; /* Maximum path length */
68 int num = player->cnum;
69 struct natstr *np = getnatp(player->cnum);
73 int may_delete = 1; /* may messages be deleted? */
77 if (*player->argp[0] == 'w') {
78 kind = "announcement";
79 if (player->argp[1] && isdigit(*player->argp[1])) {
80 delta = days(atoi(player->argp[1]));
84 then = np->nat_annotim;
88 if (player->god && player->argp[1] &&
89 (mineq(player->argp[1], "yes") == ME_MISMATCH) &&
90 (mineq(player->argp[1], "no") == ME_MISMATCH)) {
91 if ((n = natarg(player->argp[1], NULL)) < 0)
96 mbox = mailbox(mbox_buf, num);
99 if (!(telfp = fopen(mbox, "rb+"))) {
100 logerror("telegram file %s", mbox);
104 size = fsize(fileno(telfp));
106 while ((res = tel_read_header(telfp, mbox, &tgm)) > 0) {
108 if ((!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO))
109 || tgm.tel_date < then) {
110 res = tel_read_body(telfp, mbox, &tgm, NULL, NULL);
116 if (!teles && *kind == 'a')
117 pr("\nAnnouncements since %s", ctime(&then));
118 if (!teles || !tgm.tel_cont) {
120 pr("%s ", telnames[tgm.tel_type]);
121 if ((tgm.tel_type == TEL_NORM) ||
122 (tgm.tel_type == TEL_ANNOUNCE) ||
123 (tgm.tel_type == TEL_BULLETIN))
124 pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
125 pr(" dated %s", ctime(&tgm.tel_date));
128 res = tel_read_body(telfp, mbox, &tgm, print_sink, NULL);
133 pr("\n> Mailbox corrupt, tell the deity.\n");
139 if (np->nat_flags & NF_INFORM) {
140 pr_inform(player, "\n");
145 if (teles > 0 && player->cnum == num && may_delete) {
149 p = "Forget this one? ";
151 p = "Shall I burn it? ";
154 p = "Into the shredder, boss? ";
156 p = "Can I throw away these old love letters? ";
158 p = getstarg(player->argp[1], p, buf);
159 if (p && *p == 'y') {
160 if ((filelen = fsize(fileno(telfp))) > size) {
161 pr("Wait a sec! A new %s has arrived...\n", kind);
162 /* force stdio to re-read tel file */
164 (void)fseek(telfp, (long)size, SEEK_SET);
170 np->nat_annotim = now;
173 /* Here, we just re-open the file for "w" only,
174 and that will wipe the file clean automatically */
176 telfp = fopen(mbox, "wb");
181 if (player->cnum == num)
182 pr("No %ss for you at the moment...\n", kind);
184 pr("No %ss for %s at the moment...\n", kind, cname(num));
191 * Print first telegram in file FNAME.
194 show_first_tel(char *fname)
200 if ((fp = fopen(fname, "rb")) == NULL) {
204 logerror("Could not open %s.\n", fname);
209 ret = tel_read_header(fp, fname, &tgm);
212 ret = tel_read_body(fp, fname, &tgm, print_sink, NULL);
220 print_sink(char *chunk, size_t sz, void *arg)