2018-01-11 13:15:54 -08:00
|
|
|
package main
|
|
|
|
|
2018-01-12 10:08:47 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-31 10:41:32 -07:00
|
|
|
"log"
|
2018-01-12 10:08:47 -08:00
|
|
|
|
2018-12-26 09:50:01 -08:00
|
|
|
"gosrc.io/xmpp"
|
2019-06-26 08:14:52 -07:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2018-01-12 10:08:47 -08:00
|
|
|
)
|
2018-01-11 13:15:54 -08:00
|
|
|
|
|
|
|
func main() {
|
2019-06-08 10:42:02 -07:00
|
|
|
opts := xmpp.ComponentOptions{
|
2019-10-10 21:41:15 -07:00
|
|
|
TransportConfiguration: xmpp.TransportConfiguration{
|
|
|
|
Address: "localhost:8888",
|
|
|
|
},
|
2019-06-18 03:19:39 -07:00
|
|
|
Domain: "service2.localhost",
|
2019-06-08 10:42:02 -07:00
|
|
|
Secret: "mypass",
|
|
|
|
Name: "Test Component",
|
|
|
|
Category: "gateway",
|
|
|
|
Type: "service",
|
|
|
|
}
|
2019-06-18 03:19:39 -07:00
|
|
|
|
|
|
|
router := xmpp.NewRouter()
|
2019-06-19 02:19:49 -07:00
|
|
|
router.HandleFunc("message", handleMessage)
|
2019-06-18 03:19:39 -07:00
|
|
|
router.NewRoute().
|
2019-06-26 08:14:52 -07:00
|
|
|
IQNamespaces(stanza.NSDiscoInfo).
|
|
|
|
HandlerFunc(func(s xmpp.Sender, p stanza.Packet) {
|
2019-06-19 02:19:49 -07:00
|
|
|
discoInfo(s, p, opts)
|
2019-06-18 03:19:39 -07:00
|
|
|
})
|
|
|
|
router.NewRoute().
|
2019-06-26 08:14:52 -07:00
|
|
|
IQNamespaces(stanza.NSDiscoItems).
|
2019-06-19 02:19:49 -07:00
|
|
|
HandlerFunc(discoItems)
|
2019-06-18 03:19:39 -07:00
|
|
|
router.NewRoute().
|
|
|
|
IQNamespaces("jabber:iq:version").
|
2019-06-19 02:19:49 -07:00
|
|
|
HandlerFunc(handleVersion)
|
2019-06-18 03:19:39 -07:00
|
|
|
|
|
|
|
component, err := xmpp.NewComponent(opts, router)
|
2019-06-08 10:42:02 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%+v", err)
|
2019-05-31 10:41:32 -07:00
|
|
|
}
|
2018-01-12 10:08:47 -08:00
|
|
|
|
2019-06-18 03:19:39 -07:00
|
|
|
// If you pass the component to a stream manager, it will handle the reconnect policy
|
2019-06-08 10:42:02 -07:00
|
|
|
// for you automatically.
|
2019-06-18 03:19:39 -07:00
|
|
|
// TODO: Post Connect could be a feature of the router or the client. Move it somewhere else.
|
2019-06-08 10:42:02 -07:00
|
|
|
cm := xmpp.NewStreamManager(component, nil)
|
2019-06-18 03:19:39 -07:00
|
|
|
log.Fatal(cm.Run())
|
|
|
|
}
|
2018-01-13 09:50:17 -08:00
|
|
|
|
2019-06-26 08:14:52 -07:00
|
|
|
func handleMessage(_ xmpp.Sender, p stanza.Packet) {
|
|
|
|
msg, ok := p.(stanza.Message)
|
2019-06-18 03:19:39 -07:00
|
|
|
if !ok {
|
|
|
|
return
|
2018-01-12 10:08:47 -08:00
|
|
|
}
|
2019-06-18 03:19:39 -07:00
|
|
|
fmt.Println("Received message:", msg.Body)
|
2018-01-11 13:15:54 -08:00
|
|
|
}
|
2018-01-14 07:54:12 -08:00
|
|
|
|
2019-06-26 08:14:52 -07:00
|
|
|
func discoInfo(c xmpp.Sender, p stanza.Packet, opts xmpp.ComponentOptions) {
|
2019-06-18 03:19:39 -07:00
|
|
|
// Type conversion & sanity checks
|
2019-06-26 08:14:52 -07:00
|
|
|
iq, ok := p.(stanza.IQ)
|
2019-06-20 02:41:29 -07:00
|
|
|
if !ok || iq.Type != "get" {
|
2019-06-20 02:36:22 -07:00
|
|
|
return
|
|
|
|
}
|
2019-06-09 04:02:58 -07:00
|
|
|
|
2019-06-26 08:14:52 -07:00
|
|
|
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
|
2019-07-09 09:05:26 -07:00
|
|
|
disco := iqResp.DiscoInfo()
|
|
|
|
disco.AddIdentity(opts.Name, opts.Category, opts.Type)
|
|
|
|
disco.AddFeatures(stanza.NSDiscoInfo, stanza.NSDiscoItems, "jabber:iq:version", "urn:xmpp:delegation:1")
|
2019-06-18 03:19:39 -07:00
|
|
|
_ = c.Send(iqResp)
|
2019-06-09 04:02:58 -07:00
|
|
|
}
|
|
|
|
|
2019-06-18 03:19:39 -07:00
|
|
|
// TODO: Handle iq error responses
|
2019-06-26 08:14:52 -07:00
|
|
|
func discoItems(c xmpp.Sender, p stanza.Packet) {
|
2019-06-18 03:19:39 -07:00
|
|
|
// Type conversion & sanity checks
|
2019-06-26 08:14:52 -07:00
|
|
|
iq, ok := p.(stanza.IQ)
|
2019-06-20 02:41:29 -07:00
|
|
|
if !ok || iq.Type != "get" {
|
2019-06-18 03:19:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-26 08:14:52 -07:00
|
|
|
discoItems, ok := iq.Payload.(*stanza.DiscoItems)
|
2019-06-20 02:36:22 -07:00
|
|
|
if !ok {
|
2019-06-18 03:19:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-26 08:14:52 -07:00
|
|
|
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
|
2019-07-09 09:05:26 -07:00
|
|
|
items := iqResp.DiscoItems()
|
2018-01-26 02:40:59 -08:00
|
|
|
|
2019-06-18 03:19:39 -07:00
|
|
|
if discoItems.Node == "" {
|
2019-07-09 09:05:26 -07:00
|
|
|
items.AddItem("service.localhost", "node1", "test node")
|
2018-01-26 02:40:59 -08:00
|
|
|
}
|
2019-06-18 03:19:39 -07:00
|
|
|
_ = c.Send(iqResp)
|
2018-01-14 07:54:12 -08:00
|
|
|
}
|
2019-06-10 03:35:48 -07:00
|
|
|
|
2019-06-26 08:14:52 -07:00
|
|
|
func handleVersion(c xmpp.Sender, p stanza.Packet) {
|
2019-06-18 03:19:39 -07:00
|
|
|
// Type conversion & sanity checks
|
2019-06-26 08:14:52 -07:00
|
|
|
iq, ok := p.(stanza.IQ)
|
2019-06-18 03:19:39 -07:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-06-10 03:35:48 -07:00
|
|
|
|
2019-06-26 08:14:52 -07:00
|
|
|
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
|
2019-07-09 09:05:26 -07:00
|
|
|
iqResp.Version().SetInfo("Fluux XMPP Component", "0.0.1", "")
|
2019-06-18 03:19:39 -07:00
|
|
|
_ = c.Send(iqResp)
|
2019-06-10 03:35:48 -07:00
|
|
|
}
|