Example #1
0
        static public ConfigValues ProcessConfig(string[] configArray)
        {
            ConfigValues returnableConfig = new ConfigValues();

            char[]   splits = { '=', };
            string[] aftersplit;
            for (int i = 0; i < configArray.Length; i++)
            {
                try
                {
                    aftersplit = configArray[i].Split(splits);
                }
                catch
                {
                    break;
                }
                if (aftersplit[0] == "name")
                {
                    returnableConfig.name = aftersplit[1];
                }

                if (aftersplit[0] == "host")
                {
                    returnableConfig.host = aftersplit[1];
                }

                if (aftersplit[0] == "port")
                {
                    returnableConfig.port = Int32.Parse(aftersplit[1]);
                }

                if (aftersplit[0] == "channel")
                {
                    returnableConfig.channel = aftersplit[1];
                }

                if (aftersplit[0] == "verbose")
                {
                    if (aftersplit[1] == "true")
                    {
                        returnableConfig.verbose = true;
                    }
                    else
                    {
                        returnableConfig.verbose = false;
                    }
                }
                if (aftersplit[0] == "version")
                {
                    returnableConfig.version = aftersplit[1];
                }
                if (aftersplit[0] == "badwordfilter")
                {
                    if (aftersplit[1] == "true")
                    {
                        returnableConfig.badwordfilter = true;
                    }
                    else
                    {
                        returnableConfig.badwordfilter = false;
                    }
                }
                if (aftersplit [0] == "password")
                {
                    returnableConfig.password = aftersplit [1];
                }
                if (aftersplit [0] == "minetest.autovoiceservers")
                {
                    if (aftersplit [1] == "true")
                    {
                        returnableConfig.minetest_autovoiceservers = true;
                    }
                    else if (aftersplit [1] == "false")
                    {
                        returnableConfig.minetest_autovoiceservers = false;
                    }
                }
            }

            return(returnableConfig);
        }
        public static ConfigValues ProcessConfig(string[] configArray)
        {
            ConfigValues returnableConfig = new ConfigValues();
            char[] splits = {'=',};
            string[] aftersplit;
            for (int i = 0; i < configArray.Length; i++) {
                try
                {
                aftersplit = configArray[i].Split (splits);
                }
                catch
                {
                    break;
                }
                if(aftersplit[0] == "name")
                {
                    returnableConfig.name = aftersplit[1];
                }

                if(aftersplit[0] == "host")
                {
                    returnableConfig.host = aftersplit[1];
                }

                if(aftersplit[0] == "port")
                {
                    returnableConfig.port = Int32.Parse (aftersplit[1]);
                }

                if(aftersplit[0] == "channel")
                {
                    returnableConfig.channel = aftersplit[1];
                }

                if(aftersplit[0] == "verbose")
                {
                    if(aftersplit[1] == "true")
                    {
                        returnableConfig.verbose = true;
                    }
                    else
                    {
                        returnableConfig.verbose = false;
                    }
                }
                if(aftersplit[0] == "version")
                {
                    returnableConfig.version = aftersplit[1];
                }
                if(aftersplit[0] == "badwordfilter")
                {
                    if(aftersplit[1] == "true")
                    {
                        returnableConfig.badwordfilter = true;
                    }
                    else
                    {
                        returnableConfig.badwordfilter = false;
                    }
                }
                if (aftersplit [0] == "password") {
                    returnableConfig.password = aftersplit [1];
                }
                if (aftersplit [0] == "minetest.autovoiceservers") {
                    if (aftersplit [1] == "true") {
                        returnableConfig.minetest_autovoiceservers = true;
                    } else if (aftersplit [1] == "false") {
                        returnableConfig.minetest_autovoiceservers = false;
                    }
                }
            }

            return returnableConfig;
        }
        public static void Main(string[] args)
        {
            string nick              = "LastManStanding";
            string host              = "irc.freenode.net";
            int    port              = 6667;
            string channel           = "#lms_default";
            string auxConfigFilename = "config.cfg";
            bool   badwordfilter     = true;
            string password          = "******";


            // Read config values
            StreamReader configReader = new StreamReader(auxConfigFilename);

            string[] configArray = new string[10000];
            for (int i = 0; !configReader.EndOfStream; i++)
            {
                configArray [i] = configReader.ReadLine();
            }

            ConfigValues alreadyReadConfig = Configuration.ProcessConfig(configArray);

            try {
                if (alreadyReadConfig.name != null)
                {
                    nick = alreadyReadConfig.name;
                }
                if (alreadyReadConfig.host != null)
                {
                    host = alreadyReadConfig.host;
                }
                if (alreadyReadConfig.channel != null)
                {
                    channel = alreadyReadConfig.channel;
                }
                if (alreadyReadConfig.port != 0)
                {
                    port = alreadyReadConfig.port;
                }
                if (alreadyReadConfig.version != null)
                {
                    version = alreadyReadConfig.version;
                }
                badwordfilter             = alreadyReadConfig.badwordfilter;
                verbose                   = alreadyReadConfig.verbose;
                password                  = alreadyReadConfig.password;
                minetest_autovoiceservers = alreadyReadConfig.minetest_autovoiceservers;
            } catch (Exception e) {
            }

            Input.DebugOutput("{STATUS} nick = " + nick);
            Input.DebugOutput("{STATUS} host = " + host);
            Input.DebugOutput("{STATUS} port = " + port);
            Input.DebugOutput("{STATUS} channel = " + channel);
            Input.DebugOutput("{STATUS} auxConfigFilename = " + auxConfigFilename);

            try {
                client = new TcpClient(host, port);
                stream = client.GetStream();
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);

                Input input = new Input(reader, writer, nick, channel, verbose, badwordfilter, password, minetest_autovoiceservers);

                Verbose("Input thread initialized.");

                Thread inputThread = new Thread(input.Read);
                inputThread.Start();
                Verbose("Input thread starting.");

                while (!inputThread.IsAlive)
                {
                    Console.Write("Waiting for thread to come alive.\r");
                }
                Console.WriteLine();                  // Why is this here?

                Thread.Sleep(1);

                Verbose("Async thread running simultaneously.\nWaiting 3 seconds to send USER.");
                Thread.Sleep(3000);
                Send(writer, "USER " + nick + " " + nick + " " + nick + " :Custom bot created by Microchip, coded in C#, and compiled in MonoDevelop.");
                Verbose("USER sent.\nWaiting 3 seconds till NICK.");
                Thread.Sleep(3000);
                Send(writer, "NICK " + nick);
                Verbose("NICK sent.");
                Console.WriteLine("{STATUS} Waiting 10 seconds to join channel.");
                Thread.Sleep(10000);
                Send(writer, "JOIN " + channel);
                Verbose("JOIN sent.");
                Send(writer, "PRIVMSG NickServ :identify " + password);
                Verbose("NickServ identification sent.");
                //Send (writer, "PRIVMSG " + channel + " :Hello.");

                Console.WriteLine("Emergency command interpreter starting.");
                for (int i = 0;; i++)
                {
                    try
                    {
                        Console.Write(i + "> ");
                        string   command    = Console.ReadLine();
                        char[]   splits     = { ' ', };
                        string[] aftersplit = command.Split(splits);
                        if (aftersplit[0] == "join")
                        {
                            Send(writer, "PART " + channel + " :Manual disconnect.");
                            channel = aftersplit[1];
                            Send(writer, "JOIN " + channel);
                        }

                        if (aftersplit[0] == "part")
                        {
                            Send(writer, "PART " + aftersplit[1] + " :Manual disconnect.");
                        }

                        if (aftersplit[0] == "quit")
                        {
                            Send(writer, "PART " + channel + " :Quitting. Bye!");
                            Environment.Exit(0);
                        }

                        if (aftersplit[0] == "say")
                        {
                            try{
                                aftersplit[2] = aftersplit[2];
                                StringBuilder builder = new StringBuilder();
                                for (int j = 1; j < aftersplit.Length; j++)
                                {
                                    builder.Append(aftersplit[j] + " ");
                                }

                                Send(writer, "PRIVMSG " + channel + " :" + builder.ToString());
                            }

                            catch (Exception e)
                            {
                                Send(writer, "PRIVMSG " + channel + " :" + aftersplit[1]);
                            }
                        }
                    }

                    catch (Exception e)
                    {
                    }
                }
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }