Using SmartIrc4net - IrcCommands
(Page 3 of 4 )
The next layer is provided by theIrcCommands class. IrcCommands derives from IrcConnection, but it adds several dozen additional methods that provide shortcuts to WriteLine (though WriteLine is still available should you ever need it). We can easily modify the previous application to use IrcCommands rather than IrcConnection. First, make irc an instance of IrcCommands rather than IrcConnection:
private IrcCommandsirc = new IrcCommands();
Then, we simply have to replace two methods:
void OnConnected(object sender, EventArgs e)
{
Console.WriteLine("Connected.");
irc.RfcNick("CSharp", Priority.Critical);
irc.RfcUser("CSharp", 0, "CSharp Bot",
Priority.Critical);
irc.RfcJoin(channel);
irc.Listen();
}
void OnReadLine(object sender, ReadLineEventArgs e)
{
if (e.Line.StartsWith("PING"))
{
string server = e.Line.Split(' ')[1];
irc.RfcPong(server, Priority.Critical);
Console.WriteLine("Responded to ping at {0}.",
DateTime.Now.ToShortTimeString());
}
}
There, our application now makes use of the new methods provided by IrcCommands. IrcCommands also defines a number of other methods, including methods such as Ban, Voice and Deop. These methods are pretty straightforward, but the SendMessage method is worth taking a look at. It requires three arguments, but it can also take an additional fourth argument containing a member of the Priority enumeration. Here are a few examples of the SendMessage method in use:
irc.SendMessage(SendType.Message, "Peyton", "Hello.");
irc.SendMessage(SendType.Message, channel, "Hello,
channel.", Priority.BelowMedium);
irc.SendMessage(SendType.Action, channel, "dances");
irc.SendMessage(SendType.Notice, "Peyton", "THIS IS A
NOTICE.");
irc.SendMessage(SendType.CtcpRequest, "Peyton",
"VERSION");
// Convert the time to a UNIX timestamp
TimeSpan timestamp = (DateTime.UtcNow - new DateTime
(1970, 1, 1));
irc.SendMessage(SendType.CtcpReply, "Peyton", "PING"+
(int)timestamp.TotalSeconds);
Next: IrcClient >>
More .NET Articles
More By Peyton McCullough