'////////////////////////////////////////////////////////////////////////
'// PHRASEBANS SCRIPT FOR 9.4
'// WRITTEN BY: Chriso
'// This script will enable you to ban people who say certain phrases.
'////////////////////////////////////////////////////////////////////////

Dim PB

'// If Wildcard is set to True then Matches will be used, for example:
'// Phrases will need to be added as "*Spam*"
'// If Wildcard is set to False then InStr will be used instead, example:
'// Phrases will need to be added as "Spam"
Const Wildcard = False

'// Return Script Information
Sub Script(Name, Major, Minor, Build, Author, Commands, Description)
	Name = "PhraseBans Script"
	Major = 1
	Minor = 0
	Build = 0
	Author = "Chriso"
	Commands = "addphrase, delphrase, phrases"
	Description = "Ban people who say certain phrases."
End Sub

'// Called when the script is loaded for a particular bot
Sub Event_Load()
	Set PB = New PhraseBans
	PB.LoadPhrases()
    CreateCommand "addphrase", "PhraseBan", "Command_AddPhrase", "", "phrase", "Add a phrase to the phrasebans list", 0, True
    CreateCommand "delphrase", "PhraseBan", "Command_DelPhrase", "", "phrase", "Delete a phrase from the phrasebans list", 0, True
    CreateCommand "phrases", "PhraseBan", "Command_Phrases", "", "", "Return the list of phrasebans", 0, True
End Sub

Sub Event_Unload()
	PB.SavePhrases()
End Sub

'// Callback for "addphrase" Command
Sub Command_AddPhrase(CS)
    If Len(CS.Message) = 0 Then
		CS.Reply "Please specify a phrase to add."
	Else
		If PB.AddPhrase(CS.Message) Then
			CS.Reply "Added phrase: " & CS.Message
		Else
			CS.Reply "Phrase already exists!"
		End If
	End If
End Sub

'// Callback for "delphrase" Command
Sub Command_DelPhrase(CS)
	If Len(CS.Message) = 0 THen
		CS.Reply "Please specify the phrase to delete."
	Else
		If PB.DeletePhrase(CS.Message) Then
			CS.Reply "Deleted Phrase: " & CS.Message
		Else
			CS.Reply "Phrase does not exist!"
		End If
	End If
End Sub

'// Callback for "phrases" Command
Sub Command_Phrases(CS)
	Dim I, B, F
	F = False
	For I = 0 To UBound(PB.Phrases)
		If Len(PB.Phrases(I)) Then B = B & PB.Phrases(I) & ", ": F = True
		If Len(B) > 50 Then
			CS.Reply Left(B, Len(B)-2)
			B = vbNullString
		End If
	Next
	If Len(B) > 2 Then
		CS.Reply Left(B, Len(B)-2)
		B = vbNullString
	End If
	If F = False Then
		CS.Reply "No phrases found!"
	End If
End Sub

'// Called when a user speaks
Sub Event_UserTalk(CE)
	If IsSafe(CE.Username) Then Exit Sub
	Dim I
	For I = 0 To UBound(PB.Phrases)
		If Len(PB.Phrases(I)) Then
			If Wildcard Then
				If Matches(LCase(CE.Message), LCase(PB.Phrases(I))) Then
					Send "/ban " & GetAccount(CE.Username) & " PhraseBan"
					Exit Sub
				End If
			Else
				If InStr(LCase(CE.Message), LCase(PB.Phrases(I))) <> 0 Then
					Send "/ban " & GetAccount(CE.Username) & " PhraseBan"
					Exit Sub
				End If
			End If
		End If
	Next
End Sub

'// Called when a user emotes
Sub Event_UserEmote(CE)
	Call Event_UserTalk(CE)
End Sub

Class PhraseBans
	
	Public Phrases
	
	Sub LoadPhrases()
		Dim Content
		ReDim Phrases(0)
		Content = LoadFile(GetBotPath & "PhraseBans.txt")
		If Len(Content) Then
			If InStr(Content, vbNewLine) Then
				Phrases = Split(Content, vbNewLine)
			End If
		End If
		AddChat vbGreen, "Loaded " & UBound(Phrases) & " phrases."
	End Sub
	
	Sub SavePhrases()
		Call SaveFile(GetBotPath & "PhraseBans.txt", Join(Phrases, vbNewLine))
	End Sub
	
	Function AddPhrase(Phrase)
		'// Check if Phrase Exists
		If FindPhrase(Phrase) > -1 Then: AddPhrase = False: Exit Function
		'// Add item to Array
		If Len(Phrases(UBound(Phrases))) > 0 Then ReDim Preserve Phrases(UBound(Phrases)+1)
		'// Assign value to Array
		Phrases(UBound(Phrases)) = Phrase
		AddPhrase = True
		SavePhrases
	End Function
	
	Function FindPhrase(Phrase)
		'// Loop Through Array
		Dim I
		For I = 0 To UBound(Phrases)
			'// Check Element
			If LCase(Phrase) = LCase(Phrases(I)) Then
				'// Found
				FindPhrase = I
				Exit Function
			End If
		Next
		'// Not Found
		FindPhrase = -1
	End Function
	
	Function DeletePhrase(Phrase)
		Dim F, N
		'//Check if Phrase Exists
		F = FindPhrase(Phrase)
		'// Nope, Exit
		If F = -1 Then DeletePhrase = False: Exit Function
		'// Yep Lets Delete It
		For N = F To UBound(Phrases) -1
			Phrases(N) = Phrases(N+1)
		Next
		ReDim Preserve Phrases(UBound(Phrases)-1)
		DeletePhrase = True
		SavePhrases
	End Function
	
End Class