]> git.pond.sub.org Git - empserver/blob - src/lib/commands/flash.c
Fix trailing whitespace
[empserver] / src / lib / commands / flash.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 chat(struct natstr *, struct natstr *, char *);
42 static int sendmessage(struct natstr *, 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 && 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     return chat(us, to, player->comtail[2]);
73 }
74
75 int
76 wall(void)
77 {
78     return chat(getnatp(player->cnum), 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 *us, 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(us, to, buf, 1);
97     } else {
98         sendmessage(us, to, "...", 1);
99         while (ugetstring("> ", buf)) {
100             if (*buf == '.')
101                 break;
102             sendmessage(us, to, buf, 0);
103         }
104         sendmessage(us, 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 *us, struct natstr *to, char *message, int verbose)
118 {
119     struct player *other;
120     struct tm *tm;
121     time_t now;
122     int sent = 0;
123     struct natstr *wto;
124
125     time(&now);
126     tm = localtime(&now);
127     for (other = player_next(0); other != 0; other = player_next(other)) {
128         if (other->state != PS_PLAYING)
129             continue;
130         if (to && other->cnum != to->nat_cnum)
131             continue;
132         if (!(wto = getnatp(other->cnum)))
133             continue;
134         if (!to && !player->god && getrel(wto, player->cnum) != ALLIED)
135             continue;
136         if (!player->god && !(wto->nat_flags & NF_FLASH))
137             continue;
138         if (player == other)
139             continue;
140         if (verbose)
141             if (to)
142                 pr_flash(other, "FLASH from %s (#%d) @ %02d:%02d%s\n",
143                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
144                          tm->tm_min, message);
145             else
146                 pr_flash(other, "BROADCAST from %s (#%d) @ %02d:%02d%s\n",
147                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
148                          tm->tm_min, message);
149
150         else
151             pr_flash(other, "%s (#%d): %s\n",
152                      us->nat_cnam, us->nat_cnum, message);
153         sent++;
154     }
155     if (player->god) {
156         if (to)
157             if (sent)
158                 pr("Flash sent to %s\n", to->nat_cnam);
159             else
160                 pr("%s is not logged on\n", to->nat_cnam);
161         else if (sent)
162             pr("Broadcast sent to %d players\n", sent);
163         else
164             pr("No-one is logged in\n");
165     }
166     if (to && !player->god) {
167         /* If they are allied with us, we would normally see that
168          * they are logged in anyway, so just tell us */
169         if ((getrel(to, player->cnum) == ALLIED) && !sent) {
170             if (to->nat_flags & NF_FLASH)
171                 pr("%s is not logged on\n", to->nat_cnam);
172             else
173                 pr("%s is not accepting flashes\n", to->nat_cnam);
174         }
175     }
176     return 0;
177 }