From 3339b6a16fb5ae02529f8f74d9b5ba161816ea4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 15 Oct 2022 14:12:36 +0800 Subject: [PATCH] fix runcode --- data | 2 +- go.mod | 2 +- go.sum | 4 +- plugin/runcode/code_runner.go | 141 ++-------------------------------- 4 files changed, 10 insertions(+), 139 deletions(-) diff --git a/data b/data index c066abeb..7bc1828d 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit c066abebe7e230ee96ac90e408d9ac555211bdcc +Subproject commit 7bc1828db55f8549e04ccb85cd6f90f2964e2062 diff --git a/go.mod b/go.mod index a5b6fa7e..2a43dd0f 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/Coloured-glaze/gg v1.3.4 - github.com/FloatTech/AnimeAPI v1.5.2-0.20221014062715-20d9e9dcd1b0 + github.com/FloatTech/AnimeAPI v1.5.2-0.20221015060924-fe2f85a3cf45 github.com/FloatTech/floatbox v0.0.0-20221011153549-68005767c531 github.com/FloatTech/sqlite v0.5.0 github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b diff --git a/go.sum b/go.sum index c402068b..3cdc1e84 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym github.com/Coloured-glaze/gg v1.3.4 h1:l31zIF/HaVwkzjrj+A56RGQoSKyKuR1IWtIrqXGFStI= github.com/Coloured-glaze/gg v1.3.4/go.mod h1:Ih5NLNNDHOy3RJbB0EPqGTreIzq/H02TGThIagh8HJg= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= -github.com/FloatTech/AnimeAPI v1.5.2-0.20221014062715-20d9e9dcd1b0 h1:/hbz2ngVRGCzfPGkR1rTGmNNRnXa3f+xq0Ya0BwUJqI= -github.com/FloatTech/AnimeAPI v1.5.2-0.20221014062715-20d9e9dcd1b0/go.mod h1:RgcMDA1S7C81bq7HQjygMEEo+EXwAlsutvKMv7DafgY= +github.com/FloatTech/AnimeAPI v1.5.2-0.20221015060924-fe2f85a3cf45 h1:XbNlD0irJELgdR304TvqmFrxdH7hsxA/Ah9xLHFP3eQ= +github.com/FloatTech/AnimeAPI v1.5.2-0.20221015060924-fe2f85a3cf45/go.mod h1:RgcMDA1S7C81bq7HQjygMEEo+EXwAlsutvKMv7DafgY= github.com/FloatTech/floatbox v0.0.0-20221011153549-68005767c531 h1:Z0yn6LFhEyC12hj+TBXc2P7/kWlCd/jlwv4JFndgpnw= github.com/FloatTech/floatbox v0.0.0-20221011153549-68005767c531/go.mod h1:4tfIeB74L1RzhNp3nNjaTw8m3IEnc+q/k6k/MhL07ks= github.com/FloatTech/sqlite v0.5.0 h1:U7J5Omc534PqmH6csfu+ypCo3DS8L91l5lTsxUu3b/U= diff --git a/plugin/runcode/code_runner.go b/plugin/runcode/code_runner.go index ff3a908d..4aad5020 100644 --- a/plugin/runcode/code_runner.go +++ b/plugin/runcode/code_runner.go @@ -2,94 +2,17 @@ package runcode import ( - "errors" - "io" - "net/http" - "net/url" "strings" - "time" + "github.com/FloatTech/AnimeAPI/runoob" ctrl "github.com/FloatTech/zbpctrl" "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" - - "github.com/tidwall/gjson" ) -var ( - templates = map[string]string{ - "py2": "print 'Hello World!'", - "ruby": "puts \"Hello World!\";", - "rb": "puts \"Hello World!\";", - "php": "", - "javascript": "console.log(\"Hello World!\");", - "js": "console.log(\"Hello World!\");", - "node.js": "console.log(\"Hello World!\");", - "scala": "object Main {\n def main(args:Array[String])\n {\n println(\"Hello World!\")\n }\n\t\t\n}", - "go": "package main\n\nimport \"fmt\"\n\nfunc main() {\n fmt.Println(\"Hello, World!\")\n}", - "c": "#include \n\nint main()\n{\n printf(\"Hello, World! \n\");\n return 0;\n}", - "c++": "#include \nusing namespace std;\n\nint main()\n{\n cout << \"Hello World\";\n return 0;\n}", - "cpp": "#include \nusing namespace std;\n\nint main()\n{\n cout << \"Hello World\";\n return 0;\n}", - "java": "public class HelloWorld {\n public static void main(String []args) {\n System.out.println(\"Hello World!\");\n }\n}", - "rust": "fn main() {\n println!(\"Hello World!\");\n}", - "rs": "fn main() {\n println!(\"Hello World!\");\n}", - "c#": "using System;\nnamespace HelloWorldApplication\n{\n class HelloWorld\n {\n static void Main(string[] args)\n {\n Console.WriteLine(\"Hello World!\");\n }\n }\n}", - "cs": "using System;\nnamespace HelloWorldApplication\n{\n class HelloWorld\n {\n static void Main(string[] args)\n {\n Console.WriteLine(\"Hello World!\");\n }\n }\n}", - "csharp": "using System;\nnamespace HelloWorldApplication\n{\n class HelloWorld\n {\n static void Main(string[] args)\n {\n Console.WriteLine(\"Hello World!\");\n }\n }\n}", - "shell": "echo 'Hello World!'", - "bash": "echo 'Hello World!'", - "erlang": "% escript will ignore the first line\n\nmain(_) ->\n io:format(\"Hello World!~n\").", - "perl": "print \"Hello, World!\n\";", - "python": "print(\"Hello, World!\")", - "py": "print(\"Hello, World!\")", - "swift": "var myString = \"Hello, World!\"\nprint(myString)", - "lua": "var myString = \"Hello, World!\"\nprint(myString)", - "pascal": "runcode Hello;\nbegin\n writeln ('Hello, world!')\nend.", - "kotlin": "fun main(args : Array){\n println(\"Hello World!\")\n}", - "kt": "fun main(args : Array){\n println(\"Hello World!\")\n}", - "r": "myString <- \"Hello, World!\"\nprint ( myString)", - "vb": "Module Module1\n\n Sub Main()\n Console.WriteLine(\"Hello World!\")\n End Sub\n\nEnd Module", - "typescript": "const hello : string = \"Hello World!\"\nconsole.log(hello)", - "ts": "const hello : string = \"Hello World!\"\nconsole.log(hello)", - } - table = map[string][2]string{ - "py2": {"0", "py"}, - "ruby": {"1", "rb"}, - "rb": {"1", "rb"}, - "php": {"3", "php"}, - "javascript": {"4", "js"}, - "js": {"4", "js"}, - "node.js": {"4", "js"}, - "scala": {"5", "scala"}, - "go": {"6", "go"}, - "c": {"7", "c"}, - "c++": {"7", "cpp"}, - "cpp": {"7", "cpp"}, - "java": {"8", "java"}, - "rust": {"9", "rs"}, - "rs": {"9", "rs"}, - "c#": {"10", "cs"}, - "cs": {"10", "cs"}, - "csharp": {"10", "cs"}, - "shell": {"10", "sh"}, - "bash": {"10", "sh"}, - "erlang": {"12", "erl"}, - "perl": {"14", "pl"}, - "python": {"15", "py3"}, - "py": {"15", "py3"}, - "swift": {"16", "swift"}, - "lua": {"17", "lua"}, - "pascal": {"18", "pas"}, - "kotlin": {"19", "kt"}, - "kt": {"19", "kt"}, - "r": {"80", "r"}, - "vb": {"84", "vb"}, - "typescript": {"1010", "ts"}, - "ts": {"1010", "ts"}, - } -) +var ro = runoob.NewRunOOB("b6365362a90ac2ac7098ba52c13e352b") func init() { control.Register("runcode", &ctrl.Options[*zero.Ctx]{ @@ -108,7 +31,7 @@ func init() { israw := ctx.State["regex_matched"].([]string)[1] != "" language := ctx.State["regex_matched"].([]string)[2] language = strings.ToLower(language) - if runType, exist := table[language]; !exist { + if _, exist := runoob.LangTable[language]; !exist { // 不支持语言 ctx.SendChain( message.Text("> ", ctx.Event.Sender.NickName, "\n"), @@ -123,11 +46,11 @@ func init() { message.Text("> ", ctx.Event.Sender.NickName, " ", language, "-template:\n"), message.Text( ">runcode ", language, "\n", - templates[language], + runoob.Templates[language], ), ) default: - if output, err := runCode(block, runType); err != nil { + if output, err := ro.Run(block, language, ""); err != nil { // 运行失败 ctx.SendChain( message.Text("> ", ctx.Event.Sender.NickName, "\n"), @@ -135,6 +58,7 @@ func init() { ) } else { // 运行成功 + output = cutTooLong(strings.Trim(output, "\n")) if israw { ctx.SendChain(message.Text(output)) } else { @@ -149,59 +73,6 @@ func init() { }) } -func runCode(code string, runType [2]string) (string, error) { - // 对菜鸟api发送数据并返回结果 - api := "https://tool.runoob.com/compile2.php" - - header := http.Header{ - "Content-Type": []string{"application/x-www-form-urlencoded; charset=UTF-8"}, - "Referer": []string{"https://c.runoob.com/"}, - "User-Agent": []string{"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0"}, - } - - val := url.Values{ - "code": []string{code}, - "token": []string{"4381fe197827ec87cbac9552f14ec62a"}, - "stdin": []string{""}, - "language": []string{runType[0]}, - "fileext": []string{runType[1]}, - } - // 发送请求 - client := &http.Client{ - Timeout: 15 * time.Second, - } - request, _ := http.NewRequest("POST", api, strings.NewReader(val.Encode())) - request.Header = header - body, err := client.Do(request) - if err != nil { - return "", err - } - defer body.Body.Close() - if body.StatusCode != http.StatusOK { - return "", errors.New("code not 200") - } - res, err := io.ReadAll(body.Body) - if err != nil { - return "", err - } - // 结果处理 - content := gjson.ParseBytes(res) - if e := content.Get("errors").Str; e != "\n\n" { - return "", errors.New(cutTooLong(clearNewLineSuffix(e))) - } - output := content.Get("output").Str - - return cutTooLong(clearNewLineSuffix(output)), nil -} - -// 清除末尾多余的换行符 -func clearNewLineSuffix(text string) string { - for strings.HasSuffix(text, "\n") { - text = text[:len(text)-1] - } - return text -} - // 截断过长文本 func cutTooLong(text string) string { temp := []rune(text)