From ff2da776d3219817a18dbae10128111eb6f0f96f Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Sun, 14 Jan 2018 16:54:12 +0100 Subject: [PATCH] Basic test component (disco Info) --- cmd/xmpp_component/xmpp_component.go | 54 +++++++++++++++++++++++++--- component.go | 7 ++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/cmd/xmpp_component/xmpp_component.go b/cmd/xmpp_component/xmpp_component.go index 7b794c2..3303c47 100644 --- a/cmd/xmpp_component/xmpp_component.go +++ b/cmd/xmpp_component/xmpp_component.go @@ -7,11 +7,12 @@ import ( ) func main() { - component := xmpp.Component{Host: "mqtt.localhost", Secret: "mypass"} - component.Connect("localhost:8888") + component := MyComponent{Name: "MQTT Component", Category: "gateway", Type: "mqtt"} + component.xmpp = &xmpp.Component{Host: "mqtt.localhost", Secret: "mypass"} + component.xmpp.Connect("localhost:8888") for { - packet, err := component.ReadPacket() + packet, err := component.xmpp.ReadPacket() if err != nil { fmt.Println("read error", err) return @@ -21,7 +22,7 @@ func main() { case xmpp.IQ: switch inner := p.Payload.(type) { case *xmpp.Node: - fmt.Println("Node:", inner.XMLName.Space, inner.XMLName.Local) + component.processIQ(p.Type, p.Id, p.From, inner) default: fmt.Println("default") } @@ -30,3 +31,48 @@ func main() { } } } + +const ( + NSDiscoInfo = "http://jabber.org/protocol/disco#info" +) + +type MyComponent struct { + Name string + // Typical categories and types: https://xmpp.org/registrar/disco-categories.html + Category string + Type string + + xmpp *xmpp.Component +} + +func (c MyComponent) processIQ(iqType, id, from string, inner *xmpp.Node) { + fmt.Println("Node:", inner.XMLName.Space, inner.XMLName.Local) + switch inner.XMLName.Space + " " + iqType { + case NSDiscoInfo + " get": + fmt.Println("Send Disco Info") + result := fmt.Sprintf(` + + + + + +`, c.xmpp.Host, from, id, c.Category, c.Type, c.Name) + c.xmpp.Send(result) + default: + iqErr := fmt.Sprintf(` + + + +`, c.xmpp.Host, from, id) + c.xmpp.Send(iqErr) + } +} diff --git a/component.go b/component.go index 6474660..aa5d8d0 100644 --- a/component.go +++ b/component.go @@ -94,6 +94,13 @@ func (c *Component) ReadPacket() (Packet, error) { return next(c.decoder) } +func (c *Component) Send(packet string) error { + if _, err := fmt.Fprintf(c.conn, packet); err != nil { + return errors.New("cannot send packet " + err.Error()) + } + return nil +} + // ============================================================================ // Handshake Packet