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