Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2119a7fe5 |
+49
-21
@@ -96,7 +96,20 @@ func (a *App) handleInput(text string) {
|
||||
a.ui.AddStatus(
|
||||
"[red]Send error: " + err.Error(),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format(timeFormat)
|
||||
|
||||
a.mu.Lock()
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
|
||||
a.ui.AddLine(target, fmt.Sprintf(
|
||||
"[gray]%s [green]<%s>[white] %s",
|
||||
timestamp, nick, text,
|
||||
))
|
||||
}
|
||||
|
||||
func (a *App) handleCommand(text string) {
|
||||
@@ -288,6 +301,9 @@ func (a *App) cmdJoin(channel string) {
|
||||
a.mu.Unlock()
|
||||
|
||||
a.ui.SwitchToBuffer(channel)
|
||||
a.ui.AddLine(channel,
|
||||
"[yellow]*** Joined "+channel,
|
||||
)
|
||||
a.ui.SetStatus(nick, channel, "connected")
|
||||
}
|
||||
|
||||
@@ -322,6 +338,10 @@ func (a *App) cmdPart(channel string) {
|
||||
return
|
||||
}
|
||||
|
||||
a.ui.AddLine(channel,
|
||||
"[yellow]*** Left "+channel,
|
||||
)
|
||||
|
||||
a.mu.Lock()
|
||||
if a.target == channel {
|
||||
a.target = ""
|
||||
@@ -348,6 +368,7 @@ func (a *App) cmdMsg(args string) {
|
||||
|
||||
a.mu.Lock()
|
||||
connected := a.connected
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
|
||||
if !connected {
|
||||
@@ -365,7 +386,16 @@ func (a *App) cmdMsg(args string) {
|
||||
a.ui.AddStatus(fmt.Sprintf(
|
||||
"[red]Send failed: %v", err,
|
||||
))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format(timeFormat)
|
||||
|
||||
a.ui.AddLine(target, fmt.Sprintf(
|
||||
"[gray]%s [green]<%s>[white] %s",
|
||||
timestamp, nick, text,
|
||||
))
|
||||
}
|
||||
|
||||
func (a *App) cmdQuery(nick string) {
|
||||
@@ -755,10 +785,12 @@ func (a *App) handlePrivmsgEvent(
|
||||
lines := msg.BodyLines()
|
||||
text := strings.Join(lines, " ")
|
||||
|
||||
// A direct message shows in the buffer of the other user:
|
||||
// the recipient of one the user sent, else the sender.
|
||||
if msg.From == myNick {
|
||||
return
|
||||
}
|
||||
|
||||
target := msg.To
|
||||
if !strings.HasPrefix(target, "#") && msg.From != myNick {
|
||||
if !strings.HasPrefix(target, "#") {
|
||||
target = msg.From
|
||||
}
|
||||
|
||||
@@ -781,35 +813,31 @@ func (a *App) handleJoinEvent(
|
||||
))
|
||||
}
|
||||
|
||||
// handlePartEvent shows a part in the channel's buffer. The
|
||||
// user's own part does not mark the channel unread: the user
|
||||
// has just left it.
|
||||
// handlePartEvent shows another user's part. The server
|
||||
// also sends the user's own part back to it, which cmdPart
|
||||
// has already shown; showing it again would add a second
|
||||
// line and mark the channel just left as unread.
|
||||
func (a *App) handlePartEvent(
|
||||
msg *api.Message, timestamp, myNick string,
|
||||
) {
|
||||
if msg.To == "" {
|
||||
if msg.To == "" || msg.From == myNick {
|
||||
return
|
||||
}
|
||||
|
||||
lines := msg.BodyLines()
|
||||
reason := strings.Join(lines, " ")
|
||||
|
||||
line := fmt.Sprintf(
|
||||
"[gray]%s [yellow]*** %s has left %s",
|
||||
timestamp, msg.From, msg.To,
|
||||
)
|
||||
|
||||
if reason != "" {
|
||||
line += " (" + reason + ")"
|
||||
a.ui.AddLine(msg.To, fmt.Sprintf(
|
||||
"[gray]%s [yellow]*** %s has left %s (%s)",
|
||||
timestamp, msg.From, msg.To, reason,
|
||||
))
|
||||
} else {
|
||||
a.ui.AddLine(msg.To, fmt.Sprintf(
|
||||
"[gray]%s [yellow]*** %s has left %s",
|
||||
timestamp, msg.From, msg.To,
|
||||
))
|
||||
}
|
||||
|
||||
if msg.From == myNick {
|
||||
a.ui.AddLineAsRead(msg.To, line)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
a.ui.AddLine(msg.To, line)
|
||||
}
|
||||
|
||||
func (a *App) handleQuitEvent(
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
@@ -10,15 +8,6 @@ import (
|
||||
"sneak.berlin/go/neoirc/pkg/irc"
|
||||
)
|
||||
|
||||
// The user's nick, a channel it is in, another user's nick and
|
||||
// a message text, for the tests below.
|
||||
const (
|
||||
testNick = "erin"
|
||||
testChannel = "#chan"
|
||||
testOtherNick = "bob"
|
||||
testText = "hello"
|
||||
)
|
||||
|
||||
func TestMemberPrefix(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -64,190 +53,48 @@ func TestMemberPrefix(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestPartEvent checks that a PART event adds a line to the
|
||||
// channel's buffer and marks it unread, except that the
|
||||
// user's own part does not mark it unread.
|
||||
// channel's buffer and marks it unread, except for the
|
||||
// user's own part, which cmdPart has already shown.
|
||||
func TestPartEvent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
from string
|
||||
wantLines int
|
||||
wantUnread int
|
||||
}{
|
||||
{name: "own part", from: testNick, wantUnread: 0},
|
||||
{name: "other member", from: testOtherNick, wantUnread: 1},
|
||||
{name: "own part", from: "alice", wantLines: 0, wantUnread: 0},
|
||||
{name: "other member", from: "bob", wantLines: 1, wantUnread: 1},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := &App{ui: NewUI(), nick: testNick}
|
||||
app := &App{ui: NewUI(), nick: "alice"}
|
||||
runUI(t, app.ui)
|
||||
|
||||
app.handleServerMessage(&api.Message{
|
||||
Command: irc.CmdPart, From: test.from, To: testChannel,
|
||||
Command: irc.CmdPart, From: test.from, To: "#chan",
|
||||
})
|
||||
|
||||
lines, unread := bufferState(app.ui, testChannel)
|
||||
if lines != 1 || unread != test.wantUnread {
|
||||
t.Fatalf("got %d lines and %d unread, want 1 and %d",
|
||||
lines, unread, test.wantUnread)
|
||||
var lines, unread int
|
||||
|
||||
app.ui.app.QueueUpdate(func() {
|
||||
buf := app.ui.getOrCreateBuffer("#chan")
|
||||
lines = len(buf.Lines)
|
||||
unread = buf.Unread
|
||||
})
|
||||
|
||||
if lines != test.wantLines || unread != test.wantUnread {
|
||||
t.Fatalf("got %d lines and %d unread, want %d and %d",
|
||||
lines, unread, test.wantLines, test.wantUnread)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestOwnCommandShownOnce checks that a message, join or
|
||||
// part made from neoirc-cli shows as one line in the
|
||||
// channel's buffer once the server's event for it arrives,
|
||||
// and does not mark the channel unread.
|
||||
func TestOwnCommandShownOnce(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
send func(app *App)
|
||||
event api.Message
|
||||
}{
|
||||
{
|
||||
name: "message",
|
||||
send: func(app *App) { app.handleInput(testText) },
|
||||
event: api.Message{
|
||||
Command: irc.CmdPrivmsg, From: testNick, To: testChannel,
|
||||
Body: []string{testText},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "join",
|
||||
send: func(app *App) { app.cmdJoin(testChannel) },
|
||||
event: api.Message{
|
||||
Command: irc.CmdJoin, From: testNick, To: testChannel,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "part",
|
||||
send: func(app *App) { app.cmdPart(testChannel) },
|
||||
event: api.Message{
|
||||
Command: irc.CmdPart, From: testNick, To: testChannel,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// The server accepts every command and answers nothing.
|
||||
server := httptest.NewServer(http.HandlerFunc(
|
||||
func(http.ResponseWriter, *http.Request) {},
|
||||
))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
app := &App{
|
||||
ui: NewUI(),
|
||||
client: api.NewClient(server.URL),
|
||||
nick: testNick,
|
||||
target: testChannel,
|
||||
connected: true,
|
||||
}
|
||||
runUI(t, app.ui)
|
||||
app.ui.SwitchToBuffer(testChannel)
|
||||
|
||||
test.send(app)
|
||||
app.handleServerMessage(&test.event)
|
||||
|
||||
lines, unread := bufferState(app.ui, testChannel)
|
||||
if lines != 1 || unread != 0 {
|
||||
t.Fatalf("got %d lines and %d unread, want 1 and 0",
|
||||
lines, unread)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestOwnEventFromOtherClient checks that a message, notice,
|
||||
// join or part that the user's session makes from another
|
||||
// client shows as one line in the buffer it belongs to.
|
||||
func TestOwnEventFromOtherClient(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
event api.Message
|
||||
buffer string
|
||||
}{
|
||||
{
|
||||
name: "channel message",
|
||||
event: api.Message{
|
||||
Command: irc.CmdPrivmsg, From: testNick, To: testChannel,
|
||||
Body: []string{testText},
|
||||
},
|
||||
buffer: testChannel,
|
||||
},
|
||||
{
|
||||
name: "direct message",
|
||||
event: api.Message{
|
||||
Command: irc.CmdPrivmsg, From: testNick, To: testOtherNick,
|
||||
Body: []string{testText},
|
||||
},
|
||||
buffer: testOtherNick,
|
||||
},
|
||||
{
|
||||
name: "notice",
|
||||
event: api.Message{
|
||||
Command: irc.CmdNotice, From: testNick, To: testChannel,
|
||||
Body: []string{testText},
|
||||
},
|
||||
buffer: "(status)",
|
||||
},
|
||||
{
|
||||
name: "join",
|
||||
event: api.Message{
|
||||
Command: irc.CmdJoin, From: testNick, To: testChannel,
|
||||
},
|
||||
buffer: testChannel,
|
||||
},
|
||||
{
|
||||
name: "part",
|
||||
event: api.Message{
|
||||
Command: irc.CmdPart, From: testNick, To: testChannel,
|
||||
},
|
||||
buffer: testChannel,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := &App{ui: NewUI(), nick: testNick}
|
||||
runUI(t, app.ui)
|
||||
|
||||
app.handleServerMessage(&test.event)
|
||||
|
||||
lines, _ := bufferState(app.ui, test.buffer)
|
||||
if lines != 1 {
|
||||
t.Fatalf("got %d lines in %s, want 1", lines, test.buffer)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// bufferState returns the number of lines in the named
|
||||
// buffer and how many of them are unread.
|
||||
func bufferState(ui *UI, name string) (int, int) {
|
||||
var lines, unread int
|
||||
|
||||
ui.app.QueueUpdate(func() {
|
||||
buf := ui.getOrCreateBuffer(name)
|
||||
lines = len(buf.Lines)
|
||||
unread = buf.Unread
|
||||
})
|
||||
|
||||
return lines, unread
|
||||
}
|
||||
|
||||
// runUI runs ui on a simulated screen until the test ends.
|
||||
func runUI(t *testing.T, ui *UI) {
|
||||
t.Helper()
|
||||
|
||||
@@ -95,19 +95,6 @@ func (ui *UI) AddLine(bufferName, line string) {
|
||||
})
|
||||
}
|
||||
|
||||
// AddLineAsRead adds a line to the specified buffer, as
|
||||
// AddLine does, but does not mark the buffer unread.
|
||||
func (ui *UI) AddLineAsRead(bufferName, line string) {
|
||||
ui.app.QueueUpdateDraw(func() {
|
||||
buf := ui.getOrCreateBuffer(bufferName)
|
||||
buf.Lines = append(buf.Lines, line)
|
||||
|
||||
if ui.buffers[ui.currentBuffer] == buf {
|
||||
_, _ = fmt.Fprintln(ui.messages, line)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// AddStatus adds a line to the status buffer.
|
||||
func (ui *UI) AddStatus(line string) {
|
||||
ts := time.Now().Format("15:04")
|
||||
|
||||
@@ -264,25 +264,6 @@ func (database *Database) GetSessionByNick(
|
||||
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
|
||||
// for a session.
|
||||
type SessionHostInfo struct {
|
||||
|
||||
@@ -81,7 +81,7 @@ func (c *Conn) handleNick(
|
||||
}
|
||||
|
||||
oldMask := c.hostmask()
|
||||
oldNick := c.currentNick()
|
||||
oldNick := c.nick
|
||||
|
||||
err := c.svc.ChangeNick(
|
||||
ctx, c.sessionID, c.clientID, oldNick, newNick,
|
||||
@@ -92,10 +92,9 @@ func (c *Conn) handleNick(
|
||||
return
|
||||
}
|
||||
|
||||
// Take the nick the session holds, not newNick: another
|
||||
// client of the session may have changed it again since
|
||||
// ChangeNick stored newNick.
|
||||
c.loadSessionNick(ctx)
|
||||
c.mu.Lock()
|
||||
c.nick = newNick
|
||||
c.mu.Unlock()
|
||||
|
||||
// Echo NICK change to the client on wire.
|
||||
c.send(FormatMessage(oldMask, "NICK", newNick))
|
||||
@@ -130,7 +129,7 @@ func (c *Conn) handlePrivmsg(
|
||||
|
||||
if strings.HasPrefix(target, "#") {
|
||||
_, err := c.svc.SendChannelMessage(
|
||||
ctx, c.sessionID, c.clientID, c.currentNick(),
|
||||
ctx, c.sessionID, c.clientID, c.nick,
|
||||
msg.Command, target, body, nil,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -138,7 +137,7 @@ func (c *Conn) handlePrivmsg(
|
||||
}
|
||||
} else {
|
||||
result, err := c.svc.SendDirectMessage(
|
||||
ctx, c.sessionID, c.clientID, c.currentNick(),
|
||||
ctx, c.sessionID, c.clientID, c.nick,
|
||||
msg.Command, target, body, nil,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -200,7 +199,7 @@ func (c *Conn) joinChannel(
|
||||
channel, key string,
|
||||
) {
|
||||
result, err := c.svc.JoinChannel(
|
||||
ctx, c.sessionID, c.clientID, c.currentNick(), channel, key,
|
||||
ctx, c.sessionID, c.clientID, c.nick, channel, key,
|
||||
)
|
||||
if err != nil {
|
||||
c.sendIRCError(err)
|
||||
@@ -338,7 +337,7 @@ func (c *Conn) partChannel(
|
||||
channel, reason string,
|
||||
) {
|
||||
err := c.svc.PartChannel(
|
||||
ctx, c.sessionID, c.clientID, c.currentNick(), channel, reason,
|
||||
ctx, c.sessionID, c.clientID, c.nick, channel, reason,
|
||||
)
|
||||
if err != nil {
|
||||
c.sendIRCError(err)
|
||||
@@ -402,7 +401,7 @@ func (c *Conn) handleTopic(
|
||||
newTopic := msg.Params[1]
|
||||
|
||||
err := c.svc.SetTopic(
|
||||
ctx, c.sessionID, c.clientID, c.currentNick(), channel, newTopic,
|
||||
ctx, c.sessionID, c.clientID, c.nick, channel, newTopic,
|
||||
)
|
||||
if err != nil {
|
||||
c.sendIRCError(err)
|
||||
@@ -709,7 +708,7 @@ func (c *Conn) applyChannelModes(
|
||||
c.send(FormatMessage(c.hostmask(), "MODE", params...))
|
||||
|
||||
c.svc.BroadcastMode(
|
||||
ctx, c.clientID, c.currentNick(), channel, chID, modeReply,
|
||||
ctx, c.clientID, c.nick, channel, chID, modeReply,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1186,7 +1185,7 @@ func (c *Conn) handleKick(
|
||||
}
|
||||
|
||||
err := c.svc.KickUser(
|
||||
ctx, c.sessionID, c.clientID, c.currentNick(),
|
||||
ctx, c.sessionID, c.clientID, c.nick,
|
||||
channel, targetNick, reason,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -1226,7 +1225,7 @@ func (c *Conn) handlePassPostReg(
|
||||
return
|
||||
}
|
||||
|
||||
c.sendFromServer("NOTICE", c.currentNick(),
|
||||
c.sendFromServer("NOTICE", c.nick,
|
||||
"Password set. You can reconnect using "+
|
||||
"PASS <password> with your nick.",
|
||||
)
|
||||
@@ -1250,7 +1249,7 @@ func (c *Conn) handleInvite(
|
||||
channel := msg.Params[1]
|
||||
|
||||
targetID, err := c.svc.InviteUser(
|
||||
ctx, c.sessionID, c.currentNick(), targetNick, channel,
|
||||
ctx, c.sessionID, c.nick, targetNick, channel,
|
||||
)
|
||||
if err != nil {
|
||||
c.sendIRCError(err)
|
||||
@@ -1267,7 +1266,7 @@ func (c *Conn) handleInvite(
|
||||
body, _ := json.Marshal([]string{channel}) //nolint:errchkjson
|
||||
|
||||
_, _, _ = c.svc.FanOut( //nolint:dogsled // fire-and-forget broadcast
|
||||
ctx, "INVITE", c.currentNick(), targetNick, targetID, 0,
|
||||
ctx, "INVITE", c.nick, targetNick, targetID, 0,
|
||||
nil, body, nil, []int64{targetID}, 0,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -313,22 +313,6 @@ func (c *Conn) currentNick() string {
|
||||
return c.nick
|
||||
}
|
||||
|
||||
// loadSessionNick sets c.nick to the nick the connection's
|
||||
// session holds in the database, read by session ID. The
|
||||
// connection's own NICK and the relay passing on a NICK line
|
||||
// both call it, and it reads and sets the nick under c.mu, so
|
||||
// whichever runs last leaves the connection on its session's
|
||||
// nick. If the read fails, c.nick is left as it is.
|
||||
func (c *Conn) loadSessionNick(ctx context.Context) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
nick, err := c.database.GetSessionNick(ctx, c.sessionID)
|
||||
if err == nil {
|
||||
c.nick = nick
|
||||
}
|
||||
}
|
||||
|
||||
// send writes a formatted IRC line to the connection.
|
||||
func (c *Conn) send(line string) {
|
||||
c.sendWithin(writeTimeout, line)
|
||||
@@ -361,7 +345,7 @@ func (c *Conn) sendNumeric(
|
||||
code irc.IRCMessageType,
|
||||
params ...string,
|
||||
) {
|
||||
nick := c.currentNick()
|
||||
nick := c.nick
|
||||
if nick == "" {
|
||||
nick = "*"
|
||||
}
|
||||
@@ -385,11 +369,9 @@ func (c *Conn) sendFromServer(
|
||||
// hostmask returns the client's full hostmask
|
||||
// (nick!user@host).
|
||||
func (c *Conn) hostmask() string {
|
||||
nick := c.currentNick()
|
||||
|
||||
user := c.username
|
||||
if user == "" {
|
||||
user = nick
|
||||
user = c.nick
|
||||
}
|
||||
|
||||
host := c.hostname
|
||||
@@ -397,7 +379,7 @@ func (c *Conn) hostmask() string {
|
||||
host = c.remoteIP
|
||||
}
|
||||
|
||||
return nick + "!" + user + "@" + host
|
||||
return c.nick + "!" + user + "@" + host
|
||||
}
|
||||
|
||||
// handleMessage dispatches a parsed IRC message using
|
||||
@@ -675,7 +657,7 @@ func (c *Conn) checkPasswordLength(pw string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
c.sendFromServer("NOTICE", c.currentNick(),
|
||||
c.sendFromServer("NOTICE", c.nick,
|
||||
"Password must be 8 to 72 bytes",
|
||||
)
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ func (c *Conn) drainQueue(ctx context.Context) bool {
|
||||
//
|
||||
//nolint:cyclop // dispatch table
|
||||
func (c *Conn) deliverIRCMessage(
|
||||
ctx context.Context,
|
||||
_ context.Context,
|
||||
msg *db.IRCMessage,
|
||||
) {
|
||||
command := msg.Command
|
||||
@@ -120,7 +120,7 @@ func (c *Conn) deliverIRCMessage(
|
||||
case command == irc.CmdPart:
|
||||
c.deliverPart(msg, text)
|
||||
case command == irc.CmdNick:
|
||||
c.deliverNickChange(ctx, msg, text)
|
||||
c.deliverNickChange(msg, text)
|
||||
case command == irc.CmdQuit:
|
||||
c.deliverQuitMsg(msg, text)
|
||||
case command == irc.CmdTopic:
|
||||
@@ -230,18 +230,11 @@ func (c *Conn) deliverPart(msg *db.IRCMessage, text string) {
|
||||
}
|
||||
}
|
||||
|
||||
// deliverNickChange sends a NICK change notification. The
|
||||
// change may be its own session's, made from another client,
|
||||
// so the connection first takes the nick its session holds
|
||||
// now. It does not go by the line's old nick: the connection
|
||||
// may have taken that nick after another user gave it up.
|
||||
// deliverNickChange sends a NICK change notification.
|
||||
func (c *Conn) deliverNickChange(
|
||||
ctx context.Context,
|
||||
msg *db.IRCMessage,
|
||||
newNick string,
|
||||
) {
|
||||
c.loadSessionNick(ctx)
|
||||
|
||||
prefix := msg.From + "!" + msg.From + "@*"
|
||||
|
||||
c.send(FormatMessage(prefix, "NICK", newNick))
|
||||
|
||||
@@ -511,10 +511,13 @@ func TestRelayOwnSessionOnce(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ownSessionCopies sets up alice with aliceWithHTTPClient. 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.
|
||||
// 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,
|
||||
@@ -522,9 +525,27 @@ func ownSessionCopies(
|
||||
) (int, int) {
|
||||
t.Helper()
|
||||
|
||||
const password = "alicepass"
|
||||
|
||||
ctx := t.Context()
|
||||
env := newTestEnv(t)
|
||||
alice, _, sessionID, httpClientID := aliceWithHTTPClient(t, env)
|
||||
|
||||
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
|
||||
|
||||
@@ -534,7 +555,7 @@ func ownSessionCopies(
|
||||
// Her echo comes before the reply to her next command.
|
||||
lines = alice.sendAndExpect("PING :done", "PONG")
|
||||
} else {
|
||||
err := api(ctx, env.srv.Service(), sessionID)
|
||||
err = api(ctx, env.srv.Service(), sessionID)
|
||||
if err != nil {
|
||||
t.Fatalf("%s over the HTTP API: %v", line, err)
|
||||
}
|
||||
@@ -580,118 +601,3 @@ func ownSessionCopies(
|
||||
|
||||
return ircCopies, httpCopies
|
||||
}
|
||||
|
||||
// aliceWithHTTPClient 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 returns her connection, bob's
|
||||
// connection, her session and her HTTP API client.
|
||||
func aliceWithHTTPClient(
|
||||
t *testing.T, env *testEnv,
|
||||
) (*testClient, *testClient, int64, int64) {
|
||||
t.Helper()
|
||||
|
||||
const password = "alicepass"
|
||||
|
||||
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(
|
||||
t.Context(), "alice", password, "127.0.0.1", "localhost",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("log in as alice: %v", err)
|
||||
}
|
||||
|
||||
return alice, bob, sessionID, httpClientID
|
||||
}
|
||||
|
||||
// TestRelayNickChangeFromHTTPAPI checks that once alice's nick
|
||||
// is changed from her HTTP API client, her IRC listener
|
||||
// connection uses the new nick: what she then sends from it
|
||||
// reaches bob under the new nick.
|
||||
func TestRelayNickChangeFromHTTPAPI(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
env := newTestEnv(t)
|
||||
alice, bob, sessionID, _ := aliceWithHTTPClient(t, env)
|
||||
|
||||
bob.joinAndDrain("#other")
|
||||
|
||||
// As the HTTP API does.
|
||||
err := env.srv.Service().ChangeNick(
|
||||
t.Context(), sessionID, 0, "alice", "alice2",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("change nick over the HTTP API: %v", err)
|
||||
}
|
||||
|
||||
// Her connection has taken the new nick once it has sent her
|
||||
// the change.
|
||||
const change = ":alice!alice@* NICK alice2"
|
||||
|
||||
alice.readUntil(func(l string) bool { return l == change })
|
||||
bob.readUntil(func(l string) bool { return l == change })
|
||||
|
||||
for _, step := range []struct {
|
||||
line, want string
|
||||
}{
|
||||
{"PRIVMSG #chan :hi there", ":alice2!alice2@* PRIVMSG #chan :hi there"},
|
||||
{"JOIN #other", ":alice2!alice2@* JOIN #other"},
|
||||
{"NICK alice3", ":alice2!alice2@* NICK alice3"},
|
||||
} {
|
||||
alice.send(step.line)
|
||||
|
||||
got := bob.readLine()
|
||||
if got != step.want {
|
||||
t.Errorf("%s: got %q, want %q", step.line, got, step.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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