]> git.pond.sub.org Git - empserver/blob - src/lib/commands/rea.c
Make struct telstr members tel_type and tel_length unsigned
[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 int
47 rea(void)
48 {
49     static char *telnames[] = {
50         /* must follow TEL_ defines in tel.h */
51         "Telegram", "Announcement", "BULLETIN", "Production Report"
52     };
53     char *p;
54     char *mbox;
55     char mbox_buf[256];         /* Maximum path length */
56     struct telstr tgm;
57     FILE *telfp;
58     int teles;
59     int size;
60     unsigned nbytes;
61     char buf[1024];
62     char msgbuf[4096];          /* UTF-8 */
63     int lasttype;
64     int lastcnum;
65     time_t lastdate;
66     int header;
67     int filelen;
68     char *kind;
69     int n;
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 readit;
77     int may_delete = 1; /* may messages be deleted? */
78
79     now = time(NULL);
80
81     if (*player->argp[0] == 'w') {
82         kind = "announcement";
83         if (player->argp[1] && isdigit(*player->argp[1])) {
84             delta = days(atoi(player->argp[1]));
85             then = now - delta;
86             may_delete = 0;
87         } else
88             then = np->nat_annotim;
89         mbox = annfil;
90     } else {
91         kind = "telegram";
92         if (player->god && player->argp[1] &&
93             (mineq(player->argp[1], "yes") == ME_MISMATCH) &&
94             (mineq(player->argp[1], "no") == ME_MISMATCH)) {
95             if ((n = natarg(player->argp[1], NULL)) < 0)
96                 return RET_SYN;
97             num = n;
98             may_delete = 0;
99         }
100         mbox = mailbox(mbox_buf, num);
101         clear_telegram_is_new(player->cnum);
102     }
103
104     if ((telfp = fopen(mbox, "rb+")) == 0) {
105         logerror("telegram file %s", mbox);
106         return RET_FAIL;
107     }
108     teles = 0;
109     fseek(telfp, 0L, SEEK_SET);
110     size = fsize(fileno(telfp));
111   more:
112     lastdate = 0;
113     lastcnum = -1;
114     lasttype = -1;
115     while (fread(&tgm, sizeof(tgm), 1, telfp) == 1) {
116         readit = 1;
117         if (tgm.tel_type > TEL_LAST) {
118             pr("Bad telegram header.  Skipping telegram...\n");
119             readit = 0;
120             goto skip;
121         }
122         if (*kind == 'a') {
123             if (!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO)) {
124                 readit = 0;
125                 goto skip;
126             }
127             if (tgm.tel_date < then) {
128                 readit = 0;
129                 goto skip;
130             }
131         }
132         if (first && *kind == 'a') {
133             pr("\nAnnouncements since %s", ctime(&then));
134             first = 0;
135         }
136         header = 0;
137         if (tgm.tel_type != lasttype || tgm.tel_from != lastcnum)
138             header++;
139         if (abs((int)(tgm.tel_date - (long)lastdate)) > TEL_SECONDS)
140             header++;
141         if (header) {
142             pr("\n> ");
143             lastcnum = tgm.tel_from;
144             lasttype = tgm.tel_type;
145             pr("%s ", telnames[tgm.tel_type]);
146             if ((tgm.tel_type == TEL_NORM) ||
147                 (tgm.tel_type == TEL_ANNOUNCE) ||
148                 (tgm.tel_type == TEL_BULLETIN))
149                 pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
150             pr("  dated %s", ctime(&tgm.tel_date));
151             lastdate = tgm.tel_date;
152         }
153         teles++;
154       skip:
155         while (tgm.tel_length > 0) {
156             nbytes = tgm.tel_length;
157             if (nbytes > sizeof(msgbuf) - 1)
158                 nbytes = sizeof(msgbuf) - 1;
159             fread(msgbuf, 1, nbytes, telfp);
160             msgbuf[nbytes] = 0;
161             if (readit)
162                 uprnf(msgbuf);
163             tgm.tel_length -= nbytes;
164         }
165     }
166     if (teles > 0 && player->cnum == num && may_delete) {
167         pr("\n");
168         if (teles == 1) {
169             if (chance(0.25))
170                 p = "Forget this one? ";
171             else
172                 p = "Shall I burn it? ";
173         } else {
174             if (chance(0.25))
175                 p = "Into the shredder, boss? ";
176             else
177                 p = "Can I throw away these old love letters? ";
178         }
179         p = getstarg(player->argp[1], p, buf);
180         if (p && *p == 'y') {
181             if ((filelen = fsize(fileno(telfp))) > size) {
182                 pr("Wait a sec!  A new %s has arrived...\n", kind);
183                 /* force stdio to re-read tel file */
184                 (void)fflush(telfp);
185                 (void)fseek(telfp, (long)size, SEEK_SET);
186                 size = filelen;
187                 now = time(NULL);
188                 goto more;
189             }
190             if (*kind == 'a') {
191                 np->nat_annotim = now;
192                 putnat(np);
193             } else {
194                 /* Here, we just re-open the file for "w" only,
195                    and that will wipe the file clean automatically */
196                 (void)fclose(telfp);
197                 telfp = fopen(mbox, "wb");
198             }
199         }
200     }
201     if (teles <= 0) {
202         if (player->cnum == num)
203             pr("No %ss for you at the moment...\n", kind);
204         else
205             pr("No %ss for %s at the moment...\n", kind, cname(num));
206     }
207     (void)fclose(telfp);
208     if (np->nat_flags & NF_INFORM) {
209         pr_inform(player, "\n");
210         np->nat_tgms = 0;
211         putnat(np);
212     }
213     return RET_OK;
214 }