wfc-server/api/kick.go
ppeb 41f7fddc4d
Revert "Revert motd, clear, and user action response"
This reverts commit 3144bd7cf8.

The reason this commit had to be made was because certain apis were PR'd
upstream, but were unwanted and thus removed in a separate commit. It
would likely have been better to drop the commits pushed upstream but
it's too late to fix that, so now we have a revert of a revert.
2025-03-05 20:33:20 -06:00

93 lines
2.2 KiB
Go

package api
import (
"encoding/json"
"io"
"net/http"
"strconv"
"wwfc/database"
"wwfc/gpcm"
)
func HandleKick(w http.ResponseWriter, r *http.Request) {
var user *database.User
var success bool
var err string
var statusCode int
if r.Method == http.MethodPost {
user, success, err, statusCode = handleKickImpl(r)
} else if r.Method == http.MethodOptions {
statusCode = http.StatusNoContent
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
} else {
err = "Incorrect request. POST only."
statusCode = http.StatusMethodNotAllowed
w.Header().Set("Allow", "POST")
}
w.Header().Set("Access-Control-Allow-Origin", "*")
if user == nil {
user = &database.User{}
}
var jsonData []byte
if statusCode != http.StatusNoContent {
w.Header().Set("Content-Type", "application/json")
jsonData, _ = json.Marshal(UserActionResponse{*user, success, err})
}
w.Header().Set("Content-Length", strconv.Itoa(len(jsonData)))
w.WriteHeader(statusCode)
w.Write(jsonData)
}
type KickRequestSpec struct {
Secret string `json:"secret"`
Reason string `json:"reason"`
ProfileID uint32 `json:"pid"`
}
func handleKickImpl(r *http.Request) (*database.User, bool, string, int) {
// TODO: Actual authentication rather than a fixed secret
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, false, "Unable to read request body", http.StatusBadRequest
}
var req KickRequestSpec
err = json.Unmarshal(body, &req)
if err != nil {
return nil, false, err.Error(), http.StatusBadRequest
}
if apiSecret == "" || req.Secret != apiSecret {
return nil, false, "Invalid API secret in request", http.StatusUnauthorized
}
if req.ProfileID == 0 {
return nil, false, "Profile ID missing or 0 in request", http.StatusBadRequest
}
if req.Reason == "" {
return nil, false, "Missing kick reason in request", http.StatusBadRequest
}
gpcm.KickPlayerCustomMessage(req.ProfileID, req.Reason, gpcm.WWFCMsgKickedCustom)
var message string
user, success := database.GetProfile(pool, ctx, req.ProfileID)
if success {
message = ""
} else {
message = "Unable to query user data from the database"
}
return &user, success, message, http.StatusOK
}