2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * wu.c: Write a telegram to a user from another
30 * Known contributors to this file:
46 #include "prototypes.h"
48 static struct telstr last_tel[MAXNOC];
51 clear_telegram_is_new(natid to)
53 last_tel[to].tel_type = 0;
54 last_tel[to].tel_from = 0;
55 last_tel[to].tel_date = 0;
59 * telegram_is_new counts new telegrams the same as read_telegrams in
60 * lib/commands/mail.c and lib/commands/rea.c
64 telegram_is_new(natid to, struct telstr *tel)
68 is_new |= tel->tel_type != last_tel[to].tel_type;
69 is_new |= tel->tel_from != last_tel[to].tel_from;
70 is_new |= !update_pending && /* sometimes updates take a long time */
71 abs(tel->tel_date - last_tel[to].tel_date) > TEL_SECONDS;
73 last_tel[to].tel_type = tel->tel_type;
74 last_tel[to].tel_from = tel->tel_from;
75 last_tel[to].tel_date = tel->tel_date;
82 wu(natid from, natid to, s_char *format, ...)
89 (void)vsprintf(buf, format, ap);
93 return typed_wu(from, to, buf, TEL_UPDATE);
94 else if (np->nat_stat & STAT_GOD)
95 return typed_wu(from, to, buf, TEL_BULLETIN);
97 return typed_wu(from, to, buf, TEL_NORM);
101 typed_wu(natid from, natid to, s_char *message, int type)
114 struct player *other;
116 if (type == TEL_ANNOUNCE)
121 if (type != TEL_ANNOUNCE)
122 if ((np = getnatp(to)) == 0 ||
123 ((np->nat_stat & STAT_NORM) == 0 &&
124 (np->nat_stat & STAT_SANCT) == 0)) {
128 if ((fd = open(box, O_WRONLY | O_APPEND, 0)) < 0) {
130 if ((fd = open(box, O_WRONLY | O_APPEND | O_BINARY, 0)) < 0) {
132 logerror("telegram 'open' of %s (#%d) failed", box, to);
136 (void)time(&tel.tel_date);
140 if (len >= MAXTELSIZE)
141 len = (MAXTELSIZE - 1);
143 tel.tel_length = len;
146 iov[0].iov_base = (caddr_t)&tel;
147 iov[0].iov_len = sizeof(tel);
148 iov[1].iov_base = message;
149 iov[1].iov_len = len;
150 if (writev(fd, iov, 2) < (int)(iov[0].iov_len + iov[1].iov_len)) {
152 if ((write(fd, &tel, sizeof(tel)) != sizeof(tel)) ||
153 (write(fd, message, len) != len)) {
155 logerror("telegram 'write' to #%d failed", to);
156 } else if (type == TEL_ANNOUNCE) {
157 for (to = 0; NULL != (np = getnatp(to)); to++) {
158 if (!(np->nat_stat & STAT_NORM) &&
159 !(np->nat_stat & STAT_SANCT))
161 if (!player->god && (getrejects(from, np) & REJ_ANNO))
163 notify = (np->nat_ann == 0);
167 player_wakeup_all(to);
170 notify = (np->nat_tgms == 0);
171 new_tele = telegram_is_new(to, &tel);
172 np->nat_tgms += new_tele || notify;
175 if (new_tele && np->nat_flags & NF_INFORM) {
176 if (NULL != (other = getplayer(to))) {
177 if (np->nat_tgms == 1)
178 pr_inform(other, "[new tele]\n");
180 pr_inform(other, "[%d new teles]\n", np->nat_tgms);
181 player_wakeup_all(to);
184 player_wakeup_all(to);