1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  rea.c: Read telegrams
28  *
29  *  Known contributors to this file:
30  *     Dave Pare
31  *     Doug Hay, 1998
32  *     Steve McClure, 1998-2000
33  *     Ron Koenderink, 2005-2007
34  *     Markus Armbruster, 2009-2016
35  */
36 
37 #include <config.h>
38 
39 #include <ctype.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include "chance.h"
43 #include "commands.h"
44 #include "match.h"
45 #include "misc.h"
46 #include "optlist.h"
47 #include "tel.h"
48 
49 static int print_sink(char *, size_t, void *);
50 
51 int
c_read(void)52 c_read(void)
53 {
54     static char *telnames[] = {
55 	/* must follow TEL_ defines in tel.h */
56 	"Telegram", "Announcement", "BULLETIN", "Production Report"
57     };
58     char *p;
59     char *mbox;
60     char mbox_buf[256];		/* Maximum path length */
61     struct telstr tgm;
62     FILE *telfp;
63     int teles, need_hdr;
64     char buf[1024];
65     char *kind;
66     int n, res;
67     int num = player->cnum;
68     struct natstr *np = getnatp(player->cnum);
69     time_t now;
70     time_t then;
71     time_t delta;
72     int may_delete = 1;
73 
74     now = time(NULL);
75 
76     if (*player->argp[0] == 'w') {
77 	kind = "announcement";
78 	if (player->argp[1] && isdigit(*player->argp[1])) {
79 	    delta = days(atoi(player->argp[1]));
80 	    then = now - delta;
81 	    may_delete = 0;
82 	} else
83 	    then = np->nat_annotim;
84 	mbox = annfil;
85     } else {
86 	kind = "telegram";
87 	if (player->god && player->argp[1] &&
88 	    (mineq(player->argp[1], "yes") == ME_MISMATCH) &&
89 	    (mineq(player->argp[1], "no") == ME_MISMATCH)) {
90 	    if ((n = natarg(player->argp[1], NULL)) < 0)
91 		return RET_SYN;
92 	    num = n;
93 	    may_delete = 0;
94 	}
95 	mbox = mailbox(mbox_buf, num);
96     }
97 
98     if (!(telfp = fopen(mbox, "rb"))) {
99 	logerror("telegram file %s", mbox);
100 	return RET_FAIL;
101     }
102     teles = 0;
103     need_hdr = 1;
104     for (;;) {
105 	res = tel_read_header(telfp, mbox, &tgm);
106     more:
107 	if (res <= 0)
108 	    break;
109 	if (*kind == 'a') {
110 	    if ((!nat_accepts(player->cnum, tgm.tel_from, REJ_ANNO))
111 		|| tgm.tel_date < then) {
112 		res = tel_read_body(telfp, mbox, &tgm, NULL, NULL);
113 		if (res < 0)
114 		    break;
115 		continue;
116 	    }
117 	}
118 	if (!teles && *kind == 'a')
119 	    pr("\nAnnouncements since %s", ctime(&then));
120 	if (need_hdr || !tgm.tel_cont) {
121 	    pr("\n> ");
122 	    pr("%s ", telnames[tgm.tel_type]);
123 	    if ((tgm.tel_type == TEL_NORM) ||
124 		(tgm.tel_type == TEL_ANNOUNCE) ||
125 		(tgm.tel_type == TEL_BULLETIN))
126 		pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
127 	    pr("  dated %s", ctime(&tgm.tel_date));
128 	}
129 	teles++;
130 	need_hdr = 0;
131 	res = tel_read_body(telfp, mbox, &tgm, print_sink, NULL);
132 	if (res < 0)
133 	    break;
134     }
135     if (res < 0) {
136 	pr("\n> Mailbox corrupt, tell the deity.\n");
137 	may_delete = 0;
138     }
139 
140     if (*kind == 'a')
141 	np->nat_ann = 0;
142     else {
143 	np->nat_tgms = 0;
144 	if (np->nat_flags & NF_INFORM) {
145 	    pr_inform(player, "\n");
146 	}
147     }
148     putnat(np);
149 
150     if (teles > 0 && player->cnum == num && may_delete) {
151 	pr("\n");
152 	if (teles == 1) {
153 	    if (chance(0.25))
154 		p = "Forget this one? ";
155 	    else
156 		p = "Shall I burn it? ";
157 	} else {
158 	    if (chance(0.25))
159 		p = "Into the shredder, boss? ";
160 	    else
161 		p = "Can I throw away these old love letters? ";
162 	}
163 	p = getstarg(player->argp[1], p, buf);
164 	if (p && *p == 'y') {
165 	    if (*kind == 'a') {
166 		np->nat_annotim = now;
167 		putnat(np);
168 	    } else {
169 		/* force stdio to re-read tel file */
170 		fflush(telfp);
171 		fseek(telfp, 0, SEEK_CUR);
172 		res = tel_read_header(telfp, mbox, &tgm);
173 		if (res != 0) {
174 		    pr("Wait a sec!  A new %s has arrived...\n", kind);
175 		    need_hdr = 1;
176 		    goto more;
177 		}
178 		/* Here, we just re-open the file for "w" only,
179 		   and that will wipe the file clean automatically */
180 		(void)fclose(telfp);
181 		telfp = fopen(mbox, "wb");
182 	    }
183 	}
184     }
185     if (teles <= 0) {
186 	if (player->cnum == num)
187 	    pr("No %ss for you at the moment...\n", kind);
188 	else
189 	    pr("No %ss for %s at the moment...\n", kind, cname(num));
190     }
191     (void)fclose(telfp);
192     return RET_OK;
193 }
194 
195 /*
196  * Print first telegram in file @fname.
197  */
198 int
show_first_tel(char * fname)199 show_first_tel(char *fname)
200 {
201     FILE *fp;
202     int ret;
203     struct telstr tgm;
204 
205     if ((fp = fopen(fname, "rb")) == NULL) {
206 	if (errno == ENOENT)
207 	    return 0;
208 	else {
209 	    logerror("Could not open %s.\n", fname);
210 	    return -1;
211 	}
212     }
213 
214     ret = tel_read_header(fp, fname, &tgm);
215     if (ret < 0)
216 	goto out;
217     ret = tel_read_body(fp, fname, &tgm, print_sink, NULL);
218 
219 out:
220     fclose(fp);
221     return ret;
222 }
223 
224 static int
print_sink(char * chunk,size_t sz,void * arg)225 print_sink(char *chunk, size_t sz, void *arg)
226 {
227     uprnf(chunk);
228     return 0;
229 }
230