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