i've this plugin that exolent made.
but there is some problem, when im add admin it add myself and exit in the menu of choosing auth id.
PHP Code:
#include <amxmodx>
#include <amxmisc>
// Access level required for managing admins
#define ADMIN_MANAGE ADMIN_RCON
new gCaseSensitive;
#define IsCaseSensitive(%1) (gCaseSensitive & (1 << (%1 & 31)))
#define SetCaseSensitive(%1) gCaseSensitive |= (1 << (%1 & 31))
#define ClearCaseSensitive(%1) gCaseSensitive &= ~(1 << (%1 & 31))
// Admin data structure
enum _:AdminData {
Admin_Name[32],
Admin_Auth[64],
Admin_Password[32],
Admin_Access,
Admin_Flags,
Admin_Level,
Admin_LastVisit
};
// Array of all admins
new Array:gAdminData;
// Size of admin data
new gNumAdmins;
// Level names for admins
new const gLevels[][] = {
"Owner",
"Manager",
"Super Admin",
"Admin",
"Vip"
};
// Authorization types for the menu
enum _:Auth {
Auth_Name,
Auth_SteamID,
Auth_IP
};
// Names for authorization types in the menu
new const gAuthNames[Auth][] = {
"Name + Pass",
"SteamID",
"IP Address"
};
// Flags for authorization types
new const gAuthFlags[Auth] = {
0,
FLAG_AUTHID,
FLAG_IP
};
// Command types for input through typing
enum _:Command {
Command_Add_Access,
Command_Add_Password,
Command_Edit_Name,
Command_Edit_Auth,
Command_Edit_Access,
Command_Edit_Password
};
// The commands used for the command type
new const gCommands[Command][] = {
"amx_addadmin_access",
"amx_addadmin_password",
"amx_editadmin_name",
"amx_editadmin_auth",
"amx_editadmin_access",
"amx_editadmin_password"
};
// Player index who is currently managing admins
new gAdminManager;
// Menu variables for manager in the menu
new gMenuLevel; // Used for adding new admins
new gMenuPlayers[32]; // Used for showing players in the menu
new gMenuPlayerIDs[32]; // Used for checking that selected player matches from the menu
new gMenuPlayerCount; // Used for size of user ids
new gMenuPlayer; // Used for selected player
new gMenuAcceptCommand; // Used for knowing what command inputs are allowed
new gMenuAccess; // Used for adding/editing admins
new gMenuFlags; // Used for adding/editing admins
new gMenuPassword[32]; // Used for adding/editing admins
new gMenuAuth; // Used for viewing admins
new gMenuData[AdminData]; // Used for editing admins
// Cvars for mode, password field, and default access for non-admins
new gCvarMode;
new gCvarPasswordField;
new gCvarDefaultAccess;
// File where admins are located
new gAdminsFile[64];
// Command for kicking players
new gKickCommand[32];
public plugin_init() {
register_plugin("Admin Manager", "0.0.1", "Exolent");
// Register multilingual files
register_dictionary("admin.txt");
register_dictionary("common.txt");
// Chat command for /manage to open the menu
register_clcmd("say /manage", "CmdManage", ADMIN_MANAGE);
register_clcmd("say_team /manage", "CmdManage", ADMIN_MANAGE);
// Loop through all type-input commands
for(new i = 0; i < Command; i++) {
// Register command to generic messagemode handler
register_clcmd(gCommands[i], "CmdMessageMode");
}
// register kick command
new l = copy(gKickCommand, charsmax(gKickCommand), "amxauthcustom");
while(l < charsmax(gKickCommand)) {
switch(random(4)) {
case 0: gKickCommand[l++] = random_num('0', '9');
case 1: gKickCommand[l++] = random_num('a', 'z');
case 2: gKickCommand[l++] = random_num('A', 'Z');
case 3: gKickCommand[l++] = '_';
}
}
register_clcmd(gKickCommand, "CmdKick");
// Register cvars
gCvarMode = register_cvar("amx_mode", "1");
gCvarPasswordField = register_cvar("amx_password_field", "_pw");
gCvarDefaultAccess = register_cvar("amx_default_access", "");
// ===========================================================
// Random cvars from admin.amxx
// ===========================================================
register_cvar("amx_vote_ratio", "0.02");
register_cvar("amx_vote_time", "10");
register_cvar("amx_vote_answers", "1");
register_cvar("amx_vote_delay", "60");
new cvarVoting = register_cvar("amx_last_voting", "0");
register_cvar("amx_show_activity", "2");
register_cvar("amx_votekick_ratio", "0.40");
register_cvar("amx_voteban_ratio", "0.40");
register_cvar("amx_votemap_ratio", "0.40");
set_pcvar_float(cvarVoting, 0.0);
register_cvar("amx_sql_table", "admins");
register_cvar("amx_sql_host", "127.0.0.1");
register_cvar("amx_sql_user", "root");
register_cvar("amx_sql_pass", "");
register_cvar("amx_sql_db", "amx");
register_cvar("amx_sql_type", "mysql");
// ===========================================================
// Create the admin array
gAdminData = ArrayCreate(AdminData);
// Locate the admins file
new configs[32];
get_configsdir(configs, charsmax(configs));
formatex(gAdminsFile, charsmax(gAdminsFile), "%s/users_custom.ini", configs);
// Load admins from file
LoadAdmins();
// Remove user flag from server
remove_user_flags(0, ADMIN_USER);
// Execute normal configs from admin.sma
server_cmd("exec %s/amxx.cfg", configs);
server_cmd("exec %s/sql.cfg", configs);
}
public plugin_end() {
// Clear the admin array from memory
ArrayDestroy(gAdminData);
}
public client_connect(id) {
// Clear the case-sensitive flag
ClearCaseSensitive(id);
}
public client_authorized(id) {
// Check if admin is turned on
if(get_pcvar_num(gCvarMode)) {
// Check admin for this user
CheckAdmin(id);
}
}
public client_putinserver(id) {
// For listen servers, check host access
if(id == 1 && get_pcvar_num(gCvarMode) && !is_dedicated_server()) {
// Check admin for host
CheckAdmin(id);
}
}
public client_infochanged(id) {
// Check if player is connected and admin is turned on
if(is_user_connected(id) && get_pcvar_num(gCvarMode)) {
// Grab new and old name
new oldName[32], newName[32];
get_user_name(id, oldName, charsmax(oldName));
get_user_info(id, "name", newName, charsmax(newName));
// Check if names changed based on case sensitive flag
if(strcmp(oldName, newName, !IsCaseSensitive(id)) == 0) {
// Name changed, check admin
CheckAdmin(id, newName);
}
}
}
public client_disconnect(id) {
// Check if this was the admin manager
if(id == gAdminManager) {
// Reset so another admin can manage admins if they want
gAdminManager = 0;
}
// Check if this was a selected player in the menu
if(id == gMenuPlayer) {
// Reset so admin will have to choose player again
gMenuPlayer = 0;
}
}
public CmdManage(id, level) {
// Make sure player has required level
if(!access(id, level)) {
// Show no access message
client_print(id, print_chat, "* You have no access to that command.");
}
// Check if an admin is already in the menu and is not this admin
else if(gAdminManager && gAdminManager != id) {
// Show the already in use message
client_print(id, print_chat, "* Admin menu already in use.");
}
// Player has admin access and menu is currently not in use
else {
// Assign this player as the manager
gAdminManager = id;
// Show the main management menu
ShowMainMenu(id);
}
// Hide from chat
return PLUGIN_HANDLED;
}
ShowMainMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nMain Menu", "MenuMain");
// Add the default menu items
menu_additem(menu, "Add Admin", "");
menu_additem(menu, "Remove Admin", "");
menu_additem(menu, "Edit Admin", "");
menu_additem(menu, "Admin Last Visit^n", "");
menu_additem(menu, "Reload Admins", "");
// Show the menu
menu_display(id, menu);
}
public MenuMain(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Figure out what was pressed
switch(item) {
// Add Admin
case 0: {
// Show the add main menu
ShowAddMainMenu(id);
}
// Remove Admin
case 1: {
// Show the remove main menu
ShowRemoveMainMenu(id);
}
// Edit Admin
case 2: {
// Show the edit main menu
ShowEditMainMenu(id);
}
// Admin Last Visit
case 3: {
// Show the last visit main menu
ShowLastMainMenu(id);
}
// Reload Admins
case 4: {
// Load the admins again
LoadAdmins();
// Check all access for players
CheckAllAccess();
// Check if player still has admin
if(access(id, ADMIN_MANAGE)) {
// Show the main menu again
ShowMainMenu(id);
} else {
// Remove them from being manager
gAdminManager = 0;
}
// Notify admin of action
client_print(id, print_chat, "* Reloaded admins.");
}
// Exit
case MENU_EXIT: {
// Remove them from being the manager
gAdminManager = 0;
}
}
}
ShowAddMainMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nAdd Admin Menu^n^nSelect level for this admin:", "MenuAddMain");
// Loop through all levels
for(new i = 0; i < sizeof(gLevels); i++) {
// Add level to the menu
menu_additem(menu, gLevels[i], "");
}
// Show the menu
menu_display(id, menu);
}
public MenuAddMain(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the main menu
ShowMainMenu(id);
}
// A level was chosen
else {
// Store the level that was chosen
gMenuLevel = item;
// Store the access that comes with this level
gMenuAccess = GetDefaultFlags();
// Show the add players menu
ShowAddPlayersMenu(id);
}
}
ShowAddPlayersMenu(id) {
// Format the title based on the level that was selected
new title[128];
formatex(title, charsmax(title), "Admins Manager^nAdd Admin Menu^n^nSelect to be %s:", gLevels[gMenuLevel]);
// Create the menu handle
new menu = menu_create(title, "MenuAddPlayers");
// Grab all players that aren't bots or HLTV
new players[32], pnum;
get_players(players, pnum, "ch");
// Reset menu user count
gMenuPlayerCount = 0;
// Variables used in player loop
new player, name[32];
// Loop through all players
for(new i = 0; i < pnum; i++) {
// Cache the player index
player = players[i];
// Ignore the current manager, or if player is already admin
if(player == id || is_user_admin(player)) {
// Go to next player
continue;
}
// Store this player in the selection
gMenuPlayers[gMenuPlayerCount] = player;
// Store the user id for this player
gMenuPlayerIDs[gMenuPlayerCount++] = get_user_userid(player);
// Grab this player's name
get_user_name(player, name, charsmax(name));
// Add player to the menu
menu_additem(menu, name, "");
}
// Check if any players were added to the menu
if(gMenuPlayerCount) {
// Show the menu
menu_display(id, menu);
}
// No players were added
else {
// Destroy the menu since it's not shown
menu_destroy(menu);
// Notify the player
client_print(id, print_chat, "* There are no non-admins to choose from for adding admins.");
// Show the main menu
ShowMainMenu(id);
}
}
public MenuAddPlayers(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the add main menu
ShowAddMainMenu(id);
}
// A player was chosen
else {
// Grab which player was chosen
gMenuPlayer = gMenuPlayers[item];
// Check if this player is connected and matches the user id
if(is_user_connected(gMenuPlayer) && get_user_userid(gMenuPlayer) == gMenuPlayerIDs[item]) {
// Show the add authorization type menu
ShowAddAuthMenu(id);
}
// Invalid player selected
else {
// Notify the player
client_print(id, print_chat, "* This player left while you were choosing a player.");
// Show the players menu again
ShowAddPlayersMenu(id);
}
}
}
ShowAddAuthMenu(id) {
// Check if the player no longer exists
if(!gMenuPlayer) {
// Notify the player
client_print(id, print_chat, "* This player left while you were choosing options to add them.");
// Show the player choose menu again
ShowAddPlayersMenu(id);
}
// Player still exists in the server
else {
// Grab the player's name
new name[32];
get_user_name(gMenuPlayer, name, charsmax(name));
// Format the title based on the level and player that were selected
new title[128];
formatex(title, charsmax(title), "Admins Manager^nAdd Admin Menu^n^nSelect auth type for %s to be %s:", name, gLevels[gMenuLevel]);
// Create the menu handle
new menu = menu_create(title, "MenuAddAuth");
// Loop through all authorization types
for(new i = 0; i < Auth; i++) {
// Add authorization type to the menu
menu_additem(menu, gAuthNames[i], "");
}
// Show the menu
menu_display(id, menu);
}
}
public MenuAddAuth(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the add players menu
ShowAddPlayersMenu(id);
}
// Check if the player left
else if(!gMenuPlayer) {
// Notify the player
client_print(id, print_chat, "* This player left while you were choosing options to add them.");
// Show the player choose menu again
ShowAddPlayersMenu(id);
}
// An auth was chosen
else {
// Grab the flags for this auth
gMenuFlags = gAuthFlags[item];
// Show the info menu
ShowAddInfoMenu(id);
}
}
ShowAddInfoMenu(id) {
// Check if the player no longer exists
if(!gMenuPlayer) {
// Notify the player
client_print(id, print_chat, "* This player left while you were choosing options to add them.");
// Show the player choose menu again
ShowAddPlayersMenu(id);
}
// Player still exists in the server
else {
// Grab the player's name
new name[32];
get_user_name(gMenuPlayer, name, charsmax(name));
// Format the title based on the level and player that were selected
new title[128];
formatex(title, charsmax(title), "Admins Manager^nAdd Admin Menu^n^nInfo for %s to be %s:", name, gLevels[gMenuLevel]);
// Create the menu handle
new menu = menu_create(title, "MenuAddAccess");
// Grab the current flags as a string
get_flags(gMenuFlags, name, charsmax(name));
// Format the menu item based on current flags
formatex(title, charsmax(title), "\uAccess:^n\w%s^n^n\yFlags:", name);
// Add item to menu
menu_additem(menu, title, "");
// Check if this is for names
if((~gMenuFlags & FLAG_AUTHID) && (~gMenuFlags & FLAG_IP)) {
// Add case-sensitive option to menu
menu_additem(menu, (gMenuFlags & FLAG_CASE_SENSITIVE) ? "Auth Case-Sensitive: Yes" : "Auth Case-Sensitive: \dNo", "");
}
// Check if a password is not required
if(gMenuFlags & FLAG_NOPASS) {
// Add item to menu
menu_additem(menu, "Password Required: \dNo", "");
}
// Password is required
else {
// Add item to menu
menu_additem(menu, "Password Required: Yes", "");
// Format the menu item based on current password
formatex(title, charsmax(title), "Password: %s", gMenuPassword);
// Add item to menu
menu_additem(menu, title, "");
// Add kick option to menu
menu_additem(menu, (gMenuFlags & FLAG_KICK) ? "Kick for Invalid Password: Yes" : "Kick for Invalid Password: \dNo", "");
}
// Add an empty line
menu_addblank(menu);
// Add the "Add Admin" option
menu_additem(menu, "\rAdd Admin^n^n", "");
// Add an exit option
menu_additem(menu, "Exit", "");
// Remove pagination from the menu
menu_setprop(menu, MPROP_PERPAGE, 0);
// Show the menu
menu_display(id, menu, 0);
}
}
public MenuAddInfo(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the add auth menu
ShowAddAuthMenu(id);
}
// Check if the player left
else if(!gMenuPlayer) {
// Notify the player
client_print(id, print_chat, "* This player left while you were choosing options to add them.");
// Show the player choose menu again
ShowAddPlayersMenu(id);
}
// An option was chosen
else {
// Check if the name options were not added
if(gMenuFlags & FLAG_AUTHID || gMenuFlags & FLAG_IP) {
// Increase the item by 1 as if there were blank options
item += _:(item > 0);
}
// Check if the password options were not added
if(gMenuFlags & FLAG_NOPASS) {
// Increase the item by 2 as if there were blank options
item += (_:(item > 2) * 2);
}
// Check what was selected
switch(item) {
// Access Flags
case 0: {
// Notify player they need to type the flags
client_print(id, print_chat, "* Type in the new access flags for this player.");
// Start the message mode for this player
MessageMode(id, Command_Add_Access);
}
// Case-Sensitive
case 1: {
// Toggle case-sensitive flag
gMenuFlags ^= FLAG_CASE_SENSITIVE;
// Show add info menu
ShowAddInfoMenu(id);
}
// Password Required
case 2: {
// Toggle password flag
gMenuFlags ^= FLAG_NOPASS;
// Show add info menu
ShowAddInfoMenu(id);
}
// Set Password
case 3: {
// Notify player they need to type the password
client_print(id, print_chat, "* Type in the new password for this player.");
// Start the message mode for this player
MessageMode(id, Command_Add_Password);
}
// Kick Invalid Password
case 4: {
// Toggle kick flag
gMenuFlags ^= FLAG_KICK;
// Show add info menu
ShowAddInfoMenu(id);
}
// Empty line
case 5: {
}
// Add Admin
case 6: {
// Prepare the admin data
new data[AdminData];
// Store the player's name into the data
get_user_name(gMenuPlayer, data[Admin_Name], charsmax(data[Admin_Name]));
// Check if this is for SteamID
if(gMenuFlags & FLAG_AUTHID) {
// Get player's SteamID
get_user_authid(gMenuPlayer, data[Admin_Auth], charsmax(data[Admin_Auth]));
}
// Check if this is for IP
else if(gMenuFlags & FLAG_IP) {
// Get player's IP
get_user_ip(gMenuPlayer, data[Admin_Auth], charsmax(data[Admin_Auth]), 1);
}
// Must be for name
else {
// Copy name from admin name
copy(data[Admin_Auth], charsmax(data[Admin_Auth]), data[Admin_Name]);
}
// Store password
copy(data[Admin_Password], charsmax(data[Admin_Password]), gMenuPassword);
// Store access, flags, level, and last visit
data[Admin_Access] = gMenuAccess;
data[Admin_Flags] = gMenuFlags;
data[Admin_Level] = gMenuLevel;
data[Admin_LastVisit] = get_systime();
// Add admin data
ArrayPushArray(gAdminData, data);
// Increase admin size
gNumAdmins++;
// Save admins
SaveAdmins();
// Check access for all users
CheckAllAccess();
// Return to main menu
ShowMainMenu(id);
// Notify admin of action
client_print(id, print_chat, "* Added %s as an admin.", data[Admin_Name]);
}
// Exit
case 7: {
// Show the add auth menu
ShowAddAuthMenu(id);
}
}
}
}
ShowRemoveMainMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nRemove Menu", "MenuRemoveMain");
// Prepare variables for loop
new data[AdminData];
new item[64];
// Loop through all admins
for(new i = 0; i < gNumAdmins; i++) {
// Format admin as the item
formatex(item, charsmax(item), "%s (%s)", data[Admin_Name], gLevels[data[Admin_Level]]);
// Add admin to the menu
menu_additem(menu, item, "");
}
// Show the menu
menu_display(id, menu);
}
public MenuRemoveMain(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the main menu
ShowMainMenu(id);
}
// An admin was chosen
else {
// Grab which admin was chosen
gMenuAuth = item;
// Show remove chosen menu
ShowRemoveChosenMenu(id);
}
}
ShowRemoveChosenMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nRemove Menu^nView Details", "MenuRemoveChosen");
// Grab the selected admin
new data[AdminData];
ArrayGetArray(gAdminData, gMenuAuth, data);
// Prepare item variable
new item[128];
// Show the selected admin's name
formatex(item, charsmax(item), "Name: %s", data[Admin_Name]);
menu_additem(menu, item, "");
// Show the selected admin's level
formatex(item, charsmax(item), "Level: %s", gLevels[data[Admin_Level]]);
menu_additem(menu, item, "");
// Show the selected admin's last visit
new l = copy(item, charsmax(item), "Last visit: ");
format_time(item[l], charsmax(item) - l, "%x %X^n^nAre you sure you want to delete?", data[Admin_LastVisit]);
menu_additem(menu, item, "");
// Add Yes answer
menu_additem(menu, "Yes", "");
// Set exit option to be "No"
menu_setprop(menu, MPROP_EXITNAME, "No");
// Show the menu
menu_display(id, menu);
}
public MenuRemoveChosen(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the remove main menu
ShowRemoveMainMenu(id);
}
// Pressed a key
else {
// Check if pressed delete
if(item == 3) {
// Grab the selected admin
new data[AdminData];
ArrayGetArray(gAdminData, gMenuAuth, data);
// Delete this admin from the list
ArrayDeleteItem(gAdminData, gMenuAuth);
// Save to file
SaveAdmins();
// Check any admins that were affected
CheckAllAccess();
// Check if admin still has access and didn't delete themself
if(access(id, ADMIN_MANAGE)) {
// Show the remove selection again
ShowRemoveMainMenu(id);
} else {
// Remove them from being manager
gAdminManager = 0;
}
// Notify admin of action
client_print(id, print_chat, "* Deleted %s from admin list.", data[Admin_Name]);
}
// Did not press delete
else {
// Show the menu again
ShowRemoveChosenMenu(id);
}
}
}
ShowEditMainMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nEdit Menu", "MenuEditMain");
// Prepare variables for loop
new data[AdminData];
new item[64];
// Loop through all admins
for(new i = 0; i < gNumAdmins; i++) {
// Format admin as the item
formatex(item, charsmax(item), "%s (%s)", data[Admin_Name], gLevels[data[Admin_Level]]);
// Add admin to the menu
menu_additem(menu, item, "");
}
// Show the menu
menu_display(id, menu);
}
public MenuEditMain(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the main menu
ShowMainMenu(id);
}
// An admin was chosen
else {
// Grab which admin was chosen
gMenuAuth = item;
// Grab the data for this admin
ArrayGetArray(gAdminData, gMenuAuth, gMenuData);
// Show edit chosen menu
ShowEditChosenMenu(id);
}
}
ShowEditChosenMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nEdit Menu^nView Details", "MenuEditChosen");
// Prepare item variable
new item[128];
// Show the selected admin's name
formatex(item, charsmax(item), "Name: %s", gMenuData[Admin_Name]);
menu_additem(menu, item, "");
// Show the selected admin's auth
formatex(item, charsmax(item), "Auth: %s", gMenuData[Admin_Auth]);
menu_additem(menu, item, "");
// Show the selected admin's level
formatex(item, charsmax(item), "Level: %s", gLevels[gMenuData[Admin_Level]]);
menu_additem(menu, item, "");
// Show the selected admin's access
new l = copy(item, charsmax(item), "Access: ");
get_flags(gMenuData[Admin_Access], item[l], charsmax(item) - l);
menu_additem(menu, item, "");
// Show the selected admin's flags
l = copy(item, charsmax(item), "Flags: ");
get_flags(gMenuData[Admin_Flags], item[l], charsmax(item) - l);
menu_additem(menu, item, "");
// Show the selected admin's password
formatex(item, charsmax(item), "%sPassword: %s^n", (gMenuData[Admin_Flags] & FLAG_NOPASS) ? "\d" : "", gMenuData[Admin_Password]);
menu_additem(menu, item, "");
// Save changes option
menu_additem(menu, "\ySave Changes", "");
// Set the exit option to be cancel
menu_setprop(menu, MPROP_EXITNAME, "Cancel");
// Show the menu
menu_display(id, menu);
}
public MenuEditChosen(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the edit main menu
ShowEditMainMenu(id);
}
// Pressed a key
else {
// Check which key was pressed
switch(item) {
// Name
case 0: {
// Notify player they need to type the name
client_print(id, print_chat, "* Type in the new name for this admin.");
// Start the message mode for this player
MessageMode(id, Command_Edit_Name);
}
// Auth
case 1: {
// Notify player they need to type the auth
client_print(id, print_chat, "* Type in the new auth for this admin.");
// Start the message mode for this player
MessageMode(id, Command_Edit_Auth);
}
// Level
case 2: {
// Show the edit level menu
ShowEditLevelMenu(id);
}
// Access
case 3: {
// Notify player they need to type the access
client_print(id, print_chat, "* Type in the new access for this admin.");
// Start the message mode for this player
MessageMode(id, Command_Edit_Access);
}
// Flags
case 4: {
// Show the edit flags menu
ShowEditFlagsMenu(id);
}
// Password
case 5: {
// Notify player they need to type the password
client_print(id, print_chat, "* Type in the new password for this admin.");
// Start the message mode for this player
MessageMode(id, Command_Edit_Password);
}
// Save Changes
case 6: {
// Update the admin data in the array
ArraySetArray(gAdminData, gMenuAuth, gMenuData);
// Save admins to file
SaveAdmins();
// Check access for all players
CheckAllAccess();
// Check if player still has access
if(access(id, ADMIN_MANAGE)) {
// Show the edit menu again
ShowEditChosenMenu(id);
} else {
// Remove them from being the manager
gAdminManager = 0;
}
// Notify admin of action
client_print(id, print_chat, "* Changes to %s in admin list.", gMenuData[Admin_Name]);
}
}
}
}
ShowEditLevelMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nEdit Menu^nSelect New Level", "MenuEditLevel");
// Prepare variables for loop
new item[128];
// Loop through all levels
for(new i = 0; i < sizeof(gLevels); i++) {
// Check if this is the current level
if(i == gMenuData[Admin_Level]) {
// Add (current) after the level name
formatex(item, charsmax(item), "%s (Current)", gLevels[i]);
// Add item to the menu
menu_additem(menu, item, "");
}
// This is not the current level
else {
// Add level name to the menu
menu_additem(menu, gLevels[i], "");
}
}
// Show the menu
menu_display(id, menu);
}
public MenuEditLevel(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the edit chosen menu
ShowEditChosenMenu(id);
}
// A level was chosen
else {
// Update the level that was chosen
gMenuData[Admin_Level] = item;
// Show the edit chosen menu
ShowEditChosenMenu(id);
}
}
ShowEditFlagsMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nEdit Menu^nView Flags", "MenuEditFlags");
// Prepare variables
new item[128], bool:isName;
// Check if this is a SteamID
if(gMenuData[Admin_Flags] & FLAG_AUTHID) {
// Say that the type is a SteamID
copy(item, charsmax(item), "\yType: \wSteamID");
}
// Check if this ia an IP
else if(gMenuData[Admin_Flags] & FLAG_IP) {
// Say that the type is an IP
copy(item, charsmax(item), "\yType: \wIP");
}
// Check if this is a tag
else if(gMenuData[Admin_Flags] & FLAG_TAG) {
// Say that the type is a tag
copy(item, charsmax(item), "\yType: \wTag");
// Cache type as a name
isName = true;
}
// Must be a name
else {
// Say that the type is a name
copy(item, charsmax(item), "\yType: \wName");
// Cache type as a name
isName = true;
}
// Add type item
menu_additem(menu, item, "");
// Check if password is required
if(~gMenuData[Admin_Flags] & FLAG_NOPASS) {
// Add password requirement
menu_additem(menu, "\yPassword Required: \wYes", "");
// Add kick for invalid password
menu_additem(menu, (gMenuData[Admin_Flags] & FLAG_KICK) ? "\yKick on Invalid Password: \wYes" : "\yKick on Invalid Password: \dNo", "");
}
// No password is required
else {
// Add password requirement
menu_additem(menu, "\yPassword Required: \dNo", "");
}
// Check if this is a name auth
if(isName) {
// Add case sensitivity
menu_additem(menu, (gMenuData[Admin_Flags] & FLAG_CASE_SENSITIVE) ? "\yCase-Sensitive Auth: \wYes" : "\yCase-Sensitive Auth: \dNo", "");
}
// Show the menu
menu_display(id, menu);
}
public MenuEditFlags(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the edit chosen menu
ShowEditChosenMenu(id);
}
// A key was pressed
else {
// Check if a password is not required
if(gMenuData[Admin_Flags] & FLAG_NOPASS) {
// Update item index if got that far
item += _:(item > 1);
}
// Check which was changed
switch(item) {
// Type
case 0: {
// Check if this is a SteamID
if(gMenuData[Admin_Flags] & FLAG_AUTHID) {
// Remove SteamID flag
gMenuData[Admin_Flags] &= ~FLAG_AUTHID;
// Add IP flag
gMenuData[Admin_Flags] |= FLAG_IP;
}
// Check if this is a IP
else if(gMenuData[Admin_Flags] & FLAG_IP) {
// Remove IP flag
gMenuData[Admin_Flags] &= ~FLAG_IP;
// Add Tag flag
gMenuData[Admin_Flags] |= FLAG_TAG;
}
// Check if this is a Tag
else if(gMenuData[Admin_Flags] & FLAG_TAG) {
// Remove Tag flag
gMenuData[Admin_Flags] &= ~FLAG_TAG;
}
// Must be a name
else {
// Add SteamID flag
gMenuData[Admin_Flags] |= FLAG_AUTHID;
}
}
// Password Required
case 1: {
// Toggle password requirement
gMenuData[Admin_Flags] ^= FLAG_NOPASS;
}
// Kick on Invalid Password
case 2: {
// Toggle kick
gMenuData[Admin_Flags] ^= FLAG_KICK;
}
// Case-Sensitive Auth
case 3: {
// Toggle case-sensitive
gMenuData[Admin_Flags] ^= FLAG_CASE_SENSITIVE;
}
}
// Show the edit flags menu
ShowEditFlagsMenu(id);
}
}
ShowLastMainMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nLast Visit Main Menu", "MenuLastMain");
// Prepare variables for loop
new data[AdminData];
new item[64];
// Loop through all admins
for(new i = 0; i < gNumAdmins; i++) {
// Format admin as the item
formatex(item, charsmax(item), "%s (%s)", data[Admin_Name], gLevels[data[Admin_Level]]);
// Add admin to the menu
menu_additem(menu, item, "");
}
// Show the menu
menu_display(id, menu);
}
public MenuLastMain(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the main menu
ShowMainMenu(id);
}
// An admin was chosen
else {
// Grab which admin was chosen
gMenuAuth = item;
// Show last chosen menu
ShowLastChosenMenu(id);
}
}
ShowLastChosenMenu(id) {
// Create the menu handle
new menu = menu_create("Admins Manager^nLast Visit Main Menu^nView Details", "MenuLastChosen");
// Grab the selected admin
new data[AdminData];
ArrayGetArray(gAdminData, gMenuAuth, data);
// Prepare item variable
new item[64];
// Show the selected admin's name
formatex(item, charsmax(item), "Name: %s", data[Admin_Name]);
menu_additem(menu, item, "");
// Show the selected admin's level
formatex(item, charsmax(item), "Level: %s", gLevels[data[Admin_Level]]);
menu_additem(menu, item, "");
// Show the selected admin's last visit
new l = copy(item, charsmax(item), "Last visit: ");
format_time(item[l], charsmax(item) - l, "%x %X", data[Admin_LastVisit]);
menu_additem(menu, item, "");
// Show the menu
menu_display(id, menu);
}
public MenuLastChosen(id, menu, item) {
// Destroy the menu after it was pressed
menu_destroy(menu);
// Check if the menu was closed
if(item == MENU_EXIT) {
// Show the last main menu
ShowLastMainMenu(id);
}
// Pressed a key
else {
// Show the menu again
ShowLastChosenMenu(id);
}
}
MessageMode(id, command) {
// Show that we want to accept the command's arguments
gMenuAcceptCommand = command;
// Start the messagemode to hook what player types
client_cmd(id, "messagemode %s", gCommands[command]);
}
public CmdMessageMode(id) {
// Check if this is the manager
if(id == gAdminManager) {
// Grab which command was used
new commandName[32];
read_argv(0, commandName, charsmax(commandName));
// Find the command index
for(new i = 0; i < Command; i++) {
// Check if the command matches
if(equali(gCommands[i], commandName)) {
// Check if this command is allowed
if(gMenuAcceptCommand == i) {
// Read the arguments
new args[256];
read_args(args, charsmax(args));
remove_quotes(args);
// Handle the arguments based on what command was used
switch(i) {
// Add Access menu
case Command_Add_Access: {
// Convert what was said to all lowercase
strtolower(args);
// Convert what was said into bits
gMenuAccess = read_flags(args);
// Check if no flags were given
if(!gMenuAccess) {
// Grab default flags
gMenuAccess = GetDefaultFlags();
}
// Show the info menu
ShowAddInfoMenu(id);
}
// Add Password menu
case Command_Add_Password: {
// Check if a password was given
if(args[0]) {
// Store new password
copy(gMenuPassword, charsmax(gMenuPassword), args);
}
// Show the info menu
ShowAddInfoMenu(id);
}
// Edit Name menu
case Command_Edit_Name: {
// Check if a name was given
if(args[0]) {
// Store new name
copy(gMenuData[Admin_Name], charsmax(gMenuData[Admin_Name]), args);
}
// Show the edit menu
ShowEditChosenMenu(id);
}
// Edit Auth menu
case Command_Edit_Auth: {
// Check if a auth was given
if(args[0]) {
// Store new auth
copy(gMenuData[Admin_Auth], charsmax(gMenuData[Admin_Auth]), args);
}
// Show the edit menu
ShowEditChosenMenu(id);
}
// Edit Access menu
case Command_Edit_Access: {
// Convert what was said to all lowercase
strtolower(args);
// Convert what was said into bits
gMenuAccess = read_flags(args);
// Check if no flags were given
if(!gMenuAccess) {
// Grab default flags
gMenuAccess = GetDefaultFlags();
}
// Show the edit menu
ShowEditChosenMenu(id);
}
// Edit Password menu
case Command_Edit_Password: {
// Check if a password was given
if(args[0]) {
// Store new password
copy(gMenuData[Admin_Password], charsmax(gMenuData[Admin_Password]), args);
}
// Show the edit menu
ShowEditChosenMenu(id);
}
}
}
// Don't look for the command anymore
break;
}
}
}
// Block command from showing
return PLUGIN_HANDLED;
}
public CmdKick(id) {
// Kick player from server
server_cmd("kick #%d ^"%L^"", get_user_userid(id), id, "NO_ENTRY");
// Hide command from console
return PLUGIN_HANDLED;
}
LoadAdmins() {
// Try opening the file for reading
new f = fopen(gAdminsFile, "rt");
// Check if the file opened
if(f) {
// Prepare variables for reading
new line[512];
new data[AdminData];
new accessString[27];
new flagString[27];
new levelString[32];
new timestampString[12];
new i;
// Loop through all lines of the file
while(!feof(f)) {
// Read current line
fgets(f, line, charsmax(line));
// Trim any whitespace off the start and end
trim(line);
// Check if this is an empty line or a comment
if(!line[0] || line[0] == ';' || line[0] == '/' && line[1] == '/') {
// Go to next line
continue;
}
// Parse line for all data
parse(line,
data[Admin_Name], charsmax(data[Admin_Name]),
data[Admin_Auth], charsmax(data[Admin_Auth]),
data[Admin_Password], charsmax(data[Admin_Password]),
accessString, charsmax(accessString),
flagString, charsmax(flagString),
levelString, charsmax(levelString),
timestampString, charsmax(timestampString)
);
// Convert access and flag strings to bits
data[Admin_Access] = read_flags(accessString);
data[Admin_Flags] = read_flags(flagString);
// Set that we didn't get the level yet
data[Admin_Level] = -1;
// Find the level index for this level name
for(i = 0; i < sizeof(gLevels); i++) {
// Check if the levels match
if(equali(gLevels[i], levelString)) {
// Store that it was found
data[Admin_Level] = i;
break;
}
}
// Check if level was not found
if(data[Admin_Level] == -1) {
// Don't load this admin
continue;
}
// Convert timestamp to an integer
data[Admin_LastVisit] = str_to_num(timestampString);
// Add admin to list
ArrayPushArray(gAdminData, data);
// Increase number of admins
gNumAdmins++;
}
// Close the file
fclose(f);
}
}
SaveAdmins() {
// Try opening the file for writing
new f = fopen(gAdminsFile, "wt");
// Check if the file opened
if(f) {
// Prepare variables for loop
new data[AdminData], accessString[27], flagString[27];
// Loop through all admins
for(new i = 0; i < gNumAdmins; i++) {
// Grab current admin data
ArrayGetArray(gAdminData, i, data);
// Convert access bits to string
get_flags(data[Admin_Access], accessString, charsmax(accessString));
// Convert flags bits to string
get_flags(data[Admin_Flags],flagString, charsmax(flagString));
// Save data to file
fprintf(f, "^"%s^" ^"%s^" ^"%s^" ^"%s^" ^"%s^" ^"%s^" ^"%d^"^n",
data[Admin_Name], data[Admin_Auth], data[Admin_Password], accessString, flagString, gLevels[data[Admin_Level]], data[Admin_LastVisit]
);
}
// Close the file
fclose(f);
}
}
CheckAllAccess() {
// Get all players that aren't bots or HLTV
new players[32], pnum;
get_players(players, pnum, "ch");
// Loop through all players
for(new i = 0; i < pnum; i++) {
// Check admin for this player
CheckAdmin(players[i]);
}
}
CheckAdmin(id, name[32] = "") {
// Remove any existing flags
remove_user_flags(id);
// Check if no name was passed
if(!name[0]) {
// Grab current name
get_user_name(id, name, charsmax(name));
}
// Set name to not be case sensitive
ClearCaseSensitive(id);
// Grab SteamID and IP as well
new steamID[35], ip[32];
get_user_authid(id, steamID, charsmax(steamID));
get_user_ip(id, ip, charsmax(ip), 1);
// Create variables we need for admin checking
new admin[AdminData];
new found = -1;
// Loop through custom admin list
for(new i = 0; i < gNumAdmins; i++) {
// Grab admin data
ArrayGetArray(gAdminData, i, admin);
// Check if player matches this admin
if(AdminMatch(id, name, steamID, ip, admin)) {
found = i;
break;
}
}
// Check if player was found for any admin at all
if(found >= 0) {
// Check if this requires a password
if(~admin[Admin_Flags] & FLAG_NOPASS) {
// Grab password field and player's password
new field[32], password[32];
get_pcvar_string(gCvarPasswordField, field, charsmax(field));
get_user_info(id, field, password, charsmax(password));
// Check if passwords don't match
if(!equal(admin[Admin_Password], password)) {
// Echo message to player
client_cmd(id, "echo ^"* %L^"", id, "INV_PAS");
// Check if this should kick players
if(admin[Admin_Flags] & FLAG_KICK) {
// Log player was kicked
log_amx("Login: ^"%s<%d><%s><>^" kicked due to invalid password (account ^"%s^") (address ^"%s^")", name, get_user_userid(id), steamID, admin[Admin_Auth], ip);
// Kick player
client_cmd(id, "%s", gKickCommand);
}
// Don't give access
return;
}
// Echo message to player
client_cmd(id, "echo ^"* %L^"", id, "PAS_ACC");
}
// Give player admin access
set_user_flags(id, admin[Admin_Access]);
// Store that the player was here at this time as an admin
admin[Admin_LastVisit] = get_systime();
// Store it in the admins list
ArraySetArray(gAdminData, found, admin);
// Save admins to file
SaveAdmins();
// Grab access as string
new accessString[27];
get_flags(admin[Admin_Access], accessString, charsmax(accessString));
// Log player accessed acount
log_amx("Login: ^"%s<%d><%s><>^" became an admin (account ^"%s^") (access ^"%s^") (address ^"%s^")", name, get_user_userid(id), steamID, admin[Admin_Auth], accessString, ip);
// Echo message to player
client_cmd(id, "echo ^"* %L^"", id, "PRIV_SET");
}
// Check if non-admins should be kicked
else if(get_pcvar_num(gCvarMode) == 2) {
// Kick player
client_cmd(id, "%s", gKickCommand);
}
// Give default flags
else {
// Give player default flags
set_user_flags(id, GetDefaultFlags());
// Echo message to player
client_cmd(id, "echo ^"* %L^"", id, "PRIV_SET");
}
}
bool:AdminMatch(id, const name[], const steamID[], const ip[], const admin[AdminData]) {
// Create variables we need
new temp;
new bool:found = false;
// Check if this is a SteamID
if(admin[Admin_Flags] & FLAG_AUTHID) {
// Check if SteamIDs match
if(equal(steamID, admin[Admin_Auth])) {
// We found the admin
found = true;
}
}
// Check if this is an IP
else if(admin[Admin_Flags] & FLAG_IP) {
// Grab length of ip in list
temp = strlen(admin[Admin_Auth]);
// Check if ends in a '.' for range checks
if(admin[Admin_Auth][temp - 1] != '.') {
// Set length to 0 to match whole string
temp = 0;
}
// Check if ip's match
if(equal(ip, admin[Admin_Auth], temp)) {
// We found the admin
found = true;
}
}
// Check if this is a tag
else if(admin[Admin_Flags] & FLAG_TAG) {
// Cache if this is case sensitive admin name
temp = admin[Admin_Flags] & FLAG_CASE_SENSITIVE;
// Check if tag is in name based on case sensitivity flag from admin list
if(strfind(name, admin[Admin_Auth], !temp) >= 0) {
// Set case sensitive flag if admin list has it
if(temp) {
SetCaseSensitive(id);
}
// We found the admin
found = true;
}
}
// Then this should be an admin name
else {
// Cache if this is case sensitive admin name
temp = admin[Admin_Flags] & FLAG_CASE_SENSITIVE;
// Check if names match based on case sensitivity flag from admin list
if(strcmp(name, admin[Admin_Auth], !temp) == 0) {
// Set case sensitive flag if admin list has it
if(temp) {
SetCaseSensitive(id);
}
// We found the admin
found = true;
}
}
// Return if we found admin
return found;
}
GetDefaultFlags() {
// Grab default flags from the cvar
new flagString[27];
get_pcvar_string(gCvarDefaultAccess, flagString, charsmax(flagString));
// Convert string to bits
new flags = read_flags(flagString);
// Check if no flags exist
if(!flags) {
// Give the user flag
flags = ADMIN_USER;
}
// Give back flags
return flags;
}