Skip to content

Commit

Permalink
Store username-based filters in memory
Browse files Browse the repository at this point in the history
This avoids several disk reads on every user event
  • Loading branch information
Davnit committed Mar 27, 2017
1 parent f1203e1 commit 3c4f06a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 59 deletions.
88 changes: 47 additions & 41 deletions trunk/frmChat.frm
Expand Up @@ -1836,6 +1836,7 @@ Private Sub Form_Load()
ReDim gOutFilters(0)
ReDim gFilters(0)
ReDim ServerRequests(0)
ReDim g_Blocklist(0)

Call BuildProductInfo

Expand Down Expand Up @@ -7258,6 +7259,7 @@ Sub ReloadConfig(Optional Mode As Byte = 0)
'LoadSafelist
LoadArray LOAD_PHRASES, Phrases()
LoadArray LOAD_FILTERS, gFilters()
LoadArray LOAD_BLOCKLIST, g_Blocklist()

ProtectMsg = Config.ChannelProtectionMessage

Expand Down Expand Up @@ -7532,55 +7534,59 @@ ERROR_HANDLER:
End Sub

Sub LoadArray(ByVal Mode As Byte, ByRef tArray() As String)
Dim f As Integer
Dim Path As String
Dim temp As String
Dim i As Integer
Dim c As Integer

f = FreeFile

Const FI As String = "TextFilters"

Dim iFileHandle As Integer ' Source file number
Dim sFilePath As String ' Full path to the source file
Dim sTemp As String ' Temporary string
Dim iFilterCount As Integer ' Total number of filters saved
Dim sFilterSection As String ' The section of filters to read (message vs users)
Dim i As Integer ' Counter

' Determine where to read from.
Select Case Mode
Case LOAD_FILTERS
Path = GetFilePath(FILE_FILTERS)
Case LOAD_FILTERS, LOAD_BLOCKLIST
sFilePath = GetFilePath(FILE_FILTERS)
sFilterSection = IIf(Mode = LOAD_FILTERS, "TextFilters", "BlockList")
Case LOAD_PHRASES
Path = GetFilePath(FILE_PHRASE_BANS)
Case LOAD_DB
Path = GetFilePath(FILE_USERDB)
Exit Sub
sFilePath = GetFilePath(FILE_PHRASE_BANS)
End Select

If Dir(Path) <> vbNullString Then
Open Path For Input As #f
If LOF(f) > 2 Then
ReDim tArray(0)
If Mode <> LOAD_FILTERS Then
Do
Line Input #f, temp
If Len(temp) > 0 Then
' removed for 2.5 - why am I PCing it ?
'If Mode = LOAD_SAFELIST Then temp = PrepareCheck(temp)
tArray(UBound(tArray)) = LCase(temp)
ReDim Preserve tArray(UBound(tArray) + 1)
End If
Loop While Not EOF(f)
Else
temp = ReadINI(FI, "Total", FILE_FILTERS)
If temp <> vbNullString And CInt(temp) > -1 Then
c = Int(temp)
For i = 1 To c
temp = ReadINI(FI, "Filter" & i, FILE_FILTERS)
If temp <> vbNullString Then
tArray(UBound(tArray)) = LCase(temp)
If i <> c Then ReDim Preserve tArray(UBound(tArray) + 1)
If Dir(sFilePath) <> vbNullString Then
' Empty the turn array.
ReDim tArray(0)

Select Case Mode
Case LOAD_FILTERS, LOAD_BLOCKLIST
' Get the total number of filters
sTemp = ReadINI(sFilterSection, "Total", FILE_FILTERS)
If ((LenB(sTemp) > 0) And (CInt(sTemp) > 0)) Then
iFilterCount = Int(sTemp)

' Read each filter into a row of the array.
For i = 1 To iFilterCount
sTemp = ReadINI(sFilterSection, "Filter" & i, FILE_FILTERS)
If LenB(sTemp) > 0 Then
tArray(UBound(tArray)) = sTemp
ReDim Preserve tArray(UBound(tArray) + 1)
End If
Next i
End If
End If
Case Else
' Read each line of the file into a row in the array.
Open sFilePath For Input As #iFileHandle
Do
Line Input #iFileHandle, sTemp
If LenB(sTemp) > 0 Then
tArray(UBound(tArray)) = sTemp
ReDim Preserve tArray(UBound(tArray) + 1)
End If
Loop While Not EOF(iFileHandle)
Close #iFileHandle
End Select

' Remove the last unused row.
If UBound(tArray) > 0 Then
ReDim Preserve tArray(UBound(tArray) - 1)
End If
Close #f
End If
End Sub

Expand Down
1 change: 1 addition & 0 deletions trunk/frmFilters.frm
Expand Up @@ -706,6 +706,7 @@ Private Sub Form_Unload(Cancel As Integer)

Call frmChat.LoadOutFilters
Call frmChat.LoadArray(LOAD_FILTERS, gFilters())
Call frmChat.LoadArray(LOAD_BLOCKLIST, g_Blocklist())
End Sub

Private Sub optBlock_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Expand Down
3 changes: 3 additions & 0 deletions trunk/modCommandsChat.bas
Expand Up @@ -62,6 +62,8 @@ Public Sub OnBlock(Command As clsCommandObj)
WriteINI "BlockList", "Filter" & (Total + 1), Command.Argument("Username"), FiltersPath
WriteINI "BlockList", "Total", Total + 1, FiltersPath
Command.Respond StringFormat("Added {0}{1}{0} to the username block list.", Chr$(34), Command.Argument("Username"))

Call frmChat.LoadArray(LOAD_BLOCKLIST, g_Blocklist())
Else
Command.Respond "Your filters file has been edited manually and is no longer valid. Please delete it."
End If
Expand Down Expand Up @@ -232,6 +234,7 @@ Public Sub OnUnBlock(Command As clsCommandObj)
Next i
WriteINI "BlockList", "Total", (Total - 1), FiltersPath
Command.Respond StringFormat("Removed {0}{1}{0} from the blocked users list.", Chr$(34), Command.Argument("Username"))
Call frmChat.LoadArray(LOAD_BLOCKLIST, g_Blocklist())
End If
Else
Command.Respond "Your filters file has been edited manually and is no longer valid. Please delete it."
Expand Down
1 change: 1 addition & 0 deletions trunk/modConstants.bas
Expand Up @@ -239,6 +239,7 @@ Public Const LOAD_SAFELIST = 1
Public Const LOAD_FILTERS = 2
Public Const LOAD_PHRASES = 3
Public Const LOAD_DB = 4
Public Const LOAD_BLOCKLIST = 5

Public Const DB_TYPE_USER = "USER"
Public Const DB_TYPE_GROUP = "GROUP"
Expand Down
1 change: 1 addition & 0 deletions trunk/modGlobals.bas
Expand Up @@ -97,6 +97,7 @@ Public Catch() As String
Public gBans() As udtBanList
Public gOutFilters() As udtOutFilters
Public gFilters() As String
Public g_Blocklist() As String

Public AutoModSafelistValue As Integer

Expand Down
23 changes: 5 additions & 18 deletions trunk/modOtherCode.bas
Expand Up @@ -765,27 +765,14 @@ Public Function CheckBlock(ByVal Username As String) As Boolean
Dim s As String
Dim i As Integer

If (LenB(Dir$(GetFilePath(FILE_FILTERS))) > 0) Then
s = ReadINI("BlockList", "Total", GetFilePath(FILE_FILTERS))

If (StrictIsNumeric(s)) Then
i = s
Else
Exit Function
End If

Username = PrepareCheck(Username)

For i = 0 To i
s = ReadINI("BlockList", "Filter" & i, GetFilePath(FILE_FILTERS))

If (Username Like PrepareCheck(s)) Then
For i = 0 To UBound(g_Blocklist)
If Len(g_Blocklist(i)) > 0 Then
If (PrepareCheck(Username) Like PrepareCheck(g_Blocklist(i))) Then
CheckBlock = True

Exit Function
End If
Next i
End If
End If
Next i
End Function

Public Function CheckMsg(ByVal Msg As String, Optional ByVal Username As String, Optional ByVal Ping As _
Expand Down

0 comments on commit 3c4f06a

Please sign in to comment.