From 8088e3fa7ef780198752aa2bf626b7b5ccbd2b83 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Tue, 29 Oct 2019 10:49:01 +0100 Subject: [PATCH] Add Client.SendIQ method --- client.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/client.go b/client.go index 686519a..32b2394 100644 --- a/client.go +++ b/client.go @@ -1,6 +1,7 @@ package xmpp import ( + "context" "encoding/xml" "errors" "fmt" @@ -82,6 +83,8 @@ func (em EventManager) streamError(error, desc string) { // Client // ============================================================================ +var ErrCanOnlySendGetOrSetIq = errors.New("SendIQ can only send get and set IQ stanzas") + // Client is the main structure used to connect as a client on an XMPP // server. type Client struct { @@ -221,6 +224,27 @@ func (c *Client) Send(packet stanza.Packet) error { return c.sendWithWriter(c.transport, data) } +// SendIQ sends an IQ set or get stanza to the server. If a result is received +// the provided handler function will automatically be called. +// +// The provided context should have a timeout to prevent the client from waiting +// forever for an IQ result. For example: +// +// ctx, _ := context.WithTimeout(context.Background(), 30 * time.Second) +// client.SendIQ(ctx, iq, func(s Sender, p stanza.Packet) { +// // Handle the result here +// }) +// +func (c *Client) SendIQ(ctx context.Context, iq stanza.IQ, handler HandlerFunc) (*IqResultRoute, error) { + if iq.Attrs.Type != "set" && iq.Attrs.Type != "get" { + return nil, ErrCanOnlySendGetOrSetIq + } + if err := c.Send(iq); err != nil { + return nil, err + } + return c.router.NewIqResultRoute(ctx, iq.Attrs.Id).HandlerFunc(handler), nil +} + // SendRaw sends an XMPP stanza as a string to the server. // It can be invalid XML or XMPP content. In that case, the server will // disconnect the client. It is up to the user of this method to