]> git.pond.sub.org Git - empserver/blob - src/lib/commands/flash.c
Support UTF-8 encoded Unicode for user communications.
[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 static int ufindbreak(char *message /* message is message text */,
43                       int num_chars);
44
45 int
46 flash(void)
47 {
48     struct natstr *us;
49     struct natstr *to;
50     char buf[1024]; /* buf is message text */
51     int tocn;
52     char *sp; /* sp is message text */
53
54     us = getnatp(player->cnum);
55     if ((tocn = natarg(player->argp[1], "to which country? ")) < 0)
56         return RET_SYN;
57     if (!(to = getnatp((natid)tocn))) {
58         pr("Bad country number\n");
59         return RET_SYN;
60     }
61
62     if (us->nat_stat & STAT_GOD) {
63         /* We are gods, we can flash anyone */
64     } else if (us->nat_stat == VIS) {
65         /* We are a visitor.  We can only flash the gods. :) */
66         if (!(to->nat_stat & STAT_GOD)) {
67             pr("Visitors can only flash the gods.\n");
68             return RET_SYN;
69         }
70     } else {
71         /* Ok, we are a normal country, can we flash them? */
72         if ((!(to->nat_stat & STAT_GOD)) &&
73             (getrel(to, player->cnum) < FRIENDLY)) {
74             pr("%s is not a deity or friendly with us.\n", to->nat_cnam);
75             return RET_SYN;
76         }
77     }
78
79     if (player->argp[2]) {
80         for (sp = &player->combuf[0]; *sp && *sp != ' '; ++sp) ;
81         for (++sp; *sp && *sp != ' '; ++sp) ;
82         sprintf(buf, ":%s", sp);
83         for(sp = buf; 0 != *sp; ++sp) {
84             if ((*sp >= 0x0 && *sp < 0x20  && *sp != '\t') ||
85                 *sp == 0x7f)
86                 *sp = '?';
87             else if (!(us->nat_flags & NF_UTF8) && (*sp & 0x80))
88                 *sp = '?';
89         }
90         sendmessage(us, to, buf, 1);
91     } else {
92         sendmessage(us, to, "...", 1);
93         while (ugetstring("> ", buf)) {
94             if (*buf == '.')
95                 break;
96             sendmessage(us, to, buf, 0);
97         }
98         sendmessage(us, to, "<EOT>", 0);
99     }
100     return RET_OK;
101 }
102
103 int
104 wall(void)
105 {
106     struct natstr *us;
107     char buf[1024]; /* buf is message text */
108     char *sp; /* sp is message text */
109
110     us = getnatp(player->cnum);
111     if (player->argp[1]) {
112         for (sp = &player->combuf[0]; *sp && *sp != ' '; ++sp) ;
113         sprintf(buf, ":%s", sp);
114         for(sp = buf; 0 != *sp; ++sp) {
115             if ((*sp >= 0x0 && *sp < 0x20  && *sp != '\t') ||
116                 *sp == 0x7f)
117                 *sp = '?';
118             else if (!(us->nat_flags & NF_UTF8) && (*sp & 0x80))
119                 *sp = '?';
120         }
121         sendmessage(us, 0, buf, 1);
122     } else {
123         sendmessage(us, 0, "...", 1);
124         while (ugetstring("> ", buf)) {
125             if (*buf == '.')
126                 break;
127             sendmessage(us, 0, buf, 0);
128         }
129         sendmessage(us, 0, "<EOT>", 0);
130     }
131     return RET_OK;
132 }
133
134 int
135 sendmessage(struct natstr *us, struct natstr *to, char *message
136             /* message is message text */, int oneshot)
137 {
138     struct player *other;
139     struct tm *tm;
140     time_t now;
141     int sent = 0;
142     struct natstr *wto;
143     char c; /* c is message text */
144     int pos;
145
146     pos = ufindbreak(message, 60);
147     c = message[pos];
148     if (c)
149         message[pos] = '\0';
150     
151     time(&now);
152     tm = localtime(&now);
153     for (other = player_next(0); other != 0; other = player_next(other)) {
154         if (to && other->cnum != to->nat_cnum)
155             continue;
156         if (!(wto = getnatp(other->cnum)))
157             continue;
158         if (!to && !player->god && getrel(wto, player->cnum) != ALLIED)
159             continue;
160         if (!player->god && !(wto->nat_flags & NF_FLASH))
161             continue;
162         if (player == other)
163             continue;
164         if (oneshot)
165             if (to)
166                 pr_flash(other, "FLASH from %s (#%d) @ %02d:%02d%s\n",
167                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
168                          tm->tm_min, message);
169             else
170                 pr_flash(other, "BROADCAST from %s (#%d) @ %02d:%02d%s\n",
171                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
172                          tm->tm_min, message);
173
174         else
175             pr_flash(other, "%s (#%d): %s\n",
176                      us->nat_cnam, us->nat_cnum, message);
177         player_wakeup(other);
178         sent++;
179     }
180     if (player->god) {
181         if (to)
182             if (sent)
183                 pr("Flash sent to %s\n", to->nat_cnam);
184             else
185                 pr("%s is not logged on\n", to->nat_cnam);
186         else if (sent)
187             pr("Broadcast sent to %d players\n", sent);
188         else
189             pr("No-one is logged in\n");
190     }
191     if (to && !player->god) {
192         /* If they are allied with us, we would normally see that
193          * they are logged in anyway, so just tell us */
194         if ((getrel(to, player->cnum) == ALLIED) && !sent) {
195             if (to->nat_flags & NF_FLASH)
196                 pr("%s is not logged on\n", to->nat_cnam);
197             else
198                 pr("%s is not accepting flashes\n", to->nat_cnam);
199         }
200     }
201     if (c) {
202         message[pos] = c;
203         sendmessage(us, to, &message[pos], 0);
204     }
205     return 0;
206 }
207
208 /*
209  * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
210  * If S doesn't have that many characters, return its length instead.
211  */
212 int
213 ufindbreak(char *s /* s is message text */, int n)
214 {
215     int i = 0;
216
217     while (n && s[i])
218     {
219         if ((s[i++] & 0xc0) == 0xc0)
220             while ((s[i] & 0xc0) == 0x80)
221                 i++;
222         --n;
223     }
224     return i;
225 }