]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
Factor out code to read mailboxes, and make read more robust
[empserver] / src / lib / player / player.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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  *     Markus Armbruster, 2004-2008
33  *     Ron Koenderink, 2004-2007
34  */
35
36 #include <config.h>
37
38 #include <errno.h>
39 #include <stdio.h>
40 #include "com.h"
41 #include "empio.h"
42 #include "empthread.h"
43 #include "file.h"
44 #include "journal.h"
45 #include "misc.h"
46 #include "nat.h"
47 #include "optlist.h"
48 #include "player.h"
49 #include "proto.h"
50 #include "prototypes.h"
51 #include "tel.h"
52
53
54 static int command(void);
55 static int status(void);
56 static int print_sink(char *, size_t, void *);
57
58 struct player *player;
59
60 void
61 player_main(struct player *p)
62 {
63     struct natstr *natp;
64     char buf[128];
65
66     p->state = PS_PLAYING;
67     player = p;
68     time(&player->curup);
69     update_timeused_login(player->curup);
70     show_motd();
71     if (init_nats() < 0) {
72         pr("Server confused, try again later\n");
73         return;
74     }
75     natp = getnatp(player->cnum);
76     if (!may_play_now(natp, player->curup, 0))
77         return;
78     if (natp->nat_stat != STAT_VIS
79         && natp->nat_last_login
80         && (strcmp(natp->nat_hostaddr, player->hostaddr)
81             || strcmp(natp->nat_userid, player->userid))) {
82         pr("Last connection from: %s", ctime(&natp->nat_last_login));
83         pr("                  to: %s",
84            natp->nat_last_login <= natp->nat_last_logout
85            ? ctime(&natp->nat_last_logout) : "?");
86         pr("                  by: %s@%s\n",
87            natp->nat_userid,
88            *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
89     }
90     strcpy(natp->nat_userid, player->userid);
91     strcpy(natp->nat_hostname, player->hostname);
92     strcpy(natp->nat_hostaddr, player->hostaddr);
93     natp->nat_last_login = player->curup;
94     putnat(natp);
95     journal_login();
96     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
97         if (natp->nat_tgms == 1)
98             pr("You have a new telegram waiting ...\n");
99         else
100             pr("You have %s new telegrams waiting ...\n",
101                numstr(buf, natp->nat_tgms));
102         natp->nat_tgms = 0;
103     }
104
105     while (status() && command()) {
106         player->aborted = player->eof;
107         empth_yield();
108     }
109     /* #*# I put the following line in to prevent server crash -KHS */
110     natp = getnatp(player->cnum);
111     time(&natp->nat_last_logout);
112     putnat(natp);
113     update_timeused(natp->nat_last_logout);
114     enforce_minimum_session_time();
115     pr("Bye-bye\n");
116     journal_logout();
117 }
118
119 static int
120 command(void)
121 {
122     char *redir;                /* UTF-8 */
123     char scanspace[1024];
124     time_t now;
125
126     if (getcommand(player->combuf) < 0)
127         return 0;
128
129     now = time(NULL);
130     update_timeused(now);
131     if (!may_play_now(getnatp(player->cnum), now, 1))
132         return 0;
133
134     if (parse(player->combuf, scanspace, player->argp, player->comtail,
135               &player->condarg, &redir) < 0) {
136         pr("See \"info Syntax\"?\n");
137     } else {
138         if (dispatch(player->combuf, redir) < 0)
139             pr("Try \"list of commands\" or \"info\"\n");
140     }
141     return 1;
142 }
143
144 static int
145 status(void)
146 {
147     struct natstr *natp;
148     int old_nstat;
149     char buf[128];
150
151     if (player->eof || player->state == PS_SHUTDOWN)
152         return 0;
153     natp = getnatp(player->cnum);
154     if (player->dolcost > 100.0)
155         pr("That just cost you $%.2f\n", player->dolcost);
156     else if (player->dolcost < -100.0)
157         pr("You just made $%.2f\n", -player->dolcost);
158     if (player->dolcost != 0.0) {
159         /*
160          * Hackish work around for a race condition in the nightly
161          * build's regression tests: sometimes the update starts right
162          * after the force command yields, sometimes a bit later.  If
163          * it is late, we use one random number here, for the bye,
164          * and throwing off the random sequence.
165          */
166         natp->nat_money -= roundavg(player->dolcost);
167         player->dolcost = 0.0;
168     }
169
170     old_nstat = player->nstat;
171     player_set_nstat(player, natp);
172     if ((old_nstat & MONEY) && !(player->nstat & MONEY))
173         pr("You are now broke; industries are on strike.\n");
174     if (!(old_nstat & MONEY) && (player->nstat & MONEY))
175         pr("You are no longer broke!\n");
176
177     time(&player->curup);
178     update_timeused(player->curup);
179     if (!may_play_now(natp, player->curup, 0))
180         return 0;
181     if (player->btused) {
182         natp->nat_btu -= player->btused;
183         player->btused = 0;
184     }
185     if (natp->nat_tgms > 0) {
186         if (!(natp->nat_flags & NF_INFORM)) {
187             if (natp->nat_tgms == 1)
188                 pr("You have a new telegram waiting ...\n");
189             else
190                 pr("You have %s new telegrams waiting ...\n",
191                    numstr(buf, natp->nat_tgms));
192             natp->nat_tgms = 0;
193         }
194     }
195     if (natp->nat_ann > 0) {
196         if (natp->nat_ann == 1)
197             pr("You have a new announcement waiting ...\n");
198         else
199             pr("You have %s new announcements waiting ...\n",
200                numstr(buf, natp->nat_ann));
201         natp->nat_ann = 0;
202     }
203     if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
204         pr("You lost your capital... better designate one (see info capital)\n");
205     putnat(natp);
206     return 1;
207 }
208
209 /*
210  * XXX This whole mess should be redone; execute block should
211  * start with "exec start", and should end with "exec end".
212  * We'll wait until 1.2 I guess.
213  */
214 int
215 execute(void)
216 {
217     char buf[1024];
218     int failed;
219     char *p;
220     char *redir;                /* UTF-8 */
221     char scanspace[1024];
222
223     failed = 0;
224
225     if (player->comtail[1])
226         p = player->comtail[1];
227     else
228         p = getstring("File? ", buf);
229     if (p == NULL || *p == '\0')
230         return RET_SYN;
231     prexec(p);
232
233     while (!failed && status()) {
234         player->nstat &= ~EXEC;
235         if (recvclient(buf, sizeof(buf)) < 0)
236             break;
237         if (parse(buf, scanspace, player->argp, player->comtail,
238                   &player->condarg, &redir) < 0) {
239             failed = 1;
240             continue;
241         }
242         pr("\nExecute : %s\n", buf);
243         if (redir) {
244             pr("Execute : redirection not supported\n");
245             failed = 1;
246         } else if (dispatch(buf, NULL) < 0)
247             failed = 1;
248     }
249     if (failed) {
250         while (recvclient(buf, sizeof(buf)) >= 0) ;
251     }
252
253     pr("Execute : %s\n", failed ? "aborted" : "terminated");
254     player->eof = 0;
255     return RET_OK;
256 }
257
258 int
259 show_motd(void)
260 {
261     show_first_tel(motdfil);
262     return RET_OK;
263 }
264
265 /*
266  * Print first telegram in file FNAME.
267  */
268 int
269 show_first_tel(char *fname)
270 {
271     FILE *fp;
272     struct telstr tgm;
273
274     if ((fp = fopen(fname, "rb")) == NULL) {
275         if (errno == ENOENT)
276             return 0;
277         else {
278             logerror("Could not open %s.\n", fname);
279             return -1;
280         }
281     }
282     if (tel_read_header(fp, fname, &tgm) < 0)
283         return -1;
284     if (tel_read_body(fp, fname, &tgm, print_sink, NULL) < 0)
285         return -1;
286     fclose(fp);
287     return 0;
288 }
289
290 static int
291 print_sink(char *chunk, size_t sz, void *arg)
292 {
293     uprnf(chunk);
294     return 0;
295 }
296
297 int
298 quit(void)
299 {
300     player->state = PS_SHUTDOWN;
301     return RET_OK;
302 }
303
304 char *
305 praddr(struct player *p)
306 {
307     return prbuf("%s@%s", p->userid,
308                  *p->hostname ? p->hostname : p->hostaddr);
309 }