]> git.pond.sub.org Git - empserver/blob - src/lib/commands/rea.c
Don't declare things in more than one place. Move some declarations
[empserver] / src / lib / commands / rea.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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
37 #ifdef aix
38 #define L_SET 0
39 #endif /* aix */
40
41 #include "misc.h"
42 #include "player.h"
43 #include "nat.h"
44 #include "file.h"
45 #include "tel.h"
46 #include "commands.h"
47 #include "optlist.h"
48
49 #include <stdio.h>
50 #include <fcntl.h>
51 #if !defined(_WIN32)
52 #include <sys/file.h>
53 #endif
54
55 int
56 rea(void)
57 {
58     static s_char *telnames[] = {
59         /* must follow TEL_ defines in tel.h */
60         "Telegram", "Announcement", "BULLETIN", "Production Report"
61     };
62     register s_char *p;
63     register s_char *mbox;
64     s_char mbox_buf[256];       /* Maximum path length */
65     struct telstr tgm;
66     FILE *telfp;
67     int teles;
68     int size;
69     unsigned int nbytes;
70     s_char buf[4096];
71     int lasttype;
72     int lastcnum;
73     time_t lastdate;
74     int header;
75     int filelen;
76     s_char kind[80];
77     int n;
78     int num = player->cnum;
79     struct natstr *np = getnatp(player->cnum);
80     time_t now;
81     time_t then;
82     time_t delta;
83     int first = 1;
84     int readit;
85
86     memset(kind, 0, sizeof(kind));
87     (void)time(&now);
88
89     if (*player->argp[0] == 'w') {
90         sprintf(kind, "announcement");
91         if (player->argp[1] && isdigit(*player->argp[1])) {
92             delta = days(atoi(player->argp[1]));
93             then = now - delta;
94         } else
95             then = np->nat_annotim;
96         mbox = annfil;
97     } else {
98         sprintf(kind, "telegram");
99         if (player->god && player->argp[1] != 0) {
100             if ((n = natarg(player->argp[1], "")) < 0)
101                 return RET_SYN;
102             num = n;
103         }
104         mbox = mailbox(mbox_buf, num);
105         clear_telegram_is_new(player->cnum);
106     }
107
108 #if !defined(_WIN32)
109     if ((telfp = fopen(mbox, "r+")) == 0) {
110 #else
111     if ((telfp = fopen(mbox, "r+b")) == 0) {
112 #endif
113         logerror("telegram file %s", mbox);
114         return RET_FAIL;
115     }
116     teles = 0;
117     fseek(telfp, 0L, 0);
118     size = fsize(fileno(telfp));
119   more:
120     lastdate = 0;
121     lastcnum = -1;
122     lasttype = -1;
123     while (fread((s_char *)&tgm, sizeof(tgm), 1, telfp) == 1) {
124         readit = 1;
125         if (tgm.tel_length < 0) {
126             logerror("bad telegram file header in %s", mbox);
127             break;
128         }
129         if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
130             pr("Bad telegram header.  Skipping telegram...\n");
131             readit = 0;
132             goto skip;
133         }
134         if (*kind == 'a') {
135             if (!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO)) {
136                 readit = 0;
137                 goto skip;
138             }
139             if (tgm.tel_date < then) {
140                 readit = 0;
141                 goto skip;
142             }
143         }
144         if (first && *kind == 'a') {
145             pr("\nAnnouncements since %s", ctime(&then));
146             first = 0;
147         }
148         header = 0;
149         if (tgm.tel_type != lasttype || tgm.tel_from != lastcnum)
150             header++;
151         if (abs((int)(tgm.tel_date - (long)lastdate)) > TEL_SECONDS)
152             header++;
153         if (header) {
154             pr("\n> ");
155             lastcnum = tgm.tel_from;
156             lasttype = tgm.tel_type;
157             pr("%s ", telnames[(int)tgm.tel_type]);
158             if ((tgm.tel_type == TEL_NORM) ||
159                 (tgm.tel_type == TEL_ANNOUNCE)) {
160                 pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
161             }
162             if (tgm.tel_type == TEL_BULLETIN) {
163                 pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
164             }
165             pr("  dated %s", ctime(&tgm.tel_date));
166             lastdate = tgm.tel_date;
167         }
168         teles++;
169       skip:
170         while (tgm.tel_length > 0) {
171             nbytes = tgm.tel_length;
172             if (nbytes > sizeof(buf) - 1)
173                 nbytes = sizeof(buf) - 1;
174             (void)fread(buf, sizeof(s_char), nbytes, telfp);
175             buf[nbytes] = 0;
176             if (readit)
177                 prnf(buf);
178             tgm.tel_length -= nbytes;
179         }
180     }
181     p = NULL;
182     if (teles > 0 && player->cnum == num) {     /* } */
183         pr("\n");
184         if (teles == 1) {
185             if (chance(0.25))
186                 p = "Forget this one? ";
187             else
188                 p = "Shall I burn it? ";
189         } else {
190             if (chance(0.25))
191                 p = "Into the shredder, boss? ";
192             else
193                 p = "Can I throw away these old love letters? ";
194         }
195         if (player->god && *kind == 't')
196             p = getstarg(player->argp[2], p, buf);
197         else
198             p = getstarg(player->argp[1], p, buf);
199         if (p && *p == 'y') {
200             if ((filelen = fsize(fileno(telfp))) > size) {
201                 pr("Wait a sec!  A new %s has arrived...\n", kind);
202                 /* force stdio to re-read tel file */
203                 (void)fflush(telfp);
204 #if !defined(_WIN32)
205                 (void)fseek(telfp, (long)size, L_SET);
206 #else
207                 (void)fseek(telfp, (long)size, SEEK_SET);
208 #endif
209                 size = filelen;
210                 (void)time(&now);
211                 goto more;
212             }
213             if (*kind == 'a') {
214                 np->nat_annotim = now;
215                 putnat(np);
216             } else {
217                 /* Here, we just re-open the file for "w" only,
218                    and that will wipe the file clean automatically */
219                 (void)fclose(telfp);
220 #if !defined(_WIN32)
221                 telfp = fopen((char *)mbox, "w");
222 #else
223                 telfp = fopen((char *)mbox, "wb");
224 #endif
225             }
226         }
227     }
228     if (teles <= 0) {
229         if (player->cnum == num)
230             pr("No %ss for you at the moment...\n", kind);
231         else
232             pr("No %ss for %s at the moment...\n", kind, cname(num));
233     }
234     (void)fclose(telfp);
235     if (np->nat_flags & NF_INFORM) {
236         pr_inform(player, "\n");
237         np->nat_tgms = 0;
238         putnat(np);
239     }
240     return RET_OK;
241 }