]> git.pond.sub.org Git - empserver/blob - src/lib/commands/flash.c
Import of Empire 4.2.12
[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, int oneshot)
124 {
125         struct  player *other;  
126         struct  tm *tm;
127         char    *p;
128         char    c;
129         time_t  now;
130         int     sent = 0;
131         struct  natstr *wto;
132
133         for (p = message; 0 != (c = *p); p++) {
134                 if (!isprint(c))
135                         *p = '*';
136         }
137         if (strlen(message) > 60) {
138                 s_char c = message[60];
139                 message[60] = '\0';
140                 sendmessage(us, to, message, oneshot);
141                 message[60] = c;
142                 sendmessage(us, to, &message[60], 0);
143                 return 0;
144         }
145         time(&now);
146         tm = localtime(&now);
147         for (other = player_next(0); other != 0; other = player_next(other)) {
148                 if (to && other->cnum != to->nat_cnum)
149                         continue;
150                 if (!(wto = getnatp(other->cnum)))
151                         continue;
152                 if (!to && !player->god && getrel(wto, player->cnum) != ALLIED)
153                         continue;
154                 if (!player->god && !(wto->nat_flags & NF_FLASH))
155                         continue;
156                 if (player == other)
157                         continue;
158                 if (oneshot)
159                         if (to)
160                                 pr_flash(other, "FLASH from %s (#%d) @ %02d:%02d%s\n",
161                                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
162                                          tm->tm_min, message);
163                         else
164                                 pr_flash(other, "BROADCAST from %s (#%d) @ %02d:%02d%s\n",
165                                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
166                                          tm->tm_min, message);
167
168                 else 
169                         pr_flash(other, "%s (#%d): %s\n",
170                                 us->nat_cnam, us->nat_cnum, message);
171                 player_wakeup(other);
172                 sent++;
173         }
174         if (player->god) {
175                 if (to)
176                         if (sent)
177                                 pr("Flash sent to %s\n", to->nat_cnam);
178                         else
179                                 pr("%s is not logged on\n", to->nat_cnam);
180                 else
181                         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 }