Attribute VB_Name = "modChatEvents"
Public Sub ChatEvent_UserUpdate(ByRef B As BNCS, ByRef CE As objEvent)
On Error GoTo hErr:
    With CE
        Dim UI As Integer
5       UI = Users.Find(.Username)
6       If UI > -1 Then
7           If Users.GetByIndex(UI).IsShown = False Then
8               AddChatUser CE
9               Users.GetByIndex(UI).IsShown = True
            Else
10              UpdateChatUser CE
            End If
11          Users.Update .Username, .Flags, .Ping
        Else
12          AddChatUser CE
13          Users.Add .Username, .Message, .Flags, .Ping, True, True
        End If
2       If LCase$(Suffix(.Username)) = LCase$(Self.Username) Then
3           Self.Ping = .Ping
4           Self.Flags = .Flags
        Else
            ModerateUser CE
        End If
        UI = Users.Find(.Username)
        If UI > -1 Then AlertUser BotIndex, CE, Users.GetByIndex(UI)
        scProcessObj "", "Event_UserUpdate", CE
        plProcessObj "", "Event_UserUpdate", CE
    End With
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Bot", "ChatEvent_UserUpdate"
End Sub

Private Sub GreetUser(ByRef CE As objEvent)
    If Config.Greet Then
        Dim GM As String
4       GM = Config.GreetMessage
5       GM = Replace$(GM, "%v", "MirageBot " & App.Major & "." & App.Minor)
6       GM = Replace$(GM, "%t", Time)
7       GM = Replace$(GM, "%h", Self.Channel)
8       GM = Replace$(GM, "%d", Date)
        GM = Replace$(GM, "%f", CE.Flags)
        GM = Replace$(GM, "%p", CE.Ping)
        GM = Replace$(GM, "%u", GetAccount(CE.Username))
9       GM = Replace$(GM, "%c", DateTimeToShortString(ConvertTickCount(DblTickCount() - TickConnect)))
10      GM = Replace$(GM, "%s", DateTimeToShortString(ConvertTickCount(DblTickCount())))
11      GM = Replace$(GM, "%b", DateTimeToShortString(ConvertTickCount(DblTickCount() - TickLaunch)))
        If InStr(GM, vbNewLine) Then
            Dim G As Integer, GMS() As String
            GMS = Split(GM, vbNewLine)
            For G = 0 To UBound(GMS)
12              If LenB(GMS(G)) Then SendText GMS(G)
            Next G
        Else
            SendText GM
        End If
    End If
End Sub

Public Sub ChatEvent_UserJoin(ByRef CE As objEvent)
On Error GoTo hErr:
    With CE
        'Moderate User
1       Call ModerateUser(CE)
        'Last Joined User
3       TickJoin = DblTickCount

        'Check Rank Message
5       Dim r As RankingStruct, U As objUser
6       r = GetAccess2(.Username, BotIndex)
7       If LenB(r.RankName) Or LenB(r.Alias) Then
8           With r
9               If LenB(.Message) Then
                    Dim M As String, Splt() As String, N As Integer
10                  M = Replace$(.Message, "%u", CE.Username)
11                  M = Replace$(M, "%t", Config.Trigger)
12                  M = Replace$(M, "%f", CE.Flags)
13                  M = Replace$(M, "%p", CE.Ping)
14                  Splt = Split(M, "; ")
15                  For N = 0 To UBound(Splt)
                        If InStr(Splt(N), " ") And Left$(Splt(N), 1) = "/" Then
                            Select Case LCase$(Mid$(Split(Splt(N), " ")(0), 2))
                            Case "ban", "kick", "b", "k", "ip", "banip", "unban", "u", "unbanip", "unip"
                                If Self.IsOperator Then
                                    SendText Splt(N)
                                End If
                            Case Else
                                SendText Splt(N)
                            End Select
                        Else
16                          SendText Splt(N)
                        End If
                    Next N
                End If
            End With
        End If
        
        'Show Alert
17      Set U = Users.GetByName(.Username)
18      If Not U Is Nothing Then AlertUser BotIndex, CE, U

        'Filter Join
        If Not r.Safe Then
            If IsFiltered(.Username, True) Then Exit Sub
19          If FloodDetected Then: FloodCount = FloodCount + 1: Exit Sub
        End If
        
        'Check Config.Greet
4       Call GreetUser(CE)

        'Show Mail...
        Mail.CheckMail CE.Username
        
        'Script Process
2       scProcessObj "", "Event_UserJoin", CE
        plProcessObj "", "Event_UserJoin", CE
        
        'Show Event
20      If Not options.FilterJoin And Not CE.Cancel Then OutputEvent frmBot.rtbChat(BotIndex), EID_JOIN, Suffix(.Username), .Message, .Flags, .Ping
    End With
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Bot", "ChatEvent_UserJoin"
End Sub

Public Sub ChatEvent_UserHere(ByRef CE As objEvent)
On Error GoTo hErr:
    With CE
1       If LCase$(Suffix(.Username)) = LCase$(Self.Username) Then
            'User is You
2           Self.Flags = .Flags
3           Self.Ping = .Ping
4           Self.Statstring = .Message
        Else
            'Moderate User
5           Call ModerateUser(CE)
        End If
        'Script Process
6       scProcessObj "", "Event_UserPresent", CE
        plProcessObj "", "Event_UserPresent", CE
        'Filter Present
7       If options.FilterPresent Then Exit Sub
        'Show Event
8       If Not CE.Cancel Then OutputEvent frmBot.rtbChat(BotIndex), EID_SHOWUSER, Suffix(.Username), .Message, .Flags, .Ping
    
    End With
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Bot", "ChatEvent_UserHere"
End Sub

Public Sub ChatEvent_UserLeft(ByRef CE As objEvent)
On Error GoTo hErr:
    With CE
        'Smart Ban
3       If Config.Op.SmartBan Then
4           Dim x As Integer, Acc As String, Count As Long
5           Acc = GetAccount(.Username)
6           For x = ShareQ.QCount To 1 Step -1
7               Dim QI As objQItem
8               Set QI = ShareQ.Queue(x)
9               If QI.Index = BotIndex Then
10                  If Len(QI.Text) >= 5 + Len(Acc) Then
11                      If LCase$(Left$(QI.Text, 5 + Len(Acc))) = "/ban " & LCase$(Acc) Then
12                          ShareQ.Queue.Remove x
                        End If
                    End If
                End If
            Next
        End If
13      Dim U As objUser, r As RankingStruct
14      Set U = Users.GetByName(.Username)
15      r = GetAccess2(.Username, .Index)
        'Filter Leave
        If FloodDetected And Not r.Safe Then: FloodCount = FloodCount + 1
16      If Not r.Safe Then
            If IsFiltered(.Username, True) Then Exit Sub
            If FloodDetected Then Exit Sub
        End If
        
        'Script Process
1       scProcessObj "", "Event_UserLeave", CE
2       plProcessObj "", "Event_UserLeave", CE

        'Event Filter
        If Not options.FilterJoin Then
17          If U Is Nothing Then
18              If Not CE.Cancel Then OutputEvent frmBot.rtbChat(BotIndex), EID_LEAVE, Suffix(.Username), vbNullString, .Flags, .Ping, "unknown"
            Else
19              AlertUser BotIndex, CE, U
20              If Not CE.Cancel Then OutputEvent frmBot.rtbChat(BotIndex), EID_LEAVE, Suffix(.Username), vbNullString, U.Flags, U.Ping, DateTimeToShortString(ConvertTickCount(DblTickCount - U.TickJoin), True)
            End If
        End If
    End With
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Bot", "ChatEvent_UserLeft"
End Sub

Public Sub ChatEvent_Channel(ByRef CE As objEvent)
On Error GoTo hErr:
    With CE
        If FloodDetected Then
            OutputEvent frmBot.rtbChat(BotIndex), &HB1, , "<b>Flood Filtering Disengaged</b>"
            OutputEvent frmBot.rtbChat(BotIndex), &HB0, , "-- Duration: " & DateTimeToLongString(ConvertTickCount(DblTickCount - FloodStart))
            OutputEvent frmBot.rtbChat(BotIndex), &HB0, , "-- Highest Filter Rate: " & FloodPerSecHi & "/sec   (Max: 500)"
            OutputEvent frmBot.rtbChat(BotIndex), &HB0, , "-- Total Events Filtered: " & FloodCount
            FloodStart = 0
            FloodCount = 0
            FloodDetected = False
            If options.AlertFlood Then ShowExclamation Self.Username & "@" & Config.ServerName, "Flood Filtering Disengaged!"
            scProcessArg "Event_FloodDetect", False
            plProcessArg "Event_FloodDetect", False
        End If
        AddPlace .Message
    
        'Clear Recent Events
        Dim I As Integer
        For I = 0 To UBound(RecentEvents)
            RecentEvents(I) = 0
        Next
        
3       Users.Clear
4       frmBot.lvUsers(BotIndex).ListItems.Clear
5       frmBot.RefreshCount
6       BanList.Clear
        TickFlood = 0
        TickTalk = 0
        
        'Void View
7       If (Not options.DisableSilentView) And (CE.Flags And CHANNEL_SILENT) Then
            SendText "/unignore " & GetAccount(Self.Username)
        End If
        
19      scProcessObj "", "Event_Channel", CE
20      plProcessObj "", "Event_Channel", CE
    
        Dim Alias As String
        Alias = Tags.GetTag(.Message)
        If LenB(Alias) = 0 Then Alias = .Message
        If Not CE.Cancel Then
            If Self.Channel = "The Void" And CE.Message = "The Void" Then
                'Don't show message
            Else
                OutputEvent frmBot.rtbChat(BotIndex), EID_CHANNEL, , Alias, .Flags
            End If
        End If
        
        With Self
            .Flags = 0
9           .PreviousChannel = .Channel
10          .TickActive = DblTickCount
11          .Channel = CE.Message
12          .ChannelFlags = CE.Flags
13          If Not .FirstChannel Then
14              .FirstAttempt = True
15              .FirstChannel = True
16              If LenB(Config.Channel) Then SendText "/home"
            Else
17              .FirstAttempt = False
18              If .IsVoid And LenB(Config.BackupChannel) Then SendText "/backup"
            End If
        End With
        
    End With
21  If SelBot = BotIndex Then ViewProfile BotToTabIndex(BotIndex)
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Bot", "ChatEvent_Channel"
End Sub

Public Sub ChatEvent_ChannelError(ByRef CE As objEvent)
On Error GoTo hErr:
1   scProcessObj "", "Event_ServerError", CE
    plProcessObj "", "Event_ServerError", CE
    If Not CE.Cancel Then OutputEvent frmBot.rtbChat(BotIndex), EID_ERROR, , CE.Message
    With Self
        If .FirstAttempt Then
            If LenB(Config.BackupChannel) Then SendText "/backup"
            .FirstAttempt = False
        End If
    End With
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Bot", "ChatEvent_ChannelError"
End Sub

Public Sub ChatEvent_ServerError(ByRef CE As objEvent)
On Error GoTo hErr
    With CE
1       scProcessObj "", "Event_ServerError", CE
        plProcessObj "", "Event_ServerError", CE
2       If ((DblTickCount - Sweep.Tick) < 5000) And (Sweep.Tick > 0) Then
            Select Case .Message
            Case "That channel does not exist."
3               SendText "Sweep failed, channel does not exist!"
            Case "You do not have permission to view that channel."
4               SendText "Sweep failed, cannot view channel!"
            Case "(If you are trying to search for a user, use the /whois command.)"
                Exit Sub
            End Select
        End If
        If options.BroadcastInList(CE.Message) = False Then
            If Not CE.Cancel Then OutputEvent frmBot.rtbChat(BotIndex), EID_ERROR, , .Message
        End If
        If InStrB(.Message, "Your account is muted") <> 0 Then
            IsMuted = True
            If Config.IsExpansion Then Cdkey2Status = CDKEY_MUTED Else Cdkey1Status = CDKEY_MUTED
            OutputEvent frmBot.rtbChat(BotIndex), EID_ERROR, , "Read more about cdkey muting here: http://www.battle.net/info/cdkeymuting.shtml"
        End If
        If InStrB(.Message, "Your account has had all chat privileges suspended") <> 0 Then
            IsJailed = True
            If Config.IsExpansion Then Cdkey2Status = CDKEY_JAILED Else Cdkey1Status = CDKEY_JAILED
            OutputEvent frmBot.rtbChat(BotIndex), EID_ERROR, , "Your cdkey is jailed!"
        End If
        If InStrB(.Message, "Failed logon attempts since then: ") <> 0 Then
            Dim login As Integer
            login = CInt(Val(Mid$(.Message, InStr(.Message, ": ") + 2)))
            If login > 3 Then
                OutputEvent frmBot.rtbChat(BotIndex), EID_ERROR, , "It appears that someone may be trying to guess your password, please read more about this here: http://www.battle.net/info/lastlogin.shtml"
            End If
        End If
    End With
    Exit Sub
hErr:
    ErrorHandler Err.Number, Err.Description, Erl, "Bot", "ChatEvent_ServerError"
End Sub

