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