2019-06-18 12:24:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"gosrc.io/xmpp"
|
2019-06-26 15:14:52 +00:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-06-18 12:24:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
opts := xmpp.ComponentOptions{
|
2019-10-11 04:41:15 +00:00
|
|
|
TransportConfiguration: xmpp.TransportConfiguration{
|
|
|
|
Address: "localhost:9999",
|
2019-10-18 18:29:54 +00:00
|
|
|
Domain: "service.localhost",
|
2019-10-11 04:41:15 +00:00
|
|
|
},
|
|
|
|
Domain: "service.localhost",
|
|
|
|
Secret: "mypass",
|
2019-06-18 12:24:33 +00:00
|
|
|
|
|
|
|
// TODO: Move that part to a component discovery handler
|
|
|
|
Name: "Test Component",
|
|
|
|
Category: "gateway",
|
|
|
|
Type: "service",
|
|
|
|
}
|
|
|
|
|
|
|
|
router := xmpp.NewRouter()
|
2019-06-19 09:19:49 +00:00
|
|
|
router.HandleFunc("message", handleMessage)
|
2019-06-18 12:24:33 +00:00
|
|
|
router.NewRoute().
|
2019-06-26 15:14:52 +00:00
|
|
|
IQNamespaces(stanza.NSDiscoInfo).
|
|
|
|
HandlerFunc(func(s xmpp.Sender, p stanza.Packet) {
|
2019-06-19 09:19:49 +00:00
|
|
|
discoInfo(s, p, opts)
|
2019-06-18 12:24:33 +00:00
|
|
|
})
|
|
|
|
router.NewRoute().
|
|
|
|
IQNamespaces("urn:xmpp:delegation:1").
|
2019-06-19 09:19:49 +00:00
|
|
|
HandlerFunc(handleDelegation)
|
2019-06-18 12:24:33 +00:00
|
|
|
|
2020-01-31 10:48:03 +00:00
|
|
|
component, err := xmpp.NewComponent(opts, router, func(err error) {
|
|
|
|
log.Println(err)
|
|
|
|
})
|
2019-06-18 12:24:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func handleMessage(_ xmpp.Sender, p stanza.Packet) {
|
|
|
|
msg, ok := p.(stanza.Message)
|
2019-06-18 12:24:33 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var msgProcessed bool
|
|
|
|
for _, ext := range msg.Extensions {
|
2019-06-26 15:14:52 +00:00
|
|
|
delegation, ok := ext.(*stanza.Delegation)
|
2019-06-18 12:24:33 +00:00
|
|
|
if ok {
|
|
|
|
msgProcessed = true
|
|
|
|
fmt.Printf("Delegation confirmed for namespace %s\n", delegation.Delegated.Namespace)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Decode privilege message
|
|
|
|
// <message to='service.localhost' from='localhost'><privilege xmlns='urn:xmpp:privilege:1'><perm type='outgoing' access='message'/><perm type='get' access='roster'/><perm type='managed_entity' access='presence'/></privilege></message>
|
|
|
|
|
|
|
|
if !msgProcessed {
|
|
|
|
fmt.Printf("Ignored received message, not related to delegation: %v\n", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
pubsubNode = "urn:xmpp:delegation:1::http://jabber.org/protocol/pubsub"
|
|
|
|
pepNode = "urn:xmpp:delegation:1:bare:http://jabber.org/protocol/pubsub"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: replace xmpp.Sender by ctx xmpp.Context ?
|
|
|
|
// ctx.Stream.Send / SendRaw
|
|
|
|
// ctx.Opts
|
2019-06-26 15:14:52 +00:00
|
|
|
func discoInfo(c xmpp.Sender, p stanza.Packet, opts xmpp.ComponentOptions) {
|
2019-06-18 12:24:33 +00:00
|
|
|
// Type conversion & sanity checks
|
2020-01-31 10:48:03 +00:00
|
|
|
iq, ok := p.(*stanza.IQ)
|
2019-06-18 12:24:33 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-06-26 15:14:52 +00:00
|
|
|
info, ok := iq.Payload.(*stanza.DiscoInfo)
|
2019-06-18 12:24:33 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-31 10:48:03 +00:00
|
|
|
iqResp, err := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create IQ response: %v", err)
|
|
|
|
}
|
2019-06-18 12:24:33 +00:00
|
|
|
|
|
|
|
switch info.Node {
|
|
|
|
case "":
|
2020-01-31 10:48:03 +00:00
|
|
|
discoInfoRoot(iqResp, opts)
|
2019-06-18 12:24:33 +00:00
|
|
|
case pubsubNode:
|
2020-01-31 10:48:03 +00:00
|
|
|
discoInfoPubSub(iqResp)
|
2019-06-18 12:24:33 +00:00
|
|
|
case pepNode:
|
2020-01-31 10:48:03 +00:00
|
|
|
discoInfoPEP(iqResp)
|
2019-06-18 12:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = c.Send(iqResp)
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func discoInfoRoot(iqResp *stanza.IQ, opts xmpp.ComponentOptions) {
|
2019-07-09 16:09:23 +00: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 12:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func discoInfoPubSub(iqResp *stanza.IQ) {
|
|
|
|
payload := stanza.DiscoInfo{
|
2019-06-18 12:24:33 +00:00
|
|
|
XMLName: xml.Name{
|
2019-06-26 15:14:52 +00:00
|
|
|
Space: stanza.NSDiscoInfo,
|
2019-06-18 12:24:33 +00:00
|
|
|
Local: "query",
|
|
|
|
},
|
|
|
|
Node: pubsubNode,
|
2019-06-26 15:14:52 +00:00
|
|
|
Features: []stanza.Feature{
|
2019-06-18 12:24:33 +00:00
|
|
|
{Var: "http://jabber.org/protocol/pubsub"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#publish"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#subscribe"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#publish-options"},
|
|
|
|
},
|
|
|
|
}
|
2019-06-19 09:19:49 +00:00
|
|
|
iqResp.Payload = &payload
|
2019-06-18 12:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func discoInfoPEP(iqResp *stanza.IQ) {
|
|
|
|
identity := stanza.Identity{
|
2019-06-18 12:24:33 +00:00
|
|
|
Category: "pubsub",
|
|
|
|
Type: "pep",
|
|
|
|
}
|
2019-06-26 15:14:52 +00:00
|
|
|
payload := stanza.DiscoInfo{
|
2019-06-18 12:24:33 +00:00
|
|
|
XMLName: xml.Name{
|
2019-06-26 15:14:52 +00:00
|
|
|
Space: stanza.NSDiscoInfo,
|
2019-06-18 12:24:33 +00:00
|
|
|
Local: "query",
|
|
|
|
},
|
2019-06-27 12:30:23 +00:00
|
|
|
Identity: []stanza.Identity{identity},
|
2019-06-18 12:24:33 +00:00
|
|
|
Node: pepNode,
|
2019-06-26 15:14:52 +00:00
|
|
|
Features: []stanza.Feature{
|
2019-06-18 12:24:33 +00:00
|
|
|
{Var: "http://jabber.org/protocol/pubsub#access-presence"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#auto-create"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#auto-subscribe"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#config-node"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#create-and-configure"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#create-nodes"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#filtered-notifications"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#persistent-items"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#publish"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#retrieve-items"},
|
|
|
|
{Var: "http://jabber.org/protocol/pubsub#subscribe"},
|
|
|
|
},
|
|
|
|
}
|
2019-06-19 09:19:49 +00:00
|
|
|
iqResp.Payload = &payload
|
2019-06-18 12:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func handleDelegation(s xmpp.Sender, p stanza.Packet) {
|
2019-06-18 12:24:33 +00:00
|
|
|
// Type conversion & sanity checks
|
2020-01-31 10:48:03 +00:00
|
|
|
iq, ok := p.(*stanza.IQ)
|
2019-06-18 12:24:33 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
delegation, ok := iq.Payload.(*stanza.Delegation)
|
2019-06-18 12:24:33 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
forwardedPacket := delegation.Forwarded.Stanza
|
|
|
|
fmt.Println(forwardedPacket)
|
2020-01-31 10:48:03 +00:00
|
|
|
forwardedIQ, ok := forwardedPacket.(*stanza.IQ)
|
2019-06-18 12:24:33 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:33:11 +00:00
|
|
|
pubsub, ok := forwardedIQ.Payload.(*stanza.PubSubGeneric)
|
2019-06-18 12:24:33 +00:00
|
|
|
if !ok {
|
|
|
|
// We only support pubsub delegation
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pubsub.Publish.XMLName.Local == "publish" {
|
|
|
|
// Prepare pubsub IQ reply
|
2020-01-31 10:48:03 +00:00
|
|
|
iqResp, err := stanza.NewIQ(stanza.Attrs{Type: "result", From: forwardedIQ.To, To: forwardedIQ.From, Id: forwardedIQ.Id})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create iqResp: %v", err)
|
|
|
|
}
|
2020-01-09 14:33:11 +00:00
|
|
|
payload := stanza.PubSubGeneric{
|
2019-06-18 12:24:33 +00:00
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "http://jabber.org/protocol/pubsub",
|
|
|
|
Local: "pubsub",
|
|
|
|
},
|
|
|
|
}
|
2019-06-19 09:19:49 +00:00
|
|
|
iqResp.Payload = &payload
|
2019-06-18 12:24:33 +00:00
|
|
|
// Wrap the reply in delegation 'forward'
|
2020-01-31 10:48:03 +00:00
|
|
|
iqForward, err := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create iqForward: %v", err)
|
|
|
|
}
|
2019-06-26 15:14:52 +00:00
|
|
|
delegPayload := stanza.Delegation{
|
2019-06-18 12:24:33 +00:00
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "urn:xmpp:delegation:1",
|
|
|
|
Local: "delegation",
|
|
|
|
},
|
2019-06-26 15:14:52 +00:00
|
|
|
Forwarded: &stanza.Forwarded{
|
2019-06-18 12:24:33 +00:00
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "urn:xmpp:forward:0",
|
|
|
|
Local: "forward",
|
|
|
|
},
|
|
|
|
Stanza: iqResp,
|
|
|
|
},
|
|
|
|
}
|
2019-06-19 09:19:49 +00:00
|
|
|
iqForward.Payload = &delegPayload
|
2019-06-18 12:24:33 +00:00
|
|
|
_ = s.Send(iqForward)
|
|
|
|
// TODO: The component should actually broadcast the mood to subscribers
|
|
|
|
}
|
|
|
|
}
|