Example #1
0
        public void Connect()
        {
            while (true)
            {
                _irc = new IrcClient();

                Logger.InfoFormat("Connecting to: {0}:{1}", _settings.Server, _settings.Port);
                try
                {
                    _irc.Encoding = System.Text.Encoding.UTF8;
                    _irc.SendDelay = 500;
                    _irc.ActiveChannelSyncing = true;
                    _irc.AutoReconnect = false;
                    _irc.AutoRejoinOnKick = true;
                    _irc.AutoRetry = true;
                    _irc.AutoRetryDelay = 10000;

                    _irc.OnQueryMessage += OnQueryMessage;
                    _irc.OnQueryNotice += OnQueryMessage;
                    _irc.OnError += OnError;
                    _irc.OnChannelMessage += OnChannelMessage;
                    //_irc.OnRawMessage += (x, e) => Logger.Info(e.Data.RawMessage);

                    _irc.Connect(_settings.Server, _settings.Port);
                }
                catch (Exception e)
                {
                    // Log this shit
                    Logger.Error(e.ToString());
                }

                Logger.Info("Connected! Joining channels");

                try
                {
                    _irc.Login(_settings.Nickname, "Name");

                    if (!String.IsNullOrWhiteSpace(_settings.Password))
                    {
                        _irc.RfcPrivmsg("NickServ", "identify " + _settings.Password);
                    }

                    foreach (var channel in _settings.GetAllChannels())
                    {
                        _irc.RfcJoin(channel);
                    }

                    _checkTimer.Change(TimeSpan.Zero, _settings.Period);
                }
                catch (Exception e)
                {
                    Logger.Error(e.ToString());
                }

                try
                {
                    _irc.Listen();
                    Logger.Error("Lost connection to IRC (Automatically reconnecting in 10 seconds)");
                    Thread.Sleep(10000);

                    if (_irc.IsConnected)
                    {
                        _irc.Disconnect();
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e.ToString());
                }
            }
        }