diff --git a/client.go b/client.go index 08ee804..8db7249 100644 --- a/client.go +++ b/client.go @@ -126,9 +126,24 @@ func (c *Client) Recv() <-chan interface{} { return ch } -// Send sends message text. -// TODO Move to Go XML Marshaller -func (c *Client) Send(packet string) error { +// Send marshalls XMPP stanza and sends it to the server. +func (c *Client) Send(packet Packet) error { + data, err := xml.Marshal(packet) + if err != nil { + return errors.New("cannot marshal packet " + err.Error()) + } + + if _, err := fmt.Fprintf(c.conn, string(data)); err != nil { + return errors.New("cannot send packet " + err.Error()) + } + return nil +} + +// SendRaw sends an XMPP stanza as a string to the server. +// It can be invalid XML or XMPP content. In that case, the server will +// disconnect the client. It is up to the user of this method to +// carefully craft the XML content to produce valid XMPP. +func (c *Client) SendRaw(packet string) error { fmt.Fprintf(c.Session.socketProxy, packet) // TODO handle errors return nil } diff --git a/cmd/xmpp_echo/xmpp_echo.go b/cmd/xmpp_echo/xmpp_echo.go index d092508..6ebda72 100644 --- a/cmd/xmpp_echo/xmpp_echo.go +++ b/cmd/xmpp_echo/xmpp_echo.go @@ -13,7 +13,13 @@ import ( ) func main() { - options := xmpp.Options{Address: "localhost:5222", Jid: "test@localhost", Password: "test", PacketLogger: os.Stdout} + options := xmpp.Options{ + Address: "localhost:5222", + Jid: "test@localhost", + Password: "test", + PacketLogger: os.Stdout, + Insecure: true, + } var client *xmpp.Client var err error @@ -34,7 +40,7 @@ func main() { case *xmpp.Message: fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", packet.Body, packet.From) reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From}, Body: packet.Body} - client.Send(reply.XMPPFormat()) + client.Send(reply) default: fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", packet) } diff --git a/cmd/xmpp_jukebox/xmpp_jukebox.go b/cmd/xmpp_jukebox/xmpp_jukebox.go index 2d7819f..c5cdf28 100644 --- a/cmd/xmpp_jukebox/xmpp_jukebox.go +++ b/cmd/xmpp_jukebox/xmpp_jukebox.go @@ -77,7 +77,7 @@ func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) { playSCURL(p, url) setResponse := new(iot.ControlSetResponse) reply := xmpp.IQ{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "result", Id: packet.Id}, Payload: []xmpp.IQPayload{setResponse}} - client.Send(reply.XMPPFormat()) + client.SendRaw(reply.XMPPFormat()) // TODO add Soundclound artist / title retrieval sendUserTune(client, "Radiohead", "Spectre") default: @@ -87,7 +87,7 @@ func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) { func sendUserTune(client *xmpp.Client, artist string, title string) { tune := pep.Tune{Artist: artist, Title: title} - client.Send(tune.XMPPFormat()) + client.SendRaw(tune.XMPPFormat()) } func playSCURL(p *mpg123.Player, rawURL string) { diff --git a/component.go b/component.go index 5077066..57bbabf 100644 --- a/component.go +++ b/component.go @@ -73,11 +73,12 @@ func (c *Component) Connect(connStr string) error { } // ReadPacket reads next incoming XMPP packet -// TODO use defined interface Packet func (c *Component) ReadPacket() (Packet, error) { + // TODO use defined interface Packet return next(c.decoder) } +// Send marshalls XMPP stanza and sends it to the server. func (c *Component) Send(packet Packet) error { data, err := xml.Marshal(packet) if err != nil { @@ -90,6 +91,15 @@ func (c *Component) Send(packet Packet) error { return nil } +// SendRaw sends an XMPP stanza as a string to the server. +// It can be invalid XML or XMPP content. In that case, the server will +// disconnect the component. It is up to the user of this method to +// carefully craft the XML content to produce valid XMPP. +func (c *Component) SendRaw(packet string) error { + fmt.Fprintf(c.conn, packet) // TODO handle errors + return nil +} + // handshake generates an authentication token based on StreamID and shared secret. func (c *Component) handshake(streamId string) string { // 1. Concatenate the Stream ID received from the server with the shared secret.