Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.34 KB | None | 0 0
  1. /* AMXModX Script
  2. *
  3. * Title: Restrict Names
  4. * Author: Brad Jones
  5. *
  6. * Current Version: 1.2
  7. * Release Date: 2005-09-27
  8. *
  9. * This plugin will check a player's name when they enter the server and also if
  10. * they change their name mid-game. If the player's name matches a regex restriction
  11. * the player will be punished as per the cvar, 'namerest_punishment' or per a
  12. * regex-specific punishment can be acheived by placing it in the restrictnames.ini
  13. * file as in: "regex" "reason" "punishmentType" "punishmentOption". The "punishmentOption"
  14. * field is only needed if "punishmentType" is "0" (rename) or "2" (ban). The value of
  15. * "punishmentOption" should be the new name for a player for a rename punishment or the
  16. * ban length for a ban punishment.
  17. *
  18. * Much appreciated help and some code came from JTP10181.
  19. *
  20. *
  21. * CVARS:
  22. *
  23. * restnames_check_names <0|1> (default: 1)
  24. * A value of 1 will turn this plugin on. A value of 0 will turn it off.
  25. *
  26. * restnames_amxban <0|1> (default: 0)
  27. * Indicates method of banning players.
  28. * 0: ban via 'banid'
  29. * 1: ban via 'amx_ban' (use only if you have the amx_bans plugin installed)
  30. *
  31. * restnames_check_bots <0|1> (default: 1)
  32. * A value of 1 will check bots for name violations. A value of 0 will not check bots.
  33. *
  34. * restnames_log_cnt <0..11> (default: 2)
  35. * Indicates the number of previous month's logs to retain. The current month is always retained.
  36. *
  37. * CONFIG FILE (.\configs\restrictnames.ini):
  38. *
  39. * Each line in the file must be in the following format:
  40. * "regex" "reason" "punishmentType" "punishmentOption"
  41. *
  42. * "regex" is the regex phrase that detects a restricted name.
  43. * "reason" is the text that is displayed to the user to indicate why they're being punished.
  44. * "punishmentType" is the method used to punish a player
  45. * 0 = rename to custom name indicated by punishmentOption
  46. * 1 = kick the player
  47. * 2 = ban the player
  48. * "punishmentOption" is only used if the punishment type is rename or ban.
  49. * - if punishmentType is 0 (rename), punishmentOption should be the new name for the player (if not provided, the player will be kicked)
  50. * - if punishmentType is 2 (ban), punishmentOption should be the ban length in minutes (0 is permanent and the ban is also permanent if not provided)
  51. *
  52. *
  53. * VERSIONS:
  54. *
  55. * 2005-09-30 1.2a - Fixed compilation error on 1.01 and lower versions of AMXX. Thanks to "ohswildcats".
  56. * - Added option to not check immune players for name violations. Thanks to "Hawk552".
  57. *
  58. * 2005-09-27 1.2 - Added log pruning functionality. New CVAR "restnames_log_cnt" indicates how many months
  59. * of previous logs to keep. Only the most recent month to be deleted will be.
  60. * - Fixed issue where a player would be warned of impending punishment and then leave the
  61. * server before punishment commenced. Players are no longer warned. Thanks to JTP10181
  62. * for discovering this issue.
  63. *
  64. * 2005-09-06 1.1 - Will now ban when "restnames_amxban" is set to 1.
  65. * - Separate log file is being created in the .\logs directory detailing punishments
  66. * with more information than before. Punishments were previously being logged in
  67. * the amxx log file.
  68. * - Will not check name if the user is HLTV.
  69. * - CVAR (restnames_check_bots) added to indicate whether to check bots for name violations.
  70. * - Removed CVARs "restnames_ban_time" and "restnames_default_name".
  71. * - All RENAME punishments now have to have the new name provided as
  72. * the "punishmentOption" segment in the config file. If not provided,
  73. * the player will be kicked.
  74. * - All BAN punishments now need to provide the ban length as
  75. * the "punishmentOption" segment in the config file. If not provided,
  76. * the ban is permanent.
  77. *
  78. * 2005-08-29 1.0 Initial release.
  79. */
  80.  
  81.  
  82. /*-----------------------------------------------------------
  83. COMPILER OPTIONS
  84. -----------------------------------------------------------*/
  85. //-----------------------------------------------------------
  86. // Set the flag that indicates if a player has immunity from
  87. // name violation checking, when restnames_immunity is 1.
  88. #define IMMUNITY ADMIN_IMMUNITY
  89. //-----------------------------------------------------------
  90.  
  91.  
  92. #include <amxmodx>
  93. #include <amxmisc>
  94. #include <amxconst>
  95. #include <fun>
  96. #include <regex>
  97.  
  98. new const PLUGIN[] = "Restrict Names"
  99. new const VERSION[] = "1.2a"
  100. new const AUTHOR[] = "Brad Jones"
  101.  
  102. new const FILESTEM[] = "restrictnames"
  103.  
  104. // max number of words in word list
  105. #define MAX_PHRASE 63
  106. // max length of each phrase
  107. #define MAX_PHRASE_LEN 96
  108. // max length of each reason
  109. #define MAX_REASON_LEN 128
  110. // max length of each reason
  111. #define MAX_PUNISH_LEN 6
  112. // max length of new name (if punishment is rename)
  113. #define MAX_NAME_LEN 28
  114. // max length of each config line
  115. #define MAX_CONFIG_LINE_LEN 96 + 128 + 6 + 28 + 2 // MAX_PHRASE_LEN + MAX_REASON_LEN + MAX_PUNISH_LEN + MAX_NAME_LEN + 3 spaces (between phrase/reason, reason/punish, punish/newname)
  116.  
  117. new phrase[MAX_PHRASE][MAX_PHRASE_LEN + 1]
  118. new reason[MAX_PHRASE][MAX_REASON_LEN + 1] // there should be one 'player friendly' reason for each regex
  119. new punish[MAX_PHRASE] = { -1, ... } // each regex has it's own punishment
  120. new punishOption[MAX_PHRASE][MAX_NAME_LEN + 1] // 'rename' punishments should supply a base name for the player
  121.  
  122. new phraseNum = 0
  123.  
  124. new g_cLogFile[32]
  125.  
  126. new bool:g_aPunishQueue[33] // keeps track of punishments so announcement isn't made multiple times
  127.  
  128. public plugin_init()
  129. {
  130. register_plugin(PLUGIN, VERSION, AUTHOR)
  131.  
  132. register_cvar("restnames_debug", "-1")
  133. register_cvar("restnames_version", VERSION, FCVAR_SERVER|FCVAR_SPONLY)
  134.  
  135. register_dictionary("restrictnames.txt")
  136.  
  137. register_cvar("restnames_check_names", "1") // 0=don't check names, 1=check names
  138. register_cvar("restnames_amxban", "0") // 0=ban with 'banid', 1=ban with 'amx_ban'
  139. register_cvar("restnames_check_bots", "1") // 0=don't check bots, 1=check bots for name violations
  140. register_cvar("restnames_log_cnt", "2") // must be set between 1 and 11. indicates how many previous months of log files to retain
  141. register_cvar("restnames_immunity", "0") // 0=all players are checked for violations, 1=everyone except admins
  142.  
  143. //BUGGY: register_concmd("restnames_reloadconfig", "read_config", ADMIN_LEVEL_A, "- reloads the name restriction phrases")
  144.  
  145. maintain_log_files()
  146. read_config()
  147. }
  148.  
  149. public client_putinserver(id)
  150. {
  151. g_aPunishQueue[id] = false
  152. set_task(30.0,"delayed__client_putinserver",id)
  153. }
  154.  
  155. public delayed__client_putinserver(id)
  156. {
  157. new playerName[32]
  158. get_user_name(id, playerName, 31)
  159. return validate_name(id, playerName)
  160. }
  161.  
  162. public client_infochanged(id)
  163. {
  164. new playerName[32]
  165. get_user_info(id, "name", playerName, 31)
  166.  
  167. if (get_user_team(id) > 0) validate_name(id, playerName)
  168.  
  169. return PLUGIN_CONTINUE
  170. }
  171.  
  172. public read_config()
  173. {
  174. // Reset the global variables to make sure we're reading clean.
  175. for (new phraseIdx = 0; phraseIdx < MAX_PHRASE; phraseIdx++ )
  176. {
  177. copy(phrase[phraseIdx], MAX_PHRASE_LEN, "")
  178. copy(reason[phraseIdx], MAX_REASON_LEN, "")
  179. copy(punishOption[phraseIdx], MAX_NAME_LEN, "")
  180. punish[phraseIdx] = -1
  181. }
  182.  
  183. new iLen, line = 0
  184. new filename[128]
  185.  
  186. get_configsdir(filename,128)
  187. format(filename, 127, "%s/%s.ini", filename, FILESTEM)
  188.  
  189. if (file_exists(filename))
  190. {
  191. // Loop through file, reading in each line, stop if we run
  192. // out of space in our phrase list or the file is empty
  193.  
  194. new configLine[MAX_CONFIG_LINE_LEN]
  195. new punishment[MAX_PUNISH_LEN]
  196. // new punishOption[MAX_NAME_LEN]
  197.  
  198. while ((phraseNum < MAX_PHRASE) && (line = read_file(filename, line, configLine, MAX_CONFIG_LINE_LEN, iLen)))
  199. {
  200. if (iLen > 0)
  201. {
  202. parse(configLine, phrase[phraseNum], MAX_PHRASE_LEN - 1, reason[phraseNum], MAX_REASON_LEN, punishment, MAX_PUNISH_LEN, punishOption[phraseNum], MAX_NAME_LEN)
  203.  
  204. punish[phraseNum] = str_to_num(punishment)
  205.  
  206. if (punish[phraseNum] == 0 && strlen(punishOption[phraseNum]) == 0)
  207. punish[phraseNum] = 1
  208.  
  209. ++phraseNum
  210. }
  211. }
  212. log_amx("%L", LANG_SERVER, "CONFIG_LOADED", phraseNum, filename)
  213. }
  214. else
  215. {
  216. log_amx("%L", LANG_SERVER, "CONFIG_LOAD_ERROR", filename)
  217. }
  218. }
  219.  
  220. public maintain_log_files()
  221. {
  222. // get the current month number
  223. new cCurrentMonth[3]
  224. get_time("%m", cCurrentMonth, 2)
  225.  
  226. // set the current log file
  227. format(g_cLogFile, 31, "%s%s.log", FILESTEM, cCurrentMonth, 31)
  228.  
  229. // delete old log file
  230. new iPrevMonthsToKeep = min(max(get_cvar_num("restnames_log_cnt"), 0), 11) // must keep between 0 and 11 months
  231. if (iPrevMonthsToKeep < 11) // always retain the current month
  232. {
  233. new iCurrentMonth = str_to_num(cCurrentMonth)
  234. new iMonthToDelete = constraint_offset(1, 12, iCurrentMonth, (-iPrevMonthsToKeep - 1))
  235. new cLogToDelete[128]
  236. get_localinfo("amxx_logdir", cLogToDelete, 127)
  237.  
  238. if (iMonthToDelete < 10)
  239. format(cLogToDelete, 127, "%s/%s0%d.log", cLogToDelete, FILESTEM, iMonthToDelete)
  240. else
  241. format(cLogToDelete, 127, "%s/%s%d.log", cLogToDelete, FILESTEM, iMonthToDelete)
  242.  
  243. if (file_exists(cLogToDelete)) delete_file(cLogToDelete)
  244. }
  245. }
  246.  
  247. public validate_name(id, playerName[])
  248. {
  249. if (get_cvar_num("restnames_check_names") && !is_user_hltv(id))
  250. {
  251. if (get_cvar_num("restnames_immunity") && get_user_flags(id) & IMMUNITY)
  252. return true
  253.  
  254. if (is_user_bot(id) && !get_cvar_num("restnames_check_bots"))
  255. return true
  256.  
  257. if (g_aPunishQueue[id])
  258. return false
  259.  
  260. new phraseIdx
  261. new matchingSegment[32]
  262. if (is_name_bad(playerName, phraseIdx, matchingSegment))
  263. {
  264. g_aPunishQueue[id] = true
  265.  
  266. // Punish the player.
  267. switch(punish[phraseIdx])
  268. {
  269. case 0: rename_player(id, reason[phraseIdx], punishOption[phraseIdx])
  270. case 1: kick_player(id, reason[phraseIdx])
  271. case 2: ban_player(id, reason[phraseIdx], punishOption[phraseIdx])
  272. }
  273.  
  274. // If the player was renamed, give personal notification of reason.
  275. if (punish[phraseIdx] == 0)
  276. {
  277. set_hudmessage(255,255,110,-1.0,0.35,0,3.0,5.0,0.1,0.1, 2)
  278. show_hudmessage(id, "%L", id, "RENAME_NOTIFY_PLAYER", reason[phraseIdx])
  279. }
  280.  
  281. // Log the transgression.
  282. switch (punish[phraseIdx])
  283. {
  284. case 0: log_to_file(g_cLogFile, "%L", LANG_SERVER, "RENAME_LOG_ENTRY", playerName, matchingSegment, phraseIdx + 1, phrase[phraseIdx], reason[phraseIdx])
  285. case 1: log_to_file(g_cLogFile, "%L", LANG_SERVER, "KICK_LOG_ENTRY", playerName, matchingSegment, phraseIdx + 1, phrase[phraseIdx], reason[phraseIdx])
  286. case 2: log_to_file(g_cLogFile, "%L", LANG_SERVER, "BAN_LOG_ENTRY", playerName, matchingSegment, phraseIdx + 1, phrase[phraseIdx], reason[phraseIdx])
  287. }
  288.  
  289. // Notify the masses that there was a scofflaw in our midst.
  290. switch (punish[phraseIdx])
  291. {
  292. case 0: client_print(0, print_chat, "%L", LANG_PLAYER, "RENAME_NOTIFY_ALL", playerName, reason[phraseIdx])
  293. case 1: client_print(0, print_chat, "%L", LANG_PLAYER, "KICK_NOTIFY_ALL", playerName, reason[phraseIdx])
  294. case 2: client_print(0, print_chat, "%L", LANG_PLAYER, "BAN_NOTIFY_ALL", playerName, reason[phraseIdx])
  295. }
  296.  
  297. g_aPunishQueue[id] = false
  298.  
  299. return false
  300. }
  301. }
  302. return true
  303. }
  304.  
  305. public is_name_bad(const playerName[], &phraseIdx, matchingSegment[])
  306. {
  307. new Regex:regexid
  308. new num, error[5], lplayerName[32]
  309.  
  310. copy(lplayerName, 31, playerName)
  311. strtolower(lplayerName)
  312.  
  313. for (new i = 0 ; i < phraseNum ; i++)
  314. {
  315. regexid = regex_match(lplayerName, phrase[i], num, error, 4)
  316. if ( (regexid >= REGEX_OK) )
  317. {
  318. regex_substr(regexid, 0, matchingSegment, 31)
  319. phraseIdx = i
  320. regex_free(regexid)
  321. return true
  322. }
  323. }
  324. return false
  325. }
  326.  
  327. public rename_player(id, reasonMatch[], newName[])
  328. {
  329. new name[33]
  330. format(name, 32, "%s (%i)", newName, id)
  331.  
  332. if (is_user_alive(id))
  333. // client_cmd(id, "name ^"%s^"", name)
  334. set_user_info(id, "name", name)
  335. else if (is_user_connected(id))
  336. {
  337. spawn(id)
  338. // client_cmd(id, "name ^"%s^"", name)
  339. set_user_info(id, "name", name)
  340. user_kill(id, 1)
  341. }
  342. }
  343.  
  344. public kick_player(id, reasonMatch[])
  345. {
  346. new userid = get_user_userid(id)
  347. server_cmd("kick #%d %L", userid, id, "KICK_REASON", reasonMatch)
  348. }
  349.  
  350. public ban_player(id, reasonMatch[], banLengthStr[])
  351. {
  352. new userid = get_user_userid(id)
  353. new banLength = str_to_num(banLengthStr)
  354. new banReason[256]
  355.  
  356. format(banReason, 255, "%L", id, "BAN_REASON", reasonMatch)
  357.  
  358. if(!get_cvar_num("restnames_amxban"))
  359. {
  360. server_cmd("banid %i #%i;writeid", banLength, userid)
  361. server_cmd("kick #%i %s", userid, banReason)
  362. }
  363. else
  364. {
  365. new authid[32]
  366. get_user_authid(id, authid, 31)
  367. server_cmd("amx_ban %i %s %s", banLength, authid, banReason)
  368. }
  369. }
  370.  
  371. #if !defined AMXX_VERSION_NUM
  372. public abs(x)
  373. {
  374. return x>0 ? x : -x;
  375. }
  376.  
  377. public constraint_offset(low, high, seed, offset)
  378. {
  379. new numElements = high - low + 1;
  380. offset += seed - low;
  381.  
  382. if (offset >= 0)
  383. return low + (offset % numElements);
  384. else
  385. return high - (abs(offset) % numElements) + 1;
  386. return 0; // Makes the compiler happy -_-
  387. }
  388. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement