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