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