Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

irc in c# to vb.net conversion

continuing from question
https://www.experts-exchange.com/questions/22057102/trying-to-get-simple-connection-to-irc.html#17926267

useing libary from
http://www.codeproject.com/cs/internet/smartirc4net.asp

vb.net code used
Imports Meebey.SmartIrc4net
Imports System.Threading
Module Module2
    Public IRC = New IrcClient

    Public Sub OnQueryMessage(ByVal sender As Object, ByVal e As Meebey.SmartIrc4net.Data)
        Select Case e.Message(0)
            Case "dump_channel"
                Dim requested_channel As String = e.Message(1)
                Dim channel As Channel = IRC.GetChannel(requested_channel)
                IRC.SendMessage(SendType.Message, e.Nick, "<channel '" + requested_channel + "'>")
                IRC.SendMessage(SendType.Message, e.Nick, "Name: '" + channel.Name + "'")
                IRC.SendMessage(SendType.Message, e.Nick, "Topic: '" + channel.Topic + "'")
                IRC.SendMessage(SendType.Message, e.Nick, "Mode: '" + channel.Mode + "'")
                IRC.SendMessage(SendType.Message, e.Nick, "Key: '" + channel.Key + "'")
                IRC.SendMessage(SendType.Message, e.Nick, "UserLimit: '" + channel.UserLimit + "'")
                Dim nickname_list As String = ""
                nickname_list += "Users: "
                Dim it As IDictionaryEnumerator = channel.Users.GetEnumerator
                While it.MoveNext
                    Dim key As String = CType(it.Key, String)
                    Dim channeluser As ChannelUser = CType(it.Value, ChannelUser)
                    nickname_list += "("
                    If channeluser.IsOp Then
                        nickname_list += "@"
                    End If
                    If channeluser.IsVoice Then
                        nickname_list += "+"
                    End If
                    nickname_list += ")" + key + " => " + channeluser.Nick + ", "
                End While
                IRC.SendMessage(SendType.Message, e.Nick, nickname_list)
                IRC.SendMessage(SendType.Message, e.Nick, "</channel>")
                ' break
            Case "gc"
                GC.Collect()
                ' break
            Case "join"
                IRC.RfcJoin(e.Message(1))
                ' break
            Case "part"
                IRC.RfcPart(e.Message(1))
                ' break
            Case "die"
                Exit Sub
                ' break
        End Select
    End Sub
    Public Sub OnError(ByVal sender As Object, ByVal e As Data)
        Form1.ListBox1.Items.Add("Error: " + e.Message)
        Exit Sub
    End Sub

    Public Sub OnRawMessage(ByVal sender As Object, ByVal e As Data)
        Form1.ListBox1.Items.Add("Received: " & e.RawMessage)
    End Sub
    Public Sub Connect(ByVal server As [String], ByVal irc_port As Int32, ByVal message As [String])
        Thread.CurrentThread.Name = "Main"
        IRC.SendDelay = 400
        'irc.SendDelay = 200;
        IRC.AutoRetry = True
        IRC.ChannelSyncing = True
        'irc.OnQueryMessage += new MessageEventHandler(OnQueryMessage);


        Dim serverlist As String()
        serverlist = New String() {server}
        Dim port As Integer = irc_port
        Dim channel As String = "#OCS"
        If (IRC.Connect(serverlist, port) = True) Then
            IRC.Login("SmartIRC", "Stupid Bot")
            IRC.Join(channel)
            IRC.Message(SendType.Message, channel, message)
            IRC.Message(SendType.Action, channel, " thinks this is cool")
            IRC.Message(SendType.Notice, channel, "SmartIrc4net rocks!")
            Call (New Thread(New ThreadStart(AddressOf ReadCommands))).Start()
            IRC.Listen()
            IRC.Disconnect()
        Else
            Form1.ListBox1.Items.Add("couldn't connect!")
        End If
    End Sub


    Public Sub ReadCommands()
        While True
            ' Form1.ListBox1.Items.Add(data.ToString)
        End While
    End Sub

    'Public  Sub Exit()
    '        System.Console.WriteLine("Exiting...")
    '        System.Environment.Exit(0)
    '    End Sub
End Module

***********************
i had to get the dll from the full project and then add it to the resources too
and i call the sub with
            Connect(txtServer.Text, txtPort.Text, "Testing this all out!!")
i have the server,port,chanel,nick on a form with textboxes
****
problems
1: how does one capture the send recive strings and display them on a form (listbox)
1a: how does one capture the  recive strings and display them on a form (listbox)

2: i coudnt get this to work either
    'Public  Sub Exit()
    '        System.Console.WriteLine("Exiting...")
    '        System.Environment.Exit(0)
    '    End Sub

NOTE: the above code does work..it connects and writes to the channel
Avatar of Mohamed Zedan
Mohamed Zedan
Flag of Canada image

can you post the original C# code for this

   'Public  Sub Exit()
    '        System.Console.WriteLine("Exiting...")
    '        System.Environment.Exit(0)
    '    End Sub
Visit this site it has the latest library version and all its documentation
Documentation at :
http://smartirc4net.meebey.net/docs/0.4.0/html/Meebey.SmartIrc4net.IrcClientMembers.html

The main library page at :
http://smartirc4net.meebey.net/jaws/
Avatar of Johnny

ASKER

i am having a really hard time with c#... im still on a learning curve for vb.net

some of the above c# code
    public static void OnError(object sender, ErrorEventArgs e)
    {
        System.Console.WriteLine("Error: " + e.ErrorMessage);
        Exit();
    }
    public static void OnRawMessage(object sender, IrcEventArgs e)
    {
        System.Console.WriteLine("Received: "+e.Data.RawMessage);
    }
   
    public static void Main(string[] args)
    {
        Thread.CurrentThread.Name = "Main";
        irc.SendDelay = 400;
        irc.ActiveChannelSyncing = true;
       
        irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
        irc.OnError += new ErrorEventHandler(OnError);
        irc.OnRawMessage += new IrcEventHandler(OnRawMessage);

        string[] serverlist;
        // the server we want to connect to, could be also a simple string
        serverlist = new string[] {"irc.dragonirc.com"};
        int port = 6667;
      string channel = "#OCS";
        try {
            irc.Connect(serverlist, port);
        } catch (ConnectionException e) {
            System.Console.WriteLine("couldn't connect! Reason: "+e.Message);
            Exit();
        }
       
        try {
            irc.Login("SmartIRC", "SmartIrc4net Test Bot");
            irc.RfcJoin(channel);
            new Thread(new ThreadStart(ReadCommands)).Start();
            irc.Listen();
            irc.Disconnect();
        } catch (ConnectionException) {
            Exit();
        } catch (Exception e) {
            System.Console.WriteLine("Error occurred! Message: "+e.Message);
            System.Console.WriteLine("Exception: "+e.StackTrace);
            Exit();
        }
    }
   
    public static void ReadCommands()
    {
        while (true) {
            irc.WriteLine(System.Console.ReadLine());
        }
    }
   
    public static void Exit()
    {
        System.Console.WriteLine("Exiting...");
        System.Environment.Exit(0);
    }
}
i think the environment.exit should work, but this is what i found in MSDN for the Environment.Exit method. apparently you need permission to call unmanaged code:

.NET Framework Security
SecurityPermission  for the ability to call unmanaged code. Associated enumeration: SecurityPermissionFlag.UnmanagedCode

do you get a SecurityException when you run this part of the code?

I found the original C# sample on a website ... the part for the sub Exit  is for the console application in the sample...
it is not necessary ... that no.2 in your problems for number 1 ....


no.1
To listen to the incoming messages ...

these 3 C# lines wire up the events in the class:
 
        irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
        irc.OnError += new ErrorEventHandler(OnError);
        irc.OnRawMessage += new IrcEventHandler(OnRawMessage);

you should either do the same when you start your application like so

   AddHandler irc.OnQueryMessage, AddressOf OnQueryMessage
   AddHandler irc.OnError, AddressOf OnError
   AddHandler irc.OnRawMessage, AddressOf OnRawMessage

or a better way is declare irc using withevents keyword

like this :
 Public withevents IRC as new IrcClient

and add handles irc.event to every sub that handles an event e.g.

 Public Shared Sub OnError(ByVal sender As Object, ByVal e As ErrorEventArgs) handles irc.OnError
...
 End Sub

 Public Shared Sub OnRawMessage(ByVal sender As Object, ByVal e As IrcEventArgs) handles irc.OnRawMessage
...
 End Sub

 Public Shared Sub OnQueryMessage(ByVal sender As Object, ByVal e As IrcEventArgs) handles irc.OnQueryMessage
...
end sub


this is the complete C# sample converted to vb.net


Imports System
Imports System.Collections
Imports System.Threading
Imports Meebey.SmartIrc4net

Public Class Test
 Public Shared irc As IrcClient = New IrcClient

 Public Shared Sub OnQueryMessage(ByVal sender As Object, ByVal e As IrcEventArgs)
   Select e.Data.MessageArray(0)
   Case "dump_channel"
     Dim requested_channel As String = e.Data.MessageArray(1)
     Dim channel As Channel = irc.GetChannel(requested_channel)
     irc.SendMessage(SendType.Message, e.Data.Nick, "CHANNEL'" + REQUESTED_CHANNEL + "'")
     irc.SendMessage(SendType.Message, e.Data.Nick, "Name:'" + channel.Name + "'")
     irc.SendMessage(SendType.Message, e.Data.Nick, "Topic:'" + channel.Topic + "'")
     irc.SendMessage(SendType.Message, e.Data.Nick, "Mode:'" + channel.Mode + "'")
     irc.SendMessage(SendType.Message, e.Data.Nick, "Key:'" + channel.Key + "'")
     irc.SendMessage(SendType.Message, e.Data.Nick, "UserLimit: '" + channel.UserLimit + "'")
     Dim nickname_list As String = "" 
     nickname_list += "Users: " 
     Dim it As IDictionaryEnumerator = channel.Users.GetEnumerator
     While it.MoveNext
       Dim key As String = CType(it.Key, String)
       Dim channeluser As ChannelUser = CType(it.Value, ChannelUser)
       nickname_list += key + " => " + channeluser.Nick + ", " 
     End While
     irc.SendMessage(SendType.Message, e.Data.Nick, nickname_list)
     irc.SendMessage(SendType.Message, e.Data.Nick, "")
     ' break
   Case "gc"
     GC.Collect
     ' break
   Case "join"
     irc.RfcJoin(e.Data.MessageArray(1))
     ' break
   Case "part"
     irc.RfcPart(e.Data.MessageArray(1))
     ' break
   Case "die"
     Exit
     ' break
   End Select
 End Sub

 Public Shared Sub OnError(ByVal sender As Object, ByVal e As ErrorEventArgs)
   System.Console.WriteLine("Error: " + e.ErrorMessage)
   Exit
 End Sub

 Public Shared Sub OnRawMessage(ByVal sender As Object, ByVal e As IrcEventArgs)
   System.Console.WriteLine("Received: " + e.Data.RawMessage)
 End Sub

 Public Shared Sub Main(ByVal args As String())
   Thread.CurrentThread.Name = "Main"
   irc.SendDelay = 200
   irc.ActiveChannelSyncing = True
   AddHandler irc.OnQueryMessage, AddressOf OnQueryMessage
   AddHandler irc.OnError, AddressOf OnError
   AddHandler irc.OnRawMessage, AddressOf OnRawMessage
   Dim serverlist As String()
   serverlist = New String() {"irc.fu-berlin.de"}
   Dim port As Integer = 6667
   Dim channel As String = "#smartirc"
   Try
     irc.Connect(serverlist, port)
   Catch e As ConnectionException
     System.Console.WriteLine("couldn't connect! Reason: " + e.Message)
     Exit
   End Try
   Try
     irc.Login("SmartIRC", "SmartIrc4net Test Bot")
     irc.RfcJoin(channel)
     Dim i As Integer = 0
     While i < 3
       irc.SendMessage(SendType.Message, channel, "test message " + i.ToString)
       irc.SendMessage(SendType.Action, channel, "thinks this is cool " + i.ToString)
       irc.SendMessage(SendType.Notice, channel, "SmartIrc4net rocks " + i.ToString)
       System.Math.Min(System.Threading.Interlocked.Increment(i),i-1)
     End While
     call (New Thread(AddressOf ReadCommands)).Start
     irc.Listen
     irc.Disconnect
   Catch generatedExceptionVariable0 As ConnectionException
     Exit
   Catch e As Exception
     System.Console.WriteLine("Error occurred! Message: " + e.Message)
     System.Console.WriteLine("Exception: " + e.StackTrace)
     Exit
   End Try
 End Sub

 Public Shared Sub ReadCommands()
   While True
     irc.WriteLine(System.Console.ReadLine)
   End While
 End Sub

 Public Shared Sub Exit()
   System.Console.WriteLine("Exiting...")
   System.Environment.Exit(0)
 End Sub
End Class
You don't need to run that code at all .... environment.Exit code that is ... this just ends the console application in the example ....
Avatar of Johnny

ASKER

the normal converting from c# to vb.net does not work right

it gives a pretty good try at it thogh

the code above gives 26 errors...
quit a few i couldnt work out too

(the code i pasted is what i came up with(and does connect and work)

this is not as easy as plug into a converter and it will run it seams the dll is diffrent somehow..maybe a diffrent version or something...

this is why i asked the questionthe way i did...

try it for your self load it up into vb.net youll see all the errors
heres the dll im useing by referance
http://dragonsworkshop.com/ee/Meebey.SmartIrc4net.dll

thanks for the suggestions tho..hopefully this will work its self out
ASKER CERTIFIED SOLUTION
Avatar of Mohamed Zedan
Mohamed Zedan
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
don't forget to tell me if it worked or not
Avatar of Johnny

ASKER

id like to figure out how to get this to print to a forms listbox

but thank you ever so much it works perfectly
i mean perfectly

would you like me to open a new question asking to make it to listboxs for send recive insted of the counsole??

id be happy to acept this and ask it in another question
no need I'll answer this for you here in 1 hour it's just I am leaving home to work ok .. no need for another question :)
There are 2 events from this class that would do the trick (hopefully) for you the
OnQueryMessage
OnChannelMessage


suppose you have a the listbox : recievedListBox
Either use Addhandler as the example code add these lines with the others like it
AddHandler irc.OnQueryMessage, Addressof OnQueryMessage
AddHandler irc.OnChannelMessage, Addressof OnChannelMessage

or  use handles as I suggested
all is up to you

 Public Shared Sub OnQueryMessage(ByVal sender As Object, ByVal e As IrcEventArgs) 'Handles irc.OnQueryMessage 'If you use with events remove the first comment mark
        recievedListBox.items.add(e.Data.Message)
 End Sub

 Public Shared Sub OnChannelMessage(ByVal sender As Object, ByVal e As IrcEventArgs) 'Handles irc.OnChannelMessage 'If you use with events remove the first comment mark
        recievedListBox.items.add(e.Data.Message)
 End Sub

Hope this helps ... :)
Also sorry for being late ... had to sort out somethings at work first :)
Avatar of Johnny

ASKER

thank you very much
did it work ?
Avatar of Johnny

ASKER

sorta...

i had the onquery all ready

i added the lines
        System.Console.WriteLine("Received: " + e.Data.RawMessage)
        Form1.ListBox1.Items.Add("Received: " + e.Data.RawMessage)
        My.Application.DoEvents()

in the onraw sub
and added a add to listbox under some of the other console writelines

it seasm to be working pretty well..
thanks again
your welcome