2019-04-17 01:42:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
2020-07-10 01:01:29 +00:00
|
|
|
"meow.tf/residentsleeper/commands"
|
|
|
|
"meow.tf/residentsleeper/events"
|
|
|
|
"meow.tf/residentsleeper/rcon"
|
|
|
|
"meow.tf/residentsleeper/scripting/config"
|
|
|
|
"meow.tf/residentsleeper/scripting/minecraft"
|
2019-04-17 01:42:46 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2020-07-10 01:01:29 +00:00
|
|
|
"strings"
|
2019-04-17 01:42:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-07-10 01:01:29 +00:00
|
|
|
serverPath string
|
|
|
|
rconPort int
|
2019-04-17 01:42:46 +00:00
|
|
|
rconPassword string
|
2020-07-10 01:01:29 +00:00
|
|
|
debug bool
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
client *rcon.Session
|
2019-04-17 01:42:46 +00:00
|
|
|
|
|
|
|
configRegexp = regexp.MustCompile("^(.*?)=(.*)$")
|
2020-07-10 01:01:29 +00:00
|
|
|
|
|
|
|
flagDebug = flag.Bool("debug", false, "debug messages")
|
2019-04-17 01:42:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.StringVar(&serverPath, "dir", "", "server directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
debug = *flagDebug
|
|
|
|
|
2019-04-17 01:42:46 +00:00
|
|
|
// Load properties
|
|
|
|
configPath := path.Join(serverPath, "server.properties")
|
|
|
|
|
|
|
|
cfg, err := loadServerConfig(configPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Unable to load config:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rconEnabled, ok := cfg["enable-rcon"]; !ok || rconEnabled != "true" {
|
|
|
|
log.Fatalln("RCON not enabled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var rconPortStr string
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if rconPortStr, ok = cfg["rcon.port"]; !ok {
|
|
|
|
log.Fatalln("RCON is not enabled: No port set")
|
|
|
|
}
|
|
|
|
|
|
|
|
rconPort, _ = strconv.Atoi(rconPortStr)
|
|
|
|
|
|
|
|
if rconPassword, ok = cfg["rcon.password"]; !ok {
|
|
|
|
log.Fatalln("RCON is not enabled: No password set")
|
|
|
|
}
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
events.On(events.Message, handleCommands)
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
client = rcon.NewSession("localhost", rconPort, rconPassword)
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
client.Debug = debug
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
scriptPath := path.Join(serverPath, "scripts")
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
if _, err := os.Stat(scriptPath); !os.IsNotExist(err) {
|
|
|
|
config.BasePath = path.Join(scriptPath, "config")
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
minecraft.BasePath = serverPath
|
|
|
|
minecraft.WorldPath = path.Join(serverPath, cfg["level-name"])
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
if _, err = os.Stat(config.BasePath); os.IsNotExist(err) {
|
|
|
|
os.Mkdir(config.BasePath, 0755)
|
2019-04-17 01:42:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
log.Println("Loading scripts from", scriptPath)
|
|
|
|
err = LoadScripts(scriptPath)
|
2019-04-17 01:42:46 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-10 01:01:29 +00:00
|
|
|
log.Println("Warning: Unable to load scripts -", err)
|
2019-04-17 01:42:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
logPath := path.Join(serverPath, "logs/latest.log")
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
logParser(logPath)
|
|
|
|
}
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
func handleCommands(eventArgs ...interface{}) {
|
|
|
|
user := eventArgs[0].(string)
|
|
|
|
str := eventArgs[1].(string)
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
if debug {
|
|
|
|
log.Println("Message from " + user + ": " + str)
|
2019-04-17 01:42:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
args := commands.ParseCommandArguments(str)
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
idx := strings.Index(str, " ")
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
var argString string
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
if idx == -1 {
|
|
|
|
argString = ""
|
2019-04-17 01:42:46 +00:00
|
|
|
} else {
|
2020-07-10 01:01:29 +00:00
|
|
|
argString = strings.TrimSpace(str[idx+1:])
|
2019-04-17 01:42:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
var command string
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
if len(args) > 1 {
|
|
|
|
command, args = args[0], args[1:]
|
|
|
|
} else {
|
|
|
|
command = str
|
|
|
|
args = []string{}
|
2019-04-17 01:42:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
// Find the channel that the message came from. Override and use State if enabled.
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
match := commands.Find("!", command, str)
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
if match == nil {
|
2019-04-17 01:42:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
ctx := &commands.CommandContext{
|
|
|
|
User: user,
|
|
|
|
Prefix: "!",
|
|
|
|
Command: command,
|
|
|
|
ArgumentString: argString,
|
|
|
|
Arguments: args,
|
|
|
|
ArgumentCount: len(args),
|
2019-04-17 01:42:46 +00:00
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
Reply: func(text string) {
|
|
|
|
client.SendMessage(user, text)
|
|
|
|
},
|
2019-04-17 01:42:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 01:01:29 +00:00
|
|
|
match.Call(ctx)
|
2019-04-17 01:42:46 +00:00
|
|
|
}
|