2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * login.c: Allow the player to login
30 * Known contributors to this file:
38 #include "prototypes.h"
43 #include "empthread.h"
57 #include <netinet/in.h>
61 struct cmndstr login_coms[];
66 player_login(void *ud)
74 player->proc = empth_self();
76 pr_id(player, C_INIT, "Empire server ready\n");
78 while (!io_eof(player->iop) && !io_error(player->iop)) {
79 io_output(player->iop, IO_WAIT);
80 if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
81 io_input(player->iop, IO_WAIT);
84 ac = parse(buf, av, 0, space, 0);
85 cmd = comtch(av[0], login_coms, 0, 0);
87 pr_id(player, C_BADCMD, "Command %s not found\n", av[0]);
90 switch (login_coms[cmd].c_addr(player, ac, av)) {
96 pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
101 if (player->state >= PS_SHUTDOWN)
104 player->state = PS_SHUTDOWN;
105 if (!io_eof(player->iop)) {
106 (void)pr_player(player, C_EXIT, "so long...\n");
107 io_noblocking(player->iop, 0);
108 while (io_output(player->iop, IO_WAIT) > 0) ;
110 player_delete(player);
116 client_cmd(struct player *player, int ac, char **av)
120 if (ac < 1 || av[1] == '\0')
123 for (i = 1; i < ac; ++i) {
125 (void)strncat(player->client, " ", sizeof(player->client) - 1);
126 (void)strncat(player->client, av[i], sizeof(player->client) - 1);
128 player->client[sizeof(player->client) - 1] = '\0';
129 pr_id(player, C_CMDOK, "talking to %s\n", player->client);
134 user_cmd(struct player *player, int ac, char **av)
136 if (ac < 1 || av[1] == '\0')
138 (void)strncpy(player->userid, av[1], sizeof(player->userid) - 1);
139 player->userid[sizeof(player->userid) - 1] = '\0';
140 pr_id(player, C_CMDOK, "hello %s\n", player->userid);
145 sanc_cmd(struct player *player, int ac, char **av)
152 pr_id(player, C_BADCMD, "Command %s not found\n", av[0]);
156 snxtitem_all(&ni, EF_NATION);
157 while (nxtitem(&ni, (s_char *)&nat)) {
158 if (nat.nat_stat != (STAT_INUSE | STAT_SANCT))
161 pr_id(player, C_DATA,
162 "The following countries are still in sanctuary:\n");
165 pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
168 pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
170 pr_id(player, C_CMDOK, "\n");
175 coun_cmd(struct player *player, int ac, char **av)
179 if (ac < 1 || av[1] == '\0')
181 if (natbyname(av[1], &cnum) < 0) {
182 pr_id(player, C_CMDERR, "country %s does not exist\n", av[1]);
186 player->validated = 0;
187 pr_id(player, C_CMDOK, "country name %s\n", av[1]);
192 pass_cmd(struct player *player, int ac, char **av)
194 if (ac < 1 || av[1] == '\0')
196 if (player->cnum == 255) {
197 pr_id(player, C_CMDERR, "need country first\n");
200 if (!natpass(player->cnum, av[1])) {
201 pr_id(player, C_CMDERR, "password bad, logging entry\n");
202 logerror("%s tried country #%d with %s",
203 praddr(player), player->cnum, av[1]);
207 pr_id(player, C_CMDOK, "password ok\n");
208 logerror("%s using country #%d", praddr(player), player->cnum);
214 play_cmd(struct player *player, int ac, char **av)
217 struct player *other;
222 (void)strncpy(player->userid, av[1], sizeof(player->userid) - 1);
223 player->userid[sizeof(player->userid) - 1] = '\0';
224 player->validated = 0;
225 if (natbyname(av[2], &cnum) < 0) {
226 pr_id(player, C_CMDERR, "country %s does not exist\n", av[2]);
229 if (!natpass(cnum, av[3])) {
230 pr_id(player, C_CMDERR, "password bad, logging entry\n");
231 logerror("%s tried country #%d with %s",
232 praddr(player), cnum, av[3]);
238 if (player->cnum == 255 || !player->validated) {
239 pr_id(player, C_CMDERR, "need country and password\n");
242 if ((other = player_find_other(player, (natid)player->cnum)) != 0) {
243 natp = getnatp(player->cnum);
244 if (natp->nat_stat != VIS) {
245 pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
247 pr_id(player, C_EXIT, "country in use\n");
251 if (match_user(banfil, player)) {
252 logerror("Attempted login by BANNED host %s", praddr(player));
253 pr("Your login has been banned from this game\n");
254 io_shutdown(player->iop, IO_READ);
258 player->state = PS_SHUTDOWN;
264 kill_cmd(struct player *player, int ac, char **av)
266 struct player *other;
268 if (player->cnum == 255 || !player->validated) {
269 pr_id(player, C_CMDERR, "need country and password\n");
272 /* XXX find cnum other than us */
273 if ((other = player_find_other(player, player->cnum)) == 0) {
274 pr_id(player, C_EXIT, "country not in use\n");
277 io_shutdown(other->iop, IO_READ | IO_WRITE);
278 pr_id(player, C_EXIT, "closed socket of offending job\n");
284 list_cmd(struct player *player, int ac, char **av)
289 if (player->cnum != 0 || !player->validated) {
290 pr_id(player, C_CMDERR, "Permission denied\n");
293 for (lp = player_next(0); lp != 0; lp = player_next(lp)) {
295 pr_id(player, C_DATA, "user@host\tcountry\tpid\n");
298 pr_id(player, C_DATA, "%s\t%d\n", praddr(lp), lp->cnum);
301 pr_id(player, C_DATA, "\n");
307 quit_cmd(struct player *player, int ac, char **av)
309 pr_id(player, C_EXIT, "so long\n");
310 io_shutdown(player->iop, IO_READ);
314 struct cmndstr login_coms[] = {
315 {"list", 0, list_cmd, 0, 0},
316 {"client clientname [version info]", 0, client_cmd, 0, 0},
317 {"user username", 0, user_cmd, 0, 0},
318 {"sanc", 0, sanc_cmd, 0, 0},
319 {"coun countryname", 0, coun_cmd, 0, 0},
320 {"pass password", 0, pass_cmd, 0, 0},
321 {"play [user country pass]", 0, play_cmd, 0, 0},
322 {"quit ", 0, quit_cmd, 0, 0},
323 {"kill {self}", 0, kill_cmd, 0, 0},