Raised This Month: $51 Target: $400
 12% 

Solved [HELP] Replace a line from .ini


Post New Thread Reply   
 
Thread Tools Display Modes
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 05-05-2018 , 08:11   Re: [HELP] Replace a line from .ini
Reply With Quote #21

Whats the problem in this?

PHP Code:
public Update_Number(idNumber[], newNumber[])
{
    
get_user_name(idg_szNamecharsmax(g_szName));
    
    
read_argv(1newNumber63);
    
    
//CHECKS IF NUMBER IS CORRECT OR NOT
    
for(new 0sizeof(gBlockTexts); i++)
    { 
        if(
containi(newNumbergBlockTexts[i]) != -1
        { 
            
ColorChat(id"%L",LANG_PLAYER"WRONG_NUM")
            return 
PLUGIN_HANDLED;
        } 
    } 
    
    for(new 
i<= strlen(newNumber); i++)
    {
        if((
isalpha(newNumber[i])) || (strlen(newNumber) < 10) || (strlen(newNumber) > 14) || ((newNumber[0] == '+')  && (strlen(newNumber) < 12)) || ((newNumber[0] != '+') && (strlen(newNumber) >10)))
        {
                
ColorChat(id"%L",LANG_PLAYER"WRONG_NUM")
                return 
PLUGIN_HANDLED;
        }
    }

    new 
FilePointer fopen(iRequestsFile"rt"
    if(
FilePointer
    { 
        new 
TempFilePath[256];
        
formatex(TempFilePathcharsmax(TempFilePath), "addons/amxmodx/configs/tempfile.ini"
        
        new 
InputFilePointer fopen(TempFilePath"wt"
        if(
InputFilePointer
        { 
            new 
FileData[128
            while(!
feof(FilePointer)) 
            { 
                
fgets(FilePointerFileDatacharsmax(FileData)) 
                
trim(FileData
            
                if(
containi(FileDatanewNumber)) 
                { 
                    
fprintf(InputFilePointer"^n"newNumber)
                    continue;
                } 

                
fprintf(InputFilePointer"%s^n"FileData
                
            } 
            
fclose(InputFilePointer
            
fclose(FilePointer)
            
            
delete_file(iRequestsFile
            
rename_file(TempFilePathiRequestsFile1
            
            
ColorChat(0"%L"LANG_PLAYER"NUM_ADDED_MSG_ALL1"g_szName);
            
ColorChat(0"%L",LANG_PLAYER"NUM_ADDED_MSG_ALL2");

            return 
PLUGIN_CONTINUE;
        } 
    } 

    return 
PLUGIN_HANDLED;

__________________
CrAzY MaN is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-05-2018 , 10:07   Re: [HELP] Replace a line from .ini
Reply With Quote #22

If you do not mention what you expect it to do and what it actually does no one can helpm
__________________
HamletEagle is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-05-2018 , 15:17   Re: [HELP] Replace a line from .ini
Reply With Quote #23

You should also include all code/files needed to test your code: gBlockTexts array and tempfile.ini
__________________
Bugsy is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 05-05-2018 , 17:35   Re: [HELP] Replace a line from .ini
Reply With Quote #24

fprintf(InputFilePointer, "^n", newNumber)

Missing '%s' in the string.

containi >> contain
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 05-05-2018 at 17:36.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
fvRate
Junior Member
Join Date: May 2018
Old 05-05-2018 , 17:55   Re: [HELP] Replace a line from .ini
Reply With Quote #25

Right, he forgot the format, "also there is no need to change contain to containi since the only difference between them one has a UTF-8 support( containi ) and the other doesn't( I'm just following the documentation ), and he is checking only int values." (Edit: wrong. I skipped the ignore case part, but still he doesn't really need it )

Last edited by fvRate; 05-05-2018 at 18:05.
fvRate is offline
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 05-06-2018 , 03:15   Re: [HELP] Replace a line from .ini
Reply With Quote #26

Okay, so my problem is it replaces all the lines in the file.
Example :

Code:
[REQUEST] 1(STEAM_ID_LAN) : 1111111111
[REQUEST] 2(STEAM_ID_LAN) : 2222222222
[REQUEST] 3(STEAM_ID_LAN) : +913333333333
After replacing by nick "2":
Code:
[REQUEST] 2(STEAM_ID_LAN) : 1231231231
[REQUEST] 2(STEAM_ID_LAN) : 1231231231
[REQUEST] 2(STEAM_ID_LAN) : 1231231231
[REQUEST] 2(STEAM_ID_LAN) : 1231231231
Full Code
PHP Code:
#include <amxmodx>

#define PLUGIN "Whatsapp_Group_Request"
#define VERSION "2.1"
#define AUTHOR "CrAzY MaN"

#define ADMIN_FLAG ADMIN_BAN

new const xPrefix[] = "!g[Whatsapp Group]!n"

new const gBlockTexts[][] =
{
    
"`",
    
"~",
    
"!",
    
"@",
    
"#",
    
"$",
    
"%",
    
"^^",
    
"&",
    
"*",
    
"(",
    
")",
    
"-",
    
"_",
    
"=",
    
"[",
    
"]",
    
"{",
    
"}",
    
"\",
    "
|",
    "
;",
    "
:",
    " ",
    "'",
    "^"",
    ",",
    ".",
    "<",
    ">",
    "/",
    "?"
}

new iRequestsFile[64], FileName[64], g_iSayText, g_szName[32], g_szAuthID[32], Number[64];

public plugin_init() 
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    
    register_cvar(PLUGIN, VERSION, FCVAR_SERVER|FCVAR_SPONLY);
    
    register_clcmd("say /whatsapp", "MainMenu");
    register_clcmd("say whatsapp", "MainMenu");
    
    register_dictionary("whatsapp_group_request.txt")
    
    register_concmd("Type_Your_Whatsapp_Number", "Request_To_Add"); //REQUESTING TO ADD
    register_concmd("Type_Your_New_Number", "Update_Number"); //REQUESTING TO ADD
    register_concmd("amx_viewrequests", "View_Requests"); //VIEW REQUESTS TO ADMIN
    
    g_iSayText = get_user_msgid("SayText")
    
    FileName = "whatsapp_group_request.ini";
    //YOU CAN SEE REQUESTS IN THIS FILE
    formatex(iRequestsFile, charsmax(iRequestsFile), "addons/amxmodx/configs/whatsapp_group_request.ini");
}

public MainMenu(id)
{
    new menu = menu_create("\wWant to \rjoin \wour \yWhatsapp Group\y?", "main_menu_handler");

    menu_additem(menu, "Yes", "", 0); // case 0
    menu_additem(menu, "Update My Number", "", 0); // case 1
    menu_additem(menu, "No", "", 0); // case 2

    menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER); // REMOVES EXIT BUTTON FROM MENU
    
    menu_display(id, menu, 0);

    return PLUGIN_HANDLED;
}

public main_menu_handler(id, menu, item)    
{
    switch(item)
    {
        //YES
        case 0 : {
                client_cmd(id, "messagemode Type_Your_Whatsapp_Number");
                ColorChat(id, "%L",LANG_PLAYER, "ENTER_NUM");
                menu_destroy(menu);
            }
        // UPDATE MY NUMBER
        case 1 : {
                client_cmd(id, "messagemode Type_Your_New_Number");
                ColorChat(id, "%L",LANG_PLAYER, "ENTER_NUM");
                menu_destroy(menu);
            }
        //NO
        case 2 : {
                
                ColorChat(id, "%L",LANG_PLAYER, "DENIED");
                menu_destroy(menu);
            }
    }
    
    menu_destroy(menu);
    MainMenu(id);
    return PLUGIN_HANDLED;
}
    
public Request_To_Add(id)
{
    new szData[128], iFIle, bool:requested = false;
    
    get_user_name(id, g_szName, charsmax(g_szName));
    get_user_authid(id, g_szAuthID, charsmax(g_szAuthID));
    
    if(file_exists(iRequestsFile))
    {
        iFIle = fopen(iRequestsFile, "rt");
        
        while(!feof(iFIle))
        {
            fgets(iFIle, szData, charsmax(szData));
            
            if(!szData[0] || szData[0] == '
;' || szData[0] == ' ' || ( szData[0] == '/' && szData[1] == '/' ))
                continue;
                
            if(containi(szData, g_szName) == -1)
                continue;
            
            else
                requested = true;
                
            break;
        }
         
        fclose(iFIle);
        
        if(requested)
        {
                ColorChat(id, "%L",LANG_PLAYER, "ALREADY_REQUESTED1");
                ColorChat(id, "%L",LANG_PLAYER, "ALREADY_REQUESTED2");
                return PLUGIN_HANDLED;
        }
    }
    
    read_argv(1, Number, 63);
    
    //CHECKS IF NUMBER IS CORRECT OR NOT
    for(new i = 0; i < sizeof(gBlockTexts); i++)
    { 
        if(containi(Number, gBlockTexts[i]) != -1) 
        { 
            ColorChat(id, "%L",LANG_PLAYER, "WRONG_NUM")
            return PLUGIN_HANDLED;
        } 
    } 
    
    for(new i; i <= strlen(Number); i++)
    {
        if((isalpha(Number[i])) || (strlen(Number) < 10) || (strlen(Number) > 14) || ((Number[0] == '
+')  && (strlen(Number) < 12)) || ((Number[0] != '+') && (strlen(Number) >10)))
        {
                ColorChat(id, "%L",LANG_PLAYER, "WRONG_NUM")
                return PLUGIN_HANDLED;
        }
    }
    
    iFIle = fopen(iRequestsFile, "at");
    
    //FORMAT IN THE FILE
    formatex(szData, charsmax(szData), "^n[REQUEST] %s(%s) : %s", g_szName, g_szAuthID, Number)
    fputs(iFIle, szData);
    fclose(iFIle);
    ColorChat(id, "%L",LANG_PLAYER, "NUM_ADDED_MSG_PLAYER");
    ColorChat(0, "%L", LANG_PLAYER, "NUM_ADDED_MSG_ALL1", g_szName);
    ColorChat(0, "%L",LANG_PLAYER, "NUM_ADDED_MSG_ALL2");
    return PLUGIN_CONTINUE;
}

public Update_Number(id)
{
    get_user_name(id, g_szName, charsmax(g_szName));
    
    new newNumber[64]
    read_argv(1, newNumber, 63);
    
    //CHECKS IF NUMBER IS CORRECT OR NOT
    for(new i = 0; i < sizeof(gBlockTexts); i++)
    { 
        if(containi(newNumber, gBlockTexts[i]) != -1) 
        { 
            ColorChat(id, "%L",LANG_PLAYER, "WRONG_NUM")
            return PLUGIN_HANDLED;
        } 
    } 
    
    for(new i; i <= strlen(newNumber); i++)
    {
        if((isalpha(newNumber[i])) || (strlen(newNumber) < 10) || (strlen(newNumber) > 14) || ((newNumber[0] == '
+')  && (strlen(newNumber) < 12)) || ((newNumber[0] != '+') && (strlen(newNumber) >10)))
        {
                ColorChat(id, "%L",LANG_PLAYER, "WRONG_NUM")
                return PLUGIN_HANDLED;
        }
    }
    
    new FilePointer = fopen(iRequestsFile, "rt")
    if(FilePointer) 
    { 
        new TempFilePath[256];
        formatex(TempFilePath, charsmax(TempFilePath), "addons/amxmodx/configs/tempfile.ini") 
        
        new InputFilePointer = fopen(TempFilePath, "wt") 
        if(InputFilePointer) 
        { 
            new FileData[128] 
            while(!feof(FilePointer)) 
            { 
                fgets(FilePointer, FileData, charsmax(FileData)) 
                trim(FileData) 
                
                if(contain(FileData, g_szName)) 
                { 
                    fprintf(InputFilePointer, "^n[REQUEST] %s(%s) : %s", g_szName, g_szAuthID, newNumber)
                    continue;
                } 
                else
                {
                    fprintf(InputFilePointer, "^n%s", FileData) 
                }
                break;
                
            } 
            fclose(InputFilePointer) 
            fclose(FilePointer)
            
            delete_file(iRequestsFile) 
            rename_file("addons/amxmodx/configs/tempfile.ini", "addons/amxmodx/configs/whatsapp_group_request.ini", 1) 
            
            ColorChat(0, "%L", LANG_PLAYER, "NUM_ADDED_MSG_ALL1", g_szName);
            ColorChat(0, "%L",LANG_PLAYER, "NUM_ADDED_MSG_ALL2");

            return PLUGIN_CONTINUE;
        } 
    } 
    return PLUGIN_HANDLED;
}
   
public View_Requests(id)
{
    if(!(get_user_flags(id) & ADMIN_FLAG))
    {
        client_print(id, print_console, "%L", LANG_PLAYER, "MSGC_ACCESS_DENIED");
        return PLUGIN_HANDLED;
    }
    
    if(!file_exists(iRequestsFile))
    {
        client_print(id, print_console, "%L", LANG_PLAYER, "MSGC_FILE_NOT_EXIST");
        return PLUGIN_HANDLED;
    }
    
    new szData[128], f;
    client_print(id, print_console, "%L", LANG_PLAYER, "MSGC_AMX_VIEWREQUESTS");
    f = fopen(iRequestsFile, "rt");
    while(!feof(f))
    {
        fgets(f, szData, charsmax(szData))
        trim(szData);
        client_print(id, print_console, szData)
    }
    fclose(f);
    return PLUGIN_HANDLED;
}

stock ColorChat(const id, const szInput[], any:...)
{
    new iPlayers[32], iCount = 1
    static szMessage[191]
    vformat(szMessage, charsmax(szMessage), szInput, 3)
    format(szMessage[0], charsmax(szMessage), "%s %s",xPrefix, szMessage)
    
    replace_all(szMessage, charsmax(szMessage), "!g", "^4")
    replace_all(szMessage, charsmax(szMessage), "!n", "^1")
    replace_all(szMessage, charsmax(szMessage), "!t", "^3")
    
    if(id)
        iPlayers[0] = id
    else
        get_players(iPlayers, iCount, "ch")
    
    for(new i, iPlayer; i < iCount; i++)
    {
        iPlayer = iPlayers[i]
        
        if(is_user_connected(iPlayer))
        {
            message_begin(MSG_ONE_UNRELIABLE, g_iSayText, _, iPlayer)
            write_byte(iPlayer)
            write_string(szMessage)
            message_end()
        }
    }

The problem is in this part but i couldn't find out!
PHP Code:
public Update_Number(id)
{
    
get_user_name(idg_szNamecharsmax(g_szName));
    
    new 
newNumber[64]
    
read_argv(1newNumber63);
    
    
//CHECKS IF NUMBER IS CORRECT OR NOT
    
for(new 0sizeof(gBlockTexts); i++)
    { 
        if(
containi(newNumbergBlockTexts[i]) != -1
        { 
            
ColorChat(id"%L",LANG_PLAYER"WRONG_NUM")
            return 
PLUGIN_HANDLED;
        } 
    } 
    
    for(new 
i<= strlen(newNumber); i++)
    {
        if((
isalpha(newNumber[i])) || (strlen(newNumber) < 10) || (strlen(newNumber) > 14) || ((newNumber[0] == '+')  && (strlen(newNumber) < 12)) || ((newNumber[0] != '+') && (strlen(newNumber) >10)))
        {
                
ColorChat(id"%L",LANG_PLAYER"WRONG_NUM")
                return 
PLUGIN_HANDLED;
        }
    }
    
    new 
FilePointer fopen(iRequestsFile"rt")
    if(
FilePointer
    { 
        new 
TempFilePath[256];
        
formatex(TempFilePathcharsmax(TempFilePath), "addons/amxmodx/configs/tempfile.ini"
        
        new 
InputFilePointer fopen(TempFilePath"wt"
        if(
InputFilePointer
        { 
            new 
FileData[128
            while(!
feof(FilePointer)) 
            { 
                
fgets(FilePointerFileDatacharsmax(FileData)) 
                
trim(FileData
                
                if(
contain(FileDatag_szName)) 
                { 
                    
fprintf(InputFilePointer"^n[REQUEST] %s(%s) : %s"g_szNameg_szAuthIDnewNumber)
                    continue;
                } 
                else
                {
                    
fprintf(InputFilePointer"^n%s"FileData
                }
                break;
                
            } 
            
fclose(InputFilePointer
            
fclose(FilePointer)
            
            
delete_file(iRequestsFile
            
rename_file("addons/amxmodx/configs/tempfile.ini""addons/amxmodx/configs/whatsapp_group_request.ini"1
            
            
ColorChat(0"%L"LANG_PLAYER"NUM_ADDED_MSG_ALL1"g_szName);
            
ColorChat(0"%L",LANG_PLAYER"NUM_ADDED_MSG_ALL2");

            return 
PLUGIN_CONTINUE;
        } 
    } 
    return 
PLUGIN_HANDLED;

__________________

Last edited by CrAzY MaN; 05-06-2018 at 03:17.
CrAzY MaN is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 05-06-2018 , 08:39   Re: [HELP] Replace a line from .ini
Reply With Quote #27

https://www.amxmodx.org/api/string/contain
Read about how the returned values work.

continue/break. Why? If you break it will discard all rows below the first negative result.

Your use of global variables, especially in combination with get_user_authid() is bad. You obviously have problems applying the logic to the scope, try using static instead. If you don't understand what I mean by this try this on: What happens if one player adds their number and another players just replaces their. When is the g_szAuthID set to a new value and is it relevant when used?

Also you have to consider the evil nature of people trying to break things. What if I set my name to a SteamID that exists in the file? It will match. The same goes if I will set my name to "[REQUEST]", it would match EVERY line and therefor replace them. A simple way to break things is to set my name to just 1 letter, it would have a pretty high chance of matching with at least one line.
I would format the file differently but a simple solution in your case would be to use equali(FileData[10], g_szName, strlen(g_szName)). Since you're then using FileData[10], it cannot match with [REQUEST], it will only match the name because of the strlen() limit and you cannot offset any result because you use equal() instead of contain().

Of course this issue is 100% non-steam related. If you would match by SteamID you would not have this issue at all.
But hey, the non-steamers are obviously idiots, how are they supposed to understand this?

If you want to understand code better and create public releases you should really learn to debug using printed messages. It will help to actually see the results of operation for yourself and at that point you could easily backtrack the issue. Here's an example:

Code:
            while(!feof(FilePointer))             {                 fgets(FilePointer, FileData, charsmax(FileData))                 trim(FileData)                 server_print("g_szName: %s", g_szName);                 server_print("FileData: %s", FileData);                 server_print("contain(FileData, g_szName): %d", contain(FileData, g_szName));                                 if(contain(FileData, g_szName))
Here's what you would see:
Code:
g_szName: 2
FileData: [REQUEST] 1(STEAM_ID_LAN) : 1111111111
contain(FileData, g_szName): -1

g_szName: 2
FileData: [REQUEST] 2(STEAM_ID_LAN) : 2222222222
contain(FileData, g_szName): 10

g_szName: 2
FileData: [REQUEST] 3(STEAM_ID_LAN) : +913333333333
contain(FileData, g_szName): -1
You would've solved this yourself in 10 minutes.
__________________

Last edited by Black Rose; 05-07-2018 at 11:02.
Black Rose is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:07.


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