Example #1
0
        private IrcState Connect()
        {
            IrcState state = new IrcState(this);

            state.Start();
            return(state);
        }
Example #2
0
 private void Disconnect(IrcState state)
 {
     try {
         state.Client.Disconnect();
     } catch (Exception ex) {
         Logger.Log("Exception while disconnecting from irc: {0}", ex.Message);
     }
 }
Example #3
0
 private void Disconnect()
 {
     if (identity.join_channels)
     {
         Disconnect(joined_state);
         joined_state = null;
     }
 }
Example #4
0
        public void Dispose()
        {
            if(State == IrcState.Closed)
                return;

            if (State == IrcState.Registered)
                _messageHandler.WriteRawMessage("QUIT", false, true);

            State = IrcState.Closing;
            _textReader.Dispose();
            _messageHandler.Dispose();
            _clientSocket.Dispose();
            State = IrcState.Closed;
        }
Example #5
0
        public void Connect(string server, int port)
        {
            State = IrcState.Connecting;
            _clientSocket = new TcpClient();
            _clientSocket.Connect(server, port);

            if (!_clientSocket.Connected)
                throw new Exception("Connection failed");

            var stream = _clientSocket.GetStream();

            _textReader = new StreamReader(stream);
            var writer = new StreamWriter(stream);
            _messageHandler = new MessageHandler(writer);
            State = IrcState.Connected;
        }
Example #6
0
        public IrcNotification(DBNotification notification)
            : base(notification)
        {
            /* Connect to server and join channels */

            using (DB db = new DB()) {
                using (IDbCommand cmd = db.CreateCommand()) {
                    cmd.CommandText = "SELECT * FROM IrcIdentity WHERE id = @id;";
                    DB.CreateParameter(cmd, "id", notification.ircidentity_id.Value);
                    using (IDataReader reader = cmd.ExecuteReader()) {
                        if (!reader.Read())
                        {
                            throw new ApplicationException(string.Format("Could not find the irc identity {0}", notification.ircidentity_id.Value));
                        }
                        identity = new DBIrcIdentity(reader);
                    }
                }
            }

            if (identity.join_channels)
            {
                joined_state = Connect();
            }
        }
Example #7
0
		private void Disconnect (IrcState state)
		{
			try {
				state.Client.Disconnect ();
			} catch (Exception ex) {
				Logger.Log ("Exception while disconnecting from irc: {0}", ex.Message);
			}
		}
Example #8
0
		private void Disconnect ()
		{
			if (identity.join_channels) {
				Disconnect (joined_state);
				joined_state = null;
			}
		}
Example #9
0
		private IrcState Connect ()
		{
			IrcState state = new IrcState (this);
			state.Start ();
			return state;
		}
Example #10
0
		public IrcNotification (DBNotification notification)
			: base (notification)
		{
			/* Connect to server and join channels */

			using (DB db = new DB ()) {
				using (IDbCommand cmd = db.CreateCommand ()) {
					cmd.CommandText = "SELECT * FROM IrcIdentity WHERE id = @id;";
					DB.CreateParameter (cmd, "id", notification.ircidentity_id.Value);
					using (IDataReader reader = cmd.ExecuteReader ()) {
						if (!reader.Read ())
							throw new ApplicationException (string.Format ("Could not find the irc identity {0}", notification.ircidentity_id.Value));
						identity = new DBIrcIdentity (reader);
					}
				}
			}

			if (identity.join_channels)
				joined_state = Connect ();
		}
Example #11
0
        /// <summary>
        /// Initialises the IRC system
        /// </summary>
        /// <param name="settings">The settings to use while running the IRC tool</param>
        public static void Init()
        {
            JtvSettings Settings = desBot.State.JtvSettings.Value;
            if (Settings == null) throw new Exception("No settings specified");
            if (string.IsNullOrEmpty(Settings.Channel)) throw new Exception("No channel specified");
            if (string.IsNullOrEmpty(Settings.Nickname)) throw new Exception("No JTV username specified");
            if (string.IsNullOrEmpty(Settings.Password)) throw new Exception("No JTV password specified");

            //disconnect
            if (client != null && client.IsConnected) client.Disconnect();
            client = null;

            //kill restart timer
            autorestart = null;
            detecthang = null;

            //initial values
            state = IrcState.None;
            Program.Log("IRC initialising");

            //attach event handler to send delay
            desBot.State.SendDelay.OnChanged += new OnPropertyValueChanged<int, int>(sendDelay_Changed);

            //set up state
            State = IrcState.None;
            Settings.Channel = Settings.Channel.ToLower(); //lower case to prevent mismatchign of string casings
            client = new IrcClient();

            //text encoding
            Encoding encoding = null;
            try
            {
                //try UTF8-encoding
                encoding = new System.Text.UTF8Encoding(false, false);
            }
            catch(Exception)
            {
                try
                {
                    //try codepage 1252 first (western european ANSI)
                    encoding = System.Text.Encoding.GetEncoding(1252);
                }
                catch (Exception)
                {
                    //fallback to ASCII encoding
                    encoding = new System.Text.ASCIIEncoding();
                }
            }
            client.Encoding = encoding;
            Program.Log("Using IRC text encoding codepage: " + encoding.CodePage.ToString());

            //set up config
            client.SendDelay = desBot.State.SendDelay.Value;
            client.AutoNickHandling = false;

            //set up event handlers
            client.OnConnectionError += new EventHandler(client_OnConnectionError);
            client.OnConnecting += new System.EventHandler(client_OnConnecting);
            client.OnConnected += new System.EventHandler(client_OnConnected);
            client.OnRegistered += new System.EventHandler(client_OnRegistered);
            client.OnDisconnecting += new System.EventHandler(client_OnDisconnecting);
            client.OnDisconnected += new System.EventHandler(client_OnDisconnected);
            client.OnOp += new OpEventHandler(client_OnOp);
            client.OnDeop += new DeopEventHandler(client_OnDeop);
            client.OnNames += new NamesEventHandler(client_OnNames);
            client.OnJoin += new JoinEventHandler(client_OnJoin);
            client.OnPart += new PartEventHandler(client_OnPart);
            client.OnQueryMessage += new IrcEventHandler(client_OnQueryMessage);
            client.OnQueryNotice += new IrcEventHandler(client_OnQueryNotice);
            client.OnChannelMessage += new IrcEventHandler(client_OnChannelMessage);
            client.OnRawMessage += new IrcEventHandler(client_OnRawMessage);

            //set up auto-reconnect
            OnStateChanged += new StateChangedEventHandler(Irc_OnStateChanged);

            //set up hang detection timer
            lastcheck = IrcState.Connecting;
            detecthang = new Timer(new TimerCallback(DetectHang), null, 60000, 60000);

            //reset bansystem
            BanSystem.Reset();

            //spawn dedicated thread
            new Thread(new ThreadStart(client_Listen)).Start();
        }
Example #12
0
 public static void DetectHang(object ignored)
 {
     lock (desBot.State.GlobalSync)
     {
         if (ignored != client) return;
         if (State < IrcState.Ready)
         {
             if (lastcheck == State)
             {
                 Disconnect("Logic hang detected");
                 detecthang = null;
             }
             else lastcheck = State;
         }
         else
         {
             detecthang = null;
         }
     }
 }
Example #13
0
        public virtual void Login(string nick, string password)
        {
            State = IrcState.Registering;
            _messageHandler.WriteRawMessage($"USER {nick}", false, true);
            _messageHandler.WriteRawMessage($"PASS {password}", false, true);
            _messageHandler.WriteRawMessage($"NICK {nick}", false, true);

            _buffer = _textReader.ReadLine();

            if(_buffer == null)
                throw new Exception("Login failed");

            if (_buffer.Split(' ')[1] != "001")
                throw new Exception("Registration Failed. Welcome message expected");

            _messageHandler.WriteRawMessage("CAP REQ :twitch.tv/membership", false, true);
            _messageHandler.WriteRawMessage("CAP REQ :twitch.tv/commands", false, true);
            _messageHandler.WriteRawMessage("CAP REQ :twitch.tv/tags", false, true);
            State = IrcState.Registered;
        }