Author Topic: BnChat Class  (Read 3738 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
BnChat Class
« on: April 04, 2005, 05:32:45 am »
The fun bit of coding we could all do anyhow, heres the BnChat class I wrote. If you find any errors, PM me or reply here, and I'll have them fixed ASAP.

Code: [Select]
'---------------------------------------------------------------------------------------
' Module    : clsBnChat
' Author    : JoeTheOdd
' Purpose   : Provide a CSB-Like interface for the BnChat protocol
'---------------------------------------------------------------------------------------


Private WithEvents sckBNET As CSocket   'Our CSocket Class - Winsock replacement
Private sServer As String               'String that will hold Server
Private sUsername As String             'String that will hold username
Private sPassword As String             'String that will hold password

'Our Events
'Each of these will be "raised" when it occurs
Public Event LoggedOnAs(Username As String)
Public Event JoinedChannel(Channel As String)
Public Event UserInChannel(Username As String, Flags As Integer, Product As String)
Public Event Info(Message As String)
Public Event Error(Message As String)
Public Event UserTalks(Username As String, Message As String)
Public Event UserJoins(Username As String, Flags As Integer, Product As String)
Public Event UserLeaves(Username As String, Flags As Integer)
Public Event Unhandled(sData As String)
Public Event sckConnecting()
Public Event sckConnected()
Public Event sckError()

'Property Let/Get's
'We will use these to set the username, password, and server strings
Public Property Let Username(Username As String)
    sUsername = Username
End Property
Public Property Get Username() As String
    Username = sUsername
End Property
Public Property Let Password(Password As String)
    sPassword = Password
End Property
Public Property Get Password() As String
    Password = sPassword
End Property
Public Property Let Server(Username As String)
    sServer = Server
End Property
Public Property Get Server() As String
    Server = sServer
End Property

'This fires when the class initiates - create our socket
Private Sub Class_Initialize()
    Set sckBNET = New CSocket
End Sub

'The user will call this sub to connect to Bnet
Public Sub Connect()
    Call sckBNET.Connect(sServer, 6112)
End Sub

'We connect to bnet, raise event and send logon info
Private Sub sckBNET_OnConnect()
        RaiseEvent sckConnected
        wsBNET.SendData Chr(3) & Chr(4) & sUsername & vbCrLf & sPassword & vbCrLf
End Sub

'We recieve data from bnet. split data and parse it
Private Sub sckBNET_OnDataArrival(ByVal bytesTotal As Long)
    wsBNET.GetData s, 8
    If CBool(InStr(1, s, vbCrLf)) Then
        Dim Splt() As String, i As Integer
        Splt = Split(s, vbCrLf)
        For i = LBound(Splt) To UBound(Splt)
            If Not CStr(Splt(i)) = "" Then Call ParseBNET(CStr(Splt(i)))
        Next i
    Else
        If Not CStr(s) = "" Then Call ParseBNET(CStr(s))
    End If
End Sub

'Theres an error with the Bnet socket
Private Sub sckBNET_OnError(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    Call AddChat(vbRed, "[BNET] Error " & CStr(Number) & Space(1) & Description)
End Sub

'This will be used to get STRING[]'s from the data
Public Function GetString(s As String) As String
    GetString = Mid(s, InStr(1, s, Chr(34)) + 1, Len(s) - (InStr(1, s, Chr(34)) + 1))
End Function

'This does all the actual parsing
Private Sub ParseBNET(sData As String)
    Dim Splt() As String
    Splt = Split(sData, Space(1))
    Select Case Splt(1)
   
        Case "from", "Password:", "NULL", "'anonymous'", "your"
            Exit Sub
           
        Case "NAME"
            Username = Splt(2)
            RaiseEvent LoggedOnAs(Username)
           
        Case "CHANNEL"
            Channel = GetString(sData)
            RaiseEvent JoinedChannel(Channel)
           
        Case "USER"
            Username = Splt(2)
            Flags = Val("&H" & Splt(3))
            Product = Mid(Splt(3), 2, 4)
            RaiseEvent UserInChannel(Username, Flags, Product)
           
        Case "INFO"
            Message = GetString(sData)
            RaiseEvent Info(Message)
           
        Case "ERROR"
            Message = GetString(sData)
            RaiseEvent Error(Message)
           
        Case "TALK"
            Username = Splt(2)
            Message = GetString(sData)
            RaiseEvent UserTalks(Username, Message)
           
        Case "JOIN"
            Username = Splt(2)
            Flags = Val("&H" & Splt(3))
            Product = Mid(Splt(3), 2, 4)
            RaiseEvent UserJoins(Username, Flags, Product)
           
        Case "LEAVE"
            Username = Splt(2)
            Flags = Val("&H" & Splt(3))
            RaiseEvent UserLeaves(Username, Flags)
           
        Case Else
            RaiseEvent Unhandled(sData)
           
    End Select
End Sub

EDIT: I got stupid and left out all the actual connection stuff. Also, please excuse my 3:42 AM comments.
« Last Edit: April 04, 2005, 05:49:35 am by Joey »
I'd personally do as Joe suggests

You might be right about that, Joe.