Пример #1
0
        public int CompilerCommand(IRCMessage sIRCMessage, bool command)
        {
            try
            {
                var sSendMessage = sIrcBase.Networks[sIRCMessage.ServerName].sSendMessage;
                var text         = sLManager.GetCommandTexts("compiler", sIRCMessage.Channel, sIRCMessage.ServerName);
                if (text.Length < 5)
                {
                    sSendMessage.SendChatMessage(sIRCMessage, sLConsole.Translations("NoFound2", sLManager.GetChannelLocalization(sIRCMessage.Channel, sIRCMessage.ServerName)));
                    return(1);
                }

                string data = string.Empty, template = string.Empty;

                if (!command && regex.IsMatch(sIRCMessage.Args.TrimEnd()))
                {
                    data = regex.Match(sIRCMessage.Args.TrimEnd()).Groups["code"].ToString();
                }
                else if (command)
                {
                    data = sIRCMessage.Args.Trim();
                }

                if (Ban(data, sIRCMessage))
                {
                    return(1);
                }

                if (!IsEntry(data) && !IsEntryAndAbstractRegex(data))
                {
                    if (!IsSchumix(data) && !IsSchumixAndOverride(data))
                    {
                        template = CompilerConfig.Referenced + " class " + CompilerConfig.MainClass + " : Schumix.Compiler.Abstract { public override void " + CompilerConfig.MainConstructor + "() { " + CleanText(data) + " } }";
                    }
                    else if (IsSchumix(data) && !IsSchumixAndOverride(data))
                    {
                        template = CompilerConfig.Referenced + " class " + CompilerConfig.MainClass + " : Schumix.Compiler.Abstract { " + Regex.Replace(CleanText(data), SchumixRegex.ToString(), @"public override void " + CompilerConfig.MainConstructor + @"() {") + " }";
                    }
                    else if (!IsSchumix(data) && IsSchumixAndOverride(data))
                    {
                        template = CompilerConfig.Referenced + " class " + CompilerConfig.MainClass + " : Schumix.Compiler.Abstract { " + CleanText(data) + " }";
                    }
                }
                else if (IsEntry(data) && !IsEntryAndAbstractRegex(data))
                {
                    if (!IsSchumix(data) && !IsSchumixAndOverride(data))
                    {
                        sSendMessage.SendChatMessage(sIRCMessage, text[0]);
                        return(1);
                    }

                    if (IsSchumix(data) && !IsSchumixAndOverride(data))
                    {
                        data = Regex.Replace(data, SchumixRegex.ToString(), @"public override void " + CompilerConfig.MainConstructor + @"() {");
                    }

                    template = CompilerConfig.Referenced + SchumixBase.Space + Regex.Replace(CleanText(data), EntryRegex.ToString(), @"class " + CompilerConfig.MainClass + @" : Schumix.Compiler.Abstract {");
                }
                else if (!IsEntry(data) && IsEntryAndAbstractRegex(data))
                {
                    if (!IsSchumix(data) && !IsSchumixAndOverride(data))
                    {
                        sSendMessage.SendChatMessage(sIRCMessage, text[0]);
                        return(1);
                    }

                    if (IsSchumix(data) && !IsSchumixAndOverride(data))
                    {
                        data = Regex.Replace(data, SchumixRegex.ToString(), @"public override void " + CompilerConfig.MainConstructor + @"() {");
                    }

                    template = CompilerConfig.Referenced + SchumixBase.Space + CleanText(data);
                }

                var s = new Sandbox(CompilerConfig.ReferencedAssemblies, CompilerConfig.CompilerOptions, CompilerConfig.WarningLevel, CompilerConfig.TreatWarningsAsErrors);
                //s.TestIsFullyTrusted(); // Test Sandbox

                string errormessage = string.Empty;
                var    asm          = s.CompileCode(template, ref errormessage);
                errormessage = errormessage.Remove(0, 2, ". ");

                if (!errormessage.IsNullOrEmpty())
                {
                    sSendMessage.SendChatMessage(sIRCMessage, sLManager.GetCommandText("compiler/code", sIRCMessage.Channel, sIRCMessage.ServerName), errormessage.TrimMessage(1000));
                }

                if (asm.IsNull())
                {
                    return(1);
                }

                AppDomain appdomain = null;
                s = Sandbox.CreateInstance(ref appdomain);
                //s.TestIsFullyTrusted(); // Test Sandbox

                object o = asm.CreateInstance(CompilerConfig.MainClass);
                if (o.IsNull())
                {
                    sSendMessage.SendChatMessage(sIRCMessage, text[1]);
                    return(1);
                }

                var writer = new StringWriter();
                Console.SetOut(writer);

                int ReturnCode = 0;
                int errorcode  = -1;
                var thread     = new Thread(() =>
                {
                    try
                    {
                        errormessage = string.Empty;
                        ReturnCode   = s.StartCode(data, CompilerConfig.MainConstructor, (Abstract)o, ref errormessage, ref errorcode);
                    }
                    catch (Exception e)
                    {
                        ReturnCode = -1;
                        ConsoleOpenStandardOutput();
                        Log.Debug("CompilerThread", sLConsole.GetString("Failure details: {0}"), e.Message);
                    }
                });

                thread.Start();
                thread.Join(3000);
                thread.Abort();
                AppDomain.Unload(appdomain);

                switch (errorcode)
                {
                case 0:
                    sSendMessage.SendChatMessage(sIRCMessage, sLManager.GetCommandText("compiler/code", sIRCMessage.Channel, sIRCMessage.ServerName), errormessage.TrimMessage(1000));
                    break;

                default:
                    break;
                }

                switch (ReturnCode)
                {
                case -1:
                    return(1);

                case 0:
                    sSendMessage.SendChatMessage(sIRCMessage, sLManager.GetCommandText("compiler/kill", sIRCMessage.Channel, sIRCMessage.ServerName));
                    return(1);

                default:
                    break;
                }

                string _write = writer.ToString();
                sSendMessage.SendChatMessage(sIRCMessage, string.Empty);

                if (_write.Length == 0)
                {
                    return(2);
                }

                var length = _write.Length;
                var lines  = CleanIrcText(_write).Split(SchumixBase.NewLine);

                if (length > 1000 && lines.Length == 1)
                {
                    sSendMessage.SendChatMessage(sIRCMessage, text[2]);
                    return(1);
                }
                else if (length <= 1000 && lines.Length == 1)
                {
                    byte i = 0, x = 0;

                    foreach (var line in lines)
                    {
                        i++;

                        if (line.IsNullOrEmpty())
                        {
                            x++;
                        }
                        else
                        {
                            sSendMessage.SendChatMessage(sIRCMessage, line);
                        }
                    }

                    if (i == x)
                    {
                        sSendMessage.SendChatMessage(sIRCMessage, text[3]);
                    }

                    return(1);
                }
                else if (lines.Length == 2)
                {
                    foreach (var line in lines)
                    {
                        if (line.IsNullOrEmpty())
                        {
                            continue;
                        }
                        else
                        {
                            sSendMessage.SendChatMessage(sIRCMessage, line);
                        }
                    }

                    return(1);
                }
                else if (lines.Length > 2)
                {
                    sSendMessage.SendChatMessage(sIRCMessage, lines[0]);
                    sSendMessage.SendChatMessage(sIRCMessage, text[4], lines.Length - 1);
                    return(1);
                }

                return(1);
            }
            catch (Exception e)
            {
                ConsoleOpenStandardOutput();
                Log.Debug("CompilerCommand", sLConsole.GetString("Failure details: {0}"), e.Message);
                return(-1);
            }
        }
Пример #2
0
 private bool IsSchumix(string data)
 {
     return(SchumixRegex.IsMatch(data));
 }