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