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