residentsleeper/main.go

164 lines
3.2 KiB
Go
Raw Normal View History

2019-04-17 01:42:46 +00:00
package main
import (
"flag"
log "github.com/sirupsen/logrus"
"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"
"strings"
2019-04-17 01:42:46 +00:00
)
var (
serverPath string
scriptPath string
rconPort int
2019-04-17 01:42:46 +00:00
rconPassword string
debug bool
2019-04-17 01:42:46 +00:00
client *rcon.Session
2019-04-17 01:42:46 +00:00
configRegexp = regexp.MustCompile("^(.*?)=(.*)$")
flagDebug = flag.Bool("debug", false, "debug messages")
2019-04-17 01:42:46 +00:00
)
func init() {
flag.StringVar(&serverPath, "dir", "", "server directory")
flag.StringVar(&scriptPath, "scriptPath", "", "script path override")
2019-04-17 01:42:46 +00:00
}
func main() {
flag.Parse()
debug = *flagDebug
if debug {
log.SetLevel(log.DebugLevel)
}
2019-04-17 01:42:46 +00:00
// Load properties
configPath := path.Join(serverPath, "server.properties")
cfg, err := loadServerConfig(configPath)
if err != nil {
log.WithError(err).Fatalln("Unable to load config")
2019-04-17 01:42:46 +00:00
}
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")
}
events.On(events.Message, handleCommands)
2019-04-17 01:42:46 +00:00
2020-07-10 03:02:01 +00:00
rconHost := "localhost"
if envHost := os.Getenv("RCON_HOST"); envHost != "" {
rconHost = envHost
}
client = rcon.NewSession(rconHost, rconPort, rconPassword)
2019-04-17 01:42:46 +00:00
client.Debug = debug
2019-04-17 01:42:46 +00:00
if scriptPath == "" {
scriptPath = path.Join(serverPath, "scripts")
}
2019-04-17 01:42:46 +00:00
if _, err := os.Stat(scriptPath); !os.IsNotExist(err) {
config.BasePath = path.Join(scriptPath, "config")
2019-04-17 01:42:46 +00:00
minecraft.BasePath = serverPath
minecraft.WorldPath = path.Join(serverPath, cfg["level-name"])
2019-04-17 01:42:46 +00:00
if _, err = os.Stat(config.BasePath); os.IsNotExist(err) {
os.Mkdir(config.BasePath, 0755)
2019-04-17 01:42:46 +00:00
}
log.WithField("path", scriptPath).Info("Loading scripts")
err = loadScripts(scriptPath)
2019-04-17 01:42:46 +00:00
if err != nil {
log.WithError(err).Warn("Unable to load scripts")
2019-04-17 01:42:46 +00:00
}
}
logPath := path.Join(serverPath, "logs/latest.log")
2019-04-17 01:42:46 +00:00
logParser(logPath)
}
2019-04-17 01:42:46 +00:00
func handleCommands(eventArgs ...interface{}) {
user := eventArgs[0].(string)
str := eventArgs[1].(string)
2019-04-17 01:42:46 +00:00
log.WithFields(log.Fields{
"user": user,
"message": str,
}).Debug("Message received")
2019-04-17 01:42:46 +00:00
args := commands.ParseCommandArguments(str)
2019-04-17 01:42:46 +00:00
idx := strings.Index(str, " ")
2019-04-17 01:42:46 +00:00
var argString string
2019-04-17 01:42:46 +00:00
if idx == -1 {
argString = ""
2019-04-17 01:42:46 +00:00
} else {
argString = strings.TrimSpace(str[idx+1:])
2019-04-17 01:42:46 +00:00
}
var command string
2019-04-17 01:42:46 +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
}
// Find the channel that the message came from. Override and use State if enabled.
2019-04-17 01:42:46 +00:00
match := commands.Find("!", command, str)
2019-04-17 01:42:46 +00:00
if match == nil {
2019-04-17 01:42:46 +00:00
return
}
ctx := &commands.CommandContext{
User: user,
Prefix: "!",
Command: command,
ArgumentString: argString,
Arguments: args,
ArgumentCount: len(args),
2019-04-17 01:42:46 +00:00
Reply: func(text string) {
client.SendMessage(user, text)
},
2019-04-17 01:42:46 +00:00
}
go match.Call(ctx)
2019-04-17 01:42:46 +00:00
}