fix: address review findings — dynamic version, deduplicate KILL, update README
All checks were successful
check / check (push) Successful in 1m3s
All checks were successful
check / check (push) Successful in 1m3s
This commit is contained in:
@@ -331,18 +331,12 @@ func (hdlr *Handlers) handleKill(
|
||||
}
|
||||
|
||||
lines := bodyLines()
|
||||
if len(lines) == 0 {
|
||||
hdlr.respondIRCError(
|
||||
writer, request, clientID, sessionID,
|
||||
irc.ErrNeedMoreParams, nick,
|
||||
[]string{irc.CmdKill},
|
||||
"Not enough parameters",
|
||||
)
|
||||
|
||||
return
|
||||
var targetNick string
|
||||
if len(lines) > 0 {
|
||||
targetNick = strings.TrimSpace(lines[0])
|
||||
}
|
||||
|
||||
targetNick := strings.TrimSpace(lines[0])
|
||||
if targetNick == "" {
|
||||
hdlr.respondIRCError(
|
||||
writer, request, clientID, sessionID,
|
||||
@@ -383,8 +377,11 @@ func (hdlr *Handlers) handleKill(
|
||||
return
|
||||
}
|
||||
|
||||
hdlr.executeKillUser(
|
||||
request, targetSID, targetNick, nick, reason,
|
||||
quitReason := "Killed (" + nick + " (" + reason + "))"
|
||||
|
||||
hdlr.svc.BroadcastQuit(
|
||||
request.Context(), targetSID,
|
||||
targetNick, quitReason,
|
||||
)
|
||||
|
||||
hdlr.respondJSON(writer, request,
|
||||
@@ -392,71 +389,6 @@ func (hdlr *Handlers) handleKill(
|
||||
http.StatusOK)
|
||||
}
|
||||
|
||||
// executeKillUser forcibly disconnects a user: broadcasts
|
||||
// QUIT to their channels, parts all channels, and deletes
|
||||
// the session.
|
||||
func (hdlr *Handlers) executeKillUser(
|
||||
request *http.Request,
|
||||
targetSID int64,
|
||||
targetNick, killerNick, reason string,
|
||||
) {
|
||||
ctx := request.Context()
|
||||
|
||||
quitMsg := "Killed (" + killerNick + " (" + reason + "))"
|
||||
|
||||
quitBody, err := json.Marshal([]string{quitMsg})
|
||||
if err != nil {
|
||||
hdlr.log.Error(
|
||||
"marshal kill quit body", "error", err,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
channels, _ := hdlr.params.Database.
|
||||
GetSessionChannels(ctx, targetSID)
|
||||
|
||||
notified := map[int64]bool{}
|
||||
|
||||
var dbID int64
|
||||
|
||||
if len(channels) > 0 {
|
||||
dbID, _, _ = hdlr.params.Database.InsertMessage(
|
||||
ctx, irc.CmdQuit, targetNick, "",
|
||||
nil, json.RawMessage(quitBody), nil,
|
||||
)
|
||||
}
|
||||
|
||||
for _, chanInfo := range channels {
|
||||
memberIDs, _ := hdlr.params.Database.
|
||||
GetChannelMemberIDs(ctx, chanInfo.ID)
|
||||
|
||||
for _, mid := range memberIDs {
|
||||
if mid != targetSID && !notified[mid] {
|
||||
notified[mid] = true
|
||||
|
||||
_ = hdlr.params.Database.EnqueueToSession(
|
||||
ctx, mid, dbID,
|
||||
)
|
||||
|
||||
hdlr.broker.Notify(mid)
|
||||
}
|
||||
}
|
||||
|
||||
_ = hdlr.params.Database.PartChannel(
|
||||
ctx, chanInfo.ID, targetSID,
|
||||
)
|
||||
|
||||
_ = hdlr.params.Database.DeleteChannelIfEmpty(
|
||||
ctx, chanInfo.ID,
|
||||
)
|
||||
}
|
||||
|
||||
_ = hdlr.params.Database.DeleteSession(
|
||||
ctx, targetSID,
|
||||
)
|
||||
}
|
||||
|
||||
// handleWallops handles the WALLOPS command.
|
||||
// Broadcasts a message to all users with +w usermode
|
||||
// (oper only).
|
||||
|
||||
@@ -8,10 +8,29 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/service"
|
||||
"sneak.berlin/go/neoirc/pkg/irc"
|
||||
)
|
||||
|
||||
// versionString returns the server version for IRC
|
||||
// responses, falling back to "neoirc-dev" when globals
|
||||
// are not set (e.g. during tests).
|
||||
func versionString() string {
|
||||
name := globals.Appname
|
||||
ver := globals.Version
|
||||
|
||||
if name == "" {
|
||||
name = "neoirc"
|
||||
}
|
||||
|
||||
if ver == "" {
|
||||
ver = "dev"
|
||||
}
|
||||
|
||||
return name + "-" + ver
|
||||
}
|
||||
|
||||
// sendIRCError maps a service.IRCError to an IRC numeric
|
||||
// reply on the wire.
|
||||
func (c *Conn) sendIRCError(err error) {
|
||||
@@ -1384,7 +1403,7 @@ func (c *Conn) handleUserhost(
|
||||
func (c *Conn) handleVersion(ctx context.Context) {
|
||||
_ = ctx
|
||||
|
||||
version := "neoirc-0.1"
|
||||
version := versionString()
|
||||
|
||||
c.sendNumeric(
|
||||
irc.RplVersion,
|
||||
@@ -1426,7 +1445,7 @@ func (c *Conn) handleInfo(ctx context.Context) {
|
||||
|
||||
infoLines := []string{
|
||||
"neoirc — IRC semantics over HTTP",
|
||||
"Version: neoirc-0.1",
|
||||
"Version: " + versionString(),
|
||||
"Written in Go",
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user