Entries:
Comments:
Discussions:

Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading user information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

IRC you with IRC.NET

You'd think IRC is so old school, wouldn't you? That it's so 90's or 00's? I did too. Until I started looking around a little more. And I found the IRC is still very much alive and well and used in many different official, business and interesting ways.

For example, one of my favorite networks, Twit (as in This Week in Tech, Windows Weekly, and the other great shows) uses IRC during its live shows for real time audience interaction. Using IRC takes their shows to places where a "traditional" show can never go.. [avoid using "to the next level"]. That's just one example of how IRC is still relevant today.

So okay, I've sold you on IRC. And being a developer, the next thing you'll want to know is how can you incorporate it in your app?

IRC.Net is just your ticket. There's a .Net 4 version, Silverlight 4 version and branches for WP7 and more...

IRC.NET

IRC.NET is a complete IRC (Internet Relay Chat) client library for the .NET Framework 4.0 and Silverlight 4.0. It aims to provide a complete and efficient implementation of the protocol as described in RFCs 1459 and 2812, as well as de-facto modern features of the protocol. The .NET Framework version of the library is also intended to compile and run under Mono 2.6 and later.

GitHub mirror at <https://github.com/alexreg/ircdotnet>.

NuGet package at <http://nuget.org/List/Packages/IrcDotNet>.
NuGet sample package at <http://nuget.org/List/Packages/IrcDotNet.Sample>.

For discussion about using or contributing to the IRC.NET library, please join the ##irc.net channel on FreeNode.

As you can see above, the IRC.Net Library is available as source and better yet, via NuGet.

The IRC.Net Library Solution includes the .Net and SL4 versions;

image

Here's a snap of the .Net 4 version;

image

Also available in a separate Solution are two samples;

These sample projects are intended to be used to familiarize oneself with the IRC.NET library and its API. The code that they contain should illustrate the  standard methods of accomplishing a variety of common tasks involving the library.

Samples
=======

* `MarkovChainTextBox`
  
   An IRC bot that runs as a Markov text generator. It trains on messages received from multiple channels across multiple IRC networks and can generate random sentences from this.
 
* `TwitterBot`
  
   An IRC bot that runs as a service for interacting with Twitter via its web API. It is capable of posting tweets sent to it for arbitrary users, as well as giving notifications whenever a new tweet by a 'followed' user is seen.

image

Here's a code snip from the TwitterBot sample;

protected override void InitializeChatCommandProcessors()
{
    base.InitializeChatCommandProcessors();

    this.ChatCommandProcessors.Add("lusers", ProcessChatCommandListUsers);
    this.ChatCommandProcessors.Add("login", ProcessChatCommandLogIn);
    this.ChatCommandProcessors.Add("logout", ProcessChatCommandLogOut);
    this.ChatCommandProcessors.Add("send", ProcessChatCommandSend);
    this.ChatCommandProcessors.Add("home", ProcessChatCommandHome);
    this.ChatCommandProcessors.Add("mentions", ProcessChatCommandMentions);
}

private void ProcessChatCommandListUsers(IrcClient client, IIrcMessageSource source,
    IList<IIrcMessageTarget> targets, string command, IList<string> parameters)
{
    var sourceUser = (IrcUser)source;

    if (parameters.Count != 0)
        throw new InvalidCommandParametersException(1);

    // List all currently logged-in twitter users.
    var replyTargets = GetDefaultReplyTarget(client, sourceUser, targets);
    client.LocalUser.SendMessage(replyTargets, "Currently logged-in Twitter users ({0}):",
        this.twitterUsers.Count);
    foreach (var tu in this.twitterUsers)
    {
        client.LocalUser.SendMessage(replyTargets, "{0} / {1} ({2} @ {3})",
            tu.TwitterUser.ScreenName, tu.TwitterUser.Name, tu.IrcUser.NickName, tu.IrcUser.Client.ServerName);
    }
}

private void ProcessChatCommandMentions(IrcClient client, IIrcMessageSource source,
    IList<IIrcMessageTarget> targets, string command, IList<string> parameters)
{
    var sourceUser = (IrcUser)source;
    var twitterBotUser = GetTwitterBotUser(sourceUser);

    if (parameters.Count != 0)
        throw new InvalidCommandParametersException(1);

    // List tweets on Home timeline of user.
    var replyTargets = GetDefaultReplyTarget(client, sourceUser, targets);
    client.LocalUser.SendMessage(replyTargets, "Recent tweets mentioning '{0}':",
        twitterBotUser.TwitterUser.ScreenName);
    foreach (var tweet in twitterBotUser.ListTweetsMentioningMe())
    {
        SendTweetInfo(client, replyTargets, tweet);
    }
}

private void SendTweetInfo(IrcClient client, IList<IIrcMessageTarget> targets, TwitterStatus tweet)
{
    client.LocalUser.SendMessage(targets, "@{0}: {1}", tweet.User.ScreenName,
        SanitizeTextForIrc(tweet.Text));
}

private void SendGreeting(IrcLocalUser localUser, IIrcMessageTarget target)
{
    localUser.SendNotice(target, "This is the {0}, welcome.", ProgramInfo.AssemblyTitle);
    localUser.SendNotice(target, "Message me with '.help' for instructions on how to use me.");
    localUser.SendNotice(target, "Remember to log in via a private message and not via the channel.");
}

private TwitterBotUser GetTwitterBotUser(IrcUser ircUser)
{
    var twitterUser = this.twitterUsers.SingleOrDefault(tu => tu.IrcUser == ircUser);
    if (twitterUser == null)
        throw new InvalidOperationException(string.Format(
            "User '{0}' is not logged in to Twitter.", ircUser.NickName));
    return twitterUser;
}

There's a couple cool touches included too. Like a Help Library doc's (i.e. the new VS2010 help);

SNAGHTML203177b7

image

And the included Class Diagram (Yeah, it's a small thing, but it's funny how fast the little things add up to big things...)

ClassDiagram_1

In short, if you're interest in IRC and building a .Net server or client app with it, the IRC.NET Library might be the perfect place to start.

 

The image picture, my Apple II IRC hangout, is courtesy of blakespot

Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation
    Unsubscribing
    Subscribing
  • NdcNdc

    Which irc server and which channel do people hang out to discuss microsoft technology?

  • GeorgeGeorge

    @Ndc, there's a very active C# channel on freenode (chat.freenode.net) at ##csharp. There's a couple other smaller channels there too for MS related things, like WPF.

Remove this comment

Remove this thread

Close

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.