2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * rea.c: Read telegrams
30 * Known contributors to this file:
33 * Steve McClure, 1998-2000
47 static int print_sink(char *, size_t, void *);
52 static char *telnames[] = {
53 /* must follow TEL_ defines in tel.h */
54 "Telegram", "Announcement", "BULLETIN", "Production Report"
58 char mbox_buf[256]; /* Maximum path length */
71 int num = player->cnum;
72 struct natstr *np = getnatp(player->cnum);
77 int may_delete = 1; /* may messages be deleted? */
81 if (*player->argp[0] == 'w') {
82 kind = "announcement";
83 if (player->argp[1] && isdigit(*player->argp[1])) {
84 delta = days(atoi(player->argp[1]));
88 then = np->nat_annotim;
92 if (player->god && player->argp[1] &&
93 (mineq(player->argp[1], "yes") == ME_MISMATCH) &&
94 (mineq(player->argp[1], "no") == ME_MISMATCH)) {
95 if ((n = natarg(player->argp[1], NULL)) < 0)
100 mbox = mailbox(mbox_buf, num);
101 clear_telegram_is_new(player->cnum);
104 if ((telfp = fopen(mbox, "rb+")) == 0) {
105 logerror("telegram file %s", mbox);
109 fseek(telfp, 0L, SEEK_SET);
110 size = fsize(fileno(telfp));
115 while ((res = tel_read_header(telfp, mbox, &tgm)) > 0) {
117 if ((!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO))
118 || tgm.tel_date < then) {
119 res = tel_read_body(telfp, mbox, &tgm, NULL, NULL);
125 if (first && *kind == 'a') {
126 pr("\nAnnouncements since %s", ctime(&then));
130 if (tgm.tel_type != lasttype || tgm.tel_from != lastcnum)
132 if (abs((int)(tgm.tel_date - (long)lastdate)) > TEL_SECONDS)
136 lastcnum = tgm.tel_from;
137 lasttype = tgm.tel_type;
138 pr("%s ", telnames[tgm.tel_type]);
139 if ((tgm.tel_type == TEL_NORM) ||
140 (tgm.tel_type == TEL_ANNOUNCE) ||
141 (tgm.tel_type == TEL_BULLETIN))
142 pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
143 pr(" dated %s", ctime(&tgm.tel_date));
144 lastdate = tgm.tel_date;
147 res = tel_read_body(telfp, mbox, &tgm, print_sink, NULL);
152 pr("\n> Mailbox corrupt, tell the deity.\n");
154 if (teles > 0 && player->cnum == num && may_delete) {
158 p = "Forget this one? ";
160 p = "Shall I burn it? ";
163 p = "Into the shredder, boss? ";
165 p = "Can I throw away these old love letters? ";
167 p = getstarg(player->argp[1], p, buf);
168 if (p && *p == 'y') {
169 if ((filelen = fsize(fileno(telfp))) > size) {
170 pr("Wait a sec! A new %s has arrived...\n", kind);
171 /* force stdio to re-read tel file */
173 (void)fseek(telfp, (long)size, SEEK_SET);
179 np->nat_annotim = now;
182 /* Here, we just re-open the file for "w" only,
183 and that will wipe the file clean automatically */
185 telfp = fopen(mbox, "wb");
190 if (player->cnum == num)
191 pr("No %ss for you at the moment...\n", kind);
193 pr("No %ss for %s at the moment...\n", kind, cname(num));
196 if (np->nat_flags & NF_INFORM) {
197 pr_inform(player, "\n");
205 * Print first telegram in file FNAME.
208 show_first_tel(char *fname)
213 if ((fp = fopen(fname, "rb")) == NULL) {
217 logerror("Could not open %s.\n", fname);
221 if (tel_read_header(fp, fname, &tgm) < 0)
223 if (tel_read_body(fp, fname, &tgm, print_sink, NULL) < 0)
230 print_sink(char *chunk, size_t sz, void *arg)