| 1 |
package xmpp |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
) |
| 7 |
|
| 8 |
// Discovery discovers items information according https://xmpp.org/extensions/xep-0030.html#items (Discovering the |
| 9 |
// Items Associated with a Jabber Entity). |
| 10 |
func (c *Client) Discovery() (string, error) { |
| 11 |
return c.RawInformationQuery(c.jid, c.domain, getUUID(), IQTypeGet, XMPPNS_DISCO_ITEMS, "") |
| 12 |
} |
| 13 |
|
| 14 |
// DiscoverNodeInfo discovers information about a node. Empty node queries info about server itself. |
| 15 |
// Discovery query performed according to https://xmpp.org/extensions/xep-0030.html#info (Discovering Information About |
| 16 |
// a Jabber Entity). |
| 17 |
func (c *Client) DiscoverNodeInfo(node string) (string, error) { |
| 18 |
return c.RawInformation( |
| 19 |
c.jid, |
| 20 |
c.domain, |
| 21 |
getUUID(), |
| 22 |
IQTypeGet, |
| 23 |
fmt.Sprintf("<query xmlns=%q node=%q/>", XMPPNS_DISCO_INFO, node), |
| 24 |
) |
| 25 |
} |
| 26 |
|
| 27 |
// DiscoverInfo discovers information about given item from given jid. |
| 28 |
// Discovery query performed according to https://xmpp.org/extensions/xep-0030.html#info (Discovering Information About |
| 29 |
// a Jabber Entity). |
| 30 |
// The only difference between DiscoverInfo() and DiscoverNodeInfo() is that DiscoverInfo() does not supply From field, |
| 31 |
// which is useful in very limited amount use cases. |
| 32 |
func (c *Client) DiscoverInfo(to string) (string, error) { |
| 33 |
query := fmt.Sprintf("<query xmlns=%q/>", XMPPNS_DISCO_INFO) |
| 34 |
|
| 35 |
return c.RawInformation(c.jid, to, getUUID(), IQTypeGet, query) |
| 36 |
} |
| 37 |
|
| 38 |
// DiscoverServerItems discovers items that the server exposes. It is actually thin wrapper for DiscoverEntityItems(). |
| 39 |
func (c *Client) DiscoverServerItems() (string, error) { |
| 40 |
return c.DiscoverEntityItems(c.domain) |
| 41 |
} |
| 42 |
|
| 43 |
// DiscoverEntityItems discovers items that an entity exposes. |
| 44 |
func (c *Client) DiscoverEntityItems(jid string) (string, error) { |
| 45 |
query := fmt.Sprintf("<query xmlns=%q/>", XMPPNS_DISCO_ITEMS) |
| 46 |
|
| 47 |
return c.RawInformation(c.jid, jid, getUUID(), IQTypeGet, query) |
| 48 |
} |
| 49 |
|
| 50 |
// RawInformationQuery sends an information query request to the server. |
| 51 |
func (c *Client) RawInformationQuery(from, to, id, iqType, requestNamespace, body string) (string, error) { |
| 52 |
_, err := fmt.Fprintf( |
| 53 |
c.stanzaWriter, |
| 54 |
"<iq from=%q to=%q id=%q type=%q><query xmlns=%q>%s</query></iq>\n", |
| 55 |
xmlEscape(from), |
| 56 |
xmlEscape(to), |
| 57 |
id, |
| 58 |
iqType, |
| 59 |
requestNamespace, |
| 60 |
body, |
| 61 |
) |
| 62 |
|
| 63 |
return id, err |
| 64 |
} |
| 65 |
|
| 66 |
// RawInformation send a IQ request with the payload body to the server. |
| 67 |
func (c *Client) RawInformation(from, to, id, iqType, body string) (string, error) { |
| 68 |
_, err := fmt.Fprintf( |
| 69 |
c.stanzaWriter, |
| 70 |
"<iq from=%q to=%q id=%q type=%q>%s</iq>\n", |
| 71 |
xmlEscape(from), |
| 72 |
xmlEscape(to), |
| 73 |
id, |
| 74 |
iqType, |
| 75 |
body, |
| 76 |
) |
| 77 |
|
| 78 |
return id, err |
| 79 |
} |
| 80 |
|
| 81 |
// UrnXMPPTimeResponse implements response to query entity's current time accodring to |
| 82 |
// https://xmpp.org/extensions/xep-0202.html#example-2 (A Response to the Query). |
| 83 |
func (c *Client) UrnXMPPTimeResponse(v IQ, timezoneOffset string) (string, error) { |
| 84 |
query := fmt.Sprintf( |
| 85 |
"<time xmlns=%q><tzo>%s</tzo><utc>%s</utc></time>", |
| 86 |
XMPPNS_TIME, |
| 87 |
timezoneOffset, |
| 88 |
time.Now().UTC().Format(time.RFC3339), |
| 89 |
) |
| 90 |
|
| 91 |
return c.RawInformation( |
| 92 |
v.To, |
| 93 |
v.From, |
| 94 |
v.ID, |
| 95 |
IQTypeResult, |
| 96 |
query, |
| 97 |
) |
| 98 |
} |
| 99 |
|
| 100 |
// IqVersionResponse responding with software version, according to example described in |
| 101 |
// https://xmpp.org/extensions/xep-0092.html#example-2 (Receiving a Reply Regarding Software Version). |
| 102 |
func (c *Client) IqVersionResponse(v IQ, name string, version string, os string) (string, error) { |
| 103 |
if name == "" { |
| 104 |
name = "go-xmpp" |
| 105 |
version = Version |
| 106 |
} |
| 107 |
|
| 108 |
if version == "" { |
| 109 |
version = "undefined" |
| 110 |
} |
| 111 |
|
| 112 |
query := fmt.Sprintf("<query xmlns=%q>", XMPPNS_IQ_VERSION) |
| 113 |
|
| 114 |
query += fmt.Sprintf("<name>%s</name>", name) |
| 115 |
query += fmt.Sprintf("<version>%s</version>", version) |
| 116 |
|
| 117 |
if os != "" { |
| 118 |
query += fmt.Sprintf("<os>%s</os>", os) |
| 119 |
} |
| 120 |
|
| 121 |
query += "</query>" |
| 122 |
|
| 123 |
return c.RawInformation( |
| 124 |
v.To, |
| 125 |
v.From, |
| 126 |
v.ID, |
| 127 |
IQTypeResult, |
| 128 |
query, |
| 129 |
) |
| 130 |
} |