diff --git a/_examples/xmpp_echo/xmpp_echo.go b/_examples/xmpp_echo/xmpp_echo.go index b4da207..8794a5a 100644 --- a/_examples/xmpp_echo/xmpp_echo.go +++ b/_examples/xmpp_echo/xmpp_echo.go @@ -18,7 +18,7 @@ func main() { Address: "localhost:5222", Jid: "test@localhost", Password: "test", - PacketLogger: os.Stdout, + StreamLogger: os.Stdout, Insecure: true, } diff --git a/_examples/xmpp_jukebox/xmpp_jukebox.go b/_examples/xmpp_jukebox/xmpp_jukebox.go index 02e2c76..ed05b10 100644 --- a/_examples/xmpp_jukebox/xmpp_jukebox.go +++ b/_examples/xmpp_jukebox/xmpp_jukebox.go @@ -35,7 +35,7 @@ func main() { Address: *address, Jid: *jid, Password: *password, - // PacketLogger: os.Stdout, + // StreamLogger: os.Stdout, Insecure: true, } diff --git a/client.go b/client.go index 2bd8224..5f134aa 100644 --- a/client.go +++ b/client.go @@ -133,7 +133,7 @@ func (c *Client) Connect() error { //fmt.Fprintf(client.conn, "%s%s", "chat", "Online") // TODO: Do we always want to send initial presence automatically ? // Do we need an option to avoid that or do we rely on client to send the presence itself ? - fmt.Fprintf(c.Session.socketProxy, "") + fmt.Fprintf(c.Session.streamLogger, "") // Start the keepalive go routine keepaliveQuit := make(chan struct{}) @@ -166,10 +166,7 @@ func (c *Client) Send(packet stanza.Packet) error { return errors.New("cannot marshal packet " + err.Error()) } - if _, err := fmt.Fprintf(conn, string(data)); err != nil { - return errors.New("cannot send packet " + err.Error()) - } - return nil + return c.sendWithLogger(string(data)) } // SendRaw sends an XMPP stanza as a string to the server. @@ -182,8 +179,12 @@ func (c *Client) SendRaw(packet string) error { return errors.New("client is not connected") } + return c.sendWithLogger(packet) +} + +func (c *Client) sendWithLogger(packet string) error { var err error - _, err = fmt.Fprintf(conn, packet) + _, err = fmt.Fprintf(c.Session.streamLogger, packet) return err } diff --git a/config.go b/config.go index e37956b..3ae4a1c 100644 --- a/config.go +++ b/config.go @@ -10,7 +10,7 @@ type Config struct { Jid string parsedJid *Jid // For easier manipulation Password string - PacketLogger *os.File // Used for debugging + StreamLogger *os.File // Used for debugging Lang string // TODO: should default to 'en' ConnectTimeout int // Client timeout in seconds. Default to 15 // Insecure can be set to true to allow to open a session without TLS. If TLS diff --git a/session.go b/session.go index 2077e7a..7e7a60a 100644 --- a/session.go +++ b/session.go @@ -26,8 +26,8 @@ type Session struct { Out chan interface{} // read / write - socketProxy io.ReadWriter - decoder *xml.Decoder + streamLogger io.ReadWriter + decoder *xml.Decoder // error management err error @@ -65,7 +65,7 @@ func (s *Session) PacketId() string { } func (s *Session) init(conn net.Conn, o Config) { - s.setProxy(nil, conn, o) + s.setStreamLogger(nil, conn, o) s.Features = s.open(o.parsedJid.Domain) } @@ -74,22 +74,21 @@ func (s *Session) reset(conn net.Conn, newConn net.Conn, o Config) { return } - s.setProxy(conn, newConn, o) + s.setStreamLogger(conn, newConn, o) s.Features = s.open(o.parsedJid.Domain) } -// TODO: setProxyLogger ? better name ? This is not a TCP / HTTP proxy -func (s *Session) setProxy(conn net.Conn, newConn net.Conn, o Config) { +func (s *Session) setStreamLogger(conn net.Conn, newConn net.Conn, o Config) { if newConn != conn { - s.socketProxy = newSocketProxy(newConn, o.PacketLogger) + s.streamLogger = newStreamLogger(newConn, o.StreamLogger) } - s.decoder = xml.NewDecoder(s.socketProxy) + s.decoder = xml.NewDecoder(s.streamLogger) s.decoder.CharsetReader = o.CharsetReader } func (s *Session) open(domain string) (f stanza.StreamFeatures) { // Send stream open tag - if _, s.err = fmt.Fprintf(s.socketProxy, xmppStreamOpen, domain, stanza.NSClient, stanza.NSStream); s.err != nil { + if _, s.err = fmt.Fprintf(s.streamLogger, xmppStreamOpen, domain, stanza.NSClient, stanza.NSStream); s.err != nil { return } @@ -112,7 +111,7 @@ func (s *Session) startTlsIfSupported(conn net.Conn, domain string) net.Conn { } if _, ok := s.Features.DoesStartTLS(); ok { - fmt.Fprintf(s.socketProxy, "") + fmt.Fprintf(s.streamLogger, "") var k stanza.TLSProceed if s.err = s.decoder.DecodeElement(&k, nil); s.err != nil { @@ -143,7 +142,7 @@ func (s *Session) auth(o Config) { return } - s.err = authSASL(s.socketProxy, s.decoder, s.Features, o.parsedJid.Node, o.Password) + s.err = authSASL(s.streamLogger, s.decoder, s.Features, o.parsedJid.Node, o.Password) } func (s *Session) bind(o Config) { @@ -154,10 +153,10 @@ func (s *Session) bind(o Config) { // Send IQ message asking to bind to the local user name. var resource = o.parsedJid.Resource if resource != "" { - fmt.Fprintf(s.socketProxy, "%s", + fmt.Fprintf(s.streamLogger, "%s", s.PacketId(), stanza.NSBind, resource) } else { - fmt.Fprintf(s.socketProxy, "", s.PacketId(), stanza.NSBind) + fmt.Fprintf(s.streamLogger, "", s.PacketId(), stanza.NSBind) } var iq stanza.IQ @@ -186,7 +185,7 @@ func (s *Session) rfc3921Session(o Config) { var iq stanza.IQ if s.Features.Session.Optional.Local != "" { - fmt.Fprintf(s.socketProxy, "", s.PacketId(), stanza.NSSession) + fmt.Fprintf(s.streamLogger, "", s.PacketId(), stanza.NSSession) if s.err = s.decoder.Decode(&iq); s.err != nil { s.err = errors.New("expecting iq result after session open: " + s.err.Error()) return diff --git a/socket_proxy.go b/stream_logger.go similarity index 71% rename from socket_proxy.go rename to stream_logger.go index 111e302..8bbecdf 100644 --- a/socket_proxy.go +++ b/stream_logger.go @@ -7,20 +7,20 @@ import ( // Mediated Read / Write on socket // Used if logFile from Config is not nil -type socketProxy struct { +type streamLogger struct { socket io.ReadWriter // Actual connection logFile *os.File } -func newSocketProxy(conn io.ReadWriter, logFile *os.File) io.ReadWriter { +func newStreamLogger(conn io.ReadWriter, logFile *os.File) io.ReadWriter { if logFile == nil { return conn } else { - return &socketProxy{conn, logFile} + return &streamLogger{conn, logFile} } } -func (sp *socketProxy) Read(p []byte) (n int, err error) { +func (sp *streamLogger) Read(p []byte) (n int, err error) { n, err = sp.socket.Read(p) if n > 0 { sp.logFile.Write([]byte("RECV:\n")) // Prefix @@ -32,7 +32,7 @@ func (sp *socketProxy) Read(p []byte) (n int, err error) { return } -func (sp *socketProxy) Write(p []byte) (n int, err error) { +func (sp *streamLogger) Write(p []byte) (n int, err error) { sp.logFile.Write([]byte("SEND:\n")) // Prefix for _, w := range []io.Writer{sp.socket, sp.logFile} { n, err = w.Write(p) @@ -47,3 +47,7 @@ func (sp *socketProxy) Write(p []byte) (n int, err error) { sp.logFile.Write([]byte("\n\n")) // Separator return len(p), nil } + +/* +TODO: Make RECV, SEND prefixes + +*/