2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
56 static s_char *telnames[] = {
57 /* must follow TEL_ defines in tel.h */
58 "Telegram", "Announcement", "BULLETIN", "Production Report"
61 register s_char *mbox;
62 s_char mbox_buf[256]; /* Maximum path length */
69 char msgbuf[4096]; /* UTF-8 */
77 int num = player->cnum;
78 struct natstr *np = getnatp(player->cnum);
84 int may_delete = 1; /* may messages be deleted? */
86 memset(kind, 0, sizeof(kind));
89 if (*player->argp[0] == 'w') {
90 strcpy(kind, "announcement");
91 if (player->argp[1] && isdigit(*player->argp[1])) {
92 delta = days(atoi(player->argp[1]));
96 then = np->nat_annotim;
99 strcpy(kind, "telegram");
100 if (player->god && player->argp[1] &&
101 (mineq(player->argp[1], "yes") == ME_MISMATCH) &&
102 (mineq(player->argp[1], "no") == ME_MISMATCH)) {
103 if ((n = natarg(player->argp[1], NULL)) < 0)
108 mbox = mailbox(mbox_buf, num);
109 clear_telegram_is_new(player->cnum);
112 if ((telfp = fopen(mbox, "rb+")) == 0) {
113 logerror("telegram file %s", mbox);
117 fseek(telfp, 0L, SEEK_SET);
118 size = fsize(fileno(telfp));
123 while (fread(&tgm, sizeof(tgm), 1, telfp) == 1) {
125 if (tgm.tel_length < 0) {
126 logerror("bad telegram file header in %s", mbox);
129 if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
130 pr("Bad telegram header. Skipping telegram...\n");
135 if (!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO)) {
139 if (tgm.tel_date < then) {
144 if (first && *kind == 'a') {
145 pr("\nAnnouncements since %s", ctime(&then));
149 if (tgm.tel_type != lasttype || tgm.tel_from != lastcnum)
151 if (abs((int)(tgm.tel_date - (long)lastdate)) > TEL_SECONDS)
155 lastcnum = tgm.tel_from;
156 lasttype = tgm.tel_type;
157 pr("%s ", telnames[(int)tgm.tel_type]);
158 if ((tgm.tel_type == TEL_NORM) ||
159 (tgm.tel_type == TEL_ANNOUNCE) ||
160 (tgm.tel_type == TEL_BULLETIN))
161 pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
162 pr(" dated %s", ctime(&tgm.tel_date));
163 lastdate = tgm.tel_date;
167 while (tgm.tel_length > 0) {
168 nbytes = tgm.tel_length;
169 if (nbytes > sizeof(msgbuf) - 1)
170 nbytes = sizeof(msgbuf) - 1;
171 (void)fread(msgbuf, sizeof(s_char), nbytes, telfp);
175 tgm.tel_length -= nbytes;
179 if (teles > 0 && player->cnum == num && may_delete) {
183 p = "Forget this one? ";
185 p = "Shall I burn it? ";
188 p = "Into the shredder, boss? ";
190 p = "Can I throw away these old love letters? ";
192 p = getstarg(player->argp[1], p, buf);
193 if (p && *p == 'y') {
194 if ((filelen = fsize(fileno(telfp))) > size) {
195 pr("Wait a sec! A new %s has arrived...\n", kind);
196 /* force stdio to re-read tel file */
198 (void)fseek(telfp, (long)size, SEEK_SET);
204 np->nat_annotim = now;
207 /* Here, we just re-open the file for "w" only,
208 and that will wipe the file clean automatically */
210 telfp = fopen((char *)mbox, "wb");
215 if (player->cnum == num)
216 pr("No %ss for you at the moment...\n", kind);
218 pr("No %ss for %s at the moment...\n", kind, cname(num));
221 if (np->nat_flags & NF_INFORM) {
222 pr_inform(player, "\n");