]> git.pond.sub.org Git - empserver/blob - src/lib/commands/rea.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / rea.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 "misc.h"
39 #include "player.h"
40 #include "nat.h"
41 #include "file.h"
42 #include "tel.h"
43 #include "commands.h"
44 #include "optlist.h"
45 #include "match.h"
46
47 #include <stdio.h>
48 #include <fcntl.h>
49 #if !defined(_WIN32)
50 #include <sys/file.h>
51 #endif
52
53 int
54 rea(void)
55 {
56     static s_char *telnames[] = {
57         /* must follow TEL_ defines in tel.h */
58         "Telegram", "Announcement", "BULLETIN", "Production Report"
59     };
60     register s_char *p;
61     register s_char *mbox;
62     s_char mbox_buf[256];       /* Maximum path length */
63     struct telstr tgm;
64     FILE *telfp;
65     int teles;
66     int size;
67     unsigned int nbytes;
68     char buf[1024];
69     char msgbuf[4096];          /* UTF-8 */
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     int may_delete = 1; /* may messages be deleted? */
85
86     memset(kind, 0, sizeof(kind));
87     now = time(NULL);
88
89     if (*player->argp[0] == 'w') {
90         strcpy(kind, "announcement");
91         if (player->argp[1] && isdigit(*player->argp[1])) {
92             delta = days(atoi(player->argp[1]));
93             then = now - delta;
94             may_delete = 0;
95         } else
96             then = np->nat_annotim;
97         mbox = annfil;
98     } else {
99         strcpy(kind, "telegram");
100         if (player->god && player->argp[1] &&
101             (mineq(player->argp[1], "yes") == ME_MISMATCH) &&
102             (mineq(player->argp[1], "no") == ME_MISMATCH)) {
103             if ((n = natarg(player->argp[1], NULL)) < 0)
104                 return RET_SYN;
105             num = n;
106             may_delete = 0;
107         }
108         mbox = mailbox(mbox_buf, num);
109         clear_telegram_is_new(player->cnum);
110     }
111
112     if ((telfp = fopen(mbox, "rb+")) == 0) {
113         logerror("telegram file %s", mbox);
114         return RET_FAIL;
115     }
116     teles = 0;
117     fseek(telfp, 0L, SEEK_SET);
118     size = fsize(fileno(telfp));
119   more:
120     lastdate = 0;
121     lastcnum = -1;
122     lasttype = -1;
123     while (fread(&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                 (tgm.tel_type == TEL_BULLETIN))
161                 pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
162             pr("  dated %s", ctime(&tgm.tel_date));
163             lastdate = tgm.tel_date;
164         }
165         teles++;
166       skip:
167         while (tgm.tel_length > 0) {
168             nbytes = tgm.tel_length;
169             if (nbytes > sizeof(msgbuf) - 1)
170                 nbytes = sizeof(msgbuf) - 1;
171             (void)fread(msgbuf, sizeof(s_char), nbytes, telfp);
172             msgbuf[nbytes] = 0;
173             if (readit)
174                 uprnf(msgbuf);
175             tgm.tel_length -= nbytes;
176         }
177     }
178     p = NULL;
179     if (teles > 0 && player->cnum == num && may_delete) {
180         pr("\n");
181         if (teles == 1) {
182             if (chance(0.25))
183                 p = "Forget this one? ";
184             else
185                 p = "Shall I burn it? ";
186         } else {
187             if (chance(0.25))
188                 p = "Into the shredder, boss? ";
189             else
190                 p = "Can I throw away these old love letters? ";
191         }
192         p = getstarg(player->argp[1], p, buf);
193         if (p && *p == 'y') {
194             if ((filelen = fsize(fileno(telfp))) > size) {
195                 pr("Wait a sec!  A new %s has arrived...\n", kind);
196                 /* force stdio to re-read tel file */
197                 (void)fflush(telfp);
198                 (void)fseek(telfp, (long)size, SEEK_SET);
199                 size = filelen;
200                 now = time(NULL);
201                 goto more;
202             }
203             if (*kind == 'a') {
204                 np->nat_annotim = now;
205                 putnat(np);
206             } else {
207                 /* Here, we just re-open the file for "w" only,
208                    and that will wipe the file clean automatically */
209                 (void)fclose(telfp);
210                 telfp = fopen((char *)mbox, "wb");
211             }
212         }
213     }
214     if (teles <= 0) {
215         if (player->cnum == num)
216             pr("No %ss for you at the moment...\n", kind);
217         else
218             pr("No %ss for %s at the moment...\n", kind, cname(num));
219     }
220     (void)fclose(telfp);
221     if (np->nat_flags & NF_INFORM) {
222         pr_inform(player, "\n");
223         np->nat_tgms = 0;
224         putnat(np);
225     }
226     return RET_OK;
227 }