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