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