fix: resolve cyclop, funlen issues by extracting helper methods
This commit is contained in:
@@ -239,51 +239,53 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
|
||||
|
||||
req.Command = strings.ToUpper(strings.TrimSpace(req.Command))
|
||||
req.To = strings.TrimSpace(req.To)
|
||||
lines := extractBodyLines(req.Body)
|
||||
|
||||
// Helper to extract body as string lines.
|
||||
bodyLines := func() []string {
|
||||
switch v := req.Body.(type) {
|
||||
case []any:
|
||||
lines := make([]string, 0, len(v))
|
||||
s.dispatchCommand(w, r, uid, nick, req.Command, req.To, lines)
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range v {
|
||||
if str, ok := item.(string); ok {
|
||||
lines = append(lines, str)
|
||||
}
|
||||
}
|
||||
// extractBodyLines converts the request body to string lines.
|
||||
func extractBodyLines(body any) []string {
|
||||
switch v := body.(type) {
|
||||
case []any:
|
||||
lines := make([]string, 0, len(v))
|
||||
|
||||
return lines
|
||||
case []string:
|
||||
return v
|
||||
default:
|
||||
return nil
|
||||
for _, item := range v {
|
||||
if str, ok := item.(string); ok {
|
||||
lines = append(lines, str)
|
||||
}
|
||||
}
|
||||
|
||||
switch req.Command {
|
||||
case "PRIVMSG", "NOTICE":
|
||||
s.handlePrivmsg(w, r, uid, nick, req.To, bodyLines())
|
||||
return lines
|
||||
case []string:
|
||||
return v
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
case "JOIN":
|
||||
s.handleJoin(w, r, uid, req.To)
|
||||
func (s *Handlers) dispatchCommand(
|
||||
w http.ResponseWriter, r *http.Request,
|
||||
uid, nick, command, to string, lines []string,
|
||||
) {
|
||||
switch command {
|
||||
case "PRIVMSG", "NOTICE":
|
||||
s.handlePrivmsg(w, r, uid, nick, to, lines)
|
||||
case "JOIN":
|
||||
s.handleJoin(w, r, uid, to)
|
||||
case "PART":
|
||||
s.handlePart(w, r, uid, to)
|
||||
case "NICK":
|
||||
s.handleNick(w, r, uid, lines)
|
||||
case "TOPIC":
|
||||
s.handleTopic(w, r, uid, to, lines)
|
||||
case "PING":
|
||||
s.respondJSON(w, r, map[string]string{"command": "PONG", "from": s.params.Config.ServerName}, http.StatusOK)
|
||||
default:
|
||||
_ = nick
|
||||
|
||||
case "PART":
|
||||
s.handlePart(w, r, uid, req.To)
|
||||
|
||||
case "NICK":
|
||||
s.handleNick(w, r, uid, bodyLines())
|
||||
|
||||
case "TOPIC":
|
||||
s.handleTopic(w, r, uid, req.To, bodyLines())
|
||||
|
||||
case "PING":
|
||||
s.respondJSON(w, r, map[string]string{"command": "PONG", "from": s.params.Config.ServerName}, http.StatusOK)
|
||||
|
||||
default:
|
||||
_ = nick // suppress unused warning
|
||||
|
||||
s.respondJSON(w, r, map[string]string{"error": "unknown command: " + req.Command}, http.StatusBadRequest)
|
||||
}
|
||||
s.respondJSON(w, r, map[string]string{"error": "unknown command: " + command}, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user