]> git.pond.sub.org Git - empserver/blob - src/lib/subs/wu.c
Fix clear_telegram_is_new() for TEL_NORM from POGO at the epoch
[empserver] / src / lib / subs / wu.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  wu.c: Write a telegram to a user from another
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 2000
31  */
32
33 #include <config.h>
34
35 #include <fcntl.h>
36 #include <stdarg.h>
37 #include <sys/uio.h>
38 #include <unistd.h>
39 #include "file.h"
40 #include "misc.h"
41 #include "nat.h"
42 #include "optlist.h"
43 #include "player.h"
44 #include "prototypes.h"
45 #include "server.h"
46 #include "tel.h"
47
48 static struct telstr last_tel[MAXNOC];
49
50 void
51 clear_telegram_is_new(natid to)
52 {
53     last_tel[to].tel_type = 0;
54     last_tel[to].tel_from = NATID_BAD;
55     last_tel[to].tel_date = 0;
56 }
57
58 /*
59  * telegram_is_new counts new telegrams the same as read_telegrams in
60  * lib/commands/mail.c and lib/commands/rea.c
61  */
62
63 static int
64 telegram_is_new(natid to, struct telstr *tel)
65 {
66     int is_new = 0;
67
68     is_new |= tel->tel_type != last_tel[to].tel_type;
69     is_new |= tel->tel_from != last_tel[to].tel_from;
70     is_new |= tel->tel_type != TEL_UPDATE &&
71         abs(tel->tel_date - last_tel[to].tel_date) > TEL_SECONDS;
72
73     last_tel[to].tel_type = tel->tel_type;
74     last_tel[to].tel_from = tel->tel_from;
75     last_tel[to].tel_date = tel->tel_date;
76
77     return is_new;
78 }
79
80 /*
81  * Send a telegram from FROM to TO.
82  * Format text to send using printf-style FORMAT and optional
83  * arguments.  It is plain ASCII.
84  * If running from the update, telegram type is TEL_UPDATE.
85  * Else if FROM is a deity, type is TEL_BULLETIN.
86  * Else it is TEL_NORM.
87  * Return 0 on success, -1 on error.
88  */
89 int
90 wu(natid from, natid to, char *format, ...)
91 {
92     struct natstr *np;
93     va_list ap;
94     char buf[4096];
95
96     va_start(ap, format);
97     (void)vsprintf(buf, format, ap);
98     va_end(ap);
99     np = getnatp(from);
100     if (update_running)
101         return typed_wu(from, to, buf, TEL_UPDATE);
102     else if (np->nat_stat == STAT_GOD)
103         return typed_wu(from, to, buf, TEL_BULLETIN);
104     else
105         return typed_wu(from, to, buf, TEL_NORM);
106 }
107
108 /*
109  * Send a telegram from FROM to TO.
110  * MESSAGE is the text to send, in UTF-8.
111  * TYPE is the telegram type.
112  * Return 0 on success, -1 on error.
113  */
114 int
115 typed_wu(natid from, natid to, char *message, int type)
116 {
117     size_t len;
118     struct telstr tel;
119     struct natstr *np;
120     struct iovec iov[2];
121     int fd;
122     char box[1024];
123     int new_tele = 0;
124     struct player *other;
125
126     if (type == TEL_ANNOUNCE)
127         strcpy(box, annfil);
128     else
129         mailbox(box, to);
130
131     if (type != TEL_ANNOUNCE)
132         if (!(np = getnatp(to)) || np->nat_stat < STAT_SANCT)
133             return -1;
134 #if !defined(_WIN32)
135     if ((fd = open(box, O_WRONLY | O_APPEND, 0)) < 0) {
136 #else
137     if ((fd = open(box, O_WRONLY | O_APPEND | O_BINARY, 0)) < 0) {
138 #endif
139         logerror("telegram 'open' of %s failed", box);
140         return -1;
141     }
142
143     memset(&tel, 0, sizeof(tel));
144     tel.tel_from = from;
145     (void)time(&tel.tel_date);
146     len = strlen(message);
147     tel.tel_length = len;
148     tel.tel_type = type;
149     iov[0].iov_base = &tel;
150     iov[0].iov_len = sizeof(tel);
151     iov[1].iov_base = message;
152     iov[1].iov_len = len;
153     if (writev(fd, iov, 2) < (int)(iov[0].iov_len + iov[1].iov_len)) {
154         logerror("telegram 'write' to %s failed", box);
155         close(fd);
156         return -1;
157     }
158     if (close(fd) < 0) {
159         logerror("telegram 'write' to %s failed to close.", box);
160         return -1;
161     }
162
163     if (type == TEL_ANNOUNCE) {
164         for (to = 0; NULL != (np = getnatp(to)); to++) {
165             if (np->nat_stat < STAT_SANCT)
166                 continue;
167             if (!player->god && (getrejects(from, np) & REJ_ANNO))
168                 continue;
169             np->nat_ann++;
170             putnat(np);
171         }
172     } else {
173         new_tele = telegram_is_new(to, &tel);
174         np->nat_tgms += new_tele || np->nat_tgms == 0;
175         putnat(np);
176
177         if (new_tele && np->nat_flags & NF_INFORM) {
178             if (NULL != (other = getplayer(to))) {
179                 if (np->nat_tgms == 1)
180                     pr_inform(other, "[new tele]\n");
181                 else
182                     pr_inform(other, "[%d new teles]\n", np->nat_tgms);
183             }
184         }
185     }
186
187     return 0;
188 }