Skip to content

Commit

Permalink
fix runcode
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Oct 15, 2022
1 parent c08e3df commit 3339b6a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 139 deletions.
2 changes: 1 addition & 1 deletion data
Submodule data updated 1 files
+4 −20 .gitignore
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
141 changes: 6 additions & 135 deletions plugin/runcode/code_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<?php\n\techo 'Hello World!';\n?>",
"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 <stdio.h>\n\nint main()\n{\n printf(\"Hello, World! \n\");\n return 0;\n}",
"c++": "#include <iostream>\nusing namespace std;\n\nint main()\n{\n cout << \"Hello World\";\n return 0;\n}",
"cpp": "#include <iostream>\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<String>){\n println(\"Hello World!\")\n}",
"kt": "fun main(args : Array<String>){\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]{
Expand All @@ -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"),
Expand All @@ -123,18 +46,19 @@ 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"),
message.Text("ERROR: ", err),
)
} else {
// 运行成功
output = cutTooLong(strings.Trim(output, "\n"))
if israw {
ctx.SendChain(message.Text(output))
} else {
Expand All @@ -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)
Expand Down

0 comments on commit 3339b6a

Please sign in to comment.