]> git.pond.sub.org Git - empserver/blob - src/lib/subs/wu.c
Import of Empire 4.2.12
[empserver] / src / lib / subs / wu.c
1 /*
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
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 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.
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
35 #include <stdarg.h>
36 #include "misc.h"
37 #include <fcntl.h>
38 #if !defined(_WIN32)
39 #include <sys/uio.h>
40 #endif
41 #include "nat.h"
42 #include "tel.h"
43 #include "file.h"
44 #include "player.h"
45 #include "prototypes.h"
46
47 static  struct  telstr last_tel[MAXNOC];
48
49 void
50 clear_telegram_is_new(natid to)
51 {
52         last_tel[to].tel_type = 0;
53         last_tel[to].tel_from = 0;
54         last_tel[to].tel_date = 0;
55 }
56
57 /*
58  * telegram_is_new counts new telegrams the same as read_telegrams in 
59  * lib/commands/mail.c and lib/commands/rea.c
60  */
61
62 static int
63 telegram_is_new (natid to, struct telstr *tel)
64 {
65         extern  int     update_pending;
66         int is_new = 0;
67
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;
72         
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;
76
77         return is_new;
78 }
79
80 /*VARARGS*/
81 int
82 wu(natid from, natid to, s_char *format, ...)
83 {
84         struct natstr *np;
85         va_list ap;
86         s_char  buf[4096];
87         extern  int     update_pending;
88
89         va_start(ap, format);
90         (void) vsprintf(buf, format, ap);
91         va_end(ap);
92         np = getnatp(from);
93         if (update_pending)
94                 return typed_wu(from, to, buf, TEL_UPDATE);
95         else if (np->nat_stat & STAT_GOD)
96                 return typed_wu(from, to, buf, TEL_BULLETIN);
97         else
98                 return typed_wu(from, to, buf, TEL_NORM);
99 }
100
101 int
102 typed_wu(natid from, natid to, s_char *message, int type)
103 {
104         register s_char *bp;
105         int     len;
106         struct  telstr tel;
107         struct  natstr *np;
108 #if !defined(_WIN32)
109         struct  iovec iov[2];
110 #endif
111         int     fd;
112         s_char  box[1024];
113         int     notify = 0;
114         int     new_tele = 0;
115         struct player *other;
116
117         if (type == TEL_ANNOUNCE)
118                 strcpy(box, annfil);
119         else
120                 mailbox(box, to);
121
122         if (type != TEL_ANNOUNCE)
123                 if ((np = getnatp(to)) == 0 ||
124                     ((np->nat_stat & STAT_NORM) == 0 &&
125                      (np->nat_stat & STAT_SANCT) == 0)) {
126                         return -1;
127                 }
128
129 #if !defined(_WIN32)
130         if ((fd = open(box, O_WRONLY|O_APPEND, 0)) < 0) {
131 #else
132         if ((fd = open(box, O_WRONLY|O_APPEND|O_BINARY, 0)) < 0) {
133 #endif
134                 logerror("telegram 'open' of %s (#%d) failed", box, to);
135                 return -1;
136         }
137         tel.tel_from = from;
138         (void) time(&tel.tel_date);
139         bp = message;
140         while (*bp++)
141                 ;
142         len = bp - message;
143         if (len >= MAXTELSIZE)
144                 len = (MAXTELSIZE - 1);
145         message[len] = 0;
146         tel.tel_length = len;
147         tel.tel_type = type;
148 #if !defined(_WIN32)
149         iov[0].iov_base = (caddr_t) &tel;
150         iov[0].iov_len = sizeof(tel);
151         iov[1].iov_base = message;
152         iov[1].iov_len = len;
153         if (writev(fd, iov, 2) < (int)(iov[0].iov_len + iov[1].iov_len)) {
154 #else
155     if ((write(fd, &tel, sizeof(tel)) != sizeof(tel)) ||
156             (write(fd, message, len) != len)) {
157 #endif
158                 logerror("telegram 'write' to #%d failed", to);
159         } else if (type == TEL_ANNOUNCE) {
160                 for (to=0; NULL != (np = getnatp(to)); to++) {
161                         if (!(np->nat_stat & STAT_NORM) &&
162                             !(np->nat_stat & STAT_SANCT))
163                                 continue;
164                         if (!player->god && (getrejects(from,np) & REJ_ANNO))
165                                 continue;
166                         notify = (np->nat_ann == 0);
167                         np->nat_ann++;
168                         putnat(np);
169                         if (notify)
170                                 player_wakeup_all(to);                  
171                 }
172         } else {
173                 notify = (np->nat_tgms == 0);
174                 new_tele = telegram_is_new(to, &tel);
175                 np->nat_tgms += new_tele || notify;
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",
184                                                   np->nat_tgms);
185                                 player_wakeup_all(to);
186                         }
187                 } else if (notify)
188                         player_wakeup_all(to);
189         }
190
191         close(fd);
192         return 0;
193 }
194