All pastes #18684 Raw Edit

Something

public text v1 · immutable
#18684 ·published 2005-07-28 06:55 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using SocketSystem;

namespace AsyncTest
{
    class Program
    {
        private CSocketClient cockSocket;
        private string nick = "oliz";
        private string username = "lort";
        private string hostname = "luder";
        private string servername = "hejhej";
        private string realname = "Ole Johansen!!!";

        #region constructor
        public Program()
        {
            try
            {
                this.cockSocket = new CSocketClient(10240, null, new CSocketClient.MESSAGE_HANDLER(this.MessageHandlerClient),
                                        new CSocketClient.CLOSE_HANDLER(this.CloseHandler), new CSocketClient.ERROR_HANDLER(this.ErrorHandler));

                this.cockSocket.Connect("127.0.0.1", 6667);
                Console.ReadLine();

                //disconnect
                this.cockSocket.Disconnect();
                this.cockSocket.Dispose();
            }
            catch (Exception pException)
            {
                Console.WriteLine(pException.Message);
            }
        }
        #endregion

        private void sendUser(string username, string hostname, string servername, string realname)
        {
            string msg = "USER " + username + " " + hostname + " " + servername + " :" + realname + "\r\n";
            this.cockSocket.Send(msg);
        }
        private void sendNick(string nick)
        {
            string msg = "NICK " + nick + "\r\n";
            this.cockSocket.Send(msg);
        }
        private void sendPong(string value)
        {
            string msg = "PONG " + value;
            this.cockSocket.Send(msg);
        }

        private void parseIrcMessage(string msg)
        {
            // Display the string to the console window
            Console.WriteLine(msg);
            if (Regex.Match(msg, @"PING :(.+)").Success)
            {
                //Console.WriteLine("PING!");
                string a = Regex.Match(msg, @"PING :(.+)").Groups[1].ToString();
                this.sendPong(a);
            }
            else if (Regex.Match(msg, @"^:[^\s]+\sNOTICE AUTH.*?").Success)
            {
                //Console.WriteLine("NOTICE AUTH");
                this.sendUser(this.username, this.hostname, this.servername, this.realname);
                this.sendNick(this.nick);
            }
            else if (Regex.Match(msg, @"^:(\S+) +(PRIVMSG|NOTICE) +(\S+) +(.+)$").Success)
            {
                Console.WriteLine("PRIVMSG | NOTICE");
            }
            else if (Regex.Match(msg, @"^:(\S+) +(\d+) +(\S+) +(.+)$").Success)
            {
                Console.WriteLine("NUMERIC EVENT");
            }
            else if (Regex.Match(msg, @"^:(\S+) +MODE +(\S+) +(.+)$").Success)
            {
                Console.WriteLine("MODE");
            }
            else if (Regex.Match(msg, @"^:(\S+) +KICK +(\S+) +(\S+) +(.+)$").Success)
            {
                Console.WriteLine("KICK");
            }
            else if (Regex.Match(msg, @"^:(\S+) +TOPIC +(\S+) +(.+)$").Success)
            {
                Console.WriteLine("TOPIC");
            }
            else if (Regex.Match(msg, @"^:(\S+) +INVITE +\S+ +(.+)$").Success)
            {
                Console.WriteLine("INVITE");
            }
            else if (Regex.Match(msg, @"^:(\S+) +WALLOPS +(.+)$").Success)
            {
                Console.WriteLine("WALLOPS");
            }
            else if (Regex.Match(msg, @"^:(\S+) +(\S+) +(.+)$").Success)
            {
                Console.WriteLine("NICK, QUIT, JOIN, PART, possibly more?");
            }
            else if (Regex.Match(msg, @"^NOTICE +\S+ +(.+)$").Success)
            {
                Console.WriteLine("SERVER NOTICE");
            }
            else if (Regex.Match(msg, @"^ERROR +(.+)$").Success)
            {
                Console.WriteLine("ERROR");
            }
            else
            {
                Console.WriteLine("THIS IS NOT GOOD!!! COCKSOCKET!!!");
            }
        }

        #region Message handler
        /// <summary> Called when a message is extracted from the socket </summary>
        /// <param name="pSocket"> The SocketClient object the message came from </param>
        /// <param name="iNumberOfBytes"> The number of bytes in the RawBuffer inside the SocketClient </param>
        private void MessageHandlerClient(CSocketClient pSocket, Int32 iNumberOfBytes)
        {
            try
            {
                // Convert the message from a byte array to a string
                String strMessage = System.Text.ASCIIEncoding.ASCII.GetString(pSocket.GetRawBuffer, 0, iNumberOfBytes);

                //Parse shit
                this.parseIrcMessage(strMessage);
            }
            catch (Exception pException)
            {
                Console.WriteLine(pException.Message);
            }
        }
        #endregion

        #region close handler
        /// <summary> Called when a socket connection is closed </summary>
        /// <param name="pSocket"> The SocketClient object the message came from </param>
        private void CloseHandler(CSocketClient pSocket)
        {
            Console.WriteLine("Close Handler");
            Console.WriteLine("IpAddress: " + pSocket.GetIpAddress);
        }
        #endregion

        #region error handler
        /// <summary> Called when a socket error occurs </summary>
        /// <param name="pSocket"> The SocketClient object the message came from </param>
        /// <param name="pException"> The reason for the error </param>
        private void ErrorHandler(CSocketClient pSocket, Exception pException)
        {
            Console.WriteLine(pException.Message);
        }
        #endregion

   }
}