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