Esempio n. 1
0
        static void Main(string[] args)
        {
            Config config = null;
            try {
                config = JsonConvert.DeserializeObject<Config>(File.ReadAllText("config.json"));
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Could not find config! Rename config.example.json to config.json and edit it.");
                return;
            }

            TcpClient client = new TcpClient(config.host, config.port);
            var stream = client.GetStream();
            var writer = new StreamWriter(stream);
            var reader = new StreamReader(stream);
            writer.AutoFlush = true;
            foreach (var line in config.connect)
            {
                writer.WriteLine(line);
            }

            Executor e = new Executor();
            var stdio = new IRCStdIo();
            e.Methods["stream-stdio"] = new Executor.NativeFunction(delegate (Executor exec)
            {
                exec.DataStack.Push(stdio);
            });

            while (true)
            {
                var msg = reader.ReadLine().Split(' ');
                if (msg[0] == "PING")
                {
                    msg[0] = "PONG";
                    writer.WriteLine(string.Join(" ", msg));
                }
                else if (msg[1] == "PRIVMSG")
                {
                    string channel = msg[2];
                    string message = string.Join(" ", msg.Skip(3)).Substring(1);
                    if (message.StartsWith("~{"))
                    {
                        stdio.Builder.SetLength(0);

                        e.CodeStack.Clear();
                        e.DataStack.Clear();

                        e.Methods["reset"] = new Executor.NativeFunction(delegate (Executor exec)
                        {
                            e = new Executor();
                            e.Methods["stream-stdio"] = new Executor.NativeFunction(delegate (Executor execu)
                            {
                                execu.DataStack.Push(stdio);
                            });
                        });

                        e.CodeStack.PushRange(Lexer.Parse(message.Substring(2)).Value);
                        string result;
                        int cyclecount = 0;
                        try {
                            for (cyclecount = 0; cyclecount < 12000 && e.CodeStack.Count > 0; cyclecount++)
                                e.Cycle();
                            if (e.CodeStack.Count > 0)
                                result = "[TIMED OUT]";
                            else
                                try {
                                    result = Encoding.UTF8.GetString(stdio.Builder.ToArray());
                                }
                                catch (ArgumentException)
                                {
                                    result = string.Join("", stdio.Builder.ToArray().Select(a => new string((char) a, 1)));
                                }
                        }
                        catch (Exception ex)
                        {
                            result = ex.Message;
                        }

                        result = result.Split('\r', '\n')[0];
                        if (result.Length > 0)
                            writer.WriteLine("PRIVMSG {0} :\u200B({2}) {1}", msg[2], result, cyclecount);
                        if (e.DataStack.Count > 0)
                            writer.WriteLine("PRIVMSG {0} :\u200B({2}) Stack: {1}", msg[2], e.DataStack.ToString(), cyclecount);
                        if (result.Length == 0 && e.DataStack.Count == 0)
                            writer.WriteLine("PRIVMSG {0} :\u200B({1}) [NO RESULT]", msg[2], cyclecount);
                    }
                }
            }
        }