Guest User

sbBan System

a guest
Oct 28th, 2015
1,414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.00 KB | None | 0 0
  1. /*
  2.                  _    ______
  3.                 | |   | ___ \
  4.              ___| |__ | |_/ / __ _ _ __
  5.             / __| '_ \| ___ \/ _` | '_ \
  6.             \__ \ |_) | |_/ / (_| | | | |
  7.             |___/_.__/\____/ \__,_|_| |_|
  8.             Version 1.0 - SecretBoss - sbBan
  9.             MySQL Ban System - Easy-Mysql.inc
  10.            
  11.             Special thanks to ThePhenix for easy-mysql include
  12.             Also to Gammix for GAdmin (examples)
  13.            
  14.             • Features
  15.                 With sbBan system you can prevent hackers rulebreaker
  16.                 from joining your server, you can give them temp bans,
  17.                 account bans and ip bans, when you ban a player his IP
  18.                 will also get banned and when he joins it will appear
  19.                 like he is banned, you don't need to import tables
  20.                 Just edit configuration to your own and recompile the
  21.                 script
  22.                
  23.             • About
  24.                 I made that system for sbAdmin but sbAdmin will may late
  25.                 to be released so I decided to release it alone (Ban System)
  26.                 When I will update something on sbAdmin's ban system I will
  27.                 also update it here so you can use it on your own
  28.                
  29.             • Bugs
  30.                 I tested this system on sbAdmin local server and it worked
  31.                 if you find any bugs please report them and I will fix them
  32.                 as soon as possible thanks...
  33. */
  34.  
  35. //================================================
  36.  
  37. #include <a_samp>                                   // SAMP Team
  38. #include <easy-mysql>                               // ThePhenix
  39. #include <izcmd>                                    // Yashas and Zeex
  40. #include <sscanf2>                                  // Emmet_
  41. #include <timestamptodate>
  42.  
  43. //================================================
  44.  
  45. // Configuration
  46. #define BANS_TABLE          "bans"                  // Table where all Bans will be saved
  47.  
  48. #define MYSQL_HOST          "localhost"             // Your MySQL's Host
  49. #define MYSQL_USER          "root"                  // Your MySQL's Username
  50. #define MYSQL_DATABASE      "sbBan"                 // Your MySQL's Database
  51. #define MYSQL_PASSWORD      ""                      // Your MySQL's Password
  52.  
  53. //================================================
  54.  
  55. #define COLOR_RED           0xFF0000FF              // Color to be used on messages
  56. #define DIALOG_SBBAN        1024                    // Random Number
  57.  
  58. //================================================
  59.  
  60. public OnFilterScriptInit()
  61. {
  62.     print("\n\n_________________________________________________________________");
  63.     print("                        sbBan                                      \n");
  64.     print("[sbBan]: Attempting to load sbBan system                             ");
  65.     SQL::Connect(MYSQL_HOST, MYSQL_USER, MYSQL_DATABASE, MYSQL_PASSWORD);
  66.  
  67.     if(!SQL::ExistsTable(""BANS_TABLE""))
  68.     {
  69.         new handle = SQL::Open(SQL::CREATE, ""BANS_TABLE"");
  70.         SQL::AddTableEntry(handle, "ban_id", SQL_TYPE_INT, 11, true);
  71.         SQL::AddTableEntry(handle, "ban_username", SQL_TYPE_VCHAR, 24);
  72.         SQL::AddTableEntry(handle, "ban_ip", SQL_TYPE_VCHAR, 24);
  73.         SQL::AddTableEntry(handle, "ban_by", SQL_TYPE_VCHAR, 24);
  74.         SQL::AddTableEntry(handle, "ban_on", SQL_TYPE_VCHAR, 24);
  75.         SQL::AddTableEntry(handle, "ban_reason", SQL_TYPE_VCHAR, 24);
  76.         SQL::AddTableEntry(handle, "ban_expire", SQL_TYPE_INT);
  77.         SQL::Close(handle);
  78.  
  79.         print("[sbBan]: '"BANS_TABLE"' table created successfully!");
  80.     }
  81.     else if(SQL::ExistsTable(""BANS_TABLE""))
  82.     {
  83.         printf("[sbBan]: Total %d bans loaded from '"BANS_TABLE"'", SQL::CountRows(""BANS_TABLE""));
  84.     }
  85.  
  86.     print("[sbBan]: sbBan Ban system loaded successfully                        ");
  87.     print("[sbBan]: Version: 1.0 | Author: SecretBoss                           ");
  88.     print("\n                       sbBan                                       ");
  89.     print("_________________________________________________________________\n\n");
  90.     return 1;
  91. }
  92.  
  93.  
  94. //================================================
  95. public OnPlayerConnect(playerid)
  96. {
  97.     new
  98.         string[6][24],
  99.         string2[156],
  100.         expire,
  101.         DIALOG[676]
  102.     ;
  103.  
  104.     if(SQL::RowExistsEx(""BANS_TABLE"", "ban_username", GetName(playerid)))
  105.     {
  106.         new handle = SQL::OpenEx(SQL::READ, ""BANS_TABLE"", "ban_username", GetName(playerid));
  107.         SQL::ReadString(handle, "ban_username", string[1], 24);
  108.         SQL::ReadString(handle, "ban_ip", string[2], 24);
  109.         SQL::ReadString(handle, "ban_by", string[3], 24);
  110.         SQL::ReadString(handle, "ban_on", string[4], 24);
  111.         SQL::ReadString(handle, "ban_reason", string[5], 24);
  112.         SQL::ReadInt(handle, "ban_expire", expire);
  113.         SQL::Close(handle);
  114.  
  115.         if(expire > gettime() || expire == 0)
  116.         {
  117.  
  118.             strcat(DIALOG, "{FFFFFF}Your account is banned from this server,\n\n");
  119.  
  120.             format(string2, sizeof(string2), "{FFFFFF}Username: {FF0000}%s\n", string[1]);
  121.             strcat(DIALOG, string2);
  122.  
  123.             format(string2, sizeof(string2), "{FFFFFF}IP: {FF0000}%s\n", string[2]);
  124.             strcat(DIALOG, string2);
  125.  
  126.             format(string2, sizeof(string2), "{FFFFFF}Banned by: {FF0000}%s\n", string[3]);
  127.             strcat(DIALOG, string2);
  128.  
  129.             format(string2, sizeof(string2), "{FFFFFF}Reason: {FF0000}%s\n", string[5]);
  130.             strcat(DIALOG, string2);
  131.  
  132.             format(string2, sizeof(string2), "{FFFFFF}Ban date: {FF0000}%s\n", string[4]);
  133.             strcat(DIALOG, string2);
  134.  
  135.             new expire2[68];
  136.             if(expire == 0) expire2 = "PERMANENT";
  137.             else expire2 = ConvertTime(expire);
  138.             format(string2, sizeof(string2), "{FFFFFF}Timeleft: {FF0000}%s\n\n", expire2);
  139.             strcat(DIALOG, string2);
  140.  
  141.             strcat(DIALOG, "{FFFFFF}If you think that you got banned wrongfully, please make an appeal on our forums.\n");
  142.             strcat(DIALOG, "Make sure you saved this box by pressing F8.");
  143.  
  144.             ShowPlayerDialog(playerid, DIALOG_SBBAN, DIALOG_STYLE_MSGBOX, "Notice", DIALOG, "Close", "");
  145.             DelayKick(playerid);
  146.  
  147.             return true;
  148.         }
  149.         else
  150.         {
  151.             SQL::DeleteRowEx(""BANS_TABLE"", "ban_username", GetName(playerid));
  152.             SendClientMessage(playerid, COLOR_RED, "[SERVER] Your accounts's ban has expired.");
  153.         }
  154.     }
  155.  
  156.     else if(SQL::RowExistsEx(""BANS_TABLE"", "ban_ip", GetIP(playerid)))
  157.     {
  158.         new handle = SQL::OpenEx(SQL::READ, ""BANS_TABLE"", "ban_ip", GetIP(playerid));
  159.         SQL::ReadString(handle, "ban_username", string[1], 24);
  160.         SQL::ReadString(handle, "ban_ip", string[2], 24);
  161.         SQL::ReadString(handle, "ban_by", string[3], 24);
  162.         SQL::ReadString(handle, "ban_on", string[4], 24);
  163.         SQL::ReadString(handle, "ban_reason", string[5], 24);
  164.         SQL::ReadInt(handle, "ban_expire", expire);
  165.         SQL::Close(handle);
  166.  
  167.         if(expire > gettime() || expire == 0)
  168.         {
  169.  
  170.             strcat(DIALOG, "{FFFFFF}Your IP is banned from this server,\n\n");
  171.  
  172.             format(string2, sizeof(string2), "{FFFFFF}Username: {FF0000}%s\n", string[1]);
  173.             strcat(DIALOG, string2);
  174.  
  175.             format(string2, sizeof(string2), "{FFFFFF}IP: {FF0000}%s\n", string[2]);
  176.             strcat(DIALOG, string2);
  177.  
  178.             format(string2, sizeof(string2), "{FFFFFF}Banned by: {FF0000}%s\n", string[3]);
  179.             strcat(DIALOG, string2);
  180.  
  181.             format(string2, sizeof(string2), "{FFFFFF}Reason: {FF0000}%s\n", string[5]);
  182.             strcat(DIALOG, string2);
  183.  
  184.             format(string2, sizeof(string2), "{FFFFFF}Ban date: {FF0000}%s\n", string[4]);
  185.             strcat(DIALOG, string2);
  186.  
  187.             new expire2[68];
  188.             if(expire == 0) expire2 = "PERMANENT";
  189.             else expire2 = ConvertTime(expire);
  190.             format(string2, sizeof(string2), "{FFFFFF}Timeleft: {FF0000}%s\n\n", expire2);
  191.             strcat(DIALOG, string2);
  192.  
  193.             strcat(DIALOG, "{FFFFFF}If you think that you got banned wrongfully, please make an appeal on our forums.\n");
  194.             strcat(DIALOG, "Make sure you saved this box by pressing F8.");
  195.  
  196.             ShowPlayerDialog(playerid, DIALOG_SBBAN, DIALOG_STYLE_MSGBOX, "Notice", DIALOG, "Close", "");
  197.             DelayKick(playerid);
  198.  
  199.             return true;
  200.         }
  201.         else
  202.         {
  203.             SQL::DeleteRowEx(""BANS_TABLE"", "ban_username", GetName(playerid));
  204.             SendClientMessage(playerid, COLOR_RED, "[SERVER] Your IP's ban has expired.");
  205.         }
  206.     }
  207.     return 1;
  208. }
  209.  
  210. //================================================
  211.  
  212. CMD:ban(playerid, params[])
  213. {
  214.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, COLOR_RED, "You are not Administrator.");
  215.     new target, reason[35], days;
  216.     if(sscanf(params, "is[35]I(0)", target, reason, days)) return SendClientMessage(playerid, COLOR_RED, "Usage: /ban [playerid] [reason] [days(0 for permanent ban)]");
  217.     if(!IsPlayerConnected(target)) return SendClientMessage(playerid, COLOR_RED, "The specified player is not conected, use /oban instead.");
  218.     if(target == playerid) return SendClientMessage(playerid, COLOR_RED, "You can't ban yourself.");
  219.     if(IsPlayerAdmin(target)) return SendClientMessage(playerid, COLOR_RED, "You cannot use this command on higher level admin.");
  220.     if(days < 0) return SendClientMessage(playerid, COLOR_RED, "Invalid days, must be greater than 0 for temp ban, or 0 for permanent ban.");
  221.     if(strlen(reason) < 3 || strlen(reason) > 35) return SendClientMessage(playerid, COLOR_RED, "Invalid reason length, must be b/w 0-35 characters.");
  222.  
  223.     new bandate[18], date[3], time;
  224.     getdate(date[0], date[1], date[2]);
  225.     format(bandate, sizeof(bandate), "%02i/%02i/%i", date[2], date[1], date[0]);
  226.  
  227.     if(days == 0) time = 0;
  228.     else time = ((days * 24 * 60 * 60) + gettime());
  229.  
  230.     new handle = SQL::Open(SQL::INSERT, ""BANS_TABLE"");
  231.  
  232.     SQL::ToggleAutoIncrement(handle, true);
  233.     SQL::WriteString(handle, "ban_username", GetName(target));
  234.     SQL::WriteString(handle, "ban_ip", GetIP(target));
  235.     SQL::WriteString(handle, "ban_by", GetName(playerid));
  236.     SQL::WriteString(handle, "ban_on", bandate);
  237.     SQL::WriteString(handle, "ban_reason", reason);
  238.     SQL::WriteInt(handle, "ban_expire", time);
  239.  
  240.     SQL::Close(handle);
  241.  
  242.     if(days == 0)
  243.     {
  244.         new string[144];
  245.         format(string, sizeof(string), "* %s(%i) has been banned by Admin %s(%d) [Reason: %s]", GetName(target), target, GetName(playerid), playerid, reason);
  246.         SendClientMessage(target, COLOR_RED, string);
  247.     }
  248.     else
  249.     {
  250.         new string[258];
  251.         format(string, sizeof(string), "* %s(%i) has been temp banned by Admin %s(%d) [Reason: %s] [Days: %i]", GetName(target), target, GetName(playerid), playerid, reason, days);
  252.         SendClientMessage(target, COLOR_RED, string);
  253.         format(string, sizeof(string), "* Temp banned for %i days [Unban on %s]", days, ConvertTime(time));
  254.         SendClientMessage(target, COLOR_RED, string);
  255.     }
  256.  
  257.     DelayKick(target);
  258.     return 1;
  259. }
  260.  
  261. //================================================
  262.  
  263. COMMAND:unban(playerid, params[])
  264. {
  265.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, COLOR_RED, "You are not Administrator.");
  266.     new name[24];
  267.     if(sscanf(params,"s[24]", name)) return SendClientMessage(playerid, COLOR_RED, "Usage: /unban [username]");
  268.  
  269.     if(SQL::RowExistsEx(""BANS_TABLE"", "ban_username", name))
  270.     {
  271.        SQL::DeleteRowEx(""BANS_TABLE"", "ban_username", name);
  272.     }
  273.     else return SendClientMessage(playerid, COLOR_RED, "The specified username is not banned.");
  274.  
  275.     new string[144];
  276.     format(string, sizeof(string), "* You have unbanned user %s successfully.", name);
  277.     SendClientMessage(playerid, COLOR_RED, string);
  278.     return 1;
  279. }
  280.  
  281. //================================================
  282.  
  283. GetName(playerid)
  284. {
  285.     new name[24];
  286.     GetPlayerName(playerid, name, sizeof(name));
  287.     return name;
  288. }
  289.  
  290. //================================================
  291.  
  292. GetIP(playerid)
  293. {
  294.     new p_ip[18];
  295.     GetPlayerIp(playerid, p_ip, sizeof(p_ip));
  296.     return p_ip;
  297. }
  298.  
  299. //================================================
  300.  
  301. ConvertTime(time)
  302. {
  303.     new string[68];
  304.     new values[6];
  305.     TimestampToDate(time, values[0], values[1], values[2], values[3], values[4], values[5], 0, 0);
  306.     format(string, sizeof(string), "%i.%i.%i (%i hours %i mins %i secs)", values[0], values[1], values[2], values[3], values[4], values[5]);
  307.     return string;
  308. }
  309.  
  310. //================================================
  311.  
  312. DelayKick(playerid)
  313. {
  314.     SetTimerEx("OnPlayerKicked", 2000, false, "i", playerid);
  315.     return 1;
  316. }
  317.  
  318. forward OnPlayerKicked(playerid);
  319. public OnPlayerKicked(playerid)
  320. {
  321.     Kick(playerid);
  322.     return 1;
  323. }
  324.  
  325. //================================================
Advertisement
Add Comment
Please, Sign In to add comment