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