From 6a727b9723a1e0b708ac2b5f406a599d086bb0ce Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 27 Feb 2018 22:55:55 +0100 Subject: [PATCH] Use our own version of go-xmpp with debug output to logrus --- bridge/xmpp/xmpp.go | 3 +- .../{mattn => matterbridge}/go-xmpp/LICENSE | 0 .../{mattn => matterbridge}/go-xmpp/xmpp.go | 62 +++++++++++++------ .../go-xmpp/xmpp_information_query.go | 7 +++ .../go-xmpp/xmpp_muc.go | 0 .../go-xmpp/xmpp_ping.go | 0 .../go-xmpp/xmpp_subscription.go | 0 vendor/manifest | 16 ++--- 8 files changed, 60 insertions(+), 28 deletions(-) rename vendor/github.com/{mattn => matterbridge}/go-xmpp/LICENSE (100%) rename vendor/github.com/{mattn => matterbridge}/go-xmpp/xmpp.go (95%) rename vendor/github.com/{mattn => matterbridge}/go-xmpp/xmpp_information_query.go (70%) rename vendor/github.com/{mattn => matterbridge}/go-xmpp/xmpp_muc.go (100%) rename vendor/github.com/{mattn => matterbridge}/go-xmpp/xmpp_ping.go (100%) rename vendor/github.com/{mattn => matterbridge}/go-xmpp/xmpp_subscription.go (100%) diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 1363096e..8b856d93 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -6,7 +6,7 @@ import ( "github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/helper" "github.com/jpillora/backoff" - "github.com/mattn/go-xmpp" + "github.com/matterbridge/go-xmpp" "strings" "time" ) @@ -105,6 +105,7 @@ func (b *Bxmpp) createXMPP() (*xmpp.Client, error) { TLSConfig: tc, Debug: b.General.Debug, + Logger: b.Log.Writer(), Session: true, Status: "", StatusMessage: "", diff --git a/vendor/github.com/mattn/go-xmpp/LICENSE b/vendor/github.com/matterbridge/go-xmpp/LICENSE similarity index 100% rename from vendor/github.com/mattn/go-xmpp/LICENSE rename to vendor/github.com/matterbridge/go-xmpp/LICENSE diff --git a/vendor/github.com/mattn/go-xmpp/xmpp.go b/vendor/github.com/matterbridge/go-xmpp/xmpp.go similarity index 95% rename from vendor/github.com/mattn/go-xmpp/xmpp.go rename to vendor/github.com/matterbridge/go-xmpp/xmpp.go index 6af9a3ed..d3f31367 100644 --- a/vendor/github.com/mattn/go-xmpp/xmpp.go +++ b/vendor/github.com/matterbridge/go-xmpp/xmpp.go @@ -68,6 +68,11 @@ func (c *Client) JID() string { return c.jid } +func containsIgnoreCase(s, substr string) bool { + s, substr = strings.ToUpper(s), strings.ToUpper(substr) + return strings.Contains(s, substr) +} + func connect(host, user, passwd string) (net.Conn, error) { addr := host @@ -81,16 +86,34 @@ func connect(host, user, passwd string) (net.Conn, error) { if len(a) == 1 { addr += ":5222" } + proxy := os.Getenv("HTTP_PROXY") if proxy == "" { proxy = os.Getenv("http_proxy") } + // test for no proxy, takes a comma separated list with substrings to match + if proxy != "" { + noproxy := os.Getenv("NO_PROXY") + if noproxy == "" { + noproxy = os.Getenv("no_proxy") + } + if noproxy != "" { + nplist := strings.Split(noproxy, ",") + for _, s := range nplist { + if containsIgnoreCase(addr, s) { + proxy = "" + break + } + } + } + } if proxy != "" { url, err := url.Parse(proxy) if err == nil { addr = url.Host } } + c, err := net.Dial("tcp", addr) if err != nil { return nil, err @@ -168,6 +191,9 @@ type Options struct { // Status message StatusMessage string + + // Logger + Logger io.Writer } // NewClient establishes a new Client connection based on a set of Options. @@ -501,7 +527,7 @@ func (c *Client) startTLSIfRequired(f *streamFeatures, o *Options, domain string // will be returned. func (c *Client) startStream(o *Options, domain string) (*streamFeatures, error) { if o.Debug { - c.p = xml.NewDecoder(tee{c.conn, os.Stderr}) + c.p = xml.NewDecoder(tee{c.conn, o.Logger}) } else { c.p = xml.NewDecoder(c.conn) } @@ -545,6 +571,8 @@ type Chat struct { Remote string Type string Text string + Subject string + Thread string Roster Roster Other []string OtherElem []XMLElement @@ -594,6 +622,8 @@ func (c *Client) Recv() (stanza interface{}, err error) { Remote: v.From, Type: v.Type, Text: v.Body, + Subject: v.Subject, + Thread: v.Thread, Other: v.OtherStrings(), OtherElem: v.Other, Stamp: stamp, @@ -609,7 +639,7 @@ func (c *Client) Recv() (stanza interface{}, err error) { return Presence{v.From, v.To, v.Type, v.Show, v.Status}, nil case *clientIQ: // TODO check more strictly - if bytes.Equal(v.Query, []byte(``)) || bytes.Equal(v.Query, []byte(``)) { + if bytes.Equal(bytes.TrimSpace(v.Query), []byte(``)) || bytes.Equal(bytes.TrimSpace(v.Query), []byte(``)) { err := c.SendResultPing(v.ID, v.From) if err != nil { return Chat{}, err @@ -622,7 +652,15 @@ func (c *Client) Recv() (stanza interface{}, err error) { // Send sends the message wrapped inside an XMPP message stanza body. func (c *Client) Send(chat Chat) (n int, err error) { - return fmt.Fprintf(c.conn, ""+"%s", + var subtext = `` + var thdtext = `` + if chat.Subject != `` { + subtext = `` + xmlEscape(chat.Subject) + `` + } + if chat.Thread != `` { + thdtext = `` + xmlEscape(chat.Thread) + `` + } + return fmt.Fprintf(c.conn, ""+subtext+"%s"+thdtext+"", xmlEscape(chat.Remote), xmlEscape(chat.Type), xmlEscape(chat.Text)) } @@ -901,24 +939,10 @@ func next(p *xml.Decoder) (xml.Name, interface{}, error) { return se.Name, nv, err } -var xmlSpecial = map[byte]string{ - '<': "<", - '>': ">", - '"': """, - '\'': "'", - '&': "&", -} - func xmlEscape(s string) string { var b bytes.Buffer - for i := 0; i < len(s); i++ { - c := s[i] - if s, ok := xmlSpecial[c]; ok { - b.WriteString(s) - } else { - b.WriteByte(c) - } - } + xml.Escape(&b, []byte(s)) + return b.String() } diff --git a/vendor/github.com/mattn/go-xmpp/xmpp_information_query.go b/vendor/github.com/matterbridge/go-xmpp/xmpp_information_query.go similarity index 70% rename from vendor/github.com/mattn/go-xmpp/xmpp_information_query.go rename to vendor/github.com/matterbridge/go-xmpp/xmpp_information_query.go index d89379af..2a699222 100644 --- a/vendor/github.com/mattn/go-xmpp/xmpp_information_query.go +++ b/vendor/github.com/matterbridge/go-xmpp/xmpp_information_query.go @@ -22,3 +22,10 @@ func (c *Client) RawInformationQuery(from, to, id, iqType, requestNamespace, bod _, err := fmt.Fprintf(c.conn, xmlIQ, xmlEscape(from), xmlEscape(to), id, iqType, requestNamespace, body) return id, err } + +// rawInformation send a IQ request with the 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) + return id, err +} diff --git a/vendor/github.com/mattn/go-xmpp/xmpp_muc.go b/vendor/github.com/matterbridge/go-xmpp/xmpp_muc.go similarity index 100% rename from vendor/github.com/mattn/go-xmpp/xmpp_muc.go rename to vendor/github.com/matterbridge/go-xmpp/xmpp_muc.go diff --git a/vendor/github.com/mattn/go-xmpp/xmpp_ping.go b/vendor/github.com/matterbridge/go-xmpp/xmpp_ping.go similarity index 100% rename from vendor/github.com/mattn/go-xmpp/xmpp_ping.go rename to vendor/github.com/matterbridge/go-xmpp/xmpp_ping.go diff --git a/vendor/github.com/mattn/go-xmpp/xmpp_subscription.go b/vendor/github.com/matterbridge/go-xmpp/xmpp_subscription.go similarity index 100% rename from vendor/github.com/mattn/go-xmpp/xmpp_subscription.go rename to vendor/github.com/matterbridge/go-xmpp/xmpp_subscription.go diff --git a/vendor/manifest b/vendor/manifest index 875c8ff2..7cd182d2 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -286,6 +286,14 @@ "branch": "master", "notests": true }, + { + "importpath": "github.com/matterbridge/go-xmpp", + "repository": "https://github.com/matterbridge/go-xmpp", + "vcs": "git", + "revision": "0aa93db586ce719b8793aace600ddea0fdc7e828", + "branch": "work", + "notests": true + }, { "importpath": "github.com/matterbridge/gomatrix", "repository": "https://github.com/matterbridge/gomatrix", @@ -427,14 +435,6 @@ "branch": "master", "notests": true }, - { - "importpath": "github.com/mattn/go-xmpp", - "repository": "https://github.com/mattn/go-xmpp", - "vcs": "git", - "revision": "d0cdb99fae16437f69616ccc40662b6fe8ac6d47", - "branch": "master", - "notests": true - }, { "importpath": "github.com/mgutz/ansi", "repository": "https://github.com/mgutz/ansi",