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