ban.inc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <YSI_Coding\y_hooks>
  2. forward OnPlayerPassedBanCheck(playerid);
  3. Ban_CheckUser(playerid)
  4. {
  5. inline OnPlayerBanLoad()
  6. {
  7. if(cache_num_rows())
  8. {
  9. new expired;
  10. new banName[MAX_PLAYER_NAME];
  11. cache_get_value_name_int(0, "expired", expired);
  12. cache_get_value_name(0, "name", banName);
  13. if(expired <= 0)
  14. {
  15. Ban_RemoveUser(INVALID_PLAYER_ID, banName);
  16. CallRemoteFunction("OnPlayerPassedBanCheck", "i", playerid);
  17. }
  18. else
  19. {
  20. new banIP[MAX_PLAYER_IP];
  21. new banAdmin[MAX_PLAYER_NAME];
  22. new banReason[MAX_BAN_REASON];
  23. new banDate[40];
  24. new unbanDate[40];
  25. cache_get_value_name(0, "ip", banIP);
  26. cache_get_value_name(0, "admin", banAdmin);
  27. cache_get_value_name(0, "reason", banReason);
  28. cache_get_value_name(0, "date_ban", banDate);
  29. cache_get_value_name(0, "date_unban", unbanDate);
  30. SendClientMessage(playerid, 0xff6666ff, "You're banned from this server.");
  31. va_SendClientMessage(playerid, 0xff6666ff, "Name: %s, Banned by: %s, Reason: %s", banName, banAdmin, banReason);
  32. va_SendClientMessage(playerid, 0xff6666ff, "Ban Date: %s, Unban Date: %s", banDate, unbanDate);
  33. Kick(playerid);
  34. }
  35. }
  36. else CallRemoteFunction("OnPlayerPassedBanCheck", "i", playerid);
  37. }
  38. MySQL_TQueryInline(MySQL_GetHandle(), using inline OnPlayerBanLoad, "SELECT *, DATE_FORMAT(ban_date, '%%%%d %%%%M %%%%Y at %%%%r') AS date_ban, DATE_FORMAT(unban_date, '%%%%d %%%%M %%%%Y at %%%%r') AS date_unban, TIMESTAMPDIFF(SECOND, CURRENT_TIMESTAMP(), unban_date) AS expired FROM bans WHERE name = '%e' OR ip = '%e' LIMIT 1", Account_GetName(playerid), Account_GetIP(playerid));
  39. }
  40. Ban_AddUser(byid, bannedid, const reason[], intervalType, banDuration)
  41. {
  42. new interval[6];
  43. switch(intervalType)
  44. {
  45. case 0: StrCpy(interval, "DAY", sizeof(interval));
  46. case 1: StrCpy(interval, "MONTH", sizeof(interval));
  47. case 2: StrCpy(interval, "YEAR", sizeof(interval));
  48. }
  49. new adminName[MAX_PLAYER_NAME];
  50. if(byid == INVALID_PLAYER_ID)
  51. {
  52. StrCpy(adminName, "System", sizeof(adminName));
  53. }
  54. else StrCpy(adminName, Account_GetName(byid), sizeof(adminName));
  55. new query[512];
  56. mysql_format(MySQL_GetHandle(), query, sizeof(query), "INSERT INTO bans (name, ip, gpci, admin, reason, ban_date, unban_date) VALUES ('%e', '%e', '%e', '%e', '%e', CURRENT_TIMESTAMP(), DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL %i %s))", Account_GetName(bannedid), Account_GetIP(bannedid), Account_GetGPCI(bannedid), adminName, reason, banDuration, interval);
  57. mysql_tquery(MySQL_GetHandle(), query);
  58. Kick(bannedid);
  59. }
  60. Ban_AddOfflineUser(byid, const banned[], const reason[], intervalType, banDuration)
  61. {
  62. new bannedName[MAX_PLAYER_NAME];
  63. new banReason[MAX_BAN_REASON];
  64. new bannerName[MAX_PLAYER_NAME];
  65. StrCpy(bannedName, banned, sizeof(bannedName));
  66. StrCpy(banReason, reason, sizeof(banReason));
  67. StrCpy(bannerName, Account_GetName(byid), sizeof(bannerName));
  68. inline OnPlayerOffBan()
  69. {
  70. if(cache_num_rows())
  71. {
  72. new adminLevel;
  73. new playerIP[MAX_PLAYER_IP];
  74. new playerGPCI[MAX_PLAYER_GPCI];
  75. cache_get_value_name_int(0, "admin", adminLevel);
  76. cache_get_value_name(0, "ip", playerIP);
  77. cache_get_value_name(0, "gpci", playerGPCI);
  78. if(adminLevel >= Admin_GetLevel(byid))
  79. {
  80. return SendErrorMessageF(byid, "You can't use this command on %s.", bannedName);
  81. }
  82. new interval[6];
  83. new durationStr[8];
  84. switch(intervalType)
  85. {
  86. case 0: StrCpy(interval, "DAY", sizeof(interval)), durationStr = "days";
  87. case 1: StrCpy(interval, "MONTH", sizeof(interval)), durationStr = "months";
  88. case 2: StrCpy(interval, "YEAR", sizeof(interval)), durationStr = "years";
  89. }
  90. new query[512];
  91. mysql_format(MySQL_GetHandle(), query, sizeof(query), "INSERT INTO bans (name, ip, gpci, reason, admin, ban_date, unban_date) VALUES ('%e', '%e', '%e', '%e', '%e', CURRENT_TIMESTAMP(), DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL %i %s))", bannedName, playerIP, playerGPCI, banReason, bannerName, banDuration, interval);
  92. mysql_tquery(MySQL_GetHandle(), query);
  93. if(strlen(banReason) > 20)
  94. {
  95. SendAdmCmdF("%s was banned (offline) by %s for %d %s, reason: %.20s ...", bannedName, bannerName, banDuration, durationStr, banReason);
  96. SendAdmCmdF("... %s", banReason[20]);
  97. }
  98. else SendAdmCmdF("%s was banned (offline) by %s for %d %s, reason: %s", bannedName, bannerName, banDuration, durationStr, banReason);
  99. }
  100. else SendErrorMessageF(byid, "%s could not be found in the database.", bannedName);
  101. }
  102. MySQL_TQueryInline(MySQL_GetHandle(), using inline OnPlayerOffBan, "SELECT * FROM accounts WHERE name = '%e'", bannedName);
  103. return 1;
  104. }
  105. Ban_RemoveUser(playerid, const banned[])
  106. {
  107. new bannedName[MAX_PLAYER_NAME];
  108. new unbannerName[MAX_PLAYER_NAME];
  109. StrCpy(bannedName, banned, sizeof(bannedName));
  110. StrCpy(unbannerName, Account_GetName(playerid), sizeof(unbannerName));
  111. inline OnPlayerRemoveBan()
  112. {
  113. if(cache_affected_rows())
  114. {
  115. if(playerid != INVALID_PLAYER_ID) SendAdmCmdF("%s unbanned %s.", unbannerName, bannedName);
  116. }
  117. else SendErrorMessageF(playerid, "%s could not be found in the database.", bannedName);
  118. }
  119. MySQL_TQueryInline(MySQL_GetHandle(), using inline OnPlayerRemoveBan, "DELETE FROM bans WHERE name = '%e'", bannedName);
  120. }
  121. CMD:ban(playerid, params[])
  122. {
  123. if(isnull(params)) return SendSyntaxMessage(playerid, "/ban (options: d(ay), m(onth), y(ear))");
  124. new targetid;
  125. new duration;
  126. new interval;
  127. new reason[MAX_BAN_REASON];
  128. if(!strcmp(params, "d", true, 1))
  129. {
  130. if(sscanf(params, "{s[2]}iuS(Unspecified)[128]", duration, targetid, reason)) return SendSyntaxMessage(playerid, "/ban d (duration) (playerid) (reason)");
  131. interval = 0;
  132. }
  133. else if(!strcmp(params, "m", true, 1))
  134. {
  135. if(sscanf(params, "{s[2]}iuS(Unspecified)[128]", duration, targetid, reason)) return SendSyntaxMessage(playerid, "/ban m (duration) (playerid) (reason)");
  136. interval = 1;
  137. }
  138. else if(!strcmp(params, "y", true, 1))
  139. {
  140. if(sscanf(params, "{s[2]}iuS(Unspecified)[128]", duration, targetid, reason)) return SendSyntaxMessage(playerid, "/ban y (duration) (playerid) (reason)");
  141. interval = 2;
  142. }
  143. else return SendSyntaxMessage(playerid, "/ban (options: d(ay), m(onth), y(ear))");
  144. if(targetid == playerid) return SendErrorMessage(playerid, "You can't ban yourself.");
  145. if(duration < 1 || duration > 1000) return SendErrorMessage(playerid, "Invalid duration, must be between 1 and 1000.");
  146. if(!IsPlayerConnected(targetid)) return SendErrorMessage(playerid, "You specified an invalid player.");
  147. if(Admin_GetLevel(targetid) >= Admin_GetLevel(playerid)) return SendErrorMessageF(playerid, "You can't use this command on %s.", Account_GetName(targetid));
  148. if(strlen(reason) > MAX_BAN_REASON - 1) return SendErrorMessageF(playerid, "Invalid reason length, maximum characters is %d.", MAX_BAN_REASON - 1);
  149. new durationStr[8];
  150. switch(interval)
  151. {
  152. case 0: durationStr = "days";
  153. case 1: durationStr = "months";
  154. case 2: durationStr = "years";
  155. }
  156. if(strlen(reason) > 20)
  157. {
  158. SendAdmCmdF("%s was banned by %s for %d %s, reason: %.20s ...", Account_GetName(targetid), Account_GetName(playerid), duration, durationStr, reason);
  159. SendAdmCmdF("... %s", reason[20]);
  160. }
  161. else SendAdmCmdF("%s was banned by %s for %d %s, reason: %s", Account_GetName(targetid), Account_GetName(playerid), duration, durationStr, reason);
  162. Ban_AddUser(playerid, targetid, reason, interval, duration);
  163. return 1;
  164. }
  165. CMD:offban(playerid, params[])
  166. {
  167. if(isnull(params) || IsNumeric(params)) return SendSyntaxMessage(playerid, "/offban (options: d(ay), m(onth), y(ear))");
  168. new duration;
  169. new interval;
  170. new targetName[MAX_PLAYER_NAME];
  171. new reason[MAX_BAN_REASON];
  172. if(!strcmp(params, "d", true, 1))
  173. {
  174. if(sscanf(params, "{s[2]}is[24]S(Unspecified)[128]", duration, targetName, reason)) return SendSyntaxMessage(playerid, "/offban d (days) (name) (reason)");
  175. interval = 0;
  176. }
  177. else if(!strcmp(params, "m", true, 1))
  178. {
  179. if(sscanf(params, "{s[2]}is[24]S(Unspecified)[128]", duration, targetName, reason)) return SendSyntaxMessage(playerid, "/offban m (months) (name) (reason)");
  180. interval = 1;
  181. }
  182. else if(!strcmp(params, "y", true, 1))
  183. {
  184. if(sscanf(params, "{s[2]}is[24]S(Unspecified)[128]", duration, targetName, reason)) return SendSyntaxMessage(playerid, "/offban y (years) (name) (reason)");
  185. interval = 2;
  186. }
  187. else return SendSyntaxMessage(playerid, "/offban (options: d(ay), m(onth), y(ear))");
  188. if(duration < 1 || duration > 1000) return SendErrorMessage(playerid, "Invalid duration, must be between 1 and 1000.");
  189. if(strlen(reason) > MAX_BAN_REASON - 1) return SendErrorMessageF(playerid, "Invalid reason length, maximum characters is %d.", MAX_BAN_REASON - 1);
  190. foreach(new i : Player) if(!strcmp(targetName, Account_GetName(i)))
  191. {
  192. return SendErrorMessage(playerid, "That player is connected, use /ban insetead.");
  193. }
  194. Ban_AddOfflineUser(playerid, targetName, reason, interval, duration);
  195. return 1;
  196. }
  197. CMD:unban(playerid, params[])
  198. {
  199. if(isnull(params) || strval(params)) return SendSyntaxMessage(playerid, "/unban (name)");
  200. if(strlen(params) > MAX_PLAYER_NAME) return SendErrorMessageF(playerid, "Invalid name length, maximum characters is %d.", MAX_PLAYER_NAME);
  201. Ban_RemoveUser(playerid, params);
  202. return 1;
  203. }