]> git.pond.sub.org Git - empserver/blob - src/lib/commands/flash.c
ffd0fb8d56f6bfcb1504c2b0c21f257d225da400
[empserver] / src / lib / commands / flash.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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-2009
35  */
36
37 #include <config.h>
38
39 #include "commands.h"
40
41 static int chat(struct natstr *, char *);
42 static int sendmessage(struct natstr *, char *, int);
43
44 int
45 flash(void)
46 {
47     struct natstr *us;
48     struct natstr *to;
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
67             && relations_with(tocn, 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     return chat(to, player->comtail[2]);
74 }
75
76 int
77 wall(void)
78 {
79     return chat(NULL, player->comtail[1]);
80 }
81
82 /*
83  * Send flash message(s) from US to TO.
84  * Null TO broadcasts to all.
85  * MESSAGE is UTF-8.  If it is null, prompt for messages interactively.
86  * Return RET_OK.
87  */
88 static int
89 chat(struct natstr *to, char *message)
90 {
91     char buf[1024];             /* UTF-8 */
92
93     if (message) {
94         buf[0] = ':';
95         buf[1] = ' ';
96         strcpy(buf+2, message);
97         sendmessage(to, buf, 1);
98     } else {
99         sendmessage(to, "...", 1);
100         while (ugetstring("> ", buf)) {
101             if (*buf == '.')
102                 break;
103             sendmessage(to, buf, 0);
104         }
105         sendmessage(to, "<EOT>", 0);
106     }
107     return RET_OK;
108 }
109
110 /*
111  * Send flash message MESSAGE from US to TO.
112  * MESSAGE is UTF-8.
113  * Null TO broadcasts to all.
114  * A header identifying US is prepended to the message.  It is more
115  * verbose if VERBOSE.
116  */
117 static int
118 sendmessage(struct natstr *to, char *message, int verbose)
119 {
120     struct player *other;
121     struct tm *tm;
122     time_t now;
123     int sent = 0, rejected = 0;
124     struct natstr *wto;
125
126     time(&now);
127     tm = localtime(&now);
128     for (other = player_next(NULL); other; other = player_next(other)) {
129         if (other->state != PS_PLAYING)
130             continue;
131         if (to) {
132             /* flash */
133             if (other->cnum != to->nat_cnum)
134                 continue;
135             wto = to;
136         } else {
137             /* wall */
138             if (player == other)
139                 continue;
140             wto = getnatp(other->cnum);
141             if (CANT_HAPPEN(!wto))
142                 continue;
143             if (!player->god
144                 && relations_with(other->cnum, player->cnum) != ALLIED)
145                 continue;
146         }
147         if (!player->god && !(wto->nat_flags & NF_FLASH)) {
148             rejected++;
149             continue;
150         }
151
152         if (verbose)
153             if (to)
154                 pr_flash(other, "FLASH from %s (#%d) @ %02d:%02d%s\n",
155                          cname(player->cnum), player->cnum, tm->tm_hour,
156                          tm->tm_min, message);
157             else
158                 pr_flash(other, "BROADCAST from %s (#%d) @ %02d:%02d%s\n",
159                          cname(player->cnum), player->cnum, tm->tm_hour,
160                          tm->tm_min, message);
161
162         else
163             pr_flash(other, "%s (#%d): %s\n",
164                      cname(player->cnum), player->cnum, message);
165         sent++;
166     }
167
168     if (to) {
169         /* flash */
170         if (player->god
171             || relations_with(to->nat_cnum, player->cnum) == ALLIED) {
172             /* Can see TO logged in anyway, so it's okay to tell */
173             if (rejected)
174                 pr("%s is not accepting flashes\n", to->nat_cnam);
175             else if (!sent) {
176                 pr("%s is not logged on\n", to->nat_cnam);
177             }
178         }
179     } else {
180         /* wall */
181         if (player->god) {
182             if (sent)
183                 pr("Broadcast sent to %d players\n", sent);
184             else
185                 pr("No-one is logged in\n");
186         }
187     }
188     return 0;
189 }