Search code examples
vb.netstringirc

Clearing needless text from IRC Server input


I'm making a simple IRC client for myself because I really don't see the need for a lot of mIRC's functionality, but I'm having problems cleaning up the input from the server.

Right now, on connect, I'm getting the following:

[16:37] :young.home.net NOTICE AUTH :** Looking up your hostname...
:young.home.net NOTICE AUTH :** Found your hostname
PING :DE7AED31
[16:37] :DE7AED31!nospoof@young.home.net PRIVMSG Logan :VERSION
:young.home.net 451 PING :You have not registered
[16:37] :young.home.net 001 Logan :Welcome to the YoungNet IRC Network Logan!lyoung@127.0.0.1
:young.home.net 002 Logan :Your host is young.home.net, running version Unreal3.2.8.1 :young.home.net 003 Logan :This server was created Sun Apr 12 14:47:33 2009

I'm trying to clean it up to read like this:

[16:37] -young.home.net- ** Looking up your hostname...
[16:37] -young.home.net- ** Found your hostname
[16:37] [Logan] VERSION
[16:37] You have not registered
Welcome to the YoungNet IRC Network Logan!lyoung@127.0.0.1
Your host is young.home.net, running version Unreal3.2.8.1 This server was created Sun Apr 12 14:47:33 2009

I've read the IRCP (RFC 1459) and I understand the formatting of the server input, but I can't seem to strip out the unwanted stuff... A friend suggested loading the input into an array and deal with each item individually, but I can't seem to make it work. I have tried, but it doesn't seem to make a difference... Here's my code

Public Function recv() As String
    Dim mail As String
    Try
        Dim Data(4096) As Byte
        sock.Receive(Data, 4096, Net.Sockets.SocketFlags.None)
        mail = System.Text.ASCIIEncoding.UTF8.GetString(Data)
        If mail.Contains(" ") Then
            If mail.Contains("PING") Then
                Dim pserv As String = mail.Substring(mail.IndexOf(":"), mail.Length - mail.IndexOf(":"))
                pserv = pserv.TrimEnd(Chr(0))
                'MsgBox("pserv: " & pserv)
                send("PONG " & pserv)
            ElseIf mail.Substring(mail.IndexOf(" ") + 1, 7) = "PRIVMSG" Then
                Dim tmparr() As String = Nothing
                Dim rnick, rmsg As String
                mail = mail.Remove(0, 1)
                tmparr = mail.Split("!")
                rnick = tmparr(0)
                tmparr = mail.Split(":")
                rmsg = tmparr(1)
                mail = "<" & rnick & "> " & rmsg
            ElseIf mail.Substring(mail.IndexOf(" ") + 1, 6) = "NOTICE" Then
                Dim tmparr() As String = Nothing
                Dim rnick, rmsg As String
                mail = mail.Remove(0, 1)
                tmparr = mail.Split("!")
                rnick = tmparr(0)
                tmparr = mail.Split(":")
                rmsg = tmparr(1)
                mail = "-" & rnick & "- " & rmsg
            Else
                Dim tmparr() As String = Nothing
                Dim rnick, rmsg As String
                tmparr = mail.Split(" ")
                rnick = tmparr(0)
                tmparr = mail.Split(":")
                rmsg = tmparr(1)
                mail = rmsg
            End If
        End If

        mail = g.Timestamp & mail

    Catch ex As Exception
        MsgBox(ex.ToString)
        mail = "ERROR: " & ex.Message & vbCrLf
    End Try
    Return mail
End Function

Any pointers here would be greatly appreciated.


Solution

  • OK, I got so busy after I found the solution here that I completely forgot about stackoverflow...

    [http://www.mirc.net/raws/][1]

    [1]: http://www.mirc.net/raws/ contains a list of raw numeric codes that neatly categorize the server output to the client app.

    There's a few splits to get through before you can use the raw numerics, however. See below:

    ' This function takes the raw input from the server as a parameter.
    ' NOTE: No modification has been done before this point.
    
    Private Function Clean(ByVal Target As String) As String
        ' Get each line
        Dim temp() As String = Target.Split(vbCrLf)
        Dim back As String = Nothing
    
        ' For each line in the current string
        For i As Integer = 0 To temp.Length - 1
            ' Split the line 
            Dim result() As String = temp(i).Split(New String() {" :"}, StringSplitOptions.None)
            Dim j As Integer = Nothing
            Try
                ' NOTICE AUTH messages aren't covered in raw format, so this cleans those up
                If result(0).Contains("NOTICE AUTH") Then
                    If Left(result(0), 1) <> ":" Then
                        ' g.Timestamp = "[" & Now.Hour & ":" & Now.Minute & "]"
                        back += g.Timestamp & " (" & result(0).Substring(2, result(0).IndexOf(" ") - 2) & ") " & result(1) & vbCrLf
                    Else
                        back += g.Timestamp & " (" & result(0).Substring(1, result(0).IndexOf(" ") - 1) & ") " & result(1) & vbCrLf
                        g.host = result(0).Substring(1, result(0).IndexOf(" ") - 1)
                    End If
                End If
    
                If Integer.TryParse(result(0).Substring(result(0).IndexOf(" ") + 1, 3), j) Then
                    Select Case result(0).Substring(result(0).IndexOf(" ") + 1, 3)
                        ' For each code, modify the input here, this is just an example of what I've done...
                        Case "001" : back += ":=:" & vbCrLf & g.Timestamp & result(1) & vbCrLf
                    End Select
                End If
            Catch ex As Exception
    
            End Try
        Next
    
        ' More stuff goes here (handling PING? PONG! events and such)
    
        Return back
    End Function