]> git.pond.sub.org Git - empserver/blob - src/lib/commands/flash.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / flash.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 #if defined(Rel4) || defined(_WIN32)
36 #include <time.h>
37 #else
38 #include <sys/time.h>
39 #endif /* Rel4 */
40 #include "misc.h"
41 #include "player.h"
42 #include "nat.h"
43 #include "file.h"
44 #include "commands.h"
45
46 int
47 flash(void)
48 {
49     struct natstr *us;
50     struct natstr *to;
51     s_char buf[600];
52     int tocn;
53     s_char *sp;
54
55     us = getnatp(player->cnum);
56     if ((tocn = natarg(player->argp[1], "to which country? ")) < 0)
57         return RET_SYN;
58     if (!(to = getnatp((natid)tocn))) {
59         pr("Bad country number\n");
60         return RET_SYN;
61     }
62
63     if (us->nat_stat & STAT_GOD) {
64         /* We are gods, we can flash anyone */
65     } else if (us->nat_stat == VIS) {
66         /* We are a visitor.  We can only flash the gods. :) */
67         if (!(to->nat_stat & STAT_GOD)) {
68             pr("Visitors can only flash the gods.\n");
69             return RET_SYN;
70         }
71     } else {
72         /* Ok, we are a normal country, can we flash them? */
73         if ((!(to->nat_stat & STAT_GOD)) &&
74             (getrel(to, player->cnum) < FRIENDLY)) {
75             pr("%s is not a deity or friendly with us.\n", to->nat_cnam);
76             return RET_SYN;
77         }
78     }
79
80     if (player->argp[2]) {
81         for (sp = &player->combuf[0]; *sp && *sp != ' '; ++sp) ;
82         for (++sp; *sp && *sp != ' '; ++sp) ;
83         sprintf(buf, ":%s", sp);
84         sendmessage(us, to, buf, 1);
85     } else {
86         sendmessage(us, to, "...", 1);
87         while (getstring("> ", buf)) {
88             if (*buf == '.')
89                 break;
90             sendmessage(us, to, buf, 0);
91         }
92         sendmessage(us, to, "<EOT>", 0);
93     }
94     return RET_OK;
95 }
96
97 int
98 wall(void)
99 {
100     struct natstr *us;
101     s_char buf[600];
102     s_char *sp;
103
104     us = getnatp(player->cnum);
105     if (player->argp[1]) {
106         for (sp = &player->combuf[0]; *sp && *sp != ' '; ++sp) ;
107         for (++sp; *sp && *sp != ' '; ++sp) ;
108         sprintf(buf, ":%s", sp);
109         sendmessage(us, 0, buf, 1);
110     } else {
111         sendmessage(us, 0, "...", 1);
112         while (getstring("> ", buf)) {
113             if (*buf == '.')
114                 break;
115             sendmessage(us, 0, buf, 0);
116         }
117         sendmessage(us, 0, "<EOT>", 0);
118     }
119     return RET_OK;
120 }
121
122 int
123 sendmessage(struct natstr *us, struct natstr *to, char *message,
124             int oneshot)
125 {
126     struct player *other;
127     struct tm *tm;
128     char *p;
129     char c;
130     time_t now;
131     int sent = 0;
132     struct natstr *wto;
133
134     for (p = message; 0 != (c = *p); p++) {
135         if (!isprint(c))
136             *p = '*';
137     }
138     if (strlen(message) > 60) {
139         s_char c = message[60];
140         message[60] = '\0';
141         sendmessage(us, to, message, oneshot);
142         message[60] = c;
143         sendmessage(us, to, &message[60], 0);
144         return 0;
145     }
146     time(&now);
147     tm = localtime(&now);
148     for (other = player_next(0); other != 0; other = player_next(other)) {
149         if (to && other->cnum != to->nat_cnum)
150             continue;
151         if (!(wto = getnatp(other->cnum)))
152             continue;
153         if (!to && !player->god && getrel(wto, player->cnum) != ALLIED)
154             continue;
155         if (!player->god && !(wto->nat_flags & NF_FLASH))
156             continue;
157         if (player == other)
158             continue;
159         if (oneshot)
160             if (to)
161                 pr_flash(other, "FLASH from %s (#%d) @ %02d:%02d%s\n",
162                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
163                          tm->tm_min, message);
164             else
165                 pr_flash(other, "BROADCAST from %s (#%d) @ %02d:%02d%s\n",
166                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
167                          tm->tm_min, message);
168
169         else
170             pr_flash(other, "%s (#%d): %s\n",
171                      us->nat_cnam, us->nat_cnum, message);
172         player_wakeup(other);
173         sent++;
174     }
175     if (player->god) {
176         if (to)
177             if (sent)
178                 pr("Flash sent to %s\n", to->nat_cnam);
179             else
180                 pr("%s is not logged on\n", to->nat_cnam);
181         else if (sent)
182             pr("Broadcast sent to %d players\n", sent);
183         else
184             pr("No-one is logged in\n");
185     }
186     if (to && !player->god) {
187         /* If they are allied with us, we would normally see that
188          * they are logged in anyway, so just tell us */
189         if ((getrel(to, player->cnum) == ALLIED) && !sent) {
190             if (to->nat_flags & NF_FLASH)
191                 pr("%s is not logged on\n", to->nat_cnam);
192             else
193                 pr("%s is not accepting flashes\n", to->nat_cnam);
194         }
195     }
196     return 0;
197 }