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