]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
Support UTF-8 encoded Unicode for user communications.
[empserver] / src / lib / player / player.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  *  player.c: Main command loop for a player
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  *     
33  */
34
35 #include "prototypes.h"
36 #include <string.h>
37 #include "gamesdef.h"
38 #include "misc.h"
39 #include "player.h"
40 #include "proto.h"
41 #include "com.h"
42 #include "nat.h"
43 #include "sect.h"
44 #include "file.h"
45 #include "proto.h"
46 #include "empio.h"
47 #include "empthread.h"
48 #include "tel.h"
49 #include "gen.h"
50 #include "subs.h"
51 #include "common.h"
52 #include "optlist.h"
53
54 #if !defined(_WIN32)
55 #include <unistd.h>
56 #include <sys/time.h>
57 #endif
58 #include <stdio.h>
59 #include <errno.h>
60 #include <fcntl.h>
61
62 struct player *player;
63
64 void
65 player_main(struct player *p)
66 {
67     struct natstr *natp;
68     int secs;
69     char buf[128];
70
71     p->state = PS_PLAYING;
72     player = p;
73     time(&player->lasttime);
74     time(&player->curup);
75     showvers(CLIENTPROTO);
76     show_motd();
77     if (init_nats() < 0)
78         return;
79     natp = getnatp(player->cnum);
80     if (player->god && !match_user(authfil, player)) {
81         logerror("NON-AUTHed Login attempted by %s", praddr(player));
82         pr("You're not a deity!\n");
83         return;
84     }
85     if (!gamehours(player->curup)) {
86         pr("Empire hours restriction in force\n");
87         if ((natp->nat_stat & STAT_GOD) == 0)
88             return;
89     }
90     daychange(player->curup);
91     if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
92         pr("Time exceeded today\n");
93         return;
94     }
95     if ((*natp->nat_hostaddr &&
96          *player->hostaddr &&
97          strcmp(natp->nat_hostaddr, player->hostaddr)) ||
98         (*natp->nat_userid &&
99          *player->userid && strcmp(natp->nat_userid, player->userid))) {
100         if (natp->nat_stat != VIS) {
101             pr("Last connection from: %s", ctime(&natp->nat_last_login));
102             pr("                  to: %s", natp->nat_last_login <
103                natp->nat_last_logout ? ctime(&natp->
104                                              nat_last_logout) : "?");
105             pr("                  by: %s@%s\n",
106                *natp->nat_userid ? natp->nat_userid : "nobody",
107                *natp->nat_hostname ? natp->nat_hostname : *natp->
108                nat_hostaddr ? natp->nat_hostaddr : "nowhere");
109         }
110     }
111     if (*player->userid)
112         strcpy(natp->nat_userid, player->userid);
113     else
114         strcpy(natp->nat_userid, "nobody");
115
116     if (*player->hostname)
117         strcpy(natp->nat_hostname, player->hostname);
118     else
119         strcpy(natp->nat_hostname, "nowhere");
120
121     if (*player->hostaddr)
122         strcpy(natp->nat_hostaddr, player->hostaddr);
123
124     time(&natp->nat_last_login);
125     natp->nat_connected = 1;
126     putnat(natp);
127     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
128         if (natp->nat_tgms == 1)
129             pr("You have a new telegram waiting ...\n");
130         else
131             pr("You have %s new telegrams waiting ...\n",
132                numstr(buf, natp->nat_tgms));
133         natp->nat_tgms = 0;
134     }
135
136     while (status()) {
137         if (command() == 0 && !player->aborted)
138             break;
139         player->aborted = 0;
140     }
141     /* #*# I put the following line in to prevent server crash -KHS */
142     natp = getnatp(player->cnum);
143     /*
144      * randomly round up to the nearest minute,
145      * charging at least 15 seconds.
146      */
147     time(&natp->nat_last_logout);
148     secs = max(natp->nat_last_logout - player->lasttime, 15);
149     natp->nat_minused += secs / 60;
150     secs = secs % 60;
151     if (chance(secs / 60.0))
152         natp->nat_minused += 1;
153     natp->nat_connected = 0;
154     putnat(natp);
155     pr("Bye-bye\n");
156 }
157
158 int
159 command(void)
160 {
161     unsigned int x;
162     char *redir;
163     char scanspace[1024];
164
165     if (getcommand(player->combuf) < 0)
166         return 0;
167     if (parse(player->combuf, player->argp, &player->condarg,
168               scanspace, &redir) < 0) {
169         pr("See \"info Syntax\"?\n");
170     } else {
171         /* XXX don't use alarm; use a scavenger thread */
172         /* DONT USE IT!!!! alarm and sleep may and dont work
173            together -- Sasha */
174         /* alarm((unsigned int)60*60); 1 hour */
175         if (player->condarg != NULL)
176             for (x = 0; x < strlen(player->condarg); x++)
177                 if (isupper(*(player->condarg + x)))
178                     *(player->condarg + x) =
179                         tolower(*(player->condarg + x));
180         if (dispatch(player->combuf, redir) < 0)
181             pr("Try \"list of commands\" or \"info\"\n");
182     }
183     return 1;
184 }
185
186 int
187 status(void)
188 {
189     struct natstr *natp;
190     int minute;
191     struct sctstr sect;
192     char buf[128];
193
194     if (player->state == PS_SHUTDOWN)
195         return 0;
196     natp = getnatp(player->cnum);
197     if (io_error(player->iop) || io_eof(player->iop)) {
198         putnat(natp);
199         return 0;
200     }
201     player->visitor = (natp->nat_stat & (STAT_NORM | STAT_GOD)) == 0;
202     if (player->dolcost != 0.0) {
203         if (player->dolcost > 100.0)
204             pr("That just cost you $%.2f\n", player->dolcost);
205         else if (player->dolcost < -100.0)
206             pr("You just made $%.2f\n", -player->dolcost);
207         if (natp->nat_money < player->dolcost && !player->broke) {
208             player->broke = 1;
209             player->nstat &= ~MONEY;
210             pr("You are now broke; industries are on strike.\n");
211         } else if (player->broke && natp->nat_money - player->dolcost > 0) {
212             player->broke = 0;
213             player->nstat |= MONEY;
214             pr("You are no longer broke!\n");
215         }
216         natp->nat_money -= roundavg(player->dolcost);
217         player->dolcost = 0.0;
218     } else {
219         if (natp->nat_money < 0.0 && !player->broke) {
220             player->broke = 1;
221             player->nstat &= ~MONEY;
222             pr("You are now broke; industries are on strike.\n");
223         }
224         if (player->broke && natp->nat_money > 0) {
225             player->broke = 0;
226             player->nstat |= MONEY;
227             pr("You are no longer broke!\n");
228         }
229     }
230     getsect(natp->nat_xcap, natp->nat_ycap, &sect);
231     if ((sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT ||
232          sect.sct_type == SCT_SANCT) && sect.sct_own == player->cnum)
233         player->nstat |= CAP;
234     else
235         player->nstat &= ~CAP;
236     /* Ok, has the country owner reset his capital yet after it was sacked? */
237     if (natp->nat_flags & NF_SACKED)
238         player->nstat &= ~CAP;  /* No capital yet */
239     player->ncomstat = player->nstat;
240     time(&player->curup);
241     minute = (player->curup - player->lasttime) / 60;
242     if (minute > 0) {
243         player->minleft -= minute;
244         if (player->minleft <= 0) {
245             /*
246              * countdown timer "player->minleft" has expired.
247              * either day change, or hours restriction
248              */
249             daychange(player->curup);
250             if (!gamehours(player->curup)) {
251                 pr("Empire hours restriction in force\n");
252                 if ((natp->nat_stat & STAT_GOD) == 0) {
253                     putnat(natp);
254                     return 0;
255                 }
256             }
257             player->minleft = getminleft(player->curup, m_m_p_d);
258         }
259         player->lasttime += minute * 60;
260         natp->nat_minused += minute;
261     }
262     if ((player->nstat & NORM) && natp->nat_minused > m_m_p_d) {
263         pr("Max minutes per day limit exceeded.\n");
264         player->ncomstat = VIS;
265     }
266     if (player->btused) {
267         natp->nat_btu -= player->btused;
268         player->btused = 0;
269     }
270     if (natp->nat_tgms > 0) {
271         if (!(natp->nat_flags & NF_INFORM)) {
272             if (natp->nat_tgms == 1)
273                 pr("You have a new telegram waiting ...\n");
274             else
275                 pr("You have %s new telegrams waiting ...\n",
276                    numstr(buf, natp->nat_tgms));
277             natp->nat_tgms = 0;
278         }
279     }
280     if (natp->nat_ann > 0) {
281         if (natp->nat_ann == 1)
282             pr("You have a new announcement waiting ...\n");
283         else
284             pr("You have %s new announcements waiting ...\n",
285                numstr(buf, natp->nat_ann));
286         natp->nat_ann = 0;
287     }
288     if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
289         pr("You lost your capital... better designate one\n");
290     putnat(natp);
291     if (gamedown() && !player->god) {
292         pr("gamedown\n");
293         return 0;
294     }
295     return 1;
296 }
297
298 /*
299  * actually a command; redirection and piping ignored.
300  * XXX This whole mess should be redone; execute block should
301  * start with "exec start", and should end with "exec end".
302  * We'll wait until 1.2 I guess.
303  */
304 int
305 execute(void)
306 {
307     char buf[512];
308     int failed;
309     char *p;
310     char *redir;
311     char scanspace[1024];
312
313     failed = 0;
314     redir = 0;
315
316     p = getstarg(player->argp[1], "File? ", buf);
317
318     if (p == NULL || *p == '\0')
319         return RET_SYN;
320
321     prexec(player->argp[1]);
322     while (!failed && status()) {
323         if (recvclient(buf, sizeof(buf)) < 0)
324             break;
325         if (parse(buf, player->argp, &player->condarg,
326                   scanspace, &redir) < 0) {
327             failed = 1;
328             continue;
329         }
330         if (redir == 0)
331             pr("\nExecute : %s\n", buf);
332         if (dispatch(buf, redir) < 0)
333             failed = 1;
334     }
335     if (failed) {
336         while (recvclient(buf, sizeof(buf)) >= 0) ;
337     }
338     if (redir == 0)
339         pr("Execute : %s\n", failed ? "aborted" : "terminated");
340     return RET_OK;
341 }
342
343 int
344 show_motd(void)
345 {
346     FILE *motd_fp;
347     struct telstr tgm;
348     char buf[MAXTELSIZE];
349
350     if ((motd_fp = fopen(motdfil, "rb")) == NULL)
351     {
352         if (errno == ENOENT)
353             return RET_OK;
354         else
355         {
356             pr ("Could not open motd.\n");
357             logerror("Could not open motd (%s).\n", motdfil);
358             return RET_SYS;
359         }
360     }
361     if (fread((void *)&tgm, sizeof(tgm), 1, motd_fp) != 1) {
362         logerror("bad header on login message (motdfil)");
363         fclose(motd_fp);
364         return RET_FAIL;
365     }
366     if (tgm.tel_length >= (long)sizeof(buf)) {
367         logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
368         fclose(motd_fp);
369         return RET_FAIL;
370     }
371     if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
372         logerror("bad length %ld on login message", tgm.tel_length);
373         fclose(motd_fp);
374         return RET_FAIL;
375     }
376     buf[tgm.tel_length] = 0;
377     uprnf(buf);
378     fclose(motd_fp);
379     return RET_OK;
380 }
381
382 int
383 match_user(char *file, struct player *p)
384 {
385     FILE *fp;
386     int match = 0;
387     char host[256];
388     char user[256];
389
390     if ((fp = fopen(file, "r")) == NULL) {
391         /*logerror("Cannot find file %s", file); */
392         return 0;
393     }
394     match = 0;
395     while (!feof(fp) && !match) {
396         if (fgets(host, sizeof(host) - 1, fp) == NULL)
397             break;
398         if (host[0] == '#')
399             continue;
400         if (fgets(user, sizeof(user) - 1, fp) == NULL)
401             break;
402         host[strlen(host) - 1] = '\0';
403         user[strlen(user) - 1] = '\0';
404         if (strstr(p->userid, user) &&
405             (strstr(p->hostaddr, host) ||
406              strstr(p->hostname, host)))
407             ++match;
408     }
409     fclose(fp);
410     return match;
411 }
412
413 int
414 quit(void)
415 {
416     player->state = PS_SHUTDOWN;
417     return RET_OK;
418 }
419
420 char *
421 praddr(struct player *p)
422 {
423     return prbuf("%s@%s", p->userid,
424                  *p->hostname ? p->hostname : p->hostaddr);
425 }