Example #1
0
        /// <summary>
        /// Opens the IRC session and attempts to connect to a server.
        /// </summary>
        /// <param name="server">The hostname or IP representation of a server.</param>
        /// <param name="port">The IRC port.</param>
        /// <param name="isSecure">True to use an encrypted (SSL) connection, false to use plain text.</param>
        /// <param name="nickname">The desired nickname.</param>
        /// <param name="userName">The username that will be shown to other users.</param>
        /// <param name="fullname">The full name that will be shown to other users.</param>
        /// <param name="autoReconnect">Indicates whether to automatically reconnect upon disconnection.</param>
        /// <param name="password">The optional password to supply while logging in.</param>
        /// <param name="invisible">Determines whether the +i flag will be set by default.</param>
        /// <param name="findExternalAddress">Determines whether to find the external IP address by querying the IRC server upon connect.</param>
        public void Open(string server, int port, bool isSecure, string nickname,
                         string userName, string fullname, bool autoReconnect, string password = null, bool invisible = false, bool findExternalAddress = true,
                         ProxyInfo proxy = null)
        {
            if (string.IsNullOrEmpty(nickname))
            {
                throw new ArgumentNullException("Nickname");
            }
            _password            = password;
            _isInvisible         = invisible;
            _findExternalAddress = findExternalAddress;
            this.Nickname        = nickname;
            this.Server          = server;
            this.Port            = port;
            this.IsSecure        = isSecure;
            this.Username        = userName;
            this.FullName        = fullname;
            this.NetworkName     = this.Server;
            this.UserModes       = new char[0];
            this.AutoReconnect   = autoReconnect;
            this.Proxy           = proxy;

            _captures = new List <IrcCodeHandler>();
            _conn.Open(server, port, isSecure, this.Proxy);
            this.State = IrcSessionState.Connecting;
        }
Example #2
0
        /// <summary>
        ///     Opens the IRC session and attempts to connect to a server.
        /// </summary>
        /// <param name="server">The hostname or IP representation of a server.</param>
        /// <param name="port">The IRC port.</param>
        /// <param name="isSecure">True to use an encrypted (SSL) connection, false to use plain text.</param>
        /// <param name="nickname">The desired nickname.</param>
        /// <param name="userName">The username that will be shown to other users.</param>
        /// <param name="fullname">The full name that will be shown to other users.</param>
        /// <param name="autoReconnect">Indicates whether to automatically reconnect upon disconnection.</param>
        /// <param name="password">The optional password to supply while logging in.</param>
        /// <param name="invisible">Determines whether the +i flag will be set by default.</param>
        /// <param name="findExternalAddress">
        ///     Determines whether to find the external IP address by querying the IRC server upon
        ///     connect.
        /// </param>
        public void Open(string server, int port, bool isSecure, string nickname,
                         string userName, string fullname, bool autoReconnect, string password = null, bool invisible = false,
                         bool findExternalAddress = true,
                         ProxyInfo proxy          = null)
        {
            if (string.IsNullOrEmpty(nickname))
            {
                throw new ArgumentNullException("nickname");
            }
            _password            = password;
            _isInvisible         = invisible;
            _findExternalAddress = findExternalAddress;
            Nickname             = nickname;
            Server        = server;
            Port          = port;
            IsSecure      = isSecure;
            Username      = userName;
            FullName      = fullname;
            NetworkName   = Server;
            UserModes     = new char[0];
            AutoReconnect = autoReconnect;
            Proxy         = proxy;

            _conn.Open(server, port, isSecure, Proxy);
            State = IrcSessionState.Connecting;
        }
Example #3
0
 private void OnReconnect()
 {
     if (this.State == IrcSessionState.Disconnected)
     {
         this.State = IrcSessionState.Connecting;
         _conn.Open(this.Server, this.Port, this.IsSecure, this.Proxy);
     }
 }
Example #4
0
 private void OnReconnect()
 {
     if (State == IrcSessionState.Disconnected)
     {
         State = IrcSessionState.Connecting;
         _conn.Open(Server, Port, IsSecure, Proxy);
     }
 }
Example #5
0
        /// <summary>
        ///     Default constructor.
        /// </summary>
        public IrcSession()
        {
            State        = IrcSessionState.Disconnected;
            UserModes    = new char[0];
            _syncContext = SynchronizationContext.Current;

            _conn                  = new IrcConnection();
            _conn.Connected       += ConnectedHandler;
            _conn.Disconnected    += DisconnectedHandler;
            _conn.Heartbeat       += HeartbeatHandler;
            _conn.MessageReceived += MessageReceivedHandler;
            _conn.MessageSent     += MessageSentHandler;
            _conn.Error           += ConnectionErrorHandler;

            AddHandler(new IrcCodeHandler(e =>
            {
                e.Handled = true;
                if (e.Message.Parameters.Count < 2)
                {
                    return(true);
                }

                var parts = e.Message.Parameters[1].Split('@');
                if (parts.Length > 1)
                {
                    if (!IPAddress.TryParse(parts[1], out IPAddress external))
                    {
                        async void Lookup()
                        {
                            try
                            {
                                var host = await Dns.GetHostEntryAsync(parts[1]);
                                if (host.AddressList.Length > 0)
                                {
                                    ExternalAddress = host.AddressList[0];
                                }
                            }
                            catch
                            {
                            }
                        }
                        Lookup();
                    }
                    else
                    {
                        ExternalAddress = external;
                    }
                }
                return(true);
            }, IrcCode.RplUserHost));
        }
Example #6
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        public IrcSession()
        {
            this.State     = IrcSessionState.Disconnected;
            this.UserModes = new char[0];
            _syncContext   = SynchronizationContext.Current;

            _conn                  = new IrcConnection();
            _conn.Connected       += new EventHandler(_conn_Connected);
            _conn.Disconnected    += new EventHandler(_conn_Disconnected);
            _conn.Heartbeat       += new EventHandler(_conn_Heartbeat);
            _conn.MessageReceived += new EventHandler <IrcEventArgs>(_conn_MessageReceived);
            _conn.MessageSent     += new EventHandler <IrcEventArgs>(_conn_MessageSent);
            _conn.Error           += new EventHandler <ErrorEventArgs>(_conn_ConnectionError);
        }
Example #7
0
        private void OnOther(IrcMessage message)
        {
            int code;

            if (int.TryParse(message.Command, out code))
            {
                var e = new IrcInfoEventArgs(message);
                if (e.Code == IrcCode.RPL_WELCOME)
                {
                    if (e.Text.StartsWith("Welcome to the "))
                    {
                        var parts = e.Text.Split(' ');
                        this.NetworkName = parts[3];
                    }
                    this.State = IrcSessionState.Connected;
                }

                if (_captures.Count > 0)
                {
                    lock (_captures)
                    {
                        var capture = _captures.Where((c) => c.Codes.Contains(e.Code)).FirstOrDefault();
                        if (capture != null)
                        {
                            if (capture.Handler(e))
                            {
                                _captures.Remove(capture);
                            }
                            if (e.Handled)
                            {
                                return;
                            }
                        }
                    }
                }

                this.RaiseEvent(this.InfoReceived, e);
            }
        }
 private void NotifyChangeState(IrcSessionState state, String message)
 {
     var previousState = _state;
     ChangeState(state, message);
     ChangeState(previousState);
 }
 private void NotifyChangeState(IrcSessionState state, IrcException exception)
 {
     var previousState = _state;
     ChangeState(state, exception);
     ChangeState(previousState);
 }
 private void ChangeState(IrcSessionState state, String message)
 {
     lock (_lock)
     {
         State = state;
         IrcStateChangedEventArgs arg;
         arg = new IrcStateChangedEventArgs(message);
         _events.InvokeEvent(EventStateChanged, this, arg);
     }
 }
 private void ChangeState(IrcSessionState state)
 {
     ChangeState(state, String.Empty);
 }
 private void ChangeState(IrcSessionState state, IrcException exception)
 {
     lock (_lock)
     {
         State = state;
         IrcStateChangedEventArgs arg;
         arg = new IrcStateChangedEventArgs(exception);
         _events.InvokeEvent(EventStateChanged, this, arg);
     }
 }
Example #13
0
 private void _conn_Disconnected(object sender, EventArgs e)
 {
     this.State = IrcSessionState.Disconnected;
 }
Example #14
0
 private void DisconnectedHandler(object sender, EventArgs e)
 {
     State = IrcSessionState.Disconnected;
 }