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