]> git.pond.sub.org Git - empserver/blob - src/lib/commands/flash.c
POSIX has all we need in <time.h>. No need to fool around with
[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 #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[600];
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[600];
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         for (++sp; *sp && *sp != ' '; ++sp) ;
104         sprintf(buf, ":%s", sp);
105         sendmessage(us, 0, buf, 1);
106     } else {
107         sendmessage(us, 0, "...", 1);
108         while (getstring("> ", 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 int
119 sendmessage(struct natstr *us, struct natstr *to, char *message,
120             int oneshot)
121 {
122     struct player *other;
123     struct tm *tm;
124     char *p;
125     char c;
126     time_t now;
127     int sent = 0;
128     struct natstr *wto;
129
130     for (p = message; 0 != (c = *p); p++) {
131         if (!isprint(c))
132             *p = '*';
133     }
134     if (strlen(message) > 60) {
135         s_char c = message[60];
136         message[60] = '\0';
137         sendmessage(us, to, message, oneshot);
138         message[60] = c;
139         sendmessage(us, to, &message[60], 0);
140         return 0;
141     }
142     time(&now);
143     tm = localtime(&now);
144     for (other = player_next(0); other != 0; other = player_next(other)) {
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     return 0;
193 }