Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a2bd820ec |
No files matched your search
@@ -264,25 +264,6 @@ func (database *Database) GetSessionByNick(
|
|||||||
return sessionID, nil
|
return sessionID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSessionNick returns the nick a session holds.
|
|
||||||
func (database *Database) GetSessionNick(
|
|
||||||
ctx context.Context,
|
|
||||||
sessionID int64,
|
|
||||||
) (string, error) {
|
|
||||||
var nick string
|
|
||||||
|
|
||||||
err := database.conn.QueryRowContext(
|
|
||||||
ctx,
|
|
||||||
"SELECT nick FROM sessions WHERE id = ?",
|
|
||||||
sessionID,
|
|
||||||
).Scan(&nick)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("get session nick: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nick, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SessionHostInfo holds the username, hostname, and IP
|
// SessionHostInfo holds the username, hostname, and IP
|
||||||
// for a session.
|
// for a session.
|
||||||
type SessionHostInfo struct {
|
type SessionHostInfo struct {
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ func (c *Conn) drainQueue(ctx context.Context) bool {
|
|||||||
//
|
//
|
||||||
//nolint:cyclop // dispatch table
|
//nolint:cyclop // dispatch table
|
||||||
func (c *Conn) deliverIRCMessage(
|
func (c *Conn) deliverIRCMessage(
|
||||||
ctx context.Context,
|
_ context.Context,
|
||||||
msg *db.IRCMessage,
|
msg *db.IRCMessage,
|
||||||
) {
|
) {
|
||||||
command := msg.Command
|
command := msg.Command
|
||||||
@@ -120,7 +120,7 @@ func (c *Conn) deliverIRCMessage(
|
|||||||
case command == irc.CmdPart:
|
case command == irc.CmdPart:
|
||||||
c.deliverPart(msg, text)
|
c.deliverPart(msg, text)
|
||||||
case command == irc.CmdNick:
|
case command == irc.CmdNick:
|
||||||
c.deliverNickChange(ctx, msg, text)
|
c.deliverNickChange(msg, text)
|
||||||
case command == irc.CmdQuit:
|
case command == irc.CmdQuit:
|
||||||
c.deliverQuitMsg(msg, text)
|
c.deliverQuitMsg(msg, text)
|
||||||
case command == irc.CmdTopic:
|
case command == irc.CmdTopic:
|
||||||
@@ -230,23 +230,18 @@ func (c *Conn) deliverPart(msg *db.IRCMessage, text string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// deliverNickChange sends a NICK change notification. The
|
// deliverNickChange sends a NICK change notification. When
|
||||||
// change may be its own session's, made from another client,
|
// the old nick is the connection's own, the change was made
|
||||||
// so the connection first takes the nick its session holds
|
// from another client of its session, as nicks are unique,
|
||||||
// now. It does not go by the line's old nick: the connection
|
// and the connection takes the new nick too.
|
||||||
// may have taken that nick after another user gave it up. It
|
|
||||||
// reads the nick under c.mu, so that a NICK sent on the
|
|
||||||
// connection meanwhile is not undone.
|
|
||||||
func (c *Conn) deliverNickChange(
|
func (c *Conn) deliverNickChange(
|
||||||
ctx context.Context,
|
|
||||||
msg *db.IRCMessage,
|
msg *db.IRCMessage,
|
||||||
newNick string,
|
newNick string,
|
||||||
) {
|
) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
|
|
||||||
nick, err := c.database.GetSessionNick(ctx, c.sessionID)
|
if msg.From == c.nick {
|
||||||
if err == nil {
|
c.nick = newNick
|
||||||
c.nick = nick
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|||||||
@@ -655,43 +655,3 @@ func TestRelayNickChangeFromHTTPAPI(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestRelayNickChangeKeepsTakenNick checks that an IRC
|
|
||||||
// listener connection which takes a nick another user has
|
|
||||||
// given up keeps it when that user's change reaches it
|
|
||||||
// afterwards: what it then sends goes out under the nick its
|
|
||||||
// session holds.
|
|
||||||
func TestRelayNickChangeKeepsTakenNick(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
env := newTestEnv(t)
|
|
||||||
|
|
||||||
bob := env.dial(t)
|
|
||||||
bob.register("bob")
|
|
||||||
bob.sendAndExpect("NICK bob2", " NICK bob2")
|
|
||||||
|
|
||||||
alice := env.dial(t)
|
|
||||||
alice.register("alice")
|
|
||||||
alice.sendAndExpect("NICK bob", " NICK bob")
|
|
||||||
alice.joinAndDrain("#chan")
|
|
||||||
|
|
||||||
bob.joinAndDrain("#chan")
|
|
||||||
|
|
||||||
// Bob's change reaches alice only now, as it does when she
|
|
||||||
// takes his old nick before her connection has passed the
|
|
||||||
// change on. She holds bob now, so it is queued to bob.
|
|
||||||
env.queueMessage(t, "bob", "NICK", "bob", "", []string{"bob2"})
|
|
||||||
|
|
||||||
alice.readUntil(func(l string) bool {
|
|
||||||
return l == ":bob!bob@* NICK bob2"
|
|
||||||
})
|
|
||||||
|
|
||||||
alice.send("PRIVMSG #chan :hi there")
|
|
||||||
|
|
||||||
const want = ":bob!bob@* PRIVMSG #chan :hi there"
|
|
||||||
|
|
||||||
got := bob.readLine()
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("got %q, want %q", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user