Adapt examples to new routing library for components

This commit is contained in:
Mickael Remond 2019-06-18 12:19:39 +02:00 committed by Mickaël Rémond
parent f0f0d5a285
commit 9b57809e9d
2 changed files with 81 additions and 70 deletions

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/xml"
"fmt" "fmt"
"log" "log"
@ -9,114 +10,123 @@ import (
func main() { func main() {
opts := xmpp.ComponentOptions{ opts := xmpp.ComponentOptions{
Domain: "service.localhost", Domain: "service2.localhost",
Secret: "mypass", Secret: "mypass",
Address: "localhost:8888", Address: "localhost:8888",
Name: "Test Component", Name: "Test Component",
Category: "gateway", Category: "gateway",
Type: "service", Type: "service",
} }
component, err := xmpp.NewComponent(opts)
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 { if err != nil {
log.Fatalf("%+v", err) log.Fatalf("%+v", err)
} }
// If you pass the component to a connection manager, it will handle the reconnect policy // If you pass the component to a stream manager, it will handle the reconnect policy
// for you automatically. // 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) cm := xmpp.NewStreamManager(component, nil)
err = cm.Start() log.Fatal(cm.Run())
if err != nil {
log.Fatal(err)
}
// Iterator to receive packets coming from our XMPP connection
for packet := range component.Recv() {
switch p := packet.(type) {
case xmpp.IQ:
switch inner := p.Payload[0].(type) {
case *xmpp.DiscoInfo:
fmt.Println("DiscoInfo")
if p.Type == "get" {
discoResult(component, p.PacketAttrs, inner)
}
case *xmpp.DiscoItems:
fmt.Println("DiscoItems")
if p.Type == "get" {
discoItems(component, p.PacketAttrs, inner)
}
case *xmpp.Version:
fmt.Println("Version")
if p.Type == "get" {
version(component, p.PacketAttrs)
}
default:
fmt.Println("ignoring iq packet", inner)
xError := xmpp.Err{
Code: 501,
Reason: "feature-not-implemented",
Type: "cancel",
}
reply := p.MakeError(xError)
_ = component.Send(&reply)
}
case xmpp.Message:
fmt.Println("Received message:", p.Body)
case xmpp.Presence:
fmt.Println("Received presence:", p.Type)
default:
fmt.Println("ignoring packet:", packet)
}
}
} }
func discoResult(c *xmpp.Component, attrs xmpp.PacketAttrs, info *xmpp.DiscoInfo) { func HandleMessage(_ xmpp.Sender, p xmpp.Packet) {
iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en") msg, ok := p.(xmpp.Message)
var identity xmpp.Identity if !ok {
if info.Node == "" { return
identity = xmpp.Identity{
Name: c.Name,
Category: c.Category,
Type: c.Type,
} }
fmt.Println("Received message:", msg.Body)
}
func DiscoInfo(c xmpp.Sender, p xmpp.Packet, opts xmpp.ComponentOptions) {
// Type conversion & sanity checks
iq, ok := p.(xmpp.IQ)
if !ok {
return
} }
iqResp := xmpp.NewIQ("result", iq.To, iq.From, iq.Id, "en")
identity := xmpp.Identity{
Name: opts.Name,
Category: opts.Category,
Type: opts.Type,
}
payload := xmpp.DiscoInfo{ payload := xmpp.DiscoInfo{
XMLName: xml.Name{
Space: xmpp.NSDiscoInfo,
Local: "query",
},
Identity: identity, Identity: identity,
Features: []xmpp.Feature{ Features: []xmpp.Feature{
{Var: xmpp.NSDiscoInfo}, {Var: xmpp.NSDiscoInfo},
{Var: xmpp.NSDiscoItems}, {Var: xmpp.NSDiscoItems},
{Var: "jabber:iq:version"}, {Var: "jabber:iq:version"},
{Var: "urn:xmpp:delegation:1"},
}, },
} }
iq.AddPayload(&payload) iqResp.AddPayload(&payload)
_ = c.Send(iqResp)
_ = c.Send(iq)
} }
func discoItems(c *xmpp.Component, attrs xmpp.PacketAttrs, items *xmpp.DiscoItems) { // TODO: Handle iq error responses
iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en") 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")
var payload xmpp.DiscoItems var payload xmpp.DiscoItems
if items.Node == "" { if discoItems.Node == "" {
payload = xmpp.DiscoItems{ payload = xmpp.DiscoItems{
Items: []xmpp.DiscoItem{ Items: []xmpp.DiscoItem{
{Name: "test node", JID: "service.localhost", Node: "node1"}, {Name: "test node", JID: "service.localhost", Node: "node1"},
}, },
} }
} }
iq.AddPayload(&payload) iqResp.AddPayload(&payload)
_ = c.Send(iq) _ = c.Send(iqResp)
} }
func version(c *xmpp.Component, attrs xmpp.PacketAttrs) { func HandleVersion(c xmpp.Sender, p xmpp.Packet) {
iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en") // 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 var payload xmpp.Version
payload.Name = "Fluux XMPP Component" payload.Name = "Fluux XMPP Component"
payload.Version = "0.0.1" payload.Version = "0.0.1"
iq.AddPayload(&payload) iq.AddPayload(&payload)
_ = c.Send(iq) _ = c.Send(iqResp)
} }

View file

@ -74,6 +74,7 @@ func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) {
playSCURL(p, url) playSCURL(p, url)
setResponse := new(xmpp.ControlSetResponse) setResponse := new(xmpp.ControlSetResponse)
// FIXME: Broken
reply := xmpp.IQ{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "result", Id: packet.Id}, Payload: []xmpp.IQPayload{setResponse}} reply := xmpp.IQ{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "result", Id: packet.Id}, Payload: []xmpp.IQPayload{setResponse}}
_ = client.Send(reply) _ = client.Send(reply)
// TODO add Soundclound artist / title retrieval // TODO add Soundclound artist / title retrieval
@ -86,7 +87,7 @@ func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) {
func sendUserTune(client *xmpp.Client, artist string, title string) { func sendUserTune(client *xmpp.Client, artist string, title string) {
tune := xmpp.Tune{Artist: artist, Title: title} tune := xmpp.Tune{Artist: artist, Title: title}
iq := xmpp.NewIQ("set", "", "", "usertune-1", "en") iq := xmpp.NewIQ("set", "", "", "usertune-1", "en")
payload := xmpp.PubSub{Publish: xmpp.Publish{Node: "http://jabber.org/protocol/tune", Item: xmpp.Item{Tune: tune}}} payload := xmpp.PubSub{Publish: &xmpp.Publish{Node: "http://jabber.org/protocol/tune", Item: xmpp.Item{Tune: &tune}}}
iq.AddPayload(&payload) iq.AddPayload(&payload)
_ = client.Send(iq) _ = client.Send(iq)
} }