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