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