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