Solved

irc in c# to vb.net conversion

Posted on 2006-11-12
17
660 Views
Last Modified: 2011-10-03
continuing from question
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22057102.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
0
Comment
Question by:Johnny
  • 11
  • 5
17 Comments
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17926647
can you post the original C# code for this

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

Expert Comment

by:Mohamed Zedan
ID: 17926678
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/
0
 

Author Comment

by:Johnny
ID: 17926789
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);
    }
}
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17926817
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?

0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17926821
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
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17926834
You don't need to run that code at all .... environment.Exit code that is ... this just ends the console application in the example ....
0
 

Author Comment

by:Johnny
ID: 17926876
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
0
 
LVL 7

Accepted Solution

by:
Mohamed Zedan earned 500 total points
ID: 17926907
Download the library from this sourceforge site it's the original one
http://prdownloads.sourceforge.net/smartirc4net/SmartIrc4net-0.3.5.bin.zip?download

And use this code you shouldn't find any troubles I tested it ... :

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 Case 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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17926911
don't forget to tell me if it worked or not
0
 

Author Comment

by:Johnny
ID: 17927518
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
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17928482
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 :)
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17929349
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 ... :)
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17929351
Also sorry for being late ... had to sort out somethings at work first :)
0
 

Author Comment

by:Johnny
ID: 17930409
thank you very much
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17930422
did it work ?
0
 

Author Comment

by:Johnny
ID: 17930517
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
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17932280
your welcome
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now