go-xmpp/_examples/xmpp_component/xmpp_component.go

133 lines
2.7 KiB
Go
Raw Normal View History

2018-01-11 21:15:54 +00:00
package main
2018-01-12 18:08:47 +00:00
import (
"encoding/xml"
2018-01-12 18:08:47 +00:00
"fmt"
2019-05-31 17:41:32 +00:00
"log"
2018-01-12 18:08:47 +00:00
"gosrc.io/xmpp"
2018-01-12 18:08:47 +00:00
)
2018-01-11 21:15:54 +00:00
func main() {
opts := xmpp.ComponentOptions{
Domain: "service2.localhost",
Secret: "mypass",
Address: "localhost:8888",
Name: "Test Component",
Category: "gateway",
Type: "service",
}
router := xmpp.NewRouter()
router.HandleFunc("message", HandleMessage)
router.NewRoute().
IQNamespaces(xmpp.NSDiscoInfo).
HandlerFunc(func(s xmpp.Sender, p xmpp.Packet) {
DiscoInfo(s, p, opts)
})
router.NewRoute().
IQNamespaces(xmpp.NSDiscoItems).
HandlerFunc(DiscoItems)
router.NewRoute().
IQNamespaces("jabber:iq:version").
HandlerFunc(HandleVersion)
component, err := xmpp.NewComponent(opts, router)
if err != nil {
log.Fatalf("%+v", err)
2019-05-31 17:41:32 +00:00
}
2018-01-12 18:08:47 +00:00
// If you pass the component to a stream manager, it will handle the reconnect policy
// for you automatically.
// TODO: Post Connect could be a feature of the router or the client. Move it somewhere else.
cm := xmpp.NewStreamManager(component, nil)
log.Fatal(cm.Run())
}
2018-01-13 17:50:17 +00:00
func HandleMessage(_ xmpp.Sender, p xmpp.Packet) {
msg, ok := p.(xmpp.Message)
if !ok {
return
2018-01-12 18:08:47 +00:00
}
fmt.Println("Received message:", msg.Body)
2018-01-11 21:15:54 +00:00
}
2018-01-14 15:54:12 +00:00
func DiscoInfo(c xmpp.Sender, p xmpp.Packet, opts xmpp.ComponentOptions) {
// Type conversion & sanity checks
iq, ok := p.(xmpp.IQ)
if !ok {
return
2019-06-09 11:02:58 +00:00
}
iqResp := xmpp.NewIQ("result", iq.To, iq.From, iq.Id, "en")
identity := xmpp.Identity{
Name: opts.Name,
Category: opts.Category,
Type: opts.Type,
}
2019-06-09 11:02:58 +00:00
payload := xmpp.DiscoInfo{
XMLName: xml.Name{
Space: xmpp.NSDiscoInfo,
Local: "query",
},
2019-06-09 11:02:58 +00:00
Identity: identity,
Features: []xmpp.Feature{
{Var: xmpp.NSDiscoInfo},
{Var: xmpp.NSDiscoItems},
{Var: "jabber:iq:version"},
{Var: "urn:xmpp:delegation:1"},
2019-06-09 11:02:58 +00:00
},
}
iqResp.AddPayload(&payload)
_ = c.Send(iqResp)
2019-06-09 11:02:58 +00:00
}
// TODO: Handle iq error responses
func DiscoItems(c xmpp.Sender, p xmpp.Packet) {
// Type conversion & sanity checks
iq, ok := p.(xmpp.IQ)
if !ok {
return
}
if len(iq.Payload) == 0 {
return
}
discoItems, ok := iq.Payload[0].(*xmpp.DiscoItems)
if !ok {
return
}
if iq.Type != "get" {
return
}
iqResp := xmpp.NewIQ("result", iq.To, iq.From, iq.Id, "en")
2018-01-26 10:40:59 +00:00
var payload xmpp.DiscoItems
if discoItems.Node == "" {
2018-01-26 10:40:59 +00:00
payload = xmpp.DiscoItems{
Items: []xmpp.DiscoItem{
2018-01-26 11:37:27 +00:00
{Name: "test node", JID: "service.localhost", Node: "node1"},
2018-01-26 10:40:59 +00:00
},
}
}
iqResp.AddPayload(&payload)
_ = c.Send(iqResp)
2018-01-14 15:54:12 +00:00
}
func HandleVersion(c xmpp.Sender, p xmpp.Packet) {
// Type conversion & sanity checks
iq, ok := p.(xmpp.IQ)
if !ok {
return
}
iqResp := xmpp.NewIQ("result", iq.To, iq.From, iq.Id, "en")
var payload xmpp.Version
payload.Name = "Fluux XMPP Component"
payload.Version = "0.0.1"
iq.AddPayload(&payload)
_ = c.Send(iqResp)
}