Ejemplo n.º 1
0
/*  Transforms the address into a command.
 *  Returns:	-1 ifaddress not matched by reules
 *		 0 ifaddress matched and ok to forward
 *		 1 ifaddress matched and not ok to forward
 */
extern int
rewrite(dest *dp, message *mp)
{
	rule *rp;		/* rewriting rule */
	String *lower;		/* lower case version of destination */

	/*
	 *  Rewrite the address.  Matching is case insensitive.
	 */
	lower = s_clone(dp->addr);
	s_tolower(s_restart(lower));
	rp = findrule(lower, dp->authorized);
	if(rp == 0){
		s_free(lower);
		return -1;
	}
	strcpy(s_to_c(lower), s_to_c(dp->addr));
	dp->repl1 = substitute(rp->repl1, rp->subexp, mp);
	dp->repl2 = substitute(rp->repl2, rp->subexp, mp);
	dp->status = rp->type;
	if(debug){
		fprint(2, "\t->");
		if(dp->repl1)
			fprint(2, "%s", s_to_c(dp->repl1));
		if(dp->repl2)
			fprint(2, "%s", s_to_c(dp->repl2));
		fprint(2, "\n");
	}
	s_free(lower);
	return 0;
}
Ejemplo n.º 2
0
static int
getrule(String *line, String *type, char *system)
{
	rule	*rp;
	String	*re;
	int	backl;

	backl = 0;

	/* get a rule */
	re = rule_parse(s_restart(line), system, &backl);
	if(re == 0)
		return 0;
	rp = (rule *)malloc(sizeof(rule));
	if(rp == 0) {
		perror("getrules:");
		exit(1);
	}
	rp->next = 0;
	s_tolower(re);
	rp->matchre = s_new();
	s_append(rp->matchre, s_to_c(re));
	s_restart(rp->matchre);
	s_free(re);
	s_parse(line, s_restart(type));
	rp->repl1 = rule_parse(line, system, &backl);
	rp->repl2 = rule_parse(line, system, &backl);
	rp->program = 0;
	if(strcmp(s_to_c(type), "|") == 0)
		rp->type = d_pipe;
	else if(strcmp(s_to_c(type), ">>") == 0)
		rp->type = d_cat;
	else if(strcmp(s_to_c(type), "alias") == 0)
		rp->type = d_alias;
	else if(strcmp(s_to_c(type), "translate") == 0)
		rp->type = d_translate;
	else if(strcmp(s_to_c(type), "auth") == 0)
		rp->type = d_auth;
	else {
		s_free(rp->matchre);
		s_free(rp->repl1);
		s_free(rp->repl2);
		free((char *)rp);
		fprint(2,"illegal rewrite rule: %s\n", s_to_c(line));
		return 0;
	}
	if(rulep == 0)
		rulep = rlastp = rp;
	else
		rlastp = rlastp->next = rp;
	return backl;
}
Ejemplo n.º 3
0
Archivo: hunt.c Proyecto: simos/Hunt
/********************************************************************
 * buy: Checks commands' params and does the corresponding actions.
 ********************************************************************/
void buy(CommandType command, Player *player, WeaponType weaptable[])
{
    char input[MAX_INPUT+1], *tokens[2] = {NULL};
    int i, num, cost;
    bool found;

    if (command.params[0] == NULL) {
        puts(_("What do you want to buy?"));
        return;
    }

    if (!strcmp(command.params[0], "bullets") ||
        !strcmp(command.params[0], "drugs")   ||
        !strcmp(command.params[0], "weapon")) {

        if (command.params[1] == NULL) {
            puts(_("Please be more specific."));
            return;
        }

        if (!strcmp(command.params[0], "weapon")) {
            for (i = 0; i < MAX_WEAPONS; i++)
                if (! strcmp(weaptable[i].name, command.params[1])) {
                    found = true;
                    break;
                }
            if (!found) {
                puts(_("There is no weapon with such name."));
                return;
            }
            cost = weaptable[i].value;
        }
        else {
            num = strtol(command.params[1], NULL, 10);
            if (num <= 0) {
                printf(_("%s is not a valid positive integer.\n"), command.params[1]);
                return;
            }
            cost = !strcmp(command.params[0], "bullets") ? BULLET_COST:DRUG_COST;
            cost *= num;
        }

        printf(_("You need %d gold in order to buy this stuff.\n"), cost);
        if (player->gold < cost) {
            puts(_("Sorry, you don't have enough gold."));
            return;
        }

        printf(_("Are you sure that you want to proceed? (y/n) "));
        fgets(input, sizeof(input), stdin);
        s_tokenize(input, tokens, 2, " \n");

        if (tokens[0] == NULL || tokens[1] != NULL ||
                (strcmp(s_tolower(tokens[0]), "y") &&
                strcmp(s_tolower(tokens[0]), "yes"))) {
            puts(_("canceled"));
            return;
            }

        if (!strcmp(command.params[0], "bullets")) {
            player->gold -= cost;
            player->bullets += num;
        }
        else if (!strcmp(command.params[0], "drugs")){
            player->gold -= cost;
            player->health += num * DRUG_HEALTH;
            if (player->health > 100)
                player->health = 100;
        }
        else {
            player->gold -= cost;
            memcpy(&player->weapon, &weaptable[i], sizeof(WeaponType));
        }
        puts(_("Done!"));
        return;
    }
    printf(_("You can't buy %s.\n"), command.params[0]);
}