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