]> git.pond.sub.org Git - empserver/blob - src/lib/commands/flash.c
(copy_ascii_no_funny, copy_utf8_no_funny)
[empserver] / src / lib / commands / flash.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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     char buf[1024];             /* UTF-8 */
48     int tocn;
49     char *sp;                   /* points into player->combuf[], UTF-8 */
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         buf[0] = ':';
80         if (player->flags & PF_UTF8)
81             strcpy(buf+1, sp);
82         else
83             copy_utf8_to_ascii_no_funny(buf+1, sp);
84         sendmessage(us, to, buf, 1);
85     } else {
86         sendmessage(us, to, "...", 1);
87         while (ugetstring("> ", 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     char buf[1024];             /* UTF-8 */
102     char *sp;                   /* points into player->combuf[], UTF-8 */
103
104     us = getnatp(player->cnum);
105     if (player->argp[1]) {
106         for (sp = &player->combuf[0]; *sp && *sp != ' '; ++sp) ;
107         buf[0] = ':';
108         if (player->flags & PF_UTF8)
109             strcpy(buf+1, sp);
110         else
111             copy_utf8_to_ascii_no_funny(buf+1, sp);
112         sendmessage(us, 0, buf, 1);
113     } else {
114         sendmessage(us, 0, "...", 1);
115         while (ugetstring("> ", buf)) {
116             if (*buf == '.')
117                 break;
118             sendmessage(us, 0, buf, 0);
119         }
120         sendmessage(us, 0, "<EOT>", 0);
121     }
122     return RET_OK;
123 }
124
125 int
126 sendmessage(struct natstr *us, struct natstr *to, char *message
127             /* message is message text */, int oneshot)
128 {
129     struct player *other;
130     struct tm *tm;
131     time_t now;
132     int sent = 0;
133     struct natstr *wto;
134     char c; /* c is message text */
135     int pos;
136
137     pos = ufindpfx(message, 60);
138     c = message[pos];
139     if (c)
140         message[pos] = '\0';
141     
142     time(&now);
143     tm = localtime(&now);
144     for (other = player_next(0); other != 0; other = player_next(other)) {
145         if (other->state != PS_PLAYING)
146             continue;
147         if (to && other->cnum != to->nat_cnum)
148             continue;
149         if (!(wto = getnatp(other->cnum)))
150             continue;
151         if (!to && !player->god && getrel(wto, player->cnum) != ALLIED)
152             continue;
153         if (!player->god && !(wto->nat_flags & NF_FLASH))
154             continue;
155         if (player == other)
156             continue;
157         if (oneshot)
158             if (to)
159                 pr_flash(other, "FLASH from %s (#%d) @ %02d:%02d%s\n",
160                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
161                          tm->tm_min, message);
162             else
163                 pr_flash(other, "BROADCAST from %s (#%d) @ %02d:%02d%s\n",
164                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
165                          tm->tm_min, message);
166
167         else
168             pr_flash(other, "%s (#%d): %s\n",
169                      us->nat_cnam, us->nat_cnum, message);
170         player_wakeup(other);
171         sent++;
172     }
173     if (player->god) {
174         if (to)
175             if (sent)
176                 pr("Flash sent to %s\n", to->nat_cnam);
177             else
178                 pr("%s is not logged on\n", to->nat_cnam);
179         else if (sent)
180             pr("Broadcast sent to %d players\n", sent);
181         else
182             pr("No-one is logged in\n");
183     }
184     if (to && !player->god) {
185         /* If they are allied with us, we would normally see that
186          * they are logged in anyway, so just tell us */
187         if ((getrel(to, player->cnum) == ALLIED) && !sent) {
188             if (to->nat_flags & NF_FLASH)
189                 pr("%s is not logged on\n", to->nat_cnam);
190             else
191                 pr("%s is not accepting flashes\n", to->nat_cnam);
192         }
193     }
194     if (c) {
195         message[pos] = c;
196         sendmessage(us, to, &message[pos], 0);
197     }
198     return 0;
199 }