]> git.pond.sub.org Git - empserver/blob - src/lib/subs/wu.c
update server: Move update_running from server/ to update/
[empserver] / src / lib / subs / wu.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, 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  *     Markus Armbruster, 2005-2015
32  */
33
34 #include <config.h>
35
36 #include <fcntl.h>
37 #include <stdarg.h>
38 #include <sys/uio.h>
39 #include <unistd.h>
40 #include "file.h"
41 #include "misc.h"
42 #include "nat.h"
43 #include "optlist.h"
44 #include "player.h"
45 #include "prototypes.h"
46 #include "tel.h"
47 #include "update.h"
48
49 static struct telstr last_tel[MAXNOC];
50
51 void
52 clear_telegram_is_new(natid to)
53 {
54     last_tel[to].tel_from = NATID_BAD;
55 }
56
57 static int
58 telegram_is_new(natid to, struct telstr *tel)
59 {
60     if (tel->tel_from != last_tel[to].tel_from
61         || tel->tel_type != last_tel[to].tel_type
62         || (tel->tel_type != TEL_UPDATE
63             && tel->tel_date - last_tel[to].tel_date > TEL_SECONDS)) {
64         last_tel[to] = *tel;
65         return 1;
66     }
67     return 0;
68 }
69
70 /*
71  * Send a telegram from @from to @to.
72  * Format text to send using printf-style @format and optional
73  * arguments.  It is plain ASCII.
74  * If running from the update, telegram type is TEL_UPDATE.
75  * Else if @from is a deity, type is TEL_BULLETIN.
76  * Else it is TEL_NORM.
77  * Return 0 on success, -1 on error.
78  */
79 int
80 wu(natid from, natid to, char *format, ...)
81 {
82     struct natstr *np;
83     va_list ap;
84     char buf[4096];
85
86     va_start(ap, format);
87     (void)vsprintf(buf, format, ap);
88     va_end(ap);
89     np = getnatp(from);
90     if (update_running)
91         return typed_wu(from, to, buf, TEL_UPDATE);
92     else if (np->nat_stat == STAT_GOD)
93         return typed_wu(from, to, buf, TEL_BULLETIN);
94     else
95         return typed_wu(from, to, buf, TEL_NORM);
96 }
97
98 /*
99  * Send a telegram from @from to @to.
100  * @message is the text to send, in UTF-8.
101  * @type is the telegram type.
102  * Return 0 on success, -1 on error.
103  */
104 int
105 typed_wu(natid from, natid to, char *message, int type)
106 {
107     size_t len;
108     struct telstr tel;
109     struct natstr *np;
110     struct iovec iov[2];
111     int fd;
112     char box[1024];
113     struct player *other;
114
115     if (type == TEL_ANNOUNCE)
116         strcpy(box, annfil);
117     else
118         mailbox(box, to);
119
120     if (type != TEL_ANNOUNCE)
121         if (!(np = getnatp(to)) || np->nat_stat < STAT_SANCT)
122             return -1;
123 #if !defined(_WIN32)
124     if ((fd = open(box, O_WRONLY | O_APPEND, 0)) < 0) {
125 #else
126     if ((fd = open(box, O_WRONLY | O_APPEND | O_BINARY, 0)) < 0) {
127 #endif
128         logerror("telegram 'open' of %s failed", box);
129         return -1;
130     }
131
132     memset(&tel, 0, sizeof(tel));
133     tel.tel_from = from;
134     (void)time(&tel.tel_date);
135     len = strlen(message);
136     CANT_HAPPEN(len && message[len - 1] != '\n');
137     tel.tel_length = len;
138     tel.tel_type = type;
139     tel.tel_cont = !telegram_is_new(to, &tel);
140     iov[0].iov_base = &tel;
141     iov[0].iov_len = sizeof(tel);
142     iov[1].iov_base = message;
143     iov[1].iov_len = len;
144     if (writev(fd, iov, 2) < (int)(iov[0].iov_len + iov[1].iov_len)) {
145         logerror("telegram 'write' to %s failed", box);
146         close(fd);
147         return -1;
148     }
149     if (close(fd) < 0) {
150         logerror("telegram 'write' to %s failed to close.", box);
151         return -1;
152     }
153
154     if (type == TEL_ANNOUNCE) {
155         for (to = 0; NULL != (np = getnatp(to)); to++) {
156             if (np->nat_stat < STAT_SANCT)
157                 continue;
158             if (!player->god && (getrejects(from, np) & REJ_ANNO))
159                 continue;
160             if (!np->nat_ann || !tel.tel_cont) {
161                 np->nat_ann++;
162                 putnat(np);
163             }
164         }
165     } else {
166         if (!np->nat_tgms || !tel.tel_cont) {
167             np->nat_tgms++;
168             putnat(np);
169             if (np->nat_flags & NF_INFORM) {
170                 other = getplayer(to);
171                 if (other) {
172                     if (np->nat_tgms == 1)
173                         pr_inform(other, "[new tele]\n");
174                     else
175                         pr_inform(other, "[%d new teles]\n", np->nat_tgms);
176                 }
177             }
178         }
179     }
180
181     return 0;
182 }