2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * wu.c: Write a telegram to a user from another
29 * Known contributors to this file:
31 * Markus Armbruster, 2005-2011
45 #include "prototypes.h"
49 static struct telstr last_tel[MAXNOC];
52 clear_telegram_is_new(natid to)
54 last_tel[to].tel_from = NATID_BAD;
58 * telegram_is_new counts new telegrams the same as read_telegrams in
59 * lib/commands/mail.c and lib/commands/rea.c
63 telegram_is_new(natid to, struct telstr *tel)
65 if (tel->tel_from != last_tel[to].tel_from
66 || tel->tel_type != last_tel[to].tel_type
67 || (tel->tel_type != TEL_UPDATE
68 && abs(tel->tel_date - last_tel[to].tel_date) > TEL_SECONDS)) {
76 * Send a telegram from FROM to TO.
77 * Format text to send using printf-style FORMAT and optional
78 * arguments. It is plain ASCII.
79 * If running from the update, telegram type is TEL_UPDATE.
80 * Else if FROM is a deity, type is TEL_BULLETIN.
81 * Else it is TEL_NORM.
82 * Return 0 on success, -1 on error.
85 wu(natid from, natid to, char *format, ...)
92 (void)vsprintf(buf, format, ap);
96 return typed_wu(from, to, buf, TEL_UPDATE);
97 else if (np->nat_stat == STAT_GOD)
98 return typed_wu(from, to, buf, TEL_BULLETIN);
100 return typed_wu(from, to, buf, TEL_NORM);
104 * Send a telegram from FROM to TO.
105 * MESSAGE is the text to send, in UTF-8.
106 * TYPE is the telegram type.
107 * Return 0 on success, -1 on error.
110 typed_wu(natid from, natid to, char *message, int type)
118 struct player *other;
120 if (type == TEL_ANNOUNCE)
125 if (type != TEL_ANNOUNCE)
126 if (!(np = getnatp(to)) || np->nat_stat < STAT_SANCT)
129 if ((fd = open(box, O_WRONLY | O_APPEND, 0)) < 0) {
131 if ((fd = open(box, O_WRONLY | O_APPEND | O_BINARY, 0)) < 0) {
133 logerror("telegram 'open' of %s failed", box);
137 memset(&tel, 0, sizeof(tel));
139 (void)time(&tel.tel_date);
140 len = strlen(message);
141 tel.tel_length = len;
143 tel.tel_cont = !telegram_is_new(to, &tel);
144 iov[0].iov_base = &tel;
145 iov[0].iov_len = sizeof(tel);
146 iov[1].iov_base = message;
147 iov[1].iov_len = len;
148 if (writev(fd, iov, 2) < (int)(iov[0].iov_len + iov[1].iov_len)) {
149 logerror("telegram 'write' to %s failed", box);
154 logerror("telegram 'write' to %s failed to close.", box);
158 if (type == TEL_ANNOUNCE) {
159 for (to = 0; NULL != (np = getnatp(to)); to++) {
160 if (np->nat_stat < STAT_SANCT)
162 if (!player->god && (getrejects(from, np) & REJ_ANNO))
164 if (!np->nat_ann || !tel.tel_cont) {
170 if (!np->nat_tgms || !tel.tel_cont) {
173 if (np->nat_flags & NF_INFORM) {
174 other = getplayer(to);
176 if (np->nat_tgms == 1)
177 pr_inform(other, "[new tele]\n");
179 pr_inform(other, "[%d new teles]\n", np->nat_tgms);