]> git.pond.sub.org Git - empserver/blob - src/lib/commands/flash.c
9227f0539883a77af554f751760ebdfb58813f05
[empserver] / src / lib / commands / flash.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  flash.c: Flash a message to another player
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 1998
32  *     Ron Koenderink, 2005
33  *     Markus Armbruster, 2004-2013
34  */
35
36 #include <config.h>
37
38 #include "commands.h"
39
40 static int chat(struct natstr *, char *);
41 static int sendmessage(struct natstr *, char *, int);
42
43 int
44 flash(void)
45 {
46     struct natstr *us;
47     struct natstr *to;
48     int tocn;
49
50     us = getnatp(player->cnum);
51     if ((tocn = natarg(player->argp[1], "to which country? ")) < 0)
52         return RET_SYN;
53     to = getnatp(tocn);
54
55     if (us->nat_stat == STAT_GOD) {
56         /* We are gods, we can flash anyone */
57     } else if (us->nat_stat == STAT_VIS) {
58         /* We are a visitor.  We can only flash the gods. :) */
59         if (to->nat_stat != STAT_GOD) {
60             pr("Visitors can only flash the gods.\n");
61             return RET_SYN;
62         }
63     } else {
64         /* Ok, we are a normal country, can we flash them? */
65         if (to->nat_stat != STAT_GOD
66             && relations_with(tocn, 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     return chat(to, player->comtail[2]);
73 }
74
75 int
76 wall(void)
77 {
78     return chat(NULL, player->comtail[1]);
79 }
80
81 /*
82  * Send flash message(s) from US to TO.
83  * Null TO broadcasts to all.
84  * MESSAGE is UTF-8.  If it is null, prompt for messages interactively.
85  * Return RET_OK.
86  */
87 static int
88 chat(struct natstr *to, char *message)
89 {
90     char buf[1024];             /* UTF-8 */
91
92     if (message) {
93         buf[0] = ':';
94         buf[1] = ' ';
95         strcpy(buf+2, message);
96         sendmessage(to, buf, 1);
97     } else {
98         sendmessage(to, "...", 1);
99         while (ugetstring("> ", buf)) {
100             if (*buf == '.')
101                 break;
102             sendmessage(to, buf, 0);
103         }
104         sendmessage(to, "<EOT>", 0);
105     }
106     return RET_OK;
107 }
108
109 /*
110  * Send flash message MESSAGE from US to TO.
111  * MESSAGE is UTF-8.
112  * Null TO broadcasts to all.
113  * A header identifying US is prepended to the message.  It is more
114  * verbose if VERBOSE.
115  */
116 static int
117 sendmessage(struct natstr *to, char *message, int verbose)
118 {
119     struct player *other;
120     struct tm *tm;
121     time_t now;
122     int sent = 0, rejected = 0;
123     struct natstr *wto;
124
125     time(&now);
126     tm = localtime(&now);
127     for (other = player_next(NULL); other; other = player_next(other)) {
128         if (other->state != PS_PLAYING)
129             continue;
130         if (to) {
131             /* flash */
132             if (other->cnum != to->nat_cnum)
133                 continue;
134             wto = to;
135         } else {
136             /* wall */
137             if (player == other)
138                 continue;
139             wto = getnatp(other->cnum);
140             if (CANT_HAPPEN(!wto))
141                 continue;
142             if (!player->god
143                 && relations_with(other->cnum, player->cnum) != ALLIED)
144                 continue;
145         }
146         if (!player->god && !(wto->nat_flags & NF_FLASH)) {
147             rejected++;
148             continue;
149         }
150
151         if (verbose)
152             if (to)
153                 pr_flash(other, "FLASH from %s @ %02d:%02d%s\n",
154                          prnatid(player->cnum),
155                          tm->tm_hour, tm->tm_min, message);
156             else
157                 pr_flash(other, "BROADCAST from %s @ %02d:%02d%s\n",
158                          prnatid(player->cnum),
159                          tm->tm_hour, tm->tm_min, message);
160
161         else
162             pr_flash(other, "%s: %s\n",
163                      prnatid(player->cnum), message);
164         sent++;
165     }
166
167     if (to) {
168         /* flash */
169         if (player->god
170             || relations_with(to->nat_cnum, player->cnum) == ALLIED) {
171             /* Can see TO logged in anyway, so it's okay to tell */
172             if (rejected)
173                 pr("%s is not accepting flashes\n", to->nat_cnam);
174             else if (!sent) {
175                 pr("%s is not logged on\n", to->nat_cnam);
176             }
177         }
178     } else {
179         /* wall */
180         if (player->god) {
181             if (sent)
182                 pr("Broadcast sent to %d players\n", sent);
183             else
184                 pr("No-one is logged in\n");
185         }
186     }
187     return 0;
188 }