Example #1
0
 public IrcClient(string server, int port)
 {
     Server     = server;
     Port       = port;
     State      = IRCState.Closed;
     MaxRetries = 5;
 }
Example #2
0
        /// <summary>
        /// https://tools.ietf.org/html/rfc2812#section-3.1.7
        /// </summary>
        public virtual void Quit()
        {
            _checkRegistration();

            State = IRCState.Closing;
            _send("QUIT");
            _planned = true;
            _thread.Abort();
        }
Example #3
0
        /// <summary>
        /// Main thread loop
        /// </summary>
        private void _run()
        {
            TcpClient irc = new TcpClient();

            try
            {
                //  Check parameters
                if (string.IsNullOrWhiteSpace(_nick))
                {
                    throw new ArgumentNullException("Nick is required.");
                }

                //  Connect to server
                irc.Connect(Server, Port);
                Debug.WriteLine("Connected to {0}:{1}", Server, Port);

                //  Get network stream
                NetworkStream stream = irc.GetStream();
                _stream = new StreamWriter(stream);
                _channels.Clear();

                //  Connection Registration
                //  https://tools.ietf.org/html/rfc2812#section-3.1
                State = IRCState.Registering;

                //  https://tools.ietf.org/html/rfc2812#section-3.1.1
                if (!string.IsNullOrWhiteSpace(_pass))
                {
                    _send("PASS " + _pass);
                }

                //  https://tools.ietf.org/html/rfc2812#section-3.1.2
                _send("NICK " + _nick);

                //  https://tools.ietf.org/html/rfc2812#section-3.1.3
                if (!string.IsNullOrWhiteSpace(_user))
                {
                    _send("USER " + _user);
                }

                using (var sr = new StreamReader(stream))
                {
                    //  https://tools.ietf.org/html/rfc2812#section-3.1
                    if (_read(sr).Command != "001")
                    {
                        throw new Exception("Registration Failed. Welcome message expected");
                    }

                    //  Raise connect event
                    State = IRCState.Registered;
                    OnConnect();
                    Debug.WriteLine("Connected thread: {0}", Thread.CurrentThread.ManagedThreadId);

                    while (true)
                    {
                        _read(sr);
                    }
                }
            }
            catch (Exception e)
            {
                State = IRCState.Error;
                Debug.WriteLine("Unhandled Exception: {0}", e.ToString());
            }
            finally
            {
                //  Close TCP Client
                _stream = null;
                if (irc?.Connected ?? false)
                {
                    irc.Close();
                }

                //  Raise Disconnect on a new thread
                new Thread(new ThreadStart(OnDisconnect)).Start();
            }
        }
Example #4
0
 public void Connect(string nick, string user, string pass)
 {
     State = IRCState.Connecting;
     _connect(nick, user, pass);
 }