2018-01-11 21:15:54 +00:00
|
|
|
package main
|
|
|
|
|
2018-01-12 18:08:47 +00:00
|
|
|
import (
|
2018-01-15 11:28:34 +00:00
|
|
|
"encoding/xml"
|
2018-01-12 18:08:47 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"fluux.io/xmpp"
|
|
|
|
)
|
2018-01-11 21:15:54 +00:00
|
|
|
|
|
|
|
func main() {
|
2018-01-14 15:54:12 +00:00
|
|
|
component := MyComponent{Name: "MQTT Component", Category: "gateway", Type: "mqtt"}
|
|
|
|
component.xmpp = &xmpp.Component{Host: "mqtt.localhost", Secret: "mypass"}
|
|
|
|
component.xmpp.Connect("localhost:8888")
|
2018-01-12 18:08:47 +00:00
|
|
|
|
|
|
|
for {
|
2018-01-14 15:54:12 +00:00
|
|
|
packet, err := component.xmpp.ReadPacket()
|
2018-01-12 18:08:47 +00:00
|
|
|
if err != nil {
|
2018-01-13 16:46:10 +00:00
|
|
|
fmt.Println("read error", err)
|
2018-01-12 18:08:47 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-13 17:50:17 +00:00
|
|
|
|
|
|
|
switch p := packet.(type) {
|
|
|
|
case xmpp.IQ:
|
2018-01-15 11:52:28 +00:00
|
|
|
switch inner := p.Payload[0].(type) {
|
2018-01-13 18:27:46 +00:00
|
|
|
case *xmpp.Node:
|
2018-01-15 11:28:34 +00:00
|
|
|
fmt.Printf("%q\n", inner)
|
|
|
|
|
|
|
|
data, err := xml.Marshal(inner)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("cannot marshall payload")
|
|
|
|
}
|
|
|
|
fmt.Println("data=", string(data))
|
2018-01-14 15:54:12 +00:00
|
|
|
component.processIQ(p.Type, p.Id, p.From, inner)
|
2018-01-13 18:27:46 +00:00
|
|
|
default:
|
|
|
|
fmt.Println("default")
|
2018-01-13 18:14:26 +00:00
|
|
|
}
|
2018-01-13 17:50:17 +00:00
|
|
|
default:
|
|
|
|
fmt.Println("Packet unhandled packet:", packet)
|
|
|
|
}
|
2018-01-12 18:08:47 +00:00
|
|
|
}
|
2018-01-11 21:15:54 +00:00
|
|
|
}
|
2018-01-14 15:54:12 +00:00
|
|
|
|
|
|
|
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")
|
2018-01-17 17:47:34 +00:00
|
|
|
|
|
|
|
iq := xmpp.NewIQ("result", "admin@localhost", "test@localhost", "1", "en")
|
|
|
|
payload := xmpp.DiscoInfo{
|
|
|
|
Identity: xmpp.Identity{
|
|
|
|
Name: "Test Gateway",
|
|
|
|
Category: "gateway",
|
|
|
|
Type: "mqtt",
|
|
|
|
},
|
|
|
|
Features: []xmpp.Feature{
|
|
|
|
{Var: "http://jabber.org/protocol/disco#info"},
|
|
|
|
{Var: "http://jabber.org/protocol/disco#item"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
iq.AddPayload(&payload)
|
|
|
|
c.xmpp.Send(iq)
|
2018-01-14 15:54:12 +00:00
|
|
|
default:
|
|
|
|
iqErr := fmt.Sprintf(`<iq type='error'
|
|
|
|
from='%s'
|
|
|
|
to='%s'
|
|
|
|
id='%s'>
|
|
|
|
<error type="cancel" code="501">
|
|
|
|
<feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
|
|
|
|
</error>
|
|
|
|
</iq>`, c.xmpp.Host, from, id)
|
2018-01-17 17:47:34 +00:00
|
|
|
c.xmpp.SendOld(iqErr) // FIXME Remove that method
|
2018-01-14 15:54:12 +00:00
|
|
|
}
|
|
|
|
}
|