2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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:
36 #include "prototypes.h"
41 #include "empthread.h"
53 #include <netinet/in.h>
57 extern struct cmndstr login_coms[];
62 player_login(void *ud)
69 player->proc = empth_self();
71 pr_id(player, C_INIT, "Empire server ready\n");
73 while (!io_eof(player->iop) && !io_error(player->iop)) {
74 io_output(player->iop, IO_WAIT);
75 if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
76 io_input(player->iop, IO_WAIT);
79 ac = parse(buf, player->argp, NULL, space, NULL);
80 cmd = comtch(player->argp[0], login_coms, 0, 0);
82 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
85 switch (login_coms[cmd].c_addr()) {
91 pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
96 if (player->state >= PS_SHUTDOWN)
99 player->state = PS_SHUTDOWN;
100 if (!io_eof(player->iop)) {
101 pr_id(player, C_EXIT, "so long...\n");
102 io_noblocking(player->iop, 0);
103 while (io_output(player->iop, IO_WAIT) > 0) ;
105 player_delete(player);
115 if (!player->argp[1])
118 for (i = 1; player->argp[i]; ++i) {
120 strncat(player->client, " ", sizeof(player->client) - 1);
121 strncat(player->client, player->argp[i], sizeof(player->client) - 1);
123 player->client[sizeof(player->client) - 1] = '\0';
124 pr_id(player, C_CMDOK, "talking to %s\n", player->client);
131 if (!player->argp[1])
133 strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
134 player->userid[sizeof(player->userid) - 1] = '\0';
135 pr_id(player, C_CMDOK, "hello %s\n", player->userid);
147 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
151 snxtitem_all(&ni, EF_NATION);
152 while (nxtitem(&ni, &nat)) {
153 if (nat.nat_stat != (STAT_INUSE | STAT_SANCT))
156 pr_id(player, C_DATA,
157 "The following countries are still in sanctuary:\n");
160 pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
163 pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
165 pr_id(player, C_CMDOK, "\n");
174 if (!player->argp[1])
176 if (natbyname(player->argp[1], &cnum) < 0) {
177 pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
181 player->validated = 0;
182 pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
189 if (!player->argp[1])
191 if (player->cnum == 255) {
192 pr_id(player, C_CMDERR, "need country first\n");
195 if (!natpass(player->cnum, player->argp[1])) {
196 pr_id(player, C_CMDERR, "password bad, logging entry\n");
197 logerror("%s tried country #%d with %s",
198 praddr(player), player->cnum, player->argp[1]);
202 pr_id(player, C_CMDOK, "password ok\n");
203 logerror("%s using country #%d", praddr(player), player->cnum);
211 * The option mechanism allows arbitrary string values, but so far
212 * all options are flags in struct player. Should be easy to
213 * generalize if needed.
219 static struct logoptstr login_opts[] = {
227 if (!player->argp[1]) {
228 for (i = 0; i < sizeof(login_opts) / sizeof(*login_opts); ++i)
229 pr_id(player, C_DATA, "%s=%d",
231 (player->flags & login_opts[i].val) != 0);
232 pr_id(player, C_CMDOK, "\n");
235 for (ap = player->argp+1; *ap; ++ap) {
236 p = strchr(*ap, '=');
239 opt = stmtch(*ap, login_opts,
240 offsetof(struct logoptstr, name),
241 sizeof(struct logoptstr));
243 pr_id(player, C_BADCMD, "Option %s not found\n", *ap);
247 player->flags |= login_opts[opt].val;
249 player->flags &= ~login_opts[opt].val;
252 pr_id(player, C_CMDOK, "Accepted\n");
260 struct player *other;
267 strncpy(player->userid, *ap, sizeof(player->userid) - 1);
268 player->userid[sizeof(player->userid) - 1] = '\0';
269 player->validated = 0;
272 if (natbyname(*ap, &cnum) < 0) {
273 pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
278 if (!natpass(cnum, *ap)) {
279 pr_id(player, C_CMDERR, "password bad, logging entry\n");
280 logerror("%s tried country #%d with %s",
281 praddr(player), cnum, *ap);
287 if (player->cnum == 255 || !player->validated) {
288 pr_id(player, C_CMDERR, "need country and password\n");
291 other = getplayer((natid)player->cnum);
293 natp = getnatp(player->cnum);
294 if (natp->nat_stat != VIS) {
295 pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
297 pr_id(player, C_EXIT, "country in use\n");
301 if (match_user(banfil, player)) {
302 logerror("Attempted login by BANNED host %s", praddr(player));
303 pr("Your login has been banned from this game\n");
304 io_shutdown(player->iop, IO_READ);
307 logerror("%s logged in as country #%d", praddr(player), player->cnum);
308 pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
310 logerror("%s logged out, country #%d", praddr(player), player->cnum);
311 player->state = PS_SHUTDOWN;
318 struct player *other;
320 if (player->cnum == 255 || !player->validated) {
321 pr_id(player, C_CMDERR, "need country and password\n");
324 other = getplayer(player->cnum);
326 pr_id(player, C_EXIT, "country not in use\n");
329 logerror("%s killed country #%d", praddr(player), player->cnum);
330 io_shutdown(other->iop, IO_READ | IO_WRITE);
331 pr_id(player, C_EXIT, "closed socket of offending job\n");
338 pr_id(player, C_EXIT, "so long\n");
339 io_shutdown(player->iop, IO_READ);
343 struct cmndstr login_coms[] = {
344 {"client client-id...", 0, client_cmd, 0, 0},
345 {"coun country", 0, coun_cmd, 0, 0},
346 {"kill", 0, kill_cmd, 0, 0},
347 {"options option=value...", 0, options_cmd, 0, 0},
348 {"pass password", 0, pass_cmd, 0, 0},
349 {"play [user [country [password]]]", 0, play_cmd, 0, 0},
350 {"quit", 0, quit_cmd, 0, 0},
351 {"sanc", 0, sanc_cmd, 0, 0},
352 {"user name", 0, user_cmd, 0, 0},