private static string Compile(string content, CmdContext msg) { const string template = "using System;using System.Net;using System.Collections.Generic;using Discord;namespace DuckBot {public static class Script {public static string Code(string rawText,dynamic sender,dynamic server,dynamic channel){\n"; string source = template + content + "}}}"; using (CodeDomProvider compiler = CodeDomProvider.CreateProvider("CSharp")) { Assembly ass = typeof(Program).Assembly; CompilerParameters pars = new CompilerParameters(); foreach (AssemblyName an in ass.GetReferencedAssemblies()) { pars.ReferencedAssemblies.Add(Assembly.ReflectionOnlyLoad(an.FullName).Location); } pars.ReferencedAssemblies.Add(ass.Location); pars.GenerateExecutable = false; pars.GenerateInMemory = false; pars.OutputAssembly = Guid.NewGuid() + ".dll"; CompilerResults results = compiler.CompileAssemblyFromSource(pars, source); if (!results.Errors.HasErrors) { return(Path.GetFullPath(pars.OutputAssembly)); } else { StringBuilder errors = new StringBuilder(msg.GetString("err_compile") + ": "); errors.AppendFormat("{0},{1}: ``` {2} ```", results.Errors[0].Line - 1, results.Errors[0].Column, results.Errors[0].ErrorText); throw new FormatException(errors.ToString()); } } }
internal static string Execute(string content, CmdContext msg) { using (NLua.Lua lua = new NLua.Lua()) using (PrintProxy proxy = new PrintProxy()) { lua.RegisterFunction("print", proxy, proxy.GetType().GetMethod("Print", new Type[] { typeof(object[]) })); string code; using (StreamReader sr = new StreamReader(typeof(Lua).Assembly.GetManifestResourceStream("DuckBot.Resources.Sandbox.lua"))) code = sr.ReadToEnd(); try { const string template = "args = {...};rawText,sender,server,channel=args[1],args[2],args[3],args[4]\n"; string source = template + content; using (LuaFunction func = (LuaFunction)lua.DoString(code, "sandbox")[0]) { object[] rets = func.Call(source, msg.Args, msg.Sender, msg.Server, msg.Channel); if (rets.Length >= 2) { object[] arr = new object[rets.Length - 1]; Array.Copy(rets, 1, arr, 0, arr.Length); proxy.Print(arr); } string res = proxy.ToString().Trim(); return(res.Length == 0 ? msg.GetString("Strings.ret_empty_script") : res); } } catch (NLua.Exceptions.LuaScriptException ex) { return(msg.GetString("err_generic") + ": " + ex.Message + "\n``` " + ex.Source + " ```"); } } }
public override ParserResult VisitCmd([NotNull] CmdContext context) { string cmdArg = this.VisitArg(context.arg()).ArgValue; ParserResult result = new ParserResult(cmdArg); result.IsCmd = true; return(result); }
public void CmdTest() { ShellParser shellParser = MakeParser("cmdword"); CmdContext context = shellParser.cmd(); ShellVisitor visitor = new ShellVisitor(); ParserResult result = visitor.Visit(context); result.IsCmd.Should().BeTrue(); result.CmdValue.Should().Be("cmdword"); }
internal static string Execute(string content, CmdContext msg) { AppDomainSetup setup = new AppDomainSetup() { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory, DisallowBindingRedirects = true, DisallowCodeDownload = true, DisallowPublisherPolicy = true }; PermissionSet ps = new PermissionSet(PermissionState.None); ps.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, setup.ApplicationBase)); ps.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution | SecurityPermissionFlag.UnmanagedCode | SecurityPermissionFlag.ControlThread)); ps.AddPermission(new System.Net.WebPermission(PermissionState.Unrestricted)); AppDomain app = null; string dll = null; try { dll = Compile(content, msg); app = AppDomain.CreateDomain(dll, null, setup, ps); CS obj = (CS)app.CreateInstanceAndUnwrap(typeof(CS).Assembly.FullName, typeof(CS).FullName); using (StringWriter sw = new StringWriter()) { try { sw.WriteLine(obj.Remote(CultureInfo.CurrentCulture, sw, dll, msg.Args, Proxy.GetProxy(msg.Sender), Proxy.GetProxy(msg.Server), Proxy.GetProxy(msg.Channel))); } catch (TargetInvocationException) { return(msg.GetString("err_scrtimeout")); } string res = sw.ToString().Trim(); return(res.Length == 0 ? msg.GetString("ret_empty_script") : res); } } catch (FormatException ex) { return(ex.Message); } finally { if (app != null) { AppDomain.Unload(app); } if (dll != null) { File.Delete(dll); } } }
public CmdContext cmd() { CmdContext _localctx = new CmdContext(Context, State); EnterRule(_localctx, 2, RULE_cmd); try { State = 19; ErrorHandler.Sync(this); switch (Interpreter.AdaptivePredict(TokenStream, 1, Context)) { case 1: _localctx = new AssignmentCommandContext(_localctx); EnterOuterAlt(_localctx, 1); { State = 13; Match(ID); State = 14; Match(ASS); State = 15; expr(0); State = 16; Match(COMMA); } break; case 2: _localctx = new ExpressionCommandContext(_localctx); EnterOuterAlt(_localctx, 2); { State = 18; expr(0); } break; } } catch (RecognitionException re) { _localctx.exception = re; ErrorHandler.ReportError(this, re); ErrorHandler.Recover(this, re); } finally { ExitRule(); } return(_localctx); }
public CmdContext cmd() { CmdContext _localctx = new CmdContext(Context, State); EnterRule(_localctx, 2, RULE_cmd); try { EnterOuterAlt(_localctx, 1); { State = 15; switch (TokenStream.La(1)) { case MOV: { State = 13; move(); } break; case ROT: { State = 14; rotate(); } break; default: throw new NoViableAltException(this); } State = 17; Match(NEWLINE); } } catch (RecognitionException re) { _localctx.exception = re; ErrorHandler.ReportError(this, re); ErrorHandler.Recover(this, re); } finally { ExitRule(); } return(_localctx); }
public AssignmentCommandContext(CmdContext context) { CopyFrom(context); }
public ExpressionCommandContext(CmdContext context) { CopyFrom(context); }
public virtual void CopyFrom(CmdContext context) { base.CopyFrom(context); }
public CmdContext cmd() { CmdContext _localctx = new CmdContext(Context, State); EnterRule(_localctx, 2, RULE_cmd); try { EnterOuterAlt(_localctx, 1); { State = 15; switch (TokenStream.La(1)) { case MOV: { State = 13; move(); } break; case ROT: { State = 14; rotate(); } break; default: throw new NoViableAltException(this); } State = 17; Match(NEWLINE); } } catch (RecognitionException re) { _localctx.exception = re; ErrorHandler.ReportError(this, re); ErrorHandler.Recover(this, re); } finally { ExitRule(); } return _localctx; }
public SwitchEvaluator(CmdContext ctx) => ScriptEvaluator = new ScriptEvaluator(ctx);
public ScriptEvaluator(CmdContext ctx) => Context = ctx;
public void Reply(string message) => LastMessage = CmdContext.RespondAsync(ParseMessageMention(message)).ConfigureAwait(false).GetAwaiter().GetResult();