From 34d683d25afe5e53b2e787d81a166074bcac3040 Mon Sep 17 00:00:00 2001 From: Eleksir Date: Thu, 11 Jan 2024 01:24:28 +0300 Subject: [PATCH] Extend SendPresence() stub func to allow send useful statuses (#150) --- xmpp.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/xmpp.go b/xmpp.go index 839b483..77fc909 100644 --- a/xmpp.go +++ b/xmpp.go @@ -1128,8 +1128,50 @@ func (c *Client) SendOrg(org string) (n int, err error) { return fmt.Fprint(c.stanzaWriter, org+"\n") } +// SendPresence sends Presence wrapped inside XMPP presence stanza. func (c *Client) SendPresence(presence Presence) (n int, err error) { - return fmt.Fprintf(c.stanzaWriter, "\n", xmlEscape(presence.From), xmlEscape(presence.To)) + // Forge opening presence tag + var buf string = "" + + // TODO: there may be optional tag "priority", but former presence type does not take this into account + // so either we must follow std, change type xmpp.Presence and break backward compatibility + // or leave it as-is and potentially break client software + + if presence.Show != "" { + // https://www.ietf.org/rfc/rfc3921.txt 2.2.2.1, show can be only + // away, chat, dnd, xa + switch presence.Show { + case "away", "chat", "dnd", "xa": + buf = buf + fmt.Sprintf("%s", xmlEscape(presence.Show)) + } + } + + if presence.Status != "" { + buf = buf + fmt.Sprintf("%s", xmlEscape(presence.Status)) + } + + buf = buf + "" + + return fmt.Fprint(c.stanzaWriter, buf) } // SendKeepAlive sends a "whitespace keepalive" as described in chapter 4.6.1 of RFC6120.