From 6138e9dbe53cca76738f77486098d11da7eafa79 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Mon, 14 Aug 2023 10:28:33 +0200 Subject: [PATCH] Harmonize newlines Now there should be no more newlines in between any stanza and a newline after every stanza. This should not affect functionality but is looking better if stanzas are printed for debugging. --- xmpp.go | 20 ++++----- xmpp_information_query.go | 4 +- xmpp_muc.go | 88 +++++++++++++++++++-------------------- xmpp_ping.go | 14 +++---- xmpp_subscription.go | 8 ++-- 5 files changed, 67 insertions(+), 67 deletions(-) diff --git a/xmpp.go b/xmpp.go index b8b60c7..1f9109d 100644 --- a/xmpp.go +++ b/xmpp.go @@ -436,7 +436,7 @@ func (c *Client) init(o *Options) error { } clientNonce := cnonce() clientFirstMessage := "n=" + user + ",r=" + clientNonce - fmt.Fprintf(c.stanzaWriter, "%s", + fmt.Fprintf(c.stanzaWriter, "%s\n", nsSASL, mechanism, base64.StdEncoding.EncodeToString([]byte("n,,"+ clientFirstMessage))) var sfm string @@ -536,7 +536,7 @@ func (c *Client) init(o *Options) error { } clientFinalMessage := base64.StdEncoding.EncodeToString([]byte(clientFinalMessageBare + ",p=" + base64.StdEncoding.EncodeToString(clientProof))) - fmt.Fprintf(c.stanzaWriter, "%s", nsSASL, + fmt.Fprintf(c.stanzaWriter, "%s\n", nsSASL, clientFinalMessage) } if mechanism == "X-OAUTH2" && o.OAuthToken != "" && o.OAuthScope != "" { @@ -665,11 +665,11 @@ func (c *Client) init(o *Options) error { if o.Session { //if server support session, open it - fmt.Fprintf(c.stanzaWriter, "", xmlEscape(domain), cookie, nsSession) + fmt.Fprintf(c.stanzaWriter, "\n", xmlEscape(domain), cookie, nsSession) } // We're connected and can now receive and send messages. - fmt.Fprintf(c.stanzaWriter, "%s%s", o.Status, o.StatusMessage) + fmt.Fprintf(c.stanzaWriter, "%s%s\n", o.Status, o.StatusMessage) return nil } @@ -732,7 +732,7 @@ func (c *Client) startStream(o *Options, domain string) (*streamFeatures, error) _, err := fmt.Fprintf(c.stanzaWriter, ""+ "", + " xmlns:stream='%s' version='1.0'>\n", xmlEscape(domain), nsClient, nsStream) if err != nil { return nil, err @@ -1056,7 +1056,7 @@ func (c *Client) Send(chat Chat) (n int, err error) { oobtext += `` } - stanza := "" + subtext + "%s" + oobtext + thdtext + "" + stanza := "" + subtext + "%s" + oobtext + thdtext + "\n" return fmt.Fprintf(c.stanzaWriter, stanza, xmlEscape(chat.Remote), xmlEscape(chat.Type), cnonce(), xmlEscape(chat.Text)) @@ -1075,17 +1075,17 @@ func (c *Client) SendOOB(chat Chat) (n int, err error) { } oobtext += `` } - return fmt.Fprintf(c.stanzaWriter, ""+oobtext+thdtext+"", + return fmt.Fprintf(c.stanzaWriter, ""+oobtext+thdtext+"\n", xmlEscape(chat.Remote), xmlEscape(chat.Type), cnonce()) } // SendOrg sends the original text without being wrapped in an XMPP message stanza. func (c *Client) SendOrg(org string) (n int, err error) { - return fmt.Fprint(c.stanzaWriter, org) + return fmt.Fprint(c.stanzaWriter, org+"\n") } func (c *Client) SendPresence(presence Presence) (n int, err error) { - return fmt.Fprintf(c.stanzaWriter, "", xmlEscape(presence.From), xmlEscape(presence.To)) + return fmt.Fprintf(c.stanzaWriter, "\n", xmlEscape(presence.From), xmlEscape(presence.To)) } // SendKeepAlive sends a "whitespace keepalive" as described in chapter 4.6.1 of RFC6120. @@ -1097,7 +1097,7 @@ func (c *Client) SendKeepAlive() (n int, err error) { func (c *Client) SendHtml(chat Chat) (n int, err error) { return fmt.Fprintf(c.stanzaWriter, ""+ "%s"+ - "%s", + "%s\n", xmlEscape(chat.Remote), xmlEscape(chat.Type), xmlEscape(chat.Text), chat.Text) } diff --git a/xmpp_information_query.go b/xmpp_information_query.go index bda5d86..3abac58 100644 --- a/xmpp_information_query.go +++ b/xmpp_information_query.go @@ -34,14 +34,14 @@ func (c *Client) DiscoverEntityItems(jid string) (string, error) { // RawInformationQuery sends an information query request to the server. func (c *Client) RawInformationQuery(from, to, id, iqType, requestNamespace, body string) (string, error) { - const xmlIQ = "%s" + const xmlIQ = "%s\n" _, err := fmt.Fprintf(c.stanzaWriter, xmlIQ, xmlEscape(from), xmlEscape(to), id, iqType, requestNamespace, body) return id, err } // rawInformation send 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" + const xmlIQ = "%s\n" _, err := fmt.Fprintf(c.stanzaWriter, xmlIQ, xmlEscape(from), xmlEscape(to), id, iqType, body) return id, err } diff --git a/xmpp_muc.go b/xmpp_muc.go index aec89bc..25b247e 100644 --- a/xmpp_muc.go +++ b/xmpp_muc.go @@ -25,7 +25,7 @@ const ( // Send sends room topic wrapped inside an XMPP message stanza body. func (c *Client) SendTopic(chat Chat) (n int, err error) { - return fmt.Fprintf(c.stanzaWriter, ""+"%s", + return fmt.Fprintf(c.stanzaWriter, ""+"%s\n", xmlEscape(chat.Remote), xmlEscape(chat.Type), xmlEscape(chat.Text)) } @@ -47,34 +47,34 @@ func (c *Client) JoinMUC(jid, nick string, history_type, history int, history_da } switch history_type { case NoHistory: - return fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "", + return fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC) case CharHistory: - return fmt.Fprintf(c.stanzaWriter, "\n"+ + return fmt.Fprintf(c.stanzaWriter, ""+ "\n"+ - "\n"+ - "", + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC, history) case StanzaHistory: - return fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "\n"+ - "", + return fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC, history) case SecondsHistory: - return fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "\n"+ - "", + return fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC, history) case SinceHistory: if history_date != nil { - return fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "\n"+ - "", + return fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC, history_date.Format(time.RFC3339)) } } @@ -88,40 +88,40 @@ func (c *Client) JoinProtectedMUC(jid, nick string, password string, history_typ } switch history_type { case NoHistory: - return fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ + return fmt.Fprintf(c.stanzaWriter, ""+ + ""+ "%s"+ - "\n"+ - "", + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password)) case CharHistory: - return fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "%s\n"+ - "\n"+ - "", + return fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + "%s"+ + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history) case StanzaHistory: - return fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "%s\n"+ - "\n"+ - "", + return fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + "%s"+ + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history) case SecondsHistory: - return fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "%s\n"+ - "\n"+ - "", + return fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + "%s"+ + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history) case SinceHistory: if history_date != nil { - return fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "%s\n"+ - "\n"+ - "", + return fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + "%s"+ + ""+ + "\n", xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history_date.Format(time.RFC3339)) } } @@ -130,6 +130,6 @@ func (c *Client) JoinProtectedMUC(jid, nick string, password string, history_typ // xep-0045 7.14 func (c *Client) LeaveMUC(jid string) (n int, err error) { - return fmt.Fprintf(c.stanzaWriter, "", + return fmt.Fprintf(c.stanzaWriter, "\n", c.jid, xmlEscape(jid)) } diff --git a/xmpp_ping.go b/xmpp_ping.go index b22e52d..f851d41 100644 --- a/xmpp_ping.go +++ b/xmpp_ping.go @@ -11,23 +11,23 @@ func (c *Client) PingC2S(jid, server string) error { if server == "" { server = c.domain } - _, err := fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "", + _, err := fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + "\n", xmlEscape(jid), xmlEscape(server)) return err } func (c *Client) PingS2S(fromServer, toServer string) error { - _, err := fmt.Fprintf(c.stanzaWriter, "\n"+ - "\n"+ - "", + _, err := fmt.Fprintf(c.stanzaWriter, ""+ + ""+ + "\n", xmlEscape(fromServer), xmlEscape(toServer)) return err } func (c *Client) SendResultPing(id, toServer string) error { - _, err := fmt.Fprintf(c.stanzaWriter, "", + _, err := fmt.Fprintf(c.stanzaWriter, "\n", xmlEscape(toServer), xmlEscape(id)) return err } diff --git a/xmpp_subscription.go b/xmpp_subscription.go index 2f90339..37a03d2 100644 --- a/xmpp_subscription.go +++ b/xmpp_subscription.go @@ -5,21 +5,21 @@ import ( ) func (c *Client) ApproveSubscription(jid string) { - fmt.Fprintf(c.stanzaWriter, "", + fmt.Fprintf(c.stanzaWriter, "\n", xmlEscape(jid)) } func (c *Client) RevokeSubscription(jid string) { - fmt.Fprintf(c.stanzaWriter, "", + fmt.Fprintf(c.stanzaWriter, "\n", xmlEscape(jid)) } func (c *Client) RetrieveSubscription(jid string) { - fmt.Fprintf(c.conn, "", + fmt.Fprintf(c.conn, "\n", xmlEscape(jid)) } func (c *Client) RequestSubscription(jid string) { - fmt.Fprintf(c.stanzaWriter, "", + fmt.Fprintf(c.stanzaWriter, "\n", xmlEscape(jid)) }