Exemple #1
0
static int
play_cmd(void)
{
    struct player *other;
    natid cnum;
    struct natstr *natp;
    char **ap;
    char buf[128];

    ap = player->argp;
    if (*++ap) {
        strncpy(player->userid, *ap, sizeof(player->userid) - 1);
        player->userid[sizeof(player->userid) - 1] = '\0';
        player->authenticated = 0;
    }
    if (*++ap) {
        if (natbyname(*ap, &cnum) < 0) {
            pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
            return RET_FAIL;
        }
    }
    if (*++ap) {
        if (!natpass(cnum, *ap)) {
            pr_id(player, C_CMDERR, "password bad, logging entry\n");
            logerror("%s tried country #%d with %s",
                     praddr(player), cnum, *ap);
            return RET_FAIL;
        }
        player->cnum = cnum;
        player->authenticated = 1;
    }
    if (!may_play())
        return RET_FAIL;
    other = getplayer(player->cnum);
    if (other) {
        natp = getnatp(player->cnum);
        if (natp->nat_stat != STAT_VIS) {
            pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
        } else {
            pr_id(player, C_EXIT, "country in use\n");
        }
        return RET_FAIL;
    }
    snprintf(buf, sizeof(buf), "Play#%d", player->cnum);
    logerror("%s logged in as country #%d", praddr(player), player->cnum);
    journal_login();
    empth_set_name(empth_self(), buf);
    pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
    empth_rwlock_rdlock(shutdown_lock);
    player->state = PS_PLAYING;
    player_main(player);
    journal_logout();
    logerror("%s logged out, country #%d", praddr(player), player->cnum);
    if (!io_eof(player->iop) && !io_error(player->iop))
        io_set_eof(player->iop);
    return RET_OK;
}
Exemple #2
0
/*
 * Execute command named by player->argp[0].
 * BUF is the raw UTF-8 command line.  It should have been passed to
 * parse() to set up player->argp.
 * If REDIR is not null, it's the command's redirection, in UTF-8.
 * Return -1 if the command is not unique or doesn't exist, else 0.
 */
int
dispatch(char *buf, char *redir)
{
    struct natstr *np;
    struct cmndstr *command;
    int cmd;

    cmd = comtch(player->argp[0], player_coms, player->nstat);
    if (cmd < 0) {
	if (cmd == M_NOTUNIQUE)
	    pr("Command \"%s\" is ambiguous -- ", player->argp[0]);
	else if (cmd == M_IGNORE)
	    return 0;
	else
	    pr("\"%s\" is not a legal command\n", player->argp[0]);
	return -1;
    }
    command = &player_coms[cmd];
    np = getnatp(player->cnum);
    if (np->nat_btu < command->c_cost && command->c_cost > 0) {
	if (player->god || opt_BLITZ)
	    np->nat_btu = max_btus;
	else {
	    pr("You don't have the BTU's, bozo\n");
	    return 0;
	}
    }
    if (!command->c_addr) {
	pr("Command not implemented\n");
	return 0;
    }
    player->may_sleep = command->c_flags & C_MOD
	? PLAYER_SLEEP_ON_INPUT : PLAYER_SLEEP_FREELY;
    player->command = command;
    empth_rwlock_rdlock(update_lock);
    if (redir) {
	prredir(redir);
	uprnf(buf);
	pr("\n");
    }
    journal_command(command->c_form);
    switch (command->c_addr()) {
    case RET_OK:
	player->btused += command->c_cost;
	break;
    case RET_FAIL:
	pr("command failed\n");
	player->btused += command->c_cost;
	break;
    case RET_SYN:
	pr("Usage: %s\n", command->c_form);
	break;
    default:
	CANT_REACH();
	break;
    }
    empth_rwlock_unlock(update_lock);
    player->command = NULL;
    if (player->may_sleep != PLAYER_SLEEP_NEVER || !io_eof(player->iop))
	player->may_sleep = PLAYER_SLEEP_FREELY;
    /* else we're being kicked out */
    return 0;
}