]> git.pond.sub.org Git - empserver/blob - src/lib/commands/flash.c
Change nation status from bits to a simple enum:
[empserver] / src / lib / commands / flash.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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  *  flash.c: Flash a message to another player
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1998
33  */
34
35 #include <config.h>
36
37 #include <time.h>
38 #include "misc.h"
39 #include "player.h"
40 #include "nat.h"
41 #include "file.h"
42 #include "commands.h"
43
44 int
45 flash(void)
46 {
47     struct natstr *us;
48     struct natstr *to;
49     char buf[1024];             /* UTF-8 */
50     int tocn;
51     char *sp;                   /* points into player->combuf[], UTF-8 */
52
53     us = getnatp(player->cnum);
54     if ((tocn = natarg(player->argp[1], "to which country? ")) < 0)
55         return RET_SYN;
56     if (!(to = getnatp((natid)tocn))) {
57         pr("Bad country number\n");
58         return RET_SYN;
59     }
60
61     if (us->nat_stat == STAT_GOD) {
62         /* We are gods, we can flash anyone */
63     } else if (us->nat_stat == STAT_VIS) {
64         /* We are a visitor.  We can only flash the gods. :) */
65         if (to->nat_stat != STAT_GOD) {
66             pr("Visitors can only flash the gods.\n");
67             return RET_SYN;
68         }
69     } else {
70         /* Ok, we are a normal country, can we flash them? */
71         if (to->nat_stat != STAT_GOD && getrel(to, player->cnum) < FRIENDLY) {
72             pr("%s is not a deity or friendly with us.\n", to->nat_cnam);
73             return RET_SYN;
74         }
75     }
76
77     if (player->argp[2]) {
78         for (sp = player->combuf; *sp && *sp != ' '; ++sp) ;
79         for (++sp; *sp && *sp != ' '; ++sp) ;
80         buf[0] = ':';
81         if (player->flags & PF_UTF8)
82             strcpy(buf+1, sp);
83         else
84             copy_utf8_to_ascii_no_funny(buf+1, sp);
85         sendmessage(us, to, buf, 1);
86     } else {
87         sendmessage(us, to, "...", 1);
88         while (ugetstring("> ", buf)) {
89             if (*buf == '.')
90                 break;
91             sendmessage(us, to, buf, 0);
92         }
93         sendmessage(us, to, "<EOT>", 0);
94     }
95     return RET_OK;
96 }
97
98 int
99 wall(void)
100 {
101     struct natstr *us;
102     char buf[1024];             /* UTF-8 */
103     char *sp;                   /* points into player->combuf[], UTF-8 */
104
105     us = getnatp(player->cnum);
106     if (player->argp[1]) {
107         for (sp = player->combuf; *sp && *sp != ' '; ++sp) ;
108         buf[0] = ':';
109         if (player->flags & PF_UTF8)
110             strcpy(buf+1, sp);
111         else
112             copy_utf8_to_ascii_no_funny(buf+1, sp);
113         sendmessage(us, 0, buf, 1);
114     } else {
115         sendmessage(us, 0, "...", 1);
116         while (ugetstring("> ", buf)) {
117             if (*buf == '.')
118                 break;
119             sendmessage(us, 0, buf, 0);
120         }
121         sendmessage(us, 0, "<EOT>", 0);
122     }
123     return RET_OK;
124 }
125
126 /*
127  * Send flash message MESSAGE from US to TO.
128  * MESSAGE is UTF-8.  Long messages are broken into several parts.
129  * A header identifying US is prepended to each part.  The first
130  * header is more verbose if ONESHOT.
131  */
132 int
133 sendmessage(struct natstr *us, struct natstr *to, char *message, int oneshot)
134 {
135     struct player *other;
136     struct tm *tm;
137     time_t now;
138     int sent = 0;
139     struct natstr *wto;
140     char c;
141     int pos;
142
143     pos = ufindpfx(message, 60);
144     c = message[pos];
145     if (c)
146         message[pos] = '\0';
147
148     time(&now);
149     tm = localtime(&now);
150     for (other = player_next(0); other != 0; other = player_next(other)) {
151         if (other->state != PS_PLAYING)
152             continue;
153         if (to && other->cnum != to->nat_cnum)
154             continue;
155         if (!(wto = getnatp(other->cnum)))
156             continue;
157         if (!to && !player->god && getrel(wto, player->cnum) != ALLIED)
158             continue;
159         if (!player->god && !(wto->nat_flags & NF_FLASH))
160             continue;
161         if (player == other)
162             continue;
163         if (oneshot)
164             if (to)
165                 pr_flash(other, "FLASH from %s (#%d) @ %02d:%02d%s\n",
166                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
167                          tm->tm_min, message);
168             else
169                 pr_flash(other, "BROADCAST from %s (#%d) @ %02d:%02d%s\n",
170                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
171                          tm->tm_min, message);
172
173         else
174             pr_flash(other, "%s (#%d): %s\n",
175                      us->nat_cnam, us->nat_cnum, message);
176         player_wakeup(other);
177         sent++;
178     }
179     if (player->god) {
180         if (to)
181             if (sent)
182                 pr("Flash sent to %s\n", to->nat_cnam);
183             else
184                 pr("%s is not logged on\n", to->nat_cnam);
185         else if (sent)
186             pr("Broadcast sent to %d players\n", sent);
187         else
188             pr("No-one is logged in\n");
189     }
190     if (to && !player->god) {
191         /* If they are allied with us, we would normally see that
192          * they are logged in anyway, so just tell us */
193         if ((getrel(to, player->cnum) == ALLIED) && !sent) {
194             if (to->nat_flags & NF_FLASH)
195                 pr("%s is not logged on\n", to->nat_cnam);
196             else
197                 pr("%s is not accepting flashes\n", to->nat_cnam);
198         }
199     }
200     if (c) {
201         message[pos] = c;
202         sendmessage(us, to, &message[pos], 0);
203     }
204     return 0;
205 }