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)
111 player_delete(player);
117 client_cmd(struct player *player, int ac, char **av)
121 if (ac < 1 || av[1] == '\0')
124 for (i = 1; i < ac; ++i) {
126 (void) strncat(player->client, " ", sizeof(player->client)-1);
127 (void) strncat(player->client, av[i], sizeof(player->client)-1);
129 player->client[sizeof(player->client)-1] = '\0';
130 pr_id(player, C_CMDOK, "talking to %s\n", player->client);
135 user_cmd(struct player *player, int ac, char **av)
137 if (ac < 1 || av[1] == '\0')
139 (void) strncpy(player->userid, av[1], sizeof(player->userid)-1);
140 player->userid[sizeof(player->userid)-1] = '\0';
141 pr_id(player, C_CMDOK, "hello %s\n", player->userid);
146 sanc_cmd(struct player *player, int ac, char **av)
153 pr_id(player, C_BADCMD, "Command %s not found\n", av[0]);
157 snxtitem_all(&ni, EF_NATION);
158 while (nxtitem(&ni, (s_char *)&nat)) {
159 if (nat.nat_stat != (STAT_INUSE|STAT_SANCT))
162 pr_id(player, C_DATA, "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",
248 pr_id(player, C_EXIT, "country in use\n");
252 if (match_user(banfil, player)) {
253 logerror("Attempted login by BANNED host %s",
255 pr("Your login has been banned from this game\n");
256 io_shutdown(player->iop, IO_READ);
260 player->state = PS_SHUTDOWN;
266 kill_cmd(struct player *player, int ac, char **av)
268 struct player *other;
270 if (player->cnum == 255 || !player->validated) {
271 pr_id(player, C_CMDERR, "need country and password\n");
274 /* XXX find cnum other than us */
275 if ((other = player_find_other(player, player->cnum)) == 0) {
276 pr_id(player, C_EXIT, "country not in use\n");
279 io_shutdown(other->iop, IO_READ|IO_WRITE);
280 pr_id(player, C_EXIT, "closed socket of offending job\n");
286 list_cmd(struct player *player, int ac, char **av)
291 if (player->cnum != 0 || !player->validated) {
292 pr_id(player, C_CMDERR, "Permission denied\n");
295 for (lp = player_next(0); lp != 0; lp = player_next(lp)) {
297 pr_id(player, C_DATA, "user@host\tcountry\tpid\n");
300 pr_id(player, C_DATA, "%s\t%d\n", praddr(lp), lp->cnum);
303 pr_id(player, C_DATA, "\n");
309 quit_cmd(struct player *player, int ac, char **av)
311 pr_id(player, C_EXIT, "so long\n");
312 io_shutdown(player->iop, IO_READ);
316 struct cmndstr login_coms[] = {
317 { "list", 0, list_cmd, 0, 0 },
318 { "client clientname [version info]", 0, client_cmd, 0, 0 },
319 { "user username", 0, user_cmd, 0, 0 },
320 { "sanc", 0, sanc_cmd, 0, 0 },
321 { "coun countryname", 0, coun_cmd, 0, 0 },
322 { "pass password", 0, pass_cmd, 0, 0 },
323 { "play [user country pass]", 0, play_cmd, 0, 0 },
324 { "quit ", 0, quit_cmd, 0, 0 },
325 { "kill {self}", 0, kill_cmd, 0, 0 },