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