]> git.pond.sub.org Git - empserver/blob - src/lib/player/empdis.c
Fix execute to filter input just like the main command loop
[empserver] / src / lib / player / empdis.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  empdis.c: Empire dispatcher stuff
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1994
31  *     Steve McClure, 2000
32  *     Markus Armbruster, 2006-2010
33  *     Ron Koenderink, 2004-2009
34  */
35
36 #include <config.h>
37
38 #include <stdio.h>
39 #include <time.h>
40 #include "com.h"
41 #include "empio.h"
42 #include "file.h"
43 #include "game.h"
44 #include "match.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
52
53 #define KEEP_COMMANDS 50
54
55 /* ring buffer of most recent command prompts and commands, user text */
56 static char player_commands[KEEP_COMMANDS][1024 + 8];
57
58 /* the slot holding the most recent command in player_commands[] */
59 static int player_commands_index = 0;
60
61 static void disable_coms(void);
62
63 /*
64  * Get a command from the current player into COMBUFP[1024], in UTF-8.
65  * This may block for input, yielding the processor.  Flush buffered
66  * output when blocking, to make sure player sees the prompt.
67  * Return command's byte length on success, -1 on error.
68  */
69 int
70 getcommand(char *combufp)
71 {
72     char buf[1024];             /* user text */
73
74     if (recvclient(buf, sizeof(buf)) < 0)
75         return -1;
76
77     if (++player_commands_index >= KEEP_COMMANDS)
78         player_commands_index = 0;
79     sprintf(player_commands[player_commands_index], "%3d %3d %s",
80             player_commands_index, player->cnum, buf);
81
82     if (player->flags & PF_UTF8)
83         return copy_utf8_no_funny(combufp, buf);
84     return copy_ascii_no_funny(combufp, buf);
85 }
86
87 void
88 init_player_commands(void)
89 {
90     int i;
91
92     for (i = 0; i < KEEP_COMMANDS; ++i)
93         *player_commands[i] = 0;
94
95     disable_coms();
96 }
97
98 void
99 log_last_commands(void)
100 {
101     int i;
102
103     logerror("Most recent player commands:");
104     for (i = player_commands_index; i >= 0; --i)
105         if (*player_commands[i])
106             logerror("%s", player_commands[i] + 4);
107     for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
108         if (*player_commands[i])
109             logerror("%s", player_commands[i] + 4);
110 }
111
112 int
113 explain(void)
114 {
115     struct cmndstr *com;
116
117     pr("\t\tCurrent EMPIRE Command List\n"
118        "\t\t------- ------ ------- ----\n"
119        "Initial number is cost in B.T.U. units.\n"
120        "Next 2 chars (if present) are:\n"
121        "$ - must be non-broke\tc -- must have capital\n"
122        "Args in [brackets] are optional.\n"
123        "All-caps args in <angle brackets>"
124        " have the following meanings:\n"
125        /* FIXME incomplete */
126        "  <NUM> :: a number in unspecified units\n"
127        "  <COMM> :: a commodity such as `food', `guns', etc\n"
128        "  <TYPE> :: an item type such as `ship', `plane', etc\n");
129     for (com = player_coms; com->c_form; com++) {
130         if ((com->c_permit & player->nstat) == com->c_permit) {
131             pr("%2d ", com->c_cost);
132             if (com->c_permit & MONEY)
133                 pr("$");
134             else
135                 pr(" ");
136             if (com->c_permit & CAP)
137                 pr("c");
138             else
139                 pr(" ");
140             pr(" %s\n", com->c_form);
141         }
142     }
143     pr("For further info on command syntax see \"info Syntax\".\n");
144     return RET_OK;
145 }
146
147 static void
148 disable_coms(void)
149 {
150     char *tmp = strdup(disabled_commands);
151     char *name;
152     int cmd;
153
154     for (name = strtok(tmp, " \t"); name; name = strtok(NULL, " \t")) {
155         cmd = comtch(name, player_coms, -1);
156         if (cmd < 0) {
157             logerror("Warning: not disabling %s command %s\n",
158                      cmd == M_NOTUNIQUE ? "ambiguous" : "unknown", name);
159             continue;
160         }
161         player_coms[cmd].c_permit |= GOD;
162     }
163
164     free(tmp);
165 }
166
167 static int
168 seconds_since_midnight(time_t time)
169 {
170     struct tm *tm = localtime(&time);
171
172     tm->tm_hour = 0;
173     tm->tm_min = 0;
174     tm->tm_sec = 0;
175     tm->tm_isdst = -1;
176     return time - mktime(tm);
177 }
178
179 void
180 update_timeused_login(time_t now)
181 {
182     struct natstr *natp = getnatp(player->cnum);
183     time_t midnight_secs = seconds_since_midnight(now);
184
185     if (now - natp->nat_last_logout > midnight_secs) {
186         natp->nat_timeused = 0;
187         putnat(natp);
188     }
189     player->lasttime = now;
190 }
191
192 void
193 update_timeused(time_t now)
194 {
195     struct natstr *natp = getnatp(player->cnum);
196     time_t midnight_secs = seconds_since_midnight(now);
197     time_t dt = now - player->lasttime;
198
199     if (dt > midnight_secs)
200         natp->nat_timeused = midnight_secs;
201     else
202         natp->nat_timeused += dt;
203     player->lasttime = now;
204     putnat(natp);
205 }
206
207 void
208 enforce_minimum_session_time(void)
209 {
210     struct natstr *natp = getnatp(player->cnum);
211
212     time_t dt = natp->nat_last_logout - natp->nat_last_login;
213     if (dt > seconds_since_midnight(natp->nat_last_logout))
214         dt = seconds_since_midnight(natp->nat_last_logout);
215     if (dt < 15)
216         natp->nat_timeused += 15 - dt;
217     putnat(natp);
218 }
219
220 int
221 may_play_now(struct natstr *natp, time_t now)
222 {
223     if (CANT_HAPPEN(natp->nat_cnum != player->cnum))
224         return 0;
225
226     if (gamehours(now)) {
227         if (player->flags & PF_HOURS) {
228             pr("\nEmpire hours restriction lifted\n");
229             player->flags &= ~PF_HOURS;
230         }
231     } else {
232         if (!(player->flags & PF_HOURS)) {
233             pr("\nEmpire hours restriction in force\n");
234             player->flags |= PF_HOURS;
235         }
236         if (natp->nat_stat != STAT_GOD)
237             return 0;
238     }
239
240     if (game_play_disabled()) {
241         if (!(player->flags & PF_DOWN)) {
242             show_first_tel(downfil);
243             pr("\nThe game is down\n");
244             player->flags |= PF_DOWN;
245         }
246         if (natp->nat_stat != STAT_GOD)
247             return 0;
248     } else
249         player->flags &= ~PF_DOWN;
250
251     if ((natp->nat_stat != STAT_GOD && natp->nat_stat != STAT_VIS)
252         && natp->nat_timeused > m_m_p_d * 60) {
253         pr("Max minutes per day limit exceeded.\n");
254         return 0;
255     }
256     return 1;
257 }