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