how to be admin in my own server??

Started by ahmedzead, Feb 06, 2017, 01:47 PM

Previous topic - Next topic

iReaL

Quote from: vito on Feb 10, 2017, 04:56 PM
Quote from: jWeb on Feb 10, 2017, 03:58 PMAs for the UID. Well, that can be stolen and replicated.
Nobody will do that. Even if he will set password 123456 nobody will hack it. It's vcmp.

Yes...

jWeb

#16
Here you go. The simplest admin implementation I can think of. It uses a .txt file to store and load administrators based on their nicks. Uses a table with name->password as the key->value pair to represent the list of admins and be efficient when searching.

Load/save functions:
// ============================================================================
function LoadAdmins(filepath) {
    local f = file(filepath, "rb+"), t = {};
    while (f.eos() == null) {
        local name = "", pass = "", c = 0;
        for (c = f.readn('c'); c != '|' && f.eos() == null; c = f.readn('c')) {
            name += format("%c", c);
        }
        if (f.eos() != null) break;
        for (c = f.readn('c'); c != '\n' && f.eos() == null; c = f.readn('c')) {
            pass += format("%c", c);
        }
        if (f.eos() != null && c != '\n' && c != '\r') pass += format("%c", c);
        name = strip(name), pass = strip(pass);
        if (name.len() > 0 && pass.len() > 0) t.rawset(name, pass);
    }
    return t;
}

function SaveAdmins(adminlist, filepath) {
    local f = file(filepath, "wb+");
    foreach (name, pass in adminlist) {
        name = strip(name), pass = strip(pass);
        foreach (c in name) f.writen(c, 'c');
        f.writen('|', 'c');
        foreach (c in pass) f.writen(c, 'c');
        f.writen('\n', 'c');
    }
}


Helper functions:
// ============================================================================
function EncryptPassword(password) {
    return password; // INSERT YOUR HASHING METHOD HERE! ex: SHA256(password);
}
function AdminCheck(player) {
    if (player.IsAdmin == true) return true;
    MessagePlayer("You do not have the privilege to perform this action!", player);
    return false;
}

NOTE: I would suggest using the Hashing plugin to encrypt the passwords by updating the EncryptPassword() function accordingly. But that's your choice.

Load and save admins:
// ============================================================================
function onServerStart() {
    g_AdminList <- LoadAdmins("adminlist.txt");
}

function onServerStop() {
    SaveAdmins(g_AdminList, "adminlist.txt");
}

NOTE: Make sure you have a "adminlist.txt" file in the server folder.

And 3 simple commands to manage them:
// ============================================================================
function onPlayerCommand(player, cmd, args) {
    args  = strip(args);
    switch (cmd) {
        // ====================================================================
        case "getadmin": {
            if (player.IsAdmin == true) MessagePlayer("You are already an administrator", player);
            else if (args.len() <= 0) {
                MessagePlayer("Syntax: cmd <password>", player);
                return;
            }
            else if (g_AdminList.rawin(player.Name)) {
                local pass = g_AdminList.rawget(player.Name), args = EncryptPassword(args);
                if (pass != args)
                    MessagePlayer("Wrong administrator password", player);
                else {
                    player.IsAdmin = true;
                    MessagePlayer("You are now recognized as administrator", player);
                }
            }
        } break;
        // ====================================================================
        case "setadmin": {
            if (!AdminCheck(player)) return;
            else if (args.len() <= 0) {
                MessagePlayer("Syntax: cmd <player_id> <password>", player);
                return;
            }
            local sep = args.find(" ");
            if (sep == null) {
                MessagePlayer("Syntax: cmd <player_id> <password>", player);
                return;
            }
            local player_id = null;
            try {
                player_id = args.slice(0, sep).tointeger();
            } catch (e) {
                MessagePlayer("Invalid player id", player);
                return;
            }
            local admin = FindPlayer(player_id)
            if (admin == null) {
                MessagePlayer("No such player on the server", player);
                return;
            }
            local password = null;
            try {
                password = strip(args.slice(sep + 1).tostring());
            } catch(e) {
                MessagePlayer("Invalid or missing password", player);
                return;
            }
            if (password.len() <= 0) {
                MessagePlayer("Invalid or empty password", player);
                return;
            }
            g_AdminList.rawset(admin.Name, EncryptPassword(password));
            admin.IsAdmin = true;
            MessagePlayer("Successfully included (" + admin.Name + ") into administrators", player);
        } break;
        // ====================================================================
        case "unsetadmin": {
            if (!AdminCheck(player)) return;
            else if (args.len() <= 0) {
                MessagePlayer("Syntax: cmd <admin_name>", player);
                return;
            } else if (g_AdminList.rawin(args)) {
                g_AdminList.rawdelete(args);
                local admin = FindPlayer(args);
                if (admin != null) admin.IsAdmin = false;
                MessagePlayer("Successfully excluded (" + admin.Name + ") from administrators", player);
            } else MessagePlayer("No administrator named (" + args + ")", player);
        } break;
    }
}

When a player switches to admin mode. It simple sets the player.IsAdmin property to true. So if (player.IsAdmin == true) then that player is an admin. Other wise it's just a regular player.

I'm not sure how you can get more simple than this.

Cool

@jweb I think You dont know he is totally new thats why no one geting interest in it Wait for hiss Next error
:D

Eva

Quote from: vito on Feb 10, 2017, 04:56 PM
Quote from: jWeb on Feb 10, 2017, 03:58 PMAs for the UID. Well, that can be stolen and replicated.
Nobody will do that. Even if he will set password 123456 nobody will hack it. It's vcmp.
I wouldnt count on it.