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