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