Raised This Month: $ Target: $400
 0% 

[HELP] I have a problem with regex in my plugin.


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
unSeen
Member
Join Date: Sep 2008
Old 04-04-2009 , 17:48   [HELP] I have a problem with regex in my plugin.
Reply With Quote #1

Hey everyone..
I've made a plugin that censor a words that written in a text file.
The code compiles and everything is allright, but when i test it, and write in the chat for example: "i love my people" and it not doing nothing.

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <regex>
 
#define PLUGIN "Flame Detector"
#define VERSION "1.0"
#define AUTHOR "unSeen"
#define STRING_OFFSET 1  // Define the len of the offset string.
#define LEN_CFGDIR 127  // Define the len of the configs folder path.
#define LEN_TOTAL 255  // Define the total len of the configs path + filename path.
#define LEN_DATA 127  // Define the len of the TXT file data.
#define LEN_TEXT 192  // Define the user text len in chat.
#define LEN_ERROR  127  // Define the error max len.
#define TXT_FILENAME  "badwords.txt" // Define the words list TXT file name.
#define MAX_WORDS 32  // Define the max bad words wich can be in the file.
#define WORDS_OFFSET  1  // Define the bad words offset.
#define NULL   0  // Define the null char.
#define CVAR_IGNORE 0  // Define the ignore cvar.
#define CVAR_CEN 1  // Define the censor cvar.
#define CVAR_KICK 2  // Define the kick cvar.
new g_arrszWords[MAX_WORDS WORDS_OFFSET]
new 
g_pcvFD
public plugin_init() {
 
// Register the plugin - name, version and author.
 
register_plugin(PLUGINVERSIONAUTHOR)
 
 
// Register cvar num. 0|1|2. (0 nothing, 1 censor the message, 2 kick the player)
 
g_pcvFD register_cvar("fd_cvar""1")
 
 
// When some one say something in the server-Chat.
 
register_clcmd("say""FlameHandler")
 
 
// When some one say something in the server-teamChat.
 
register_clcmd("say_team""FlameHandler")
 
 
// Loads the blocked words from a TXT file.
 
LoadWords()
}
LoadWords()
{
 
// Reset the array.
 
for (new 0MAX_WORDSi++)
 {
  
g_arrszWords[i] = NULL
 
}
 
 
// Variable definize
 
new cfgDir[LEN_CFGDIR STRING_OFFSET]
 new 
dirFile[LEN_TOTAL STRING_OFFSET]
 new 
fh
 
 
// Code section.
 // Gets the CFG directory.
 
get_configsdir(cfgDirLEN_CFGDIR)
 
//server_print("%s", cfgDir)  // Debug line.
 
 // Adds the file name into the directory.
 
format(dirFileLEN_TOTAL"%s/%s"cfgDirTXT_FILENAME)
 
//server_print("%s", dirFile) // Debug line.
 
 // Opens the file, and read it.
 
fh fopen(dirFile"r")
 
 
// If file exist.
 
if (fh)
 {
  
// Variable section
  
new nCount 0
 
  
// While it's not the end of the TXT file.
  
while (!feof(fh))
  {
   
// If the is more than 32 words breaks the loop.
   
if (nCount MAX_WORDS)
    break
 
   
// Variable section.
   
new fData[LEN_DATA STRING_OFFSET]
 
   
// Get the data of the line.
   
fgets(fhfDataLEN_DATA)
 
   
// Write the word in the array.
   
formatex(g_arrszWords[nCount], MAX_WORDSfData)
 
   
// Increase the counter.
   
nCount++
  }
 
 }
 
 return 
PLUGIN_CONTINUE
}
public 
FlameHandler(id)
{
 
// If the cvar forced as off, not doing nothing.
 
if (get_pcvar_num(g_pcvFD) == CVAR_IGNORE)
  return 
PLUGIN_CONTINUE
 
 
 
// Variable definize.
 
new szText[LEN_TEXT STRING_OFFSET]
 
 
// Read the user message.
 
read_args(szTextLEN_TEXT)
 
// Remove the quotes.
 
remove_quotes(szText)
 
 
// If there is a flame in the text.
 
if (isFound(szText))
 {
  
// If the cvar is "1", it censor the message.
  
if (get_pcvar_num(g_pcvFD) == CVAR_CEN) {
   return 
PLUGIN_HANDLED
  
}
  
// If the cvar is "2", it kicks the player.
  
else if (get_pcvar_num(g_pcvFD) == CVAR_KICK) {
   new 
nUserID get_user_userid(id)
 
   
server_cmd("kick #%d You used a bad word! don't repeat you'r self!"nUserID)
   return 
PLUGIN_HANDLED
  
}
 }
 
 return 
PLUGIN_CONTINUE
}
stock bool:isFound(const szCheckText[])
{
 
// Variable definize.
 
new j
 
 
// Code section.
 // Looping the badwords array.
 
for (0MAX_WORDSj++)
 {
  new 
nNumber
  
new nError[LEN_ERROR STRING_OFFSET]
  new 
Regex:rgxMatch
 
  
// If there is no more words, return false.
  
if (g_arrszWords[j] == NULL) {
   break
  }
  
rgxMatch regex_match(szCheckTextg_arrszWords[j], nNumbernErrorLEN_ERROR)
 
  
// If is match, return true.
  
if (nNumber >= 1) {
   
//server_print("regexok") // debug line
   
return true
  
}
 
  
// Free the regex memmory.
  
regex_free(rgxMatch)
 }
 return 
false 

if you prefer [ CODE ] format:
Code:
#include <amxmodx>
#include <amxmisc>
#include <regex>

#define PLUGIN "Flame Detector"
#define VERSION "1.0"
#define AUTHOR "unSeen"
#define STRING_OFFSET 1  // Define the len of the offset string.
#define LEN_CFGDIR 127  // Define the len of the configs folder path.
#define LEN_TOTAL 255  // Define the total len of the configs path + filename path.
#define LEN_DATA 127  // Define the len of the TXT file data.
#define LEN_TEXT 192  // Define the user text len in chat.
#define LEN_ERROR  127  // Define the error max len.
#define TXT_FILENAME  "badwords.txt" // Define the words list TXT file name.
#define MAX_WORDS 32  // Define the max bad words wich can be in the file.
#define WORDS_OFFSET  1  // Define the bad words offset.
#define NULL   0  // Define the null char.
#define CVAR_IGNORE 0  // Define the ignore cvar.
#define CVAR_CEN 1  // Define the censor cvar.
#define CVAR_KICK 2  // Define the kick cvar.
new g_arrszWords[MAX_WORDS + WORDS_OFFSET]
new g_pcvFD
public plugin_init() {
 // Register the plugin - name, version and author.
 register_plugin(PLUGIN, VERSION, AUTHOR)
 
 // Register cvar num. 0|1|2. (0 nothing, 1 censor the message, 2 kick the player)
 g_pcvFD = register_cvar("fd_cvar", "1")
 
 // When some one say something in the server-Chat.
 register_clcmd("say", "FlameHandler")
 
 // When some one say something in the server-teamChat.
 register_clcmd("say_team", "FlameHandler")
 
 // Loads the blocked words from a TXT file.
 LoadWords()
}
LoadWords()
{
 // Reset the array.
 for (new i = 0; i < MAX_WORDS; i++)
 {
  g_arrszWords[i] = NULL
 }
 
 // Variable definize
 new cfgDir[LEN_CFGDIR + STRING_OFFSET]
 new dirFile[LEN_TOTAL + STRING_OFFSET]
 new fh
 
 // Code section.
 // Gets the CFG directory.
 get_configsdir(cfgDir, LEN_CFGDIR)
 //server_print("%s", cfgDir)  // Debug line.
 
 // Adds the file name into the directory.
 format(dirFile, LEN_TOTAL, "%s/%s", cfgDir, TXT_FILENAME)
 //server_print("%s", dirFile) // Debug line.
 
 // Opens the file, and read it.
 fh = fopen(dirFile, "r")
 
 // If file exist.
 if (fh)
 {
  // Variable section
  new nCount = 0
  
  // While it's not the end of the TXT file.
  while (!feof(fh))
  {
   // If the is more than 32 words breaks the loop.
   if (nCount > MAX_WORDS)
    break
   
   // Variable section.
   new fData[LEN_DATA + STRING_OFFSET]
   
   // Get the data of the line.
   fgets(fh, fData, LEN_DATA)
   
   // Write the word in the array.
   formatex(g_arrszWords[nCount], MAX_WORDS, fData)
   
   // Increase the counter.
   nCount++
  }
  
 }
 
 return PLUGIN_CONTINUE
}
public FlameHandler(id)
{
 // If the cvar forced as off, not doing nothing.
 if (get_pcvar_num(g_pcvFD) == CVAR_IGNORE)
  return PLUGIN_CONTINUE
 
 
 // Variable definize.
 new szText[LEN_TEXT + STRING_OFFSET]
 
 // Read the user message.
 read_args(szText, LEN_TEXT)
 // Remove the quotes.
 remove_quotes(szText)
 
 // If there is a flame in the text.
 if (isFound(szText))
 {
  // If the cvar is "1", it censor the message.
  if (get_pcvar_num(g_pcvFD) == CVAR_CEN) {
   return PLUGIN_HANDLED
  }
  // If the cvar is "2", it kicks the player.
  else if (get_pcvar_num(g_pcvFD) == CVAR_KICK) {
   new nUserID = get_user_userid(id)
   
   server_cmd("kick #%d You used a bad word! don't repeat you'r self!", nUserID)
   return PLUGIN_HANDLED
  }
 }
 
 return PLUGIN_CONTINUE
}
stock bool:isFound(const szCheckText[])
{
 // Variable definize.
 new j
 
 // Code section.
 // Looping the badwords array.
 for (j = 0; j < MAX_WORDS; j++)
 {
  new nNumber
  new nError[LEN_ERROR + STRING_OFFSET]
  new Regex:rgxMatch
  
  // If there is no more words, return false.
  if (g_arrszWords[j] == NULL) {
   break
  }
  rgxMatch = regex_match(szCheckText, g_arrszWords[j], nNumber, nError, LEN_ERROR)
  
  // If is match, return true.
  if (nNumber >= 1) {
   //server_print("regexok") // debug line
   return true
  }
  
  // Free the regex memmory.
  regex_free(rgxMatch)
 }
 return false 
}
The words.txt looks like that:
PHP Code:
/l[o0]ve/i
/f[ua]ck/i
/n[o0][o0]b/
i tried manyways to fix it, like remove the first "/" or remove them both and not using the i header that suppost to sensivity case.

Please help me!
You'rs,
Elad.
__________________
List of important things:
1. Respect you'r mother.
2. Respect you'r father.
3. Do not hit anyone.
4. Do not steal.
5. Do not flame.
7. You didn't see that there is no number 6.
8. You checked if number 7 is true.
9. You are laughing now.
10. You are double laughing because you understand the joke ;)

Last edited by unSeen; 04-04-2009 at 17:51.
unSeen is offline
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:21.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode