Show an IRC listener user what their session sends from other clients (closes #179)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The relay dropped every PRIVMSG, NOTICE, JOIN, PART, NICK, QUIT, TOPIC, MODE and KICK sent under the connection's own nick, so an IRC listener connection never saw what its session did from another client. The relay now drops nothing by nick. Instead, the service methods that fan out a user's own command take the client whose queue to skip: the IRC listener passes its own client, which has already written the echo, and the HTTP API passes 0. Channel messages and PARTs now also go to the sender's own session, so the HTTP handler no longer queues the channel-message echo itself, SendChannelMessage no longer returns the message's row ID, and neoirc-cli ignores the PART event for its own part, which it has already shown. Model: opus-5-5
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package ircserver_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -8,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/neoirc/internal/ircserver"
|
||||
"sneak.berlin/go/neoirc/internal/service"
|
||||
)
|
||||
|
||||
// queueMessage queues a message for the user with nick
|
||||
@@ -39,7 +41,7 @@ func (env *testEnv) queueMessage(
|
||||
t.Fatalf("get session of %s: %v", recipient, err)
|
||||
}
|
||||
|
||||
err = env.database.EnqueueToSession(ctx, sessionID, msgID)
|
||||
err = env.database.EnqueueToSession(ctx, sessionID, msgID, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("enqueue: %v", err)
|
||||
}
|
||||
@@ -391,3 +393,211 @@ func TestRelayOwnChangeOnce(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRelayOwnSessionOnce checks that a command a user makes
|
||||
// from one client of their session reaches each of the
|
||||
// session's clients once: an IRC listener connection and an
|
||||
// HTTP API client. The connection that sent a command gets
|
||||
// only its own echo, and none for PRIVMSG and NOTICE, which
|
||||
// IRC does not echo.
|
||||
//
|
||||
//nolint:funlen // table-driven test
|
||||
func TestRelayOwnSessionOnce(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
hello := json.RawMessage(`["hello"]`)
|
||||
|
||||
for _, step := range []struct {
|
||||
// line is the command as alice sends it on the IRC
|
||||
// listener.
|
||||
line string
|
||||
// api makes the same command for her session the way
|
||||
// the HTTP API does.
|
||||
api func(
|
||||
ctx context.Context, svc *service.Service, sessionID int64,
|
||||
) error
|
||||
// echoes is how many copies alice's connection gets of
|
||||
// line when it sends line itself.
|
||||
echoes int
|
||||
}{
|
||||
{"PRIVMSG #chan :hello", func(
|
||||
ctx context.Context, svc *service.Service, sessionID int64,
|
||||
) error {
|
||||
_, err := svc.SendChannelMessage(
|
||||
ctx, sessionID, 0, "alice", "PRIVMSG", "#chan", hello, nil,
|
||||
)
|
||||
|
||||
return err
|
||||
}, 0},
|
||||
{"NOTICE bob :hello", func(
|
||||
ctx context.Context, svc *service.Service, sessionID int64,
|
||||
) error {
|
||||
_, err := svc.SendDirectMessage(
|
||||
ctx, sessionID, 0, "alice", "NOTICE", "bob", hello, nil,
|
||||
)
|
||||
|
||||
return err
|
||||
}, 0},
|
||||
{"JOIN #other", func(
|
||||
ctx context.Context, svc *service.Service, sessionID int64,
|
||||
) error {
|
||||
_, err := svc.JoinChannel(ctx, sessionID, 0, "alice", "#other", "")
|
||||
|
||||
return err
|
||||
}, 1},
|
||||
{"PART #chan :bye", func(
|
||||
ctx context.Context, svc *service.Service, sessionID int64,
|
||||
) error {
|
||||
return svc.PartChannel(ctx, sessionID, 0, "alice", "#chan", "bye")
|
||||
}, 1},
|
||||
{"NICK alice2", func(
|
||||
ctx context.Context, svc *service.Service, sessionID int64,
|
||||
) error {
|
||||
return svc.ChangeNick(ctx, sessionID, 0, "alice", "alice2")
|
||||
}, 1},
|
||||
{"TOPIC #chan :new topic", func(
|
||||
ctx context.Context, svc *service.Service, sessionID int64,
|
||||
) error {
|
||||
return svc.SetTopic(
|
||||
ctx, sessionID, 0, "alice", "#chan", "new topic",
|
||||
)
|
||||
}, 1},
|
||||
{"MODE #chan +m", func(
|
||||
ctx context.Context, svc *service.Service, sessionID int64,
|
||||
) error {
|
||||
chID, err := svc.ValidateChannelOp(ctx, sessionID, "#chan")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = svc.SetChannelFlag(ctx, chID, 'm', true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svc.BroadcastMode(ctx, 0, "alice", "#chan", chID, "+m")
|
||||
|
||||
return nil
|
||||
}, 1},
|
||||
{"KICK #chan bob :bye", func(
|
||||
ctx context.Context, svc *service.Service, sessionID int64,
|
||||
) error {
|
||||
return svc.KickUser(
|
||||
ctx, sessionID, 0, "alice", "#chan", "bob", "bye",
|
||||
)
|
||||
}, 1},
|
||||
} {
|
||||
command := strings.Fields(step.line)[0]
|
||||
|
||||
t.Run(command+" from IRC", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ircCopies, httpCopies := ownSessionCopies(t, step.line, nil)
|
||||
if ircCopies != step.echoes || httpCopies != 1 {
|
||||
t.Errorf("connection got %d, HTTP client got %d, want %d and 1",
|
||||
ircCopies, httpCopies, step.echoes)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run(command+" from the HTTP API", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ircCopies, httpCopies := ownSessionCopies(t, step.line, step.api)
|
||||
if ircCopies != 1 || httpCopies != 1 {
|
||||
t.Errorf("connection got %d, HTTP client got %d, want 1 and 1",
|
||||
ircCopies, httpCopies)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ownSessionCopies connects alice to the IRC listener and has
|
||||
// her join #chan, which bob is on too, then adds an HTTP API
|
||||
// client to her session with the password her connection set,
|
||||
// as POST /api/v1/login does. It makes the command line from
|
||||
// her connection, or with api from the HTTP API client when api
|
||||
// is not nil, and returns how many copies of it her connection
|
||||
// and her HTTP API client got.
|
||||
func ownSessionCopies(
|
||||
t *testing.T,
|
||||
line string,
|
||||
api func(ctx context.Context, svc *service.Service, sessionID int64) error,
|
||||
) (int, int) {
|
||||
t.Helper()
|
||||
|
||||
const password = "alicepass"
|
||||
|
||||
ctx := t.Context()
|
||||
env := newTestEnv(t)
|
||||
|
||||
alice := env.dial(t)
|
||||
alice.send("PASS " + password)
|
||||
alice.register("alice")
|
||||
// Registration stores the password before the JOIN runs.
|
||||
alice.joinAndDrain("#chan")
|
||||
|
||||
bob := env.dial(t)
|
||||
bob.register("bob")
|
||||
bob.joinAndDrain("#chan")
|
||||
|
||||
sessionID, httpClientID, _, err := env.database.LoginUser(
|
||||
ctx, "alice", password, "127.0.0.1", "localhost",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("log in as alice: %v", err)
|
||||
}
|
||||
|
||||
var lines []string
|
||||
|
||||
if api == nil {
|
||||
alice.send(line)
|
||||
|
||||
// Her echo comes before the reply to her next command.
|
||||
lines = alice.sendAndExpect("PING :done", "PONG")
|
||||
} else {
|
||||
err = api(ctx, env.srv.Service(), sessionID)
|
||||
if err != nil {
|
||||
t.Fatalf("%s over the HTTP API: %v", line, err)
|
||||
}
|
||||
}
|
||||
|
||||
msgs, _, err := env.database.PollMessages(ctx, httpClientID, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("poll the HTTP API client: %v", err)
|
||||
}
|
||||
|
||||
httpCopies := 0
|
||||
|
||||
for _, msg := range msgs {
|
||||
if msg.Command == strings.Fields(line)[0] {
|
||||
httpCopies++
|
||||
}
|
||||
}
|
||||
|
||||
// The relay sends a queue in order, so this arrives after any
|
||||
// copy. It is queued by session, as NICK changes her nick.
|
||||
endID, _, err := env.database.InsertMessage(
|
||||
ctx, "PRIVMSG", "dave", "alice", 0, 0,
|
||||
nil, json.RawMessage(`["the end"]`), nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert message: %v", err)
|
||||
}
|
||||
|
||||
err = env.database.EnqueueToSession(ctx, sessionID, endID, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("enqueue: %v", err)
|
||||
}
|
||||
|
||||
env.brk.Notify(sessionID)
|
||||
|
||||
lines = append(lines, alice.readUntil(func(l string) bool {
|
||||
return strings.HasSuffix(l, " :the end")
|
||||
})...)
|
||||
|
||||
// A copy starts with the command and its first parameter.
|
||||
start := " " + strings.Join(strings.Fields(line)[:2], " ")
|
||||
ircCopies := strings.Count(strings.Join(lines, "\n"), start)
|
||||
|
||||
return ircCopies, httpCopies
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user