From 9fc0b1236cc41c980adc745857572332073064d6 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 2 Mar 2023 05:22:44 +0100 Subject: [PATCH] Print sent stanzas in debug mode. (#141) * Print sent stanzas in debug mode. * Remove unnecessary newline. --- xmpp.go | 29 ++++++++++++++++------------- xmpp_information_query.go | 4 ++-- xmpp_muc.go | 26 +++++++++++++------------- xmpp_ping.go | 6 +++--- xmpp_subscription.go | 6 +++--- 5 files changed, 37 insertions(+), 34 deletions(-) diff --git a/xmpp.go b/xmpp.go index 023f99e..18b8aa5 100644 --- a/xmpp.go +++ b/xmpp.go @@ -47,6 +47,7 @@ var DefaultConfig = &tls.Config{} // DebugWriter is the writer used to write debugging output to. var DebugWriter io.Writer = os.Stderr +var StanzaWriter io.Writer // Cookie is a unique XMPP session identifier type Cookie uint64 @@ -368,7 +369,7 @@ func (c *Client) init(o *Options) error { foundAnonymous := false for _, m := range f.Mechanisms.Mechanism { if m == "ANONYMOUS" { - fmt.Fprintf(c.conn, "\n", nsSASL) + fmt.Fprintf(StanzaWriter, "\n", nsSASL) foundAnonymous = true break } @@ -486,9 +487,9 @@ func (c *Client) init(o *Options) error { // Send IQ message asking to bind to the local user name. if o.Resource == "" { - fmt.Fprintf(c.conn, "\n", cookie, nsBind) + fmt.Fprintf(StanzaWriter, "\n", cookie, nsBind) } else { - fmt.Fprintf(c.conn, "%s\n", cookie, nsBind, o.Resource) + fmt.Fprintf(StanzaWriter, "%s\n", cookie, nsBind, o.Resource) } var iq clientIQ if err = c.p.DecodeElement(&iq, nil); err != nil { @@ -502,11 +503,11 @@ func (c *Client) init(o *Options) error { if o.Session { //if server support session, open it - fmt.Fprintf(c.conn, "", xmlEscape(domain), cookie, nsSession) + fmt.Fprintf(StanzaWriter, "", xmlEscape(domain), cookie, nsSession) } // We're connected and can now receive and send messages. - fmt.Fprintf(c.conn, "%s%s", o.Status, o.StatusMessage) + fmt.Fprintf(StanzaWriter, "%s%s", o.Status, o.StatusMessage) return nil } @@ -528,7 +529,7 @@ func (c *Client) startTLSIfRequired(f *streamFeatures, o *Options, domain string } var err error - fmt.Fprintf(c.conn, "\n") + fmt.Fprintf(StanzaWriter, "\n") var k tlsProceed if err = c.p.DecodeElement(&k, nil); err != nil { return f, errors.New("unmarshal : " + err.Error()) @@ -561,11 +562,13 @@ func (c *Client) startTLSIfRequired(f *streamFeatures, o *Options, domain string func (c *Client) startStream(o *Options, domain string) (*streamFeatures, error) { if o.Debug { c.p = xml.NewDecoder(tee{c.conn, DebugWriter}) + StanzaWriter = io.MultiWriter(c.conn, DebugWriter) } else { c.p = xml.NewDecoder(c.conn) + StanzaWriter = c.conn } - _, err := fmt.Fprintf(c.conn, "\n"+ + _, err := fmt.Fprintf(StanzaWriter, "\n"> "\n", xmlEscape(domain), nsClient, nsStream) @@ -861,7 +864,7 @@ func (c *Client) Send(chat Chat) (n int, err error) { stanza := "" + subtext + "%s" + oobtext + thdtext + "" - return fmt.Fprintf(c.conn, stanza, + return fmt.Fprintf(StanzaWriter, stanza, xmlEscape(chat.Remote), xmlEscape(chat.Type), cnonce(), xmlEscape(chat.Text)) } @@ -878,17 +881,17 @@ func (c *Client) SendOOB(chat Chat) (n int, err error) { } oobtext += `` } - return fmt.Fprintf(c.conn, ""+oobtext+thdtext+"", + return fmt.Fprintf(StanzaWriter, ""+oobtext+thdtext+"", 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.conn, org) + return fmt.Fprint(StanzaWriter, org) } func (c *Client) SendPresence(presence Presence) (n int, err error) { - return fmt.Fprintf(c.conn, "", xmlEscape(presence.From), xmlEscape(presence.To)) + return fmt.Fprintf(StanzaWriter, "", xmlEscape(presence.From), xmlEscape(presence.To)) } // SendKeepAlive sends a "whitespace keepalive" as described in chapter 4.6.1 of RFC6120. @@ -898,7 +901,7 @@ func (c *Client) SendKeepAlive() (n int, err error) { // SendHtml sends the message as HTML as defined by XEP-0071 func (c *Client) SendHtml(chat Chat) (n int, err error) { - return fmt.Fprintf(c.conn, ""+ + return fmt.Fprintf(StanzaWriter, ""+ "%s"+ "%s", xmlEscape(chat.Remote), xmlEscape(chat.Type), xmlEscape(chat.Text), chat.Text) @@ -906,7 +909,7 @@ func (c *Client) SendHtml(chat Chat) (n int, err error) { // Roster asks for the chat roster. func (c *Client) Roster() error { - fmt.Fprintf(c.conn, "\n", xmlEscape(c.jid)) + fmt.Fprintf(StanzaWriter, "\n", xmlEscape(c.jid)) return nil } diff --git a/xmpp_information_query.go b/xmpp_information_query.go index 7f6d9c1..0154ec1 100644 --- a/xmpp_information_query.go +++ b/xmpp_information_query.go @@ -19,13 +19,13 @@ func (c *Client) Discovery() (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" - _, err := fmt.Fprintf(c.conn, xmlIQ, xmlEscape(from), xmlEscape(to), id, iqType, requestNamespace, body) + _, err := fmt.Fprintf(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" - _, err := fmt.Fprintf(c.conn, xmlIQ, xmlEscape(from), xmlEscape(to), id, iqType, body) + _, err := fmt.Fprintf(StanzaWriter, xmlIQ, xmlEscape(from), xmlEscape(to), id, iqType, body) return id, err } diff --git a/xmpp_muc.go b/xmpp_muc.go index 840f4f9..64798c2 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.conn, ""+"%s", + return fmt.Fprintf(StanzaWriter, ""+"%s", xmlEscape(chat.Remote), xmlEscape(chat.Type), xmlEscape(chat.Text)) } @@ -33,7 +33,7 @@ func (c *Client) JoinMUCNoHistory(jid, nick string) (n int, err error) { if nick == "" { nick = c.jid } - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ ""+ "\n"+ "", @@ -47,31 +47,31 @@ func (c *Client) JoinMUC(jid, nick string, history_type, history int, history_da } switch history_type { case NoHistory: - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "", xmlEscape(jid), xmlEscape(nick), nsMUC) case CharHistory: - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "\n"+ "", xmlEscape(jid), xmlEscape(nick), nsMUC, history) case StanzaHistory: - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "\n"+ "", xmlEscape(jid), xmlEscape(nick), nsMUC, history) case SecondsHistory: - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "\n"+ "", xmlEscape(jid), xmlEscape(nick), nsMUC, history) case SinceHistory: if history_date != nil { - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "\n"+ "", @@ -88,28 +88,28 @@ func (c *Client) JoinProtectedMUC(jid, nick string, password string, history_typ } switch history_type { case NoHistory: - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "%s"+ "\n"+ "", xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password)) case CharHistory: - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "%s\n"+ "\n"+ "", xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history) case StanzaHistory: - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "%s\n"+ "\n"+ "", xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history) case SecondsHistory: - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "%s\n"+ "\n"+ @@ -117,7 +117,7 @@ func (c *Client) JoinProtectedMUC(jid, nick string, password string, history_typ xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history) case SinceHistory: if history_date != nil { - return fmt.Fprintf(c.conn, "\n"+ + return fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "%s\n"+ "\n"+ @@ -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.conn, "", + return fmt.Fprintf(StanzaWriter, "", c.jid, xmlEscape(jid)) } diff --git a/xmpp_ping.go b/xmpp_ping.go index 39269d8..eb20704 100644 --- a/xmpp_ping.go +++ b/xmpp_ping.go @@ -11,7 +11,7 @@ func (c *Client) PingC2S(jid, server string) error { if server == "" { server = c.domain } - _, err := fmt.Fprintf(c.conn, "\n"+ + _, err := fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "", xmlEscape(jid), xmlEscape(server)) @@ -19,7 +19,7 @@ func (c *Client) PingC2S(jid, server string) error { } func (c *Client) PingS2S(fromServer, toServer string) error { - _, err := fmt.Fprintf(c.conn, "\n"+ + _, err := fmt.Fprintf(StanzaWriter, "\n"+ "\n"+ "", xmlEscape(fromServer), xmlEscape(toServer)) @@ -27,7 +27,7 @@ func (c *Client) PingS2S(fromServer, toServer string) error { } func (c *Client) SendResultPing(id, toServer string) error { - _, err := fmt.Fprintf(c.conn, "", + _, err := fmt.Fprintf(StanzaWriter, "", xmlEscape(toServer), xmlEscape(id)) return err } diff --git a/xmpp_subscription.go b/xmpp_subscription.go index eb8eab0..ed8289f 100644 --- a/xmpp_subscription.go +++ b/xmpp_subscription.go @@ -5,12 +5,12 @@ import ( ) func (c *Client) ApproveSubscription(jid string) { - fmt.Fprintf(c.conn, "", + fmt.Fprintf(StanzaWriter, "", xmlEscape(jid)) } func (c *Client) RevokeSubscription(jid string) { - fmt.Fprintf(c.conn, "", + fmt.Fprintf(StanzaWriter, "", xmlEscape(jid)) } @@ -20,6 +20,6 @@ func (c *Client) RetrieveSubscription(jid string) { } func (c *Client) RequestSubscription(jid string) { - fmt.Fprintf(c.conn, "", + fmt.Fprintf(StanzaWriter, "", xmlEscape(jid)) }