how to check for inavlid characters in changen name?
#1

i have problems with /changename command, the user can enter a name with inavlid characters and bug the database
i can block some invalid characters with strfind, but there are a lot of invalid characters.....

is there anyway to check if the inputttext text contains something different than the characters in "AllowedCharacters" and send the client an error?

Код:
new AllowedCharacters[71][] =
{
        {"0"},{"1"},{"2"},{"3"},{"4"},{"5"},{"6"},{"7"},{"8"},{"9"},
        {"a"},{"b"},{"c"},{"d"},{"e"},{"f"},{"g"},{"h"},{"i"},{"j"},{"k"},{"l"},{"m"},{"n"},{"o"},{"p"},{"q"},{"r"}, {"s"},{"t"},{"u"},{"v"},{"w"},{"x"},{"y"},{"z"},
        {"A"},{"B"},{"C"},{"D"},{"E"},{"F"},{"G"},{"H"},{"I"},{"J"},{"K"},{"L"},{"M"},{"N"},{"O"},{"P"},{"Q"},{"R"},{"S"},{"T"},{"U"},{"V"},{"W"},{"X"},{"Y"},{"Z"},
        {"["},{"]"},{"("},{")"},{"_"},{"="},{"$"},{"@"},{"."}
};
Reply
#2

First why do you have a bidimensional array for the AllowedCharacters?

Second, this should work:
pawn Код:
new AllowedCharacters[] =
{
        "0","1","2","3","4","5","6","7","8","9",
        "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r", "s","t","u","v","w","x","y","z",
        "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
        "[","]","(",")","_","=","$","@","."
};

CheckAllowedChar(string[])
{
    new l=strlen(string), ll=sizeof(AllowedCharacters); //get the length of the string and the size of the allowed characters
    new n; //auxiliar var
    for(new i=0; i<l; i++) //go get every char in string
    {
        for(n=0; n<ll; n++) //go get every char in allowedcharacters
            if(string[i] == AllowedCharacters[n]) //if the character in string is found in allowedcharacters then leave the loop
                break;
        if(n==ll) //if n == ll then the char in the string wasn't found in allowedchar, so return false
            return false;
    }
    return true; //every char is allowed, return true.
}
To check if the inputtext is allowed you just need to:
if(CheckAllowedChar(inputtext))
{
//NAME ALLOWED
}
Reply
#3

If the new name contains invalid characters it simply won't be set. You can use the return value of SetPlayerName directly, without all this pointless extra stuff, i.e.:

PHP код:
if(SetPlayerName(playeridnewname) != 1)
    return 
SendClientMessage(playeridRED"INVALID"):
// update with "newname" 
Reply
#4

Код:
CheckAllowedChar( const string[ ] )
{
    for( new i = 0; string[ i ] != EOS; ++i )
	{
        switch( string[ i ] )
		{
            /*here your all chars*/
            case '0'..'9', 'A'..'Z', 'a'..'z','_','@',',': continue;
            default: return false;
        }
    }
    return true;
}
Reply
#5

Thanks guys, i tried Kimossab's answer and it works fine (its the only one i understood because im new to scripting)
also i tried to solve this problem my self:

will it cause any bugs?

Код:
	new Oldname[20], Newname[20], CurrentName[20];
	if(sscanf(params, "s[20]", Newname)) return SendClientMessage(playerid, -1, "USAGE: /changename [Name]");
	
	GetPlayerName(playerid, Oldname, 20);
	SetPlayerName(playerid, Newname);	
	GetPlayerName(playerid, CurrentName, 20);
	
	if(strlen(CurrentName) != strlen(Newname)) // because SAMP deletes invalid characters on SetPlayerName
	{
		SetPlayerName(playerid, Oldname);
		SendClientMessage(playerid, -1, "{FFFFFF}Error: {0099FF}Your name contains invalid characters.");
	}
	else {
		new String[128];
		format(String, sizeof(String), "{0099FF}Your name has been successfully changed to %s.", Newname);
		SendClientMessage(playerid, -1, String);
	}
Reply
#6

Why don't you just take Vince's advice?? You're just wasting time...

pawn Код:
CMD:changename(playerid, params[])
{
    if(!strlen(params)) return SendClientMessage(playerid, -1, "USAGE: /changename [Name]"); // 'isnull' would perform much better here.
    switch(SetPlayerName(playerid, params))
    {
        case 0: SendClientMessage(playerid, -1, "Error: {0099FF}You are already using that name.");
        case 1:
        {
            new String[80];
            format(String, sizeof(String), "{0099FF}Your name has been successfully changed to %s.", params);
            SendClientMessage(playerid, -1, String);
        }
        default: SendClientMessage(playerid, -1, "Error: {0099FF}That name is currently in use or contains invalid characters.");
    }
    return 1;
}
Yes, I recommend using 'isnull' rather than '!strlen' to check if the parameters are null.

SSCANF in this situation is completely useless.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)