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