From d1edeb2b8e796c11f561bacfcb66fae074f0efa3 Mon Sep 17 00:00:00 2001 From: Udit Karode Date: Wed, 4 Nov 2020 01:12:56 +0530 Subject: [PATCH] Initial commit --- .gitignore | 3 + .idea/TgMcLink.iml | 8 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 52 ++++++++++++ config | 3 + creader.go | 31 +++++++ main.go | 196 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 307 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/TgMcLink.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 config create mode 100644 creader.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97d3276 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/testing +/TgMcLink +/Minegram diff --git a/.idea/TgMcLink.iml b/.idea/TgMcLink.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/TgMcLink.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..638a1c4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..c051c1d --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/config b/config new file mode 100644 index 0000000..a6de12a --- /dev/null +++ b/config @@ -0,0 +1,3 @@ +command = java -Xms1024M -Xms 1024M -jar server.jar +bot_token = SSF9s4FwhateverS82SD94:23847824 +target_chat = -1001251355942 diff --git a/creader.go b/creader.go new file mode 100644 index 0000000..b1dabba --- /dev/null +++ b/creader.go @@ -0,0 +1,31 @@ +package main + +import ( + "bufio" + "fmt" + "io/ioutil" + "path/filepath" + "regexp" + "strings" +) + +func readConfig(fileName string) map[string]string { + filename, _ := filepath.Abs(fileName) + confFile, err := ioutil.ReadFile(filename) + startSpace := regexp.MustCompile(`^\s`) + res := make(map[string]string) + + if err != nil { + fmt.Println("Could not open " + fileName) + } + + scanner := bufio.NewScanner(strings.NewReader(string(confFile))) + + for scanner.Scan() { + split := strings.Split(scanner.Text(), "=") + if len(split) == 2 { + res[strings.ReplaceAll(split[0], " ", "")] = startSpace.ReplaceAllString(split[1], "") + } + } + return res +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..ba19318 --- /dev/null +++ b/main.go @@ -0,0 +1,196 @@ +package main + +import ( + "bufio" + "fmt" + tb "gopkg.in/tucnak/telebot.v2" + "io" + "os" + "os/exec" + "regexp" + "strings" + "syscall" + "time" +) + +type Group struct { + id string +} + +func(g Group) Recipient() string { + return g.id +} + +func main(){ + res := readConfig("config") + + cmd := res["command"] + tok := res["bot_token"] + tchat := res["target_chat"] + + chatRegex := regexp.MustCompile(`<(.+)> (.+)`) + joinRegex := regexp.MustCompile(".* (.+) joined the game") + leaveRegex := regexp.MustCompile(".* (.+) left the game") + advancementRegex := regexp.MustCompile(".* (.+) has made the advancement (.+)") + /* death regex taken from https://github.com/trgwii/TeMiCross/blob/master/client/parser/default/messages/death.js */ + deathRegex := regexp.MustCompile(`(.+) (was (shot by .+|shot off (some vines|a ladder) by .+|pricked to death|stabbed to death|squished too much|blown up by .+|killed by .+|doomed to fall by .+|blown from a high place by .+|squashed by .+|burnt to a crisp whilst fighting .+|roasted in dragon breath( by .+)?|struck by lightning( whilst fighting .+)?|slain by .+|fireballed by .+|killed trying to hurt .+|impaled by .+|speared by .+|poked to death by a sweet berry bush( whilst trying to escape .+)?|pummeled by .+)|hugged a cactus|walked into a cactus whilst trying to escape .+|drowned( whilst trying to escape .+)?|suffocated in a wall( whilst fighting .+)?|experienced kinetic energy( whilst trying to escape .+)?|removed an elytra while flying( whilst trying to escape .+)?|blew up|hit the ground too hard( whilst trying to escape .+)?|went up in flames|burned to death|walked into fire whilst fighting .+|went off with a bang( whilst fighting .+)?|tried to swim in lava(( while trying)? to escape .+)?|discovered floor was lava|walked into danger zone due to .+|got finished off by .+|starved to death|didn't want to live in the same world as .+|withered away( whilst fighting .+)?|died( because of .+)?|fell (from a high place( and fell out of the world)?|off a ladder|off to death( whilst fighting .+)?|off some vines|out of the water|into a patch of fire|into a patch of cacti|too far and was finished by .+|out of the world))$`) + + if cmd == "" { + fmt.Println("Please enter a 'command' in the config!") + os.Exit(0) + } + + if tok == "" { + fmt.Println("Please enter a 'bot_token' in the config!") + os.Exit(0) + } + + if tchat == "" { + fmt.Println("Please enter a 'target_chat' in the config!") + os.Exit(0) + } + + var targetChat tb.Recipient + targetChat = Group{id: tchat} + + b, errz := tb.NewBot(tb.Settings { + Token: tok, + Poller: &tb.LongPoller{Timeout: 5 * time.Second}, + }) + + if errz != nil { + panic(errz) + } + + splitCmd := strings.Split(cmd, " ") + + execCmd := exec.Command(splitCmd[0], splitCmd[1:]...) + execCmd.Stderr = os.Stderr + + stdin, err := execCmd.StdinPipe() + if err != nil { + panic(err) + } + + stdout, err := execCmd.StdoutPipe() + if err != nil { + panic(err) + } + + execCmd.SysProcAttr = &syscall.SysProcAttr{ + Pdeathsig: syscall.SIGTERM, + } + + err = execCmd.Start() + + if err != nil { + panic(err) + } + + b.Handle(tb.OnText, func(m *tb.Message) { + sender := strings.ReplaceAll(m.Sender.FirstName + " " + m.Sender.LastName, "\n", "(nl)") + content := strings.ReplaceAll(m.Text, "\n", "(nl)") + if m.IsReply() { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": \"},{\"text\":\"(\",\"color\":\"yellow\"},{\"text\":\"reply\",\"bold\":true,\"color\":\"yellow\"},{\"text\":\")\",\"color\":\"yellow\"},{\"text\":\" " + content + "\"}]\n") + } else { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": " + content + "\",\"color\":\"white\"}]\n") + } + if err != nil { + fmt.Println("ERR> ", err) + } + }) + + b.Handle(tb.OnSticker, func(m *tb.Message) { + sender := strings.ReplaceAll(m.Sender.FirstName + " " + m.Sender.LastName, "\n", "(nl)") + content := "[STICKER]" + if m.IsReply() { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": \"},{\"text\":\"(\",\"color\":\"yellow\"},{\"text\":\"reply\",\"bold\":true,\"color\":\"yellow\"},{\"text\":\")\",\"color\":\"yellow\"},{\"text\":\" " + content + "\"}]\n") + } else { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": " + content + "\",\"color\":\"yellow\"}]\n") + } + if err != nil { + fmt.Println("ERR> ", err) + } + }) + + b.Handle(tb.OnPhoto, func(m *tb.Message) { + sender := strings.ReplaceAll(m.Sender.FirstName + " " + m.Sender.LastName, "\n", "(nl)") + content := "[PHOTO]" + if m.IsReply() { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": \"},{\"text\":\"(\",\"color\":\"yellow\"},{\"text\":\"reply\",\"bold\":true,\"color\":\"yellow\"},{\"text\":\")\",\"color\":\"yellow\"},{\"text\":\" " + content + "\"}]\n") + } else { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": " + content + "\",\"color\":\"yellow\"}]\n") + } + if err != nil { + fmt.Println("ERR> ", err) + } + }) + + b.Handle(tb.OnVideo, func(m *tb.Message) { + sender := strings.ReplaceAll(m.Sender.FirstName + " " + m.Sender.LastName, "\n", "(nl)") + content := "[VIDEO]" + if m.IsReply() { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": \"},{\"text\":\"(\",\"color\":\"yellow\"},{\"text\":\"reply\",\"bold\":true,\"color\":\"yellow\"},{\"text\":\")\",\"color\":\"yellow\"},{\"text\":\" " + content + "\"}]\n") + } else { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": " + content + "\",\"color\":\"yellow\"}]\n") + } + if err != nil { + fmt.Println("ERR> ", err) + } + }) + + b.Handle(tb.OnVoice, func(m *tb.Message) { + sender := strings.ReplaceAll(m.Sender.FirstName + " " + m.Sender.LastName, "\n", "(nl)") + content := "[VOICE]" + if m.IsReply() { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": \"},{\"text\":\"(\",\"color\":\"yellow\"},{\"text\":\"reply\",\"bold\":true,\"color\":\"yellow\"},{\"text\":\")\",\"color\":\"yellow\"},{\"text\":\" " + content + "\"}]\n") + } else { + _, err = io.WriteString(stdin, "/tellraw @a [\"\",{\"text\":\"[TG] " + sender + "\",\"color\":\"aqua\"},{\"text\":\": " + content + "\",\"color\":\"yellow\"}]\n") + } + if err != nil { + fmt.Println("ERR> ", err) + } + }) + + go b.Start() + + scanner := bufio.NewScanner(stdout) + for scanner.Scan() { + m := scanner.Text() + fmt.Println(m) + if strings.Contains(m, "INFO") { + if chatRegex.MatchString(m) { + result := chatRegex.FindStringSubmatch(m) + if len(result) == 3 { + _, _ = b.Send(targetChat, "`" + result[1] + "`" + "**:** " + result[2], "Markdown") + } + } else if joinRegex.MatchString(m) { + result := joinRegex.FindStringSubmatch(m) + if len(result) == 2 { + _, _ = b.Send(targetChat, "`" + result[1] + "`" + " joined the server.", "Markdown") + } + } else if leaveRegex.MatchString(m) { + result := leaveRegex.FindStringSubmatch(m) + if len(result) == 2 { + _, _ = b.Send(targetChat, "`" + result[1] + "`" + " has left the server.", "Markdown") + } + } else if advancementRegex.MatchString(m) { + result := advancementRegex.FindStringSubmatch(m) + if len(result) == 3 { + _, _ = b.Send(targetChat, "`" + result[1] + "`" + " has made the advancement `" + result[2] + "`.", "Markdown") + } + } else if deathRegex.MatchString(m) { + result := strings.Split(m, " ") + fmt.Println(result[0]) + fmt.Println(result[1]) + fmt.Println(result[2]) + fmt.Println(result[3]) + fmt.Println(result[4]) + fmt.Println(result[5]) + _, _ = b.Send(targetChat, "`" + result[3] + "` " + strings.Join(result[4:], " ") + ".", "Markdown") + } + } + } + + _ = execCmd.Wait() +}