From 2696ef2a9dbfba4050406da297169fead721f21d Mon Sep 17 00:00:00 2001 From: Sergei Date: Fri, 18 Aug 2023 13:28:00 -0700 Subject: [PATCH] Implement IQ responses to version, last activity, client time * Implement IqVersionResponse(), XEP-0092 * Implement JabberIqLastResponse(), XEP-0012 * Implement UrnXmppTimeResponse(), XEP-0202 * Fix function descriptions --- xmpp_information_query.go | 75 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/xmpp_information_query.go b/xmpp_information_query.go index de02f47..7503283 100644 --- a/xmpp_information_query.go +++ b/xmpp_information_query.go @@ -3,6 +3,7 @@ package xmpp import ( "fmt" "strconv" + "time" ) const IQTypeGet = "get" @@ -16,24 +17,24 @@ func (c *Client) Discovery() (string, error) { return c.RawInformationQuery(c.jid, c.domain, reqID, IQTypeGet, XMPPNS_DISCO_ITEMS, "") } -// Discover information about a node. Empty node queries info about server itself. +// DiscoverNodeInfo discovers information about a node. Empty node queries info about server itself. func (c *Client) DiscoverNodeInfo(node string) (string, error) { query := fmt.Sprintf("", XMPPNS_DISCO_INFO, node) return c.RawInformation(c.jid, c.domain, "info3", IQTypeGet, query) } -// Discover information about given item from given jid. +// DiscoverInfo discovers information about given item from given jid. func (c *Client) DiscoverInfo(from string, to string) (string, error) { query := fmt.Sprintf("", XMPPNS_DISCO_INFO) return c.RawInformation(from, to, "info3", IQTypeGet, query) } -// Discover items that the server exposes +// DiscoverServerItems discover items that the server exposes func (c *Client) DiscoverServerItems() (string, error) { return c.DiscoverEntityItems(c.domain) } -// Discover items that an entity exposes +// DiscoverEntityItems discovers items that an entity exposes. func (c *Client) DiscoverEntityItems(jid string) (string, error) { query := fmt.Sprintf("", XMPPNS_DISCO_ITEMS) return c.RawInformation(c.jid, jid, "info1", IQTypeGet, query) @@ -46,9 +47,73 @@ func (c *Client) RawInformationQuery(from, to, id, iqType, requestNamespace, bod return id, err } -// rawInformation send a IQ request with the payload body to the server +// RawInformation sends a IQ request with the payload body to the server. func (c *Client) RawInformation(from, to, id, iqType, body string) (string, error) { const xmlIQ = "%s" _, err := fmt.Fprintf(StanzaWriter, xmlIQ, xmlEscape(from), xmlEscape(to), id, iqType, body) return id, err } + +// IqVersionResponse responding with software version, according to xep-0092. +func (c *Client) IqVersionResponse(v IQ, name, version, os string) (string, error) { + if name == "" { + name = "go-xmpp" + } + + if version == "" { + version = "undefined" + } + + query := "" //nolint:wsl + query += fmt.Sprintf("%s", name) + query += fmt.Sprintf("%s", version) + + if os != "" { + query += fmt.Sprintf("%s", os) + } + + query += "" + + return c.RawInformation( + v.To, + v.From, + v.ID, + IQTypeResult, + query, + ) +} + +// JabberIqLastResponse responding with relative time since last activity. +// Here lastActivity is unix time stamp when last activity took place since. +func (c *Client) JabberIqLastResponse(v IQ, lastActivity int64) (string, error) { + query := fmt.Sprintf( + "", + time.Now().Unix()-lastActivity, + ) + + return c.RawInformation( + v.To, + v.From, + v.ID, + IQTypeResult, + query, + ) +} + +// UrnXmppTimeResponse implements response to query entity's current time (xep-0202). +// TimezoneOffset have format +HH:MM or -HH:MM +func (c *Client) UrnXmppTimeResponse(v IQ, timezoneOffset string) (string, error) { + query := fmt.Sprintf( + "", + timezoneOffset, + time.Now().UTC().Format(time.RFC3339), + ) + + return c.RawInformation( + v.To, + v.From, + v.ID, + IQTypeResult, + query, + ) +}