Skip to content

Commit

Permalink
Merge pull request #74 from slingamn/sync_api.1
Browse files Browse the repository at this point in the history
ircevent: add synchronous GetLabeledResponse API
  • Loading branch information
slingamn authored May 29, 2022
2 parents 167f657 + 2e8d20b commit 19e3d64
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
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)
}

0 comments on commit 19e3d64

Please sign in to comment.