Question dotMSN; cant keep a connection.

Mixx

New member
Joined
Apr 19, 2011
Messages
3
Location
Veenendaal, Netherlands
Programming Experience
Beginner
Hello, this is my first post here! :)
For an assignment in school i have to write a really simple vb.net msn bot that answers one question. I am working with dotMSN (DotMSN - .NET Messenger library - Home) and i've been able to start up a connection with msn, but i whenever im connected, i disconnect instantly. This is the code:

VB.NET:
Option Strict Off
Imports System.Web
Imports System.Windows.Forms
Imports System.IO
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Threading
Imports System.Text
Imports System
Imports XihSolutions.DotMSN
Imports XihSolutions.DotMSN.DataTransfer

Public Class main
    Public WithEvents msn As XihSolutions.DotMSN.Messenger
    Public WithEvents SBHandler As XihSolutions.DotMSN.SBMessageHandler
    Public MSNHandler As XihSolutions.DotMSN.DataTransfer.MSNSLPHandler

    Public Message As XihSolutions.DotMSN.TextMessage
    Public PingMessage As PNG_Message
    Public Ping_Timer As System.Timers.Timer
    Public Connection_Timer As System.Timers.Timer

    Public Contacts As XihSolutions.DotMSN.ContactList
    Public ContactList As XihSolutions.DotMSN.ContactList.ListEnumerator
    Public Contact As XihSolutions.DotMSN.Contact

    Public i As Integer
    Public AccessLocked As Integer = 1

    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)




    Public Delegate Sub ConnectionEstablished(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Delegate Sub ConnectionClosed(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Delegate Sub ConnectingException(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Delegate Sub ConversationCreated(ByVal sender As System.Object, ByVal e As ConversationCreatedEventArgs)
    Public Delegate Sub SignedIn(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Delegate Sub SignedOff(ByVal sender As Object, ByVal e As SignedOffEventArgs)
    Public Delegate Sub SRVRErrorReceived(ByVal sender As Object, ByVal e As MSNErrorEventArgs)
    Public Delegate Sub ExceptionOccurred(ByVal sender As Object, ByVal e As ExceptionEventArgs)
    Public Delegate Sub HandleContactOnline(ByVal sender As Object, ByVal e As ContactEventArgs)
    Public Delegate Sub HandleContactOffline(ByVal sender As Object, ByVal e As ContactEventArgs)
    Public Delegate Sub ContactJoined(ByVal sender As Object, ByVal e As ContactEventArgs)
    Public Delegate Sub ContactLeft(ByVal sender As Object, ByVal e As ContactEventArgs)
    Public Delegate Sub AllContactsLeft(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Delegate Sub SessionClosed(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Delegate Sub ConversationException(ByVal sender As Object, ByVal e As ExceptionEventArgs)
    Public Delegate Sub TransferFinished(ByVal sender As Object, ByVal e As XihSolutions.DotMSN.DataTransfer.MSNSLPInvitationEventArgs)
    Public Delegate Sub TextMessageReceived(ByVal sender As Object, ByVal e As TextMessageEventArgs)


    ' CONSTANTS '         

    '(Remote user MSN account)'
    Public Const MSN_Target As String = "x@gmail.com"
    '(BOT account)'  
    Public Const MSN_Self As String = "y@hotmail.com"
    '(BOT account password)'
    Public Const MSN_PWD As String = "MSN_PWD"
    '(Other user password)'
    Public Const Remote_PWD As String = "Remote_PWD"
    '(File transfer directory)'
    Public Const FileExch_Dir As String = "C:\Program Files\Homeseer 2\FileExchange\"


    ' MENUS '              


    ' MAIN MENU'
    '-------------------------------------------------------------------------'
    Public MN(,) As String = New String(6, 3) _
                            {{"hi", "hello", "hey", "allo"}, _
                            {"quit", "exit", "kill", "terminate"}, _
                            {"temperature", "temp", "tmp", "tp"}, _
                            {"report", "rep", "rpt", "r"}, _
                            {"exec", "execx10", "x10", "x"}, _
                            {"sendfile", "sf", "send", ""}, _
                            {"signoff", "disconnect", "leave", "so"}}
    ' SUB MENUS '
    '-------------------------------------------------------------------------'
    Public MT(,) As String = New String(4, 3) _
                            {{"attic", "roof", "grenier", "[10"}, _
                            {"basement", "cave", "bast", "[68"}, _
                            {"office", "bureau", "off", "[64"}, _
                            {"bedroom", "main bedroom", "mbrt", "[67"}, _
                            {"exterior", "ext", "outside", "T1"}}
    '  



    Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        ownfunc.addLog("Loading")
        msn = New XihSolutions.DotMSN.Messenger
        Message = New XihSolutions.DotMSN.TextMessage

        msn.Credentials.Account = MSN_Self
        msn.Credentials.Password = MSN_PWD
        msn.Credentials.ClientID = "ClientID"
        msn.Credentials.ClientCode = "ClientCode"

        AddHandler msn.NameserverProcessor.ConnectionEstablished, AddressOf Connection_Established
        AddHandler msn.NameserverProcessor.ConnectionClosed, AddressOf Connection_Closed
        AddHandler msn.NameserverProcessor.ConnectingException, AddressOf Connecting_Exception
        AddHandler msn.ConversationCreated, AddressOf Conversation_Created
        AddHandler msn.Nameserver.SignedIn, AddressOf Signed_In
        AddHandler msn.Nameserver.SignedOff, AddressOf Signed_Off
        AddHandler msn.Nameserver.PingAnswer, AddressOf PingAnswerEventHandler
        AddHandler msn.Nameserver.ServerErrorReceived, AddressOf SRVR_Error_Received
        AddHandler msn.Nameserver.ExceptionOccurred, AddressOf Exception_Occurred
        AddHandler msn.Nameserver.ContactOnline, AddressOf Handle_Contact_Online
        AddHandler msn.Nameserver.ContactOffline, AddressOf Handle_Contact_Offline

        Ping_Timer = New System.Timers.Timer(10000)
        AddHandler Ping_Timer.Elapsed, AddressOf PingTimerHandler
        Ping_Timer.Enabled = True
        Ping_Timer.Stop()

        Connection_Timer = New System.Timers.Timer(20000)
        AddHandler Connection_Timer.Elapsed, AddressOf ConnectionTimerHandler
        Connection_Timer.Enabled = False

        MSNConnect()

    End Sub
    Private Sub MSNConnect()
connect:
        If msn.Owner.Status = PresenceStatus.Offline Then
            Try
                msn.Connect()
            Catch ex As Exception
                ownfunc.addLog("Can't log in to MSN" & vbCrLf)
                Exit Sub
            End Try
        End If
    End Sub

    Private Sub PingTimerHandler(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        SendPing(New PNG_Message)
    End Sub

    Private Sub ConnectionTimerHandler(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        MSNConnect()
    End Sub

    Public Sub PingAnswerEventHandler(ByVal sender As Object, ByVal e As PingAnswerEventArgs)
        Ping_Timer.Interval = e.SecondsToWait * 1000
    End Sub

    Private Sub SendPing(ByVal PingMessage)
        If msn.Connected Then
            Try
                msn.NameserverProcessor.SendMessage(PingMessage)
            Catch ex As Exception
                ownfunc.addLog("Error sending ping")
                msn.Disconnect()
            End Try
        End If
    End Sub
    ' '
    Public Sub Connection_Established(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BeginInvoke(New ConnectionEstablished(AddressOf Connected), New Object() {sender, e})
    End Sub

    Public Sub Connection_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BeginInvoke(New ConnectionClosed(AddressOf Disconnected), New Object() {sender, e})
    End Sub

    Public Sub Connecting_Exception(ByVal sender As Object, ByVal e As ExceptionEventArgs)
        Me.BeginInvoke(New ConnectingException(AddressOf Conn_Exception), New Object() {sender, e})
    End Sub

    Private Sub Conversation_Created(ByVal sender As System.Object, ByVal e As ConversationCreatedEventArgs)
        Me.BeginInvoke(New ConversationCreated(AddressOf New_Conversation), New Object() {sender, e})
    End Sub

    Public Sub Text_Message_Received(ByVal sender As Object, ByVal e As TextMessageEventArgs)
        Me.BeginInvoke(New TextMessageReceived(AddressOf MessageReceived), New Object() {sender, e})
    End Sub

    Private Sub Signed_In(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BeginInvoke(New SignedIn(AddressOf Logged_In), New Object() {sender, e})
    End Sub

    Private Sub Signed_Off(ByVal sender As Object, ByVal e As SignedOffEventArgs)
        Me.BeginInvoke(New SignedOff(AddressOf Logged_Off), New Object() {sender, e})
    End Sub

    Public Sub SRVR_Error_Received(ByVal sender As Object, ByVal e As MSNErrorEventArgs)
        Me.BeginInvoke(New SRVRErrorReceived(AddressOf ServerError), New Object() {sender, e})
    End Sub

    Public Sub Exception_Occurred(ByVal sender As Object, ByVal e As ExceptionEventArgs)
        Me.BeginInvoke(New ExceptionOccurred(AddressOf Exception), New Object() {sender, e})
    End Sub

    Private Sub Handle_Contact_Online(ByVal sender As Object, ByVal e As ContactEventArgs)
        Me.BeginInvoke(New HandleContactOnline(AddressOf Contact_Online), New Object() {sender, e})
    End Sub

    Private Sub Handle_Contact_Offline(ByVal sender As Object, ByVal e As ContactEventArgs)
        Me.BeginInvoke(New HandleContactOffline(AddressOf Contact_Offline), New Object() {sender, e})
    End Sub

    Public Sub Contact_Joined(ByVal sender As Object, ByVal e As ContactEventArgs)
        Me.BeginInvoke(New ContactJoined(AddressOf Cont_Joined), New Object() {sender, e})
    End Sub

    Public Sub Contact_Left(ByVal sender As Object, ByVal e As ContactEventArgs)
        Me.BeginInvoke(New ContactLeft(AddressOf Cont_Left), New Object() {sender, e})
    End Sub

    Public Sub All_Contacts_Left(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BeginInvoke(New AllContactsLeft(AddressOf All_Left), New Object() {sender, e})
    End Sub

    Public Sub Session_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BeginInvoke(New SessionClosed(AddressOf S_Closed), New Object() {sender, e})
    End Sub

    Public Sub Conversation_Exception(ByVal sender As Object, ByVal e As ExceptionEventArgs)
        Me.BeginInvoke(New ConversationException(AddressOf Conv_Exception), New Object() {sender, e})
    End Sub

    Public Sub msnTransferInvitationReceived(ByVal sender As Object, ByVal e As MSNSLPInvitationEventArgs) Handles msn.TransferInvitationReceived
        e.Accept = True
        If Not Directory.Exists(FileExch_Dir) Then
            Directory.CreateDirectory(FileExch_Dir)
        End If
        AddHandler e.TransferSession.TransferFinished, AddressOf Transfer_Finished
        e.TransferSession.AutoCloseStream = True
        e.TransferSession.StartDataTransfer(True)
        e.TransferSession.DataStream = New FileStream(FileExch_Dir & e.Filename, FileMode.Create, FileAccess.Write)
    End Sub

    Private Sub Transfer_Finished(ByVal sender As Object, ByVal e As XihSolutions.DotMSN.DataTransfer.MSNSLPInvitationEventArgs)
        Me.BeginInvoke(New TransferFinished(AddressOf Xfer_Finished), New Object() {sender, e})
    End Sub

    ' '
    Public Sub Connected(ByVal sender As Object, ByVal e As System.EventArgs)
        ownfunc.addLog("Connected to the server.")
    End Sub

    Public Sub Disconnected(ByVal sender As Object, ByVal e As System.EventArgs)
        ownfunc.addLog("Connection to the server has been closed.")
        Ping_Timer.Stop()
        Connection_Timer.Enabled = True
        Connection_Timer.Start()
    End Sub

    Public Sub Conn_Exception(ByVal sender As Object, ByVal e As ExceptionEventArgs)
        ownfunc.addLog("Connecting Exception Occurred" & e.Exception.ToString)
        Ping_Timer.Stop()
        Connection_Timer.Enabled = True
        Connection_Timer.Start()
    End Sub

    Private Sub New_Conversation(ByVal sender As System.Object, ByVal e As ConversationCreatedEventArgs)
        AddHandler e.Conversation.Switchboard.TextMessageReceived, AddressOf Text_Message_Received
        AddHandler e.Conversation.Switchboard.ContactJoined, AddressOf Contact_Joined
        AddHandler e.Conversation.Switchboard.SessionClosed, AddressOf Session_Closed
        AddHandler e.Conversation.Switchboard.AllContactsLeft, AddressOf All_Contacts_Left
        AddHandler e.Conversation.Switchboard.ContactLeft, AddressOf Contact_Left
        AddHandler e.Conversation.Switchboard.ExceptionOccurred, AddressOf Conversation_Exception
        AccessLocked = 2
        Sleep(2000)
    End Sub

    Private Sub Logged_In(ByVal sender As Object, ByVal e As System.EventArgs)
        ownfunc.addLog("Logged in to MSN" & vbCrLf)
        If Connection_Timer.Enabled Then Connection_Timer.Enabled = False
        Ping_Timer.Start()
        Sleep(1000)
        msn.Owner.Status = PresenceStatus.Online
        msn.Owner.Name = "Home"
    End Sub

    Private Sub Logged_Off(ByVal sender As Object, ByVal e As SignedOffEventArgs)
        ownfunc.addLog("Client disconnected" & vbCrLf)
        Ping_Timer.Stop()
        Connection_Timer.Enabled = True
        Connection_Timer.Start()
    End Sub

    Public Sub ServerError(ByVal sender As Object, ByVal e As MSNErrorEventArgs)
        ownfunc.addLog("Server error received " & e.MSNError.ToString)
    End Sub

    Public Sub Exception(ByVal sender As Object, ByVal e As ExceptionEventArgs)
        ownfunc.addLog("Exception Occurred " & e.Exception.ToString)
    End Sub

    Private Sub Contact_Online(ByVal sender As Object, ByVal e As ContactEventArgs)
        ownfunc.addLog(e.Contact.Mail & " is online.")
    End Sub

    Private Sub Contact_Offline(ByVal sender As Object, ByVal e As ContactEventArgs)
        ownfunc.addLog(e.Contact.Mail & " went offline.")
    End Sub

    Public Sub Cont_Joined(ByVal sender As Object, ByVal e As ContactEventArgs)
        ownfunc.addLog(e.Contact.Name + " has joined the conversation.")
    End Sub

    Public Sub Cont_Left(ByVal sender As Object, ByVal e As ContactEventArgs)
        ownfunc.addLog(e.Contact.Name + " has left the conversation.")
    End Sub

    Public Sub All_Left(ByVal sender As Object, ByVal e As System.EventArgs)
        ownfunc.addLog("There's no one left in the conversation.")
    End Sub

    Public Sub S_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
        ownfunc.addLog("The conversation is closed.")
    End Sub

    Public Sub Conv_Exception(ByVal sender As Object, ByVal e As ExceptionEventArgs)
        ownfunc.addLog("Conversation Exception " + e.Exception.ToString)
    End Sub

    Private Sub Xfer_Finished(ByVal sender As Object, ByVal e As XihSolutions.DotMSN.DataTransfer.MSNSLPInvitationEventArgs)
        ownfunc.addLog("Transfer Finished ")
    End Sub

    ' '
    Public Sub MessageReceived(ByVal sender As Object, ByVal e As TextMessageEventArgs)
        Dim Answer() As String
        Dim ds As String
        Dim dvref As Integer
        Dim en As Object
        Dim FileStr As System.IO.FileStream
        Dim SB As XihSolutions.DotMSN.SBMessageHandler = sender
        Dim i As Integer

        ownfunc.addLog(e.Sender.Name & ": " & e.Message.Text & vbCrLf)

        If e.Sender.Mail = MSN_Target Then AccessLocked = 0

        If AccessLocked = 0 Then
            Message.Text = e.Message.Text
            Answer = Split(Message.Text, " ")
            Select Case Answer(0)
                Case MN(0, 0), MN(0, 1), MN(0, 2), MN(0, 3)         'Greeting'
                    Answer(0) = Strings.Right(Answer(0), Len(Answer(0)) - 1).Insert(0, UCase(Strings.Left(Answer(0), 1)))
                    Message.Text = Answer(0) & " " & e.Sender.Name & " !"
                Case MN(1, 0), MN(1, 1), MN(1, 2), MN(1, 3)         'Quit'
                    Message.Text = "Bridge terminated by user"
                Case Else
                    Message.Text = "Syntax error"
            End Select
        ElseIf AccessLocked = 2 Then
            Message.Text = "Please Enter Password"
            AccessLocked = 1
        ElseIf AccessLocked = 1 Then
            If e.Message.Text = Remote_PWD Then
                Message.Text = "You are authorized. Now accepting commands."
                AccessLocked = 0
            Else
                Message.Text = "Incorrect Password. Please try again..."
                AccessLocked = 1
            End If
        End If

        SB.SendTextMessage(Message)
        If Message.Text = "Bridge terminated by user" Then
            Sleep(1000)
            Me.Close()
        End If
        ownfunc.addLog(Message.Text)

    End Sub
End Class


Public Class PNG_Message
    Inherits XihSolutions.DotMSN.Core.NSMessage
    Public Overrides Function GetBytes() As Byte()
        GetBytes = Encoding.UTF8.GetBytes("PNG" & vbCrLf)
    End Function
End Class

I have no clue what goes wrong, it doesnt give me an error. As soon as the connection is up, the connection is closed. I can't login to the server or anything. I really don't understand what goes wrong here.
 
Last edited by a moderator:
Top Bottom