]> git.pond.sub.org Git - empserver/blob - src/lib/commands/rea.c
Plug FILE leak on show_first_tel() error paths
[empserver] / src / lib / commands / rea.c
1 /*
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
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
35  */
36
37 #include <config.h>
38
39 #include <ctype.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include "commands.h"
43 #include "match.h"
44 #include "misc.h"
45 #include "optlist.h"
46 #include "tel.h"
47
48 static int print_sink(char *, size_t, void *);
49
50 int
51 rea(void)
52 {
53     static char *telnames[] = {
54         /* must follow TEL_ defines in tel.h */
55         "Telegram", "Announcement", "BULLETIN", "Production Report"
56     };
57     char *p;
58     char *mbox;
59     char mbox_buf[256];         /* Maximum path length */
60     struct telstr tgm;
61     FILE *telfp;
62     int teles;
63     int size;
64     char buf[1024];
65     int lasttype;
66     int lastcnum;
67     time_t lastdate;
68     int header;
69     int filelen;
70     char *kind;
71     int n, res;
72     int num = player->cnum;
73     struct natstr *np = getnatp(player->cnum);
74     time_t now;
75     time_t then;
76     time_t delta;
77     int first = 1;
78     int may_delete = 1; /* may messages be deleted? */
79
80     now = time(NULL);
81
82     if (*player->argp[0] == 'w') {
83         kind = "announcement";
84         if (player->argp[1] && isdigit(*player->argp[1])) {
85             delta = days(atoi(player->argp[1]));
86             then = now - delta;
87             may_delete = 0;
88         } else
89             then = np->nat_annotim;
90         mbox = annfil;
91     } else {
92         kind = "telegram";
93         if (player->god && player->argp[1] &&
94             (mineq(player->argp[1], "yes") == ME_MISMATCH) &&
95             (mineq(player->argp[1], "no") == ME_MISMATCH)) {
96             if ((n = natarg(player->argp[1], NULL)) < 0)
97                 return RET_SYN;
98             num = n;
99             may_delete = 0;
100         }
101         mbox = mailbox(mbox_buf, num);
102         clear_telegram_is_new(player->cnum);
103     }
104
105     if (!(telfp = fopen(mbox, "rb+"))) {
106         logerror("telegram file %s", mbox);
107         return RET_FAIL;
108     }
109     teles = 0;
110     fseek(telfp, 0L, SEEK_SET);
111     size = fsize(fileno(telfp));
112   more:
113     lastdate = 0;
114     lastcnum = -1;
115     lasttype = -1;
116     while ((res = tel_read_header(telfp, mbox, &tgm)) > 0) {
117         if (*kind == 'a') {
118             if ((!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO))
119                 || tgm.tel_date < then) {
120                 res = tel_read_body(telfp, mbox, &tgm, NULL, NULL);
121                 if (res < 0)
122                     break;
123                 continue;
124             }
125         }
126         if (first && *kind == 'a') {
127             pr("\nAnnouncements since %s", ctime(&then));
128             first = 0;
129         }
130         header = 0;
131         if (tgm.tel_type != lasttype || tgm.tel_from != lastcnum)
132             header++;
133         if (abs((int)(tgm.tel_date - (long)lastdate)) > TEL_SECONDS)
134             header++;
135         if (header) {
136             pr("\n> ");
137             lastcnum = tgm.tel_from;
138             lasttype = tgm.tel_type;
139             pr("%s ", telnames[tgm.tel_type]);
140             if ((tgm.tel_type == TEL_NORM) ||
141                 (tgm.tel_type == TEL_ANNOUNCE) ||
142                 (tgm.tel_type == TEL_BULLETIN))
143                 pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
144             pr("  dated %s", ctime(&tgm.tel_date));
145             lastdate = tgm.tel_date;
146         }
147         teles++;
148         res = tel_read_body(telfp, mbox, &tgm, print_sink, NULL);
149         if (res < 0)
150             break;
151     }
152     if (res < 0)
153         pr("\n> Mailbox corrupt, tell the deity.\n");
154
155     if (teles > 0 && player->cnum == num && may_delete) {
156         pr("\n");
157         if (teles == 1) {
158             if (chance(0.25))
159                 p = "Forget this one? ";
160             else
161                 p = "Shall I burn it? ";
162         } else {
163             if (chance(0.25))
164                 p = "Into the shredder, boss? ";
165             else
166                 p = "Can I throw away these old love letters? ";
167         }
168         p = getstarg(player->argp[1], p, buf);
169         if (p && *p == 'y') {
170             if ((filelen = fsize(fileno(telfp))) > size) {
171                 pr("Wait a sec!  A new %s has arrived...\n", kind);
172                 /* force stdio to re-read tel file */
173                 (void)fflush(telfp);
174                 (void)fseek(telfp, (long)size, SEEK_SET);
175                 size = filelen;
176                 now = time(NULL);
177                 goto more;
178             }
179             if (*kind == 'a') {
180                 np->nat_annotim = now;
181                 putnat(np);
182             } else {
183                 /* Here, we just re-open the file for "w" only,
184                    and that will wipe the file clean automatically */
185                 (void)fclose(telfp);
186                 telfp = fopen(mbox, "wb");
187             }
188         }
189     }
190     if (teles <= 0) {
191         if (player->cnum == num)
192             pr("No %ss for you at the moment...\n", kind);
193         else
194             pr("No %ss for %s at the moment...\n", kind, cname(num));
195     }
196     (void)fclose(telfp);
197     if (np->nat_flags & NF_INFORM) {
198         pr_inform(player, "\n");
199         np->nat_tgms = 0;
200         putnat(np);
201     }
202     return RET_OK;
203 }
204
205 /*
206  * Print first telegram in file FNAME.
207  */
208 int
209 show_first_tel(char *fname)
210 {
211     FILE *fp;
212     int ret;
213     struct telstr tgm;
214
215     if ((fp = fopen(fname, "rb")) == NULL) {
216         if (errno == ENOENT)
217             return 0;
218         else {
219             logerror("Could not open %s.\n", fname);
220             return -1;
221         }
222     }
223
224     ret = tel_read_header(fp, fname, &tgm);
225     if (ret < 0)
226         goto out;
227     ret = tel_read_body(fp, fname, &tgm, print_sink, NULL);
228
229 out:
230     fclose(fp);
231     return ret;
232 }
233
234 static int
235 print_sink(char *chunk, size_t sz, void *arg)
236 {
237     uprnf(chunk);
238     return 0;
239 }