Server Class

Note: Currently the information is pre-release, and could be changed at any time before the release. Please refer to the code for the most accurate use of this object.
The server class contains everything that you'd need to communicate with an IRC Server.

Connecting to and irc server only takes two lines of code. The first line instantiates a new server object, and requires that you supply the server name, and port that you'd like to connect to. Then you'll need to open the connection by calling the connect command.

     Server serv = Server("irc.freenode.net", 6667);
     serv.Connect();

Once you have established your connection, the next step is to send the handshake which identifies you to the server. This is done by sending the both the NICK and USER commands to the server.

The NICK command only requires your nickname:
NICK <nickname>
While the USER command needs to have your nickname, host name, server name, and real name:
_USER <nickname> "<host name>" "<server name>" <real name>
     serv.Send("NICK Modesto");
     serv.Send("USER ModestoBot \"modesto.codeplex.com\" \"\" ModestoBot");

After that it's nothing but listening to the server and reacting to the commands.

     while (serv.IsConnected())
     {
          int length = serv.Recv(BUFFER_LENGTH, buffer);

          if(length > 0) {
               RawCmd cmd = parseCommand(received);
               serv.React(cmd);
          }
     }

Example

Below is a full example of how you can use the Server class. While the example is the suggest method to execute the code, you should feel free to experiment and use any style that you are comfortable with.

     #define BUFFER_LENGTH 2048
     char * received;

     // Assigns the address and port to connect to.
     Server serv = Server("irc.efnet.net", 6667);

     // Opens the connection
     if (serv.Connect())
     {
          // Sends the handshake information
          serv.Send("NICK Modesto");
          serv.Send("USER ModestoBot \"modesto.codeplex.com\" \"\" ModestoBot");
     }

     while (serv.IsConnected())
     {
          // Passes the received argument by reference, 
          // places the received message into that argument,
          // then returns the length of that message.
          int length = serv.Recv(BUFFER_LENGTH, buffer);

          if(length > 0) {
               RawCmd cmd = parseCommand(received);
               serv.React(cmd);

               if (serv.IsInChannel("#MyChannel")
               {
                    serv.Join("#MyChannel");
               }

          } else {
               serv.Disconnect();
          }
     }

API Reference

Method Name Description
void Server(std::string address, int port) The constructor which assigns the address to connect to, and the port through which the connection should be established.
bool Connect() Connects to the server which was specified in the Server's constructor.
bool Disconnect() Disconnects from the server.
bool IsConnected() Tells whether the connection is still established.
bool Join(std::string channel) Sends a JOIN command to the server, and then execute the servers OnJoin event. The provided string must have the full channel name, include the prefixed #
bool Part(std::string channel) Sends a PART command to the server, and then execute the servers OnPart event. The provided string must have the full channel name, include the prefixed #
bool IsInChannel(std::string channel) Check to see if the bot is currently in the specified channel.
int Send(std::string message) Sends a raw message to the server.
int Recv(const int length, char &buffer) Receives a message from the server and places it in the buffer argument, and then returns the length of the buffer.
void React(RawCmd command) Allows the server to react to the received command by calling events

Last edited Aug 30, 2013 at 6:29 AM by cbutcher, version 8

Comments

No comments yet.