go-xmpp/_examples/xmpp_component/xmpp_component.go

105 lines
2.6 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 (
"fmt"
2019-05-31 17:41:32 +00:00
"log"
2018-01-12 18:08:47 +00:00
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
2018-01-12 18:08:47 +00:00
)
2018-01-11 21:15:54 +00:00
func main() {
opts := xmpp.ComponentOptions{
TransportConfiguration: xmpp.TransportConfiguration{
Address: "localhost:8888",
Domain: "service2.localhost",
},
Domain: "service2.localhost",
Secret: "mypass",
Name: "Test Component",
Category: "gateway",
Type: "service",
}
router := xmpp.NewRouter()
2019-06-19 09:19:49 +00:00
router.HandleFunc("message", handleMessage)
router.NewRoute().
IQNamespaces(stanza.NSDiscoInfo).
HandlerFunc(func(s xmpp.Sender, p stanza.Packet) {
2019-06-19 09:19:49 +00:00
discoInfo(s, p, opts)
})
router.NewRoute().
IQNamespaces(stanza.NSDiscoItems).
2019-06-19 09:19:49 +00:00
HandlerFunc(discoItems)
router.NewRoute().
IQNamespaces("jabber:iq:version").
2019-06-19 09:19:49 +00:00
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 stanza.Packet) {
msg, ok := p.(stanza.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 stanza.Packet, opts xmpp.ComponentOptions) {
// Type conversion & sanity checks
iq, ok := p.(stanza.IQ)
2019-06-20 09:41:29 +00:00
if !ok || iq.Type != "get" {
return
}
2019-06-09 11:02:58 +00:00
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
disco := iqResp.DiscoInfo()
disco.AddIdentity(opts.Name, opts.Category, opts.Type)
disco.AddFeatures(stanza.NSDiscoInfo, stanza.NSDiscoItems, "jabber:iq:version", "urn:xmpp:delegation:1")
_ = c.Send(iqResp)
2019-06-09 11:02:58 +00:00
}
// TODO: Handle iq error responses
func discoItems(c xmpp.Sender, p stanza.Packet) {
// Type conversion & sanity checks
iq, ok := p.(stanza.IQ)
2019-06-20 09:41:29 +00:00
if !ok || iq.Type != "get" {
return
}
discoItems, ok := iq.Payload.(*stanza.DiscoItems)
if !ok {
return
}
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
items := iqResp.DiscoItems()
2018-01-26 10:40:59 +00:00
if discoItems.Node == "" {
items.AddItem("service.localhost", "node1", "test node")
2018-01-26 10:40:59 +00:00
}
_ = c.Send(iqResp)
2018-01-14 15:54:12 +00:00
}
func handleVersion(c xmpp.Sender, p stanza.Packet) {
// Type conversion & sanity checks
iq, ok := p.(stanza.IQ)
if !ok {
return
}
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
iqResp.Version().SetInfo("Fluux XMPP Component", "0.0.1", "")
_ = c.Send(iqResp)
}