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