Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ircevent: add synchronous GetLabeledResponse API #74

Merged
merged 1 commit into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions ircevent/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var (
SASLFailed = errors.New("SASL setup timed out. Does the server support SASL?")

CapabilityNotNegotiated = errors.New("The IRCv3 capability required for this was not negotiated")
NoLabeledResponse = errors.New("The server failed to send a labeled response to the command")

serverDidNotQuit = errors.New("server did not respond to QUIT")
clientHasQuit = errors.New("client has called Quit()")
Expand Down Expand Up @@ -396,6 +397,26 @@ func (irc *Connection) SendWithLabel(callback func(*Batch), tags map[string]stri
return err
}

// GetLabeledResponse sends an IRC message using the IRCv3 labeled-response
// specification, then synchronously waits for the response, which is returned
// as a *Batch. If the server fails to respond correctly, an error will be
// returned.
func (irc *Connection) GetLabeledResponse(tags map[string]string, command string, params ...string) (batch *Batch, err error) {
done := make(chan empty)
err = irc.SendWithLabel(func(b *Batch) {
batch = b
close(done)
}, tags, command, params...)
if err != nil {
return
}
<-done
if batch == nil {
err = NoLabeledResponse
}
return
}

// Send a raw string.
func (irc *Connection) SendRaw(message string) error {
mlen := len(message)
Expand Down
29 changes: 29 additions & 0 deletions ircevent/irc_labeledresponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,32 @@ func synchronize(irc *Connection) {
}, nil, "PING", "!")
<-event
}

func TestSynchronousLabeledResponse(t *testing.T) {
irccon := connForTesting("go-eventirc", "go-eventirc", false)
irccon.Debug = true
irccon.RequestCaps = []string{"message-tags", "batch", "labeled-response"}
irccon.RealName = "Al_b6AHLrxh8TZb5kNO1gw"
err := irccon.Connect()
if err != nil {
t.Fatalf("labeled response connection failed: %s", err)
}
go irccon.Loop()

batch, err := irccon.GetLabeledResponse(nil, "WHOIS", irccon.CurrentNick())
if err != nil {
t.Fatalf("labeled response failed: %v", err)
}
assertEqual(batch.Command, "BATCH")
results := make(map[string]string)
for _, line := range batch.Items {
results[line.Command] = line.Params[len(line.Params)-1]
}

// RPL_WHOISUSER, last param is the realname
assertEqual(results["311"], "Al_b6AHLrxh8TZb5kNO1gw")
if _, ok := results["379"]; !ok {
t.Errorf("Expected 379 RPL_WHOISMODES in response, but not received")
}
assertEqual(len(irccon.batches), 0)
}