]> git.pond.sub.org Git - empserver/blob - src/lib/subs/wu.c
Change nation status from bits to a simple enum:
[empserver] / src / lib / subs / wu.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  *  wu.c: Write a telegram to a user from another
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  *     
33  */
34
35 #include <config.h>
36
37 #include <stdarg.h>
38 #include "misc.h"
39 #include <fcntl.h>
40 #if !defined(_WIN32)
41 #include <sys/uio.h>
42 #endif
43 #include "nat.h"
44 #include "tel.h"
45 #include "file.h"
46 #include "player.h"
47 #include "server.h"
48 #include "optlist.h"
49 #include "prototypes.h"
50
51 static struct telstr last_tel[MAXNOC];
52
53 void
54 clear_telegram_is_new(natid to)
55 {
56     last_tel[to].tel_type = 0;
57     last_tel[to].tel_from = 0;
58     last_tel[to].tel_date = 0;
59 }
60
61 /*
62  * telegram_is_new counts new telegrams the same as read_telegrams in 
63  * lib/commands/mail.c and lib/commands/rea.c
64  */
65
66 static int
67 telegram_is_new(natid to, struct telstr *tel)
68 {
69     int is_new = 0;
70
71     is_new |= tel->tel_type != last_tel[to].tel_type;
72     is_new |= tel->tel_from != last_tel[to].tel_from;
73     is_new |= !update_pending &&        /* sometimes updates take a long time */
74         abs(tel->tel_date - last_tel[to].tel_date) > TEL_SECONDS;
75
76     last_tel[to].tel_type = tel->tel_type;
77     last_tel[to].tel_from = tel->tel_from;
78     last_tel[to].tel_date = tel->tel_date;
79
80     return is_new;
81 }
82
83 /*
84  * Send a telegram from FROM to TO.
85  * Format text to send using printf-style FORMAT and optional
86  * arguments.  It is plain ASCII.
87  * If running from the update, telegram type is TEL_UPDATE.
88  * Else if FROM is a deity, type is TEL_BULLETIN.
89  * Else it is TEL_NORM.
90  * Return 0 on success, -1 on error.
91  */
92 int
93 wu(natid from, natid to, char *format, ...)
94 {
95     struct natstr *np;
96     va_list ap;
97     char buf[4096];
98
99     va_start(ap, format);
100     (void)vsprintf(buf, format, ap);
101     va_end(ap);
102     np = getnatp(from);
103     if (update_pending)
104         return typed_wu(from, to, buf, TEL_UPDATE);
105     else if (np->nat_stat == STAT_GOD)
106         return typed_wu(from, to, buf, TEL_BULLETIN);
107     else
108         return typed_wu(from, to, buf, TEL_NORM);
109 }
110
111 /*
112  * Send a telegram from FROM to TO.
113  * MESSAGE is the text to send, in UTF-8.
114  * TYPE is the telegram type.
115  * Return 0 on success, -1 on error.
116  */
117 int
118 typed_wu(natid from, natid to, char *message, int type)
119 {
120     int len;
121     struct telstr tel;
122     struct natstr *np;
123 #if !defined(_WIN32)
124     struct iovec iov[2];
125 #endif
126     int fd;
127     char box[1024];
128     int notify = 0;
129     int write_ok = 0;
130     int new_tele = 0;
131     struct player *other;
132
133     if (type == TEL_ANNOUNCE)
134         strcpy(box, annfil);
135     else
136         mailbox(box, to);
137
138     if (type != TEL_ANNOUNCE)
139         if ((np = getnatp(to)) == 0 || np->nat_stat < STAT_SANCT)
140             return -1;
141 #if !defined(_WIN32)
142     if ((fd = open(box, O_WRONLY | O_APPEND, 0)) < 0) {
143 #else
144     if ((fd = open(box, O_WRONLY | O_APPEND | O_BINARY, 0)) < 0) {
145 #endif
146         logerror("telegram 'open' of %s (#%d) failed", box, to);
147         return -1;
148     }
149     tel.tel_from = from;
150     (void)time(&tel.tel_date);
151     len = strlen(message);
152     if (CANT_HAPPEN(len > MAXTELSIZE)) {
153         len = MAXTELSIZE;
154         message[len] = 0;
155     }
156     tel.tel_length = len;
157     tel.tel_type = type;
158 #if !defined(_WIN32)
159     iov[0].iov_base = &tel;
160     iov[0].iov_len = sizeof(tel);
161     iov[1].iov_base = message;
162     iov[1].iov_len = len;
163     if (writev(fd, iov, 2) < (int)(iov[0].iov_len + iov[1].iov_len)) {
164 #else
165     if ((write(fd, &tel, sizeof(tel)) != sizeof(tel)) ||
166         (write(fd, message, len) != len)) {
167 #endif
168         logerror("telegram 'write' to #%d failed", to);
169     } else
170         write_ok = 1;
171
172     if (close(fd) == -1) {
173         logerror("telegram 'write' to #%d failed to close.", to);
174     } else if (write_ok && type == TEL_ANNOUNCE) {
175         for (to = 0; NULL != (np = getnatp(to)); to++) {
176             if (np->nat_stat < STAT_SANCT)
177                 continue;
178             if (!player->god && (getrejects(from, np) & REJ_ANNO))
179                 continue;
180             notify = (np->nat_ann == 0);
181             np->nat_ann++;
182             putnat(np);
183             if (notify)
184                 player_wakeup_all(to);
185         }
186     } else if (write_ok) {
187         notify = (np->nat_tgms == 0);
188         new_tele = telegram_is_new(to, &tel);
189         np->nat_tgms += new_tele || notify;
190         putnat(np);
191
192         if (new_tele && np->nat_flags & NF_INFORM) {
193             if (NULL != (other = getplayer(to))) {
194                 if (np->nat_tgms == 1)
195                     pr_inform(other, "[new tele]\n");
196                 else
197                     pr_inform(other, "[%d new teles]\n", np->nat_tgms);
198                 player_wakeup_all(to);
199             }
200         } else if (notify)
201             player_wakeup_all(to);
202     }
203
204     return 0;
205 }