feat: add irc numerics package, deduplicate constants, fix dead code
All checks were successful
check / check (push) Successful in 1m5s
All checks were successful
check / check (push) Successful in 1m5s
- Create internal/irc/ package with all IRC numeric reply codes (RFC 1459/2812) and command string constants as the single source of truth - Replace all 69+ bare numeric string literals in api.go with named constants (e.g. irc.RplWelcome, irc.ErrNoSuchChannel) - Add 'code' (int) and named 'command' (e.g. RPL_YOURHOST) fields to IRC message JSON replies via irc.Name() lookup in scanMessages - Deduplicate command constants: remove local definitions from api.go, cmd/neoirc-cli/main.go, and cmd/neoirc-cli/api/client.go; all now import from internal/irc - Fix dead code: remove handleListCmd/handleWhoCmd/handleWhoisCmd/ sendWhoisNumerics that were unreachable due to dispatchCommand routing LIST/WHO/WHOIS to dispatchInfoCommand before dispatchQueryCommand. Route these commands to dispatchQueryCommand which has the improved implementations (e.g. ListAllChannelsWithCounts single-query vs N+1) - Update enqueueNumeric and respondIRCError signatures from string to int - Update test helper findNumeric to check the new 'code' JSON field Closes #52
This commit is contained in:
@@ -7,8 +7,10 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.eeqj.de/sneak/neoirc/internal/irc"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
@@ -33,6 +35,7 @@ func generateToken() (string, error) {
|
||||
type IRCMessage struct {
|
||||
ID string `json:"id"`
|
||||
Command string `json:"command"`
|
||||
Code int `json:"code,omitempty"`
|
||||
From string `json:"from,omitempty"`
|
||||
To string `json:"to,omitempty"`
|
||||
Params json.RawMessage `json:"params,omitempty"`
|
||||
@@ -42,6 +45,15 @@ type IRCMessage struct {
|
||||
DBID int64 `json:"-"`
|
||||
}
|
||||
|
||||
// isNumericCode returns true if s is exactly a 3-digit
|
||||
// IRC numeric reply code.
|
||||
func isNumericCode(s string) bool {
|
||||
return len(s) == 3 &&
|
||||
s[0] >= '0' && s[0] <= '9' &&
|
||||
s[1] >= '0' && s[1] <= '9' &&
|
||||
s[2] >= '0' && s[2] <= '9'
|
||||
}
|
||||
|
||||
// ChannelInfo is a lightweight channel representation.
|
||||
type ChannelInfo struct {
|
||||
ID int64 `json:"id"`
|
||||
@@ -717,6 +729,15 @@ func scanMessages(
|
||||
msg.DBID = qID
|
||||
lastQID = qID
|
||||
|
||||
if isNumericCode(msg.Command) {
|
||||
code, _ := strconv.Atoi(msg.Command)
|
||||
msg.Code = code
|
||||
|
||||
if name := irc.Name(code); name != "" {
|
||||
msg.Command = name
|
||||
}
|
||||
}
|
||||
|
||||
msgs = append(msgs, msg)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user