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