Пример #1
0
        /// <summary>
        /// Method that connects to irc-server
        /// </summary>
        public void Connect()
        {
            string inputLine;

            if (this.BotLog)
            {
                LogFileName = this.BotLogFileName;
                file = new FileStream(LogFileName, FileMode.Create, FileAccess.Write);
                sw = new StreamWriter(file);
            }

            try
            {
                // Connecting to server
                if (this.TextBox != null)
                    this.TextBox.AppendText("Connecting to server\n");
                irc = new TcpClient(ServerAddress, ServerPort);
                stream = irc.GetStream();
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);

                ping = new PingSender();
                ping.Start();

                writer.WriteLine (BotUser);
                writer.Flush ();
                writer.WriteLine ("NICK " + BotNick);
                writer.Flush ();
                writer.WriteLine ("JOIN " + BotChannel);
                writer.Flush ();

                while (true)
                {
                    // Oleskellaan kanavalla
                    while ( (inputLine = reader.ReadLine () ) != null )
                    {
                        if (this.BotLog)
                            sw.Write (inputLine + "\n");
                        // Ei kirjoiteta turhaa tietoa textboxiin
                        if (this.TextBox != null && inputLine.IndexOf("PONG") == -1 )
                            this.TextBox.AppendText(inputLine + "\n");

                        // Käsitellään serveriltä tuleva viesti

                        if (inputLine.StartsWith ("PING"))
                        {
                            int index = inputLine.LastIndexOf(":");
                            int numero = Int32.Parse(inputLine.Substring(index+1));
                            if (this.TextBox != null)
                                this.TextBox.AppendText("PONG :" +numero + "\n");
                            writer.WriteLine("PONG :" + numero);
                            writer.Flush();
                            Thread.Sleep (1000);
                        }

                        // Tarkistetaan päästiinkö serverille. Jos kyllä, joinataan kanavalla
                        if (inputLine.IndexOf("PONG") != -1 && this.Connected == false)
                        {
                            if (this.TextBox != null)
                                this.TextBox.AppendText("Connected to server\n");
                            this.JoinChannel(this.BotChannel);
                            if (this.TextBox != null)
                                this.TextBox.AppendText("Joined channel\n");

                            this.Connected = true;
                            Thread.Sleep(200);
                        }

                        if (inputLine.IndexOf("hello") != -1)
                        {
                            writer.WriteLine("PRIVMSG {0} : moi!", this.BotChannel);
                            writer.Flush();
                            Thread.Sleep(200);
                        }

                    }

                    // Suljetaan streamit
                    writer.Close ();
                    reader.Close ();
                    if (this.BotLog)
                        sw.Close();
                    irc.Close ();
                }
            }
            catch (Exception e)
            {
                if (this.TextBox != null)
                {
                    this.TextBox.AppendText("Connection problem");
                    this.TextBox.AppendText(e.ToString() + "\n");
                }
                Thread.Sleep(5000);
            }
        }
Пример #2
0
        /// <summary>
        /// Method that connects to irc-server
        /// </summary>
        public void Connect()
        {
            string inputLine;

            if (this.BotLog)
            {
                LogFileName = this.BotLogFileName;
                file        = new FileStream(LogFileName, FileMode.Create, FileAccess.Write);
                sw          = new StreamWriter(file);
            }

            try
            {
                // Connecting to server
                if (this.TextBox != null)
                {
                    this.TextBox.AppendText("Connecting to server\n");
                }
                irc    = new TcpClient(ServerAddress, ServerPort);
                stream = irc.GetStream();
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);

                ping = new PingSender();
                ping.Start();

                writer.WriteLine(BotUser);
                writer.Flush();
                writer.WriteLine("NICK " + BotNick);
                writer.Flush();
                writer.WriteLine("JOIN " + BotChannel);
                writer.Flush();

                while (true)
                {
                    // Oleskellaan kanavalla
                    while ((inputLine = reader.ReadLine()) != null)
                    {
                        if (this.BotLog)
                        {
                            sw.Write(inputLine + "\n");
                        }
                        // Ei kirjoiteta turhaa tietoa textboxiin
                        if (this.TextBox != null && inputLine.IndexOf("PONG") == -1)
                        {
                            this.TextBox.AppendText(inputLine + "\n");
                        }

                        // Käsitellään serveriltä tuleva viesti

                        if (inputLine.StartsWith("PING"))
                        {
                            int index  = inputLine.LastIndexOf(":");
                            int numero = Int32.Parse(inputLine.Substring(index + 1));
                            if (this.TextBox != null)
                            {
                                this.TextBox.AppendText("PONG :" + numero + "\n");
                            }
                            writer.WriteLine("PONG :" + numero);
                            writer.Flush();
                            Thread.Sleep(1000);
                        }

                        // Tarkistetaan päästiinkö serverille. Jos kyllä, joinataan kanavalla
                        if (inputLine.IndexOf("PONG") != -1 && this.Connected == false)
                        {
                            if (this.TextBox != null)
                            {
                                this.TextBox.AppendText("Connected to server\n");
                            }
                            this.JoinChannel(this.BotChannel);
                            if (this.TextBox != null)
                            {
                                this.TextBox.AppendText("Joined channel\n");
                            }

                            this.Connected = true;
                            Thread.Sleep(200);
                        }

                        if (inputLine.IndexOf("hello") != -1)
                        {
                            writer.WriteLine("PRIVMSG {0} : moi!", this.BotChannel);
                            writer.Flush();
                            Thread.Sleep(200);
                        }
                    }

                    // Suljetaan streamit
                    writer.Close();
                    reader.Close();
                    if (this.BotLog)
                    {
                        sw.Close();
                    }
                    irc.Close();
                }
            }
            catch (Exception e)
            {
                if (this.TextBox != null)
                {
                    this.TextBox.AppendText("Connection problem");
                    this.TextBox.AppendText(e.ToString() + "\n");
                }
                Thread.Sleep(5000);
            }
        }