<commit_before>package main

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"log"
	"math"
	"mcquery"
	"net/http"
	"os"
	"strings"
)

type Configuration struct {
	HiddenByDefault bool
	DefaultPort     uint16
	DefaultIp       string
	SlackToken      string
}

var globalConfiguration Configuration

func getConfiguration(filename string) Configuration {
	var cfg Configuration
	if filename == "" {
		log.Println("No configuration file, using default")
		return Configuration{
			false,
			25565, // default minecraft port
			"",
			"",
		}
	}

	file, err := os.Open(filename)
	if err != nil {
		log.Fatal(err.Error())
	}

	decoder := json.NewDecoder(file)
	err = decoder.Decode(&cfg)
	if err != nil {
		log.Fatal(err.Error())
	}
	return cfg
}

type Command struct {
	Ip     string
	Port   uint16
	Hidden bool // Whether every use can see output or not
}

func parseCommand(cmd string, cfg *Configuration) (*Command, error) {
	flagSet := flag.NewFlagSet("command", flag.ContinueOnError)
	command := new(Command)

	flagSet.BoolVar(&command.Hidden, "hidden", cfg.HiddenByDefault,
		"Determines if command output visible to everyone")

	flagSet.StringVar(&command.Ip, "ip", cfg.DefaultIp, "Minecraft server IP")

	var portNum uint64
	flagSet.Uint64Var(&portNum, "port", uint64(cfg.DefaultPort), "MC Server Port")

	err := flagSet.Parse(strings.Fields(cmd))
	if err != nil {
		return nil, err
	}
	if portNum > math.MaxUint16 {
		return nil, errors.New("Port number out of range")
	}

	command.Port = uint16(portNum)

	return command, nil
}

func handleCommand(input string, cfg *Configuration) (*Command, string, error) {

	cmd, err := parseCommand(input, cfg)
	if err != nil {
		return nil, "", err
	}

	rw, err, kill := mcquery.Connect(cmd.Ip, cmd.Port)
	if err != nil {
		kill <- true
		return cmd, "", err
	}
	challenge, err := mcquery.Handshake(rw)
	if err != nil {
		kill <- true
		return cmd, "", err
	}
	statResponse, err := mcquery.BasicStat(rw, challenge)
	if err != nil {
		kill <- true
		return cmd, "", err
	}
	kill <- true

	responseString := fmt.Sprintf("```MOTD: %s\n", statResponse.Motd)
	responseString += fmt.Sprintf("Gametype: %s\n", statResponse.Gametype)
	responseString += fmt.Sprintf("Map: %s\n", statResponse.Map)
	responseString += fmt.Sprintf("NumPlayers: %s\n", statResponse.NumPlayers)
	responseString += fmt.Sprintf("MaxPlayers: %s\n", statResponse.MaxPlayers)
	responseString += fmt.Sprintf("HostPort: %d\n", statResponse.HostPort)
	responseString += fmt.Sprintf("HostIp: %s\n```", statResponse.HostIp)

	return cmd, responseString, nil
}

func processRequest(w http.ResponseWriter, r *http.Request) {

	if globalConfiguration.SlackToken != "" &&
		globalConfiguration.SlackToken != r.FormValue("token") {
		log.Printf("Connection with bad token: %s and command: %s\n",
			r.FormValue("token"), r.FormValue("text"))
		w.WriteHeader(401) // unauthorized<commit_msg>Add support for --full flag which does full stat<commit_after>package main

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"log"
	"math"
	"mcquery"
	"net/http"
	"os"
	"strings"
)

type Configuration struct {
	HiddenByDefault bool
	DefaultPort     uint16
	DefaultIp       string
	SlackToken      string
}

var globalConfiguration Configuration

func getConfiguration(filename string) Configuration {
	var cfg Configuration
	if filename == "" {
		log.Println("No configuration file, using default")
		return Configuration{
			false,
			25565, // default minecraft port
			"",
			"",
		}
	}

	file, err := os.Open(filename)
	if err != nil {
		log.Fatal(err.Error())
	}

	decoder := json.NewDecoder(file)
	err = decoder.Decode(&cfg)
	if err != nil {
		log.Fatal(err.Error())
	}
	return cfg
}

type Command struct {
	Ip     string
	Port   uint16
	Hidden bool // Whether every use can see output or not
	Full   bool // Perform a full stat instead of small stat
}

func parseCommand(cmd string, cfg *Configuration) (*Command, error) {
	flagSet := flag.NewFlagSet("command", flag.ContinueOnError)
	command := new(Command)

	flagSet.BoolVar(&command.Hidden, "hidden", cfg.HiddenByDefault,
		"Determines if command output visible to everyone")

	flagSet.StringVar(&command.Ip, "ip", cfg.DefaultIp, "Minecraft server IP")

	// TODO make default part of configuration
	flagSet.BoolVar(&command.Full, "full", false, "Perform full stat")

	var portNum uint64
	flagSet.Uint64Var(&portNum, "port", uint64(cfg.DefaultPort), "MC Server Port")

	err := flagSet.Parse(strings.Fields(cmd))
	if err != nil {
		return nil, err
	}
	if portNum > math.MaxUint16 {
		return nil, errors.New("Port number out of range")
	}

	command.Port = uint16(portNum)

	return command, nil
}

func handleCommand(input string, cfg *Configuration) (*Command, string, error) {

	cmd, err := parseCommand(input, cfg)
	if err != nil {
		return nil, "", err
	}

	rw, err, kill := mcquery.Connect(cmd.Ip, cmd.Port)
	if err != nil {
		kill <- true
		return cmd, "", err
	}
	challenge, err := mcquery.Handshake(rw)
	if err != nil {
		kill <- true
		return cmd, "", err
	}

	var responseString string
	if !cmd.Full {
		statResponse, err := mcquery.BasicStat(rw, challenge)
		if err != nil {
			kill <- true
			return cmd, "", err
		}
		kill <- true

		responseString += fmt.Sprintf("```MOTD: %s\n", statResponse.Motd)
		responseString += fmt.Sprintf("Gametype: %s\n", statResponse.Gametype)
		responseString += fmt.Sprintf("Map: %s\n", statResponse.Map)
		responseString += fmt.Sprintf("NumPlayers: %s\n", statResponse.NumPlayers)
		responseString += fmt.Sprintf("MaxPlayers: %s\n", statResponse.MaxPlayers)
		responseString += fmt.Sprintf("HostPort: %d\n", statResponse.HostPort)
		responseString += fmt.Sprintf("HostIp: %s\n```", statResponse.HostIp)
	} else {
		statResponse, err := mcquery.FullStat(rw, challenge)
		if err != nil {
			kill <- true
			return cmd, "", err
		}
		kill <- true

		responseString += "```\n"
		for k, v := range statResponse.KeyValues {
			responseString += fmt.Sprintf("%s: %s\n", k, v)
		}
		if len(statResponse.Players) > 0 {
			responseString += "Players:\n"
			for _, player := range statResponse.Players {
				responseString += fmt.Sprintf("    %s\n", player)
			}
		} else {
			responseString += "Players: <none>\n"
		}
		responseString += "```"
	}

	return cmd, responseString, nil
}

func processRequest(w http.ResponseWriter, r *http.Request) {

	if globalConfiguration.SlackToken != "" &&
		globalConfiguration.SlackToken != r.FormValue("token") {
		log.Printf("Connection with bad token: %s and command: %s\n",
			r.FormValue("token"), r.FormValue("text"))
		w.WriteHeader(401) // unauthorized
{'fraction_non_alphanumeric': 0.14203250797508735, 'fraction_numerical': 0.006835789153881209, 'mean_word_length': 3.987878787878788, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '306963', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2180, 'n_words': 581}