﻿//--------------------------------------------------------------------------------------------------\\
//                                                                                                  \\
//                                             SF                                                   \\
//                                                                                                  \\
//--------------------------------------------------------------------------------------------------\\
//                                Smart word filter Version 1.0                                     \\
//--------------------------------------------------------------------------------------------------\\
// Created by Lobe
// Idea from: TP admin (using est_regsayhook to block messages) and Mani's Admin plugin
// Thanks to: TP pred for his TP admin script, Mattie for Eventscripts and Awuh0 for ES_tools
// Extract this file to:
// -addons/eventscripts/smart_filter
// Note: Failure to complete the extraction to the correct place may result in failure of the
// script and its functions

// Commands for clients:
// - sf_words = Prints a list of filtered words to the clients console

// Commands for console:
// - sf_addword = Adds a word to the filter database
// - sf_delword = Removes a word from the database
block load
{
  es_set sf_version 1.0
  es_makepublic sf_version
  
  // Init Vars
  echo Init Var's
  es_set smart_filtertemp1 0
  es_set smart_filtertemp2 0
  es_set sf_speaker 0
  es_set sf_tempvar2 0
  es_set sf_tempvar3 0
  es_set sf_tempvar4 0
  es_set sf_tempvar5 0
  es_set sf_tempstring1 0
  
  // Init CMD's
  echo Init CMD's
	es_xregclientcmd sf_words smart_filter/printword "Prints a list of words to the client console"
	es_xregcmd sf_words smart_filter/printwordconsole "Prints a list of words in the DB to the server console"
	es_xregcmd sf_addword smart_filter/addword "Adds a word to the database - Syntax: sf_addword [WORD]" 
	es_xregcmd sf_delword smart_filter/delword "Removes a word to the database - Syntax: sf_delword [WORD]"
	es_xregcmd sf_load smart_filter/loadpack "Loads a filterpack - Syntax: sf_load [NAME OF PACK]"
  	
	// Init DB
	echo Init DB
	es_xkeygroupload smart_filter |smart_filter
	echo Loaded DB
	
	// Init Hook
	es_xforeachkey smart_filtertemp1 in smart_filter "es est_RegSayHook server_var(smart_filtertemp1) 1"
	echo Hooked words
	
	// Init Config
	echo Init Config
	exec smart_filter/sf_config.cfg
	
	// Init Msg
	es_xmsg #multi #green#lightgreen Словарь активирован!
}

block loadpack
{
  es_set sf_tempvar2 0
  es_getargc sf_tempvar2
  if (server_var(sf_tempvar2) notequalto 2) do
  {
    echo Wrong Syntax!
    echo Syntax: sf_load [NAME OF PACK]
  }
  else do
  {
    es_set sf_tempvar5 0
    es_getargv sf_tempvar5 1
    es echo Loading filterpack: server_var(sf_tempvar5)
    es_set sf_tempstring1 0
    es_format sf_tempstring1 "smart_filter/%1" server_var(sf_tempvar5)
    es exec server_var(sf_tempstring1)
  }
}

block addword
{
  es_set sf_tempvar2 0
  es_getargc sf_tempvar2
  if (server_var(sf_tempvar2) notequalto 2) do
  {
    echo Wrong Syntax!
    echo Syntax: sf_addword [WORD]
  }
  else do
  {
    es_set sf_tempvar3 0
    es_getargv sf_tempvar3 1
    es_keycreate smart_filter server_var(sf_tempvar3)
    es echo Added server_var(sf_tempvar3) to the database!
    es est_RegSayHook server_var(sf_tempvar3) 1
  }
}

block delword
{
  es_set sf_tempvar2 0
  es_getargc sf_tempvar2
  if (server_var(sf_tempvar2) notequalto 2) do
  {
    echo Wrong Syntax!
    echo Syntax: sf_delword [WORD]
  }
  else do
  {
    es_set sf_tempvar4 0
    es_getargv sf_tempvar4 1
    es_keydelete smart_filter server_var(sf_tempvar4)
    es echo Added server_var(sf_tempvar4) to the database!
    es est_UnRegSayHook server_var(sf_tempvar4)
  }
}

event est_sayhook
{
	es_tell event_var(userid) #multi #green#lightgreen Это слово event_var(hooktext) Запрещено! Хочешь бан?
}

block unload
{
  es_foreachkey smart_filtertemp1 in smart_filter "es est_UnRegSayHook server_var(smart_filtertemp1)"
  es_keygroupdelete smart_filter
  echo Script Unloaded
  es_msg #multi #green#lightgreen Словарь не активирован!
}

event es_map_start
{
  echo Renewing word hooks
	es_xforeachkey smart_filtertemp1 in smart_filter "es est_RegSayHook server_var(smart_filtertemp1) 1"
  echo Renewed word hooks  
}

block printwordconsole
{
  es_set smart_filtertemp2 0
  echo ------------------------------------------------------------------
  echo List of filtered words found in the database
  echo ------------------------------------------------------------------
	es_xforeachkey smart_filtertemp2 in smart_filter "es echo - server_var(smart_filtertemp2)"
  echo ------------------------------------------------------------------
}

block printword
{
  es_xset sf_speaker 0
	es_xgetcmduserid sf_speaker
	es_tell server_var(sf_speaker) #green Check the console for the list of filtered words! (Defualt key for console is `)
	es_cexec server_var(sf_speaker) echo ------------------------------------------------------------------
	es_cexec server_var(sf_speaker) echo List of filtered words found in the database
	es_cexec server_var(sf_speaker) echo ------------------------------------------------------------------
	es_xforeachkey smart_filtertemp2 in smart_filter "es_cexec server_var(sf_speaker) echo - server_var(smart_filtertemp2)"
	es_cexec server_var(sf_speaker) echo ------------------------------------------------------------------
}

