AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Why not works? (((( (https://forums.alliedmods.net/showthread.php?t=319134)

karaulov 10-13-2019 15:08

Why not works? ((((
 
Code:

#include <amxmodx>
#include <amxmisc>
#include <regex>

public plugin_init() {
        register_plugin("No bad usernames", "1", "KARAULOV");
        set_task(1.0, "bad_checker", _, _, _, "b");
}

public bad_checker(xxx)
{
        new i;
        new mUserName[64]
        new players[32], count
        get_players(players, count)
        for(i = 0; i < count; i++)
        {
                new id = players[i]
                get_user_name(id, mUserName, charsmax(mUserName))
                new userid = get_user_userid( id );
                new ret, error[128]
                new Regex:regex_handle = regex_match(mUserName, "\d+\.\d+.\d+.\d+:", ret, error, charsmax(error))
                switch(regex_handle)
                {
                        case REGEX_MATCH_FAIL:
                        {
                                return;
                        }
                        case REGEX_PATTERN_FAIL:
                        {
                                return;
                        }
                        case REGEX_NO_MATCH:
                        {
                                return;
                        }
                        default:
                        {
                                server_cmd( "amx_kick #%d BAD NICKNAME!", userid)
                                server_print( "amx_kick %s - #%d BAD NICKNAME!", mUserName, userid)
                                regex_free(regex_handle);
                        }
                }
        }
}


This not kick users who contain "IP addresss: PORT" in nickname ....

georgik57 10-23-2019 20:36

Re: Why not works? ((((
 
1 Attachment(s)
Try this instead.

iNvectus 10-24-2019 01:36

Re: Why not works? ((((
 
Your regex is kinda incorrect. You have a trailing : at the end and you do nothing with it. You validate it only until 90.40.33.22: and that's it. Another thing is that you call it on plugin initialization. I'd rather do that on client_putinserver, but you know best, you are the scripter after all.

Now you have two possibilities regarding the regular expression:
Add a trailing \d+ after the : or take a look at the regex below.
PHP Code:

^(?=\d+\.\d+\.\d+\.\d+\:[0-9]{1,5}$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}\:[0-9]{1,5}$ 

What's actually happening?
We have a positive lookahead
PHP Code:

(?=\d+\.\d+\.\d+\.\d+\:[0-9]{1,5}$) 

and a non-capturing group with 5 alternatives
PHP Code:

(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]) 

. The rest of it you can understand it yourself with all the digit validations and the range of it. You can sum 2 by 2.

So your regex would look something like the following:
PHP Code:

new Regex:regex_handle regex_match(mUserName"^(?=\d+\.\d+\.\d+\.\d+\:[0-9]{1,5}$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}\:[0-9]{1,5}$"reterrorcharsmax(error)) 

PS:
Another thing, I would also use compiled regular expressions and use regex_match, not relying on the handle itself. Just a point of view, I guess.


All times are GMT -4. The time now is 02:41.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.