Imports DevComponents.DotNetBar Imports Ihabbox.Ihabbox.Tool Public Class frmPackets #Region "Packetlogger" 'required for thread safe invoking Delegate Sub ThreadSafeAppend(ByVal RichTextBox As RichTextBox, ByVal Rtf As String) 'threads queue Private ClientHigh, ServerHigh As New ThreadsQueue(Threading.ThreadPriority.BelowNormal) 'api which blocks the form's update Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Integer) As Integer 'used to replace the newline for listview compatibility Friend ReadOnly SpecialLine As String = Convert.ToChar(29) & Convert.ToChar(32) Private Sub btnSendServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendServer.Click Try Me.TopMost = False MessageBoxEx.Show("This Feature Isnt Available In Ihabbox - Download Lemon Instead", "Try Lemon!", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True Catch ex As Exception End Try End Sub Private Sub btnSendClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendClient.Click Try Dim Packet As String = txtSendClient.Text If chkConvert.Checked Then 'replace special chars If Packet.Contains("{1}") Then Packet = Packet.Replace("{1}", frmbrowser.Chr1) If Packet.Contains("{2}") Then Packet = Packet.Replace("{2}", frmbrowser.Chr2) frmbrowser.sckServer.SendData(Packet) End If Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True End Try End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCleanClient.Click, btnCleanServer.Click Try Me.TopMost = False If MessageBoxEx.Show("Do you really want to clean the packet log?", "Clearing", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then If sender Is btnCleanClient Then rtbSCKClient.Clear() Else rtbSCKServer.Clear() Me.TopMost = True End If Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True End Try End Sub Friend Sub AsyncHighlightClient(ByVal Text As String) Dim TAppend As Threading.Thread 'set the new thread TAppend = New Threading.Thread(Sub() ElaborateClientAppend(Text)) TAppend.IsBackground = True TAppend.Priority = Threading.ThreadPriority.BelowNormal ClientHigh.Enqueue(TAppend) 'get this thread into the queue End Sub Private Sub ElaborateClientAppend(ByVal Value As String) Dim TempRtb As New RichTextBox 'generate another control for thread safe purposes Dim Chr1 As Char = Convert.ToChar(1), Chr2 As Char = Convert.ToChar(2) With TempRtb .Text = Value 'set the text If .Text.Contains("Server says") Then 'Value must be a packet Dim Count As Int32 If .Text.Contains(Chr2) Then 'check for chr2s Count = System.Text.RegularExpressions.Regex.Matches(.Text, System.Text.RegularExpressions.Regex.Escape(Chr2)).Count 'count the chr1s If chkCHR.Checked Then ColouringClient(TempRtb, Chr2, Color.Blue, Count, "{2}") Else ColouringClient(TempRtb, Chr2, Color.Blue, Count) 'format End If If .Text.Contains(Chr1) Then 'check for chr1s Count = System.Text.RegularExpressions.Regex.Matches(.Text, System.Text.RegularExpressions.Regex.Escape(Chr1)).Count 'count the chr2s If chkCHR.Checked Then ColouringClient(TempRtb, Chr1, Color.Red, Count, "{1}") 'if there's no replace it's useless because chr1 is not printable End If ColouringClient(TempRtb, System.Text.RegularExpressions.Regex.Match(Value, "Server says \(\d+\.\d+\.\d+\.\d+\)\:").Value, rtbSCKClient.ForeColor, _ 1, , New Font(rtbSCKClient.Font.FontFamily, rtbSCKClient.Font.Size, FontStyle.Bold)) 'only one "Server says[..]" has to be coloured ElseIf Value.Contains("sckClient is connected.") Then 'socket's connection notification .SelectAll() .SelectionColor = Drawing.Color.Green Else 'socket's disconnection notification .SelectAll() .SelectionColor = Drawing.Color.DarkRed End If Try rtbSCKClient.Invoke(New ThreadSafeAppend(AddressOf RtfAppend), {rtbSCKClient, .Rtf}) 'append the rtf string Catch ex As Exception End Try End With End Sub 'Client syntax highlighting core Private Sub ColouringClient(ByVal TempRtb As RichTextBox, ByVal Highlight As String, ByVal Color As Color, ByVal Count As Int32, _ Optional ByVal ChrReplace As String = Nothing, Optional ByVal Font As Font = Nothing) With TempRtb Dim Index As Int32 Do Until Count = 0 'loop until there are not occurences Index = .Find(Highlight, Index, RichTextBoxFinds.MatchCase) 'select the value If ChrReplace IsNot Nothing Then .SelectedText = ChrReplace : .Select(Index, 3) 'if the user wants the special chars replace .SelectionColor = Color 'set the color If Font IsNot Nothing Then .SelectionFont = Font 'if this is the "Server says" string Index += Highlight.Length 'move the cursor after the highlight value Count -= 1 'one highlighting has been processed Loop End With End Sub Friend Sub AsyncHighlightServer(ByVal Text As String) Try Dim TAppend As Threading.Thread TAppend = New Threading.Thread(Sub() ElaborateServerAppend(Text)) TAppend.IsBackground = True TAppend.Priority = Threading.ThreadPriority.BelowNormal ServerHigh.Enqueue(TAppend) Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True End Try End Sub Private Sub ElaborateServerAppend(ByVal Value As String) Dim TempRtb As New RichTextBox 'temp richtextbox,used to apply the Rtf format With TempRtb .Text = Value.ToString If .Text.Contains("Client says") Then '.Text must be a packet .Find(System.Text.RegularExpressions.Regex.Match(.Text, _ "Client says \(\d+\.\d+\.\d+\.\d+\)\:").Value) 'get "Server says (time):" and highlight it .SelectionFont = New Font(rtbSCKServer.Font.FontFamily, rtbSCKServer.Font.Size, FontStyle.Bold) 'bold ElseIf .Text.Contains("sckServer is connected.") Then 'socket's status notification .SelectAll() .SelectionColor = Drawing.Color.Green Else 'socket's disconnection notification .SelectAll() .SelectionColor = Drawing.Color.DarkRed End If Try rtbSCKServer.Invoke(New ThreadSafeAppend(AddressOf RtfAppend), {rtbSCKServer, .Rtf}) 'append the rtf string Catch ex As Exception End Try End With End Sub Private Sub RtfAppend(ByVal RichTextBox As RichTextBox, ByVal Rtf As String) LockWindowUpdate(Handle.ToInt32) 'stop updating the form With RichTextBox If .SelectionLength > 0 Then 'check if there's something selected Dim OldSelectionStart As Int32 = .SelectionStart, OldSelectionLength As Int32 = .SelectionLength 'get the current selection parameters .SelectionStart = .TextLength 'move the cursor to the end of the richtextbox .SelectedRtf = Rtf 'append the Rtf string .Select(OldSelectionStart, OldSelectionLength) 'restore the original selection .ScrollToCaret() Else .SelectionStart = .TextLength .SelectedRtf = Rtf End If End With LockWindowUpdate(0) 'restart updating the form End Sub Private Sub cmbFormat_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbFormat.SelectedIndexChanged My.Settings.LogFormat32 = cmbFormat.SelectedIndex End Sub Private Sub Search(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchClient.Click, btnSearchServer.Click With frmSearch .Show() If sender Is btnSearchClient Then .Rtb = rtbSCKClient Else .Rtb = rtbSCKServer End With End Sub #End Region #Region "Filters" 'this is the container of the original filters (loaded from filters.dat) Private OriginalFilters As New List(Of ListViewItem) Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Try If txtOld.Text <> String.Empty Then 'this value is a must Dim ItemToAdd As ListViewItem With lstFilters ' If chkConvertCharsFilters.Checked Then 'check if the program has to convert the special chars ItemToAdd = New ListViewItem({txtOld.Text.Replace("{1}", frmbrowser.Chr1).Replace("{2}", frmbrowser.Chr2).Replace(Environment.NewLine, SpecialLine), _ txtNew.Text.Replace("{1}", frmbrowser.Chr1).Replace("{2}", frmbrowser.Chr2).Replace(Environment.NewLine, SpecialLine), _ txtDescription.Text.Replace(Environment.NewLine, SpecialLine)}) Else ItemToAdd = New ListViewItem({txtOld.Text.Replace(Environment.NewLine, SpecialLine), txtNew.Text.Replace(Environment.NewLine, SpecialLine), _ txtDescription.Text.Replace(Environment.NewLine, SpecialLine)}) End If For Each Item As ListViewItem In .Items 'check if the item already exists if it does then displays an alert and exit sub If Item.Text = ItemToAdd.Text Then MessageBoxEx.Show("This item already exists.", "Adding", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Exit Sub End If Next 'added and checked .Items.Add(ItemToAdd) .Items(.Items.Count - 1).Checked = True End With Else Throw New Exception("Old value camp is required.") End If Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True End Try End Sub Private Sub btnRemoveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Try lstFilters.Items.Clear() Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True End Try End Sub Private Sub frmPackets_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load Dim Path As String = System.AppDomain.CurrentDomain.BaseDirectory & "filters.dat", Stream As IO.FileStream = Nothing Try If IO.File.Exists(Path) Then 'clean data If OriginalFilters.Count > 0 Then OriginalFilters.Clear() If lstFilters.Items.Count > 0 Then lstFilters.Items.Clear() 'deserialize the container of listviewitems Dim Formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter Stream = New IO.FileStream(Path, IO.FileMode.Open) OriginalFilters = CType(Formatter.Deserialize(Stream), List(Of ListViewItem)) lstFilters.Items.AddRange(OriginalFilters.ToArray) 'add those items to the listview End If Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True Finally If Stream IsNot Nothing Then Stream.Close() cmbFormat.SelectedIndex = My.Settings.LogFormat32 End Try MessageBoxEx.MessageBoxTextColor = Color.White MessageBoxEx.EnableGlass = False rtbSCKClient.ForeColor = Color.Yellow End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim Writer As IO.FileStream = Nothing Try If OriginalFilters.Count > 0 Then OriginalFilters.Clear() 'clear 'save data Writer = New IO.FileStream(System.AppDomain.CurrentDomain.BaseDirectory & "filters.dat", IO.FileMode.OpenOrCreate) 'deserialize the container of listviewitems Dim Formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter For Each Item As ListViewItem In lstFilters.Items 'move data OriginalFilters.Add(Item) Next Formatter.Serialize(Writer, OriginalFilters) 'serialize into a file Me.TopMost = False MessageBoxEx.Show("Saved successfully!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information) Me.TopMost = True Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True Finally If Writer IsNot Nothing Then Writer.Close() End Try End Sub Private Sub frmPackets_FormClosing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.FormClosing My.Settings.Save() 'save options data e.Cancel = True 'avoid the lstfilters cleaning 'check for changes If lstFilters.Items.Count <> OriginalFilters.Count Then DisplayAlert() 'display an alert which warns the user about the unsaved changes Exit Sub Else For i As Int32 = 0 To OriginalFilters.Count - 1 If Not lstFilters.Items.Item(i).Equals(OriginalFilters.Item(i)) Then DisplayAlert() : Exit Sub Next End If 'there are not changes frmSearch.Hide() Hide() End Sub Private Sub DisplayAlert() Dim Response As DialogResult = MessageBoxEx.Show("There are unsaved changes, would you like to save them?", _ "Unsaved", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) If Response = Windows.Forms.DialogResult.Yes Then btnSave_Click(Me, New EventArgs) 'if the user clicks "Yes" save the new data frmSearch.Hide() Hide() ElseIf Response = Windows.Forms.DialogResult.No Then frmSearch.Hide() Hide() lstFilters.Items.AddRange(OriginalFilters.ToArray) 'if "No" restore the original data End If End Sub Private Sub ctmValue_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ctmValue.Opening If lstFilters.SelectedItems.Count = 0 Then e.Cancel = True 'if there are not selected items then abort the displaying ElseIf lstFilters.SelectedItems.Count > 1 Then ctmValue.Items.Item(0).Enabled = False 'if there are more than 1 object selected then disable the Edit menù End If End Sub Private Sub RemoveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveToolStripMenuItem.Click Try For Each Item As ListViewItem In lstFilters.SelectedItems Item.Remove() Next Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True End Try End Sub Private Sub EditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.Click With frmSetFilters .Item = lstFilters.SelectedItems(0) 'if there are more than one item selected this menù being unclickable .FullShow = True 'show txtDescription and lblDescription .Show() End With End Sub #End Region Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Try If txtOld.Text <> String.Empty Then 'this value is a must Dim ItemToAdd As ListViewItem With lstFilters ' If chkConvertCharsFilters.Checked Then 'check if the program has to convert the special chars ItemToAdd = New ListViewItem({txtOld.Text.Replace("{1}", frmbrowser.Chr1).Replace("{2}", frmbrowser.Chr2).Replace(Environment.NewLine, SpecialLine), _ txtNew.Text.Replace("{1}", frmbrowser.Chr1).Replace("{2}", frmbrowser.Chr2).Replace(Environment.NewLine, SpecialLine), _ txtDescription.Text.Replace(Environment.NewLine, SpecialLine)}) Else ItemToAdd = New ListViewItem({txtOld.Text.Replace(Environment.NewLine, SpecialLine), txtNew.Text.Replace(Environment.NewLine, SpecialLine), _ txtDescription.Text.Replace(Environment.NewLine, SpecialLine)}) End If For Each Item As ListViewItem In .Items 'check if the item already exists if it does then displays an alert and exit sub If Item.Text = ItemToAdd.Text Then Me.TopMost = False MessageBoxEx.Show("This item already exists.", "Adding", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Me.TopMost = True Exit Sub End If Next 'added and checked .Items.Add(ItemToAdd) .Items(.Items.Count - 1).Checked = True End With Else Throw New Exception("Old value camp is required.") End If Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True End Try End Sub Private Sub ButtonX2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Dim Writer As IO.FileStream = Nothing Try If OriginalFilters.Count > 0 Then OriginalFilters.Clear() 'clear 'save data Writer = New IO.FileStream(System.AppDomain.CurrentDomain.BaseDirectory & "filters.dat", IO.FileMode.OpenOrCreate) 'deserialize the container of listviewitems Dim Formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter For Each Item As ListViewItem In lstFilters.Items 'move data OriginalFilters.Add(Item) Next Formatter.Serialize(Writer, OriginalFilters) 'serialize into a file Me.TopMost = False MessageBoxEx.Show("Saved successfully!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information) Me.TopMost = True Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True Finally If Writer IsNot Nothing Then Writer.Close() End Try End Sub Private Sub ButtonX3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveAll.Click Try lstFilters.Items.Clear() Catch ex As Exception Me.TopMost = False MessageBoxEx.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Me.TopMost = True End Try End Sub End Class