Update examples

This commit is contained in:
Mickael Remond 2019-06-19 11:19:49 +02:00 committed by Mickaël Rémond
parent 24502f7cd7
commit 3f81465c6c
4 changed files with 85 additions and 91 deletions

View File

@ -21,15 +21,15 @@ func main() {
}
router := xmpp.NewRouter()
router.HandleFunc("message", HandleMessage)
router.HandleFunc("message", handleMessage)
router.NewRoute().
IQNamespaces(xmpp.NSDiscoInfo).
HandlerFunc(func(s xmpp.Sender, p xmpp.Packet) {
DiscoInfo(s, p, opts)
discoInfo(s, p, opts)
})
router.NewRoute().
IQNamespaces("urn:xmpp:delegation:1").
HandlerFunc(HandleDelegation)
HandlerFunc(handleDelegation)
component, err := xmpp.NewComponent(opts, router)
if err != nil {
@ -43,7 +43,7 @@ func main() {
log.Fatal(cm.Run())
}
func HandleMessage(_ xmpp.Sender, p xmpp.Packet) {
func handleMessage(_ xmpp.Sender, p xmpp.Packet) {
msg, ok := p.(xmpp.Message)
if !ok {
return
@ -72,13 +72,13 @@ const (
// TODO: replace xmpp.Sender by ctx xmpp.Context ?
// ctx.Stream.Send / SendRaw
// ctx.Opts
func DiscoInfo(c xmpp.Sender, p xmpp.Packet, opts xmpp.ComponentOptions) {
func discoInfo(c xmpp.Sender, p xmpp.Packet, opts xmpp.ComponentOptions) {
// Type conversion & sanity checks
iq, ok := p.(xmpp.IQ)
if !ok {
return
}
info, ok := iq.Payload[0].(*xmpp.DiscoInfo)
info, ok := iq.Payload.(*xmpp.DiscoInfo)
if !ok {
return
}
@ -87,17 +87,17 @@ func DiscoInfo(c xmpp.Sender, p xmpp.Packet, opts xmpp.ComponentOptions) {
switch info.Node {
case "":
DiscoInfoRoot(&iqResp, opts)
discoInfoRoot(&iqResp, opts)
case pubsubNode:
DiscoInfoPubSub(&iqResp)
discoInfoPubSub(&iqResp)
case pepNode:
DiscoInfoPEP(&iqResp)
discoInfoPEP(&iqResp)
}
_ = c.Send(iqResp)
}
func DiscoInfoRoot(iqResp *xmpp.IQ, opts xmpp.ComponentOptions) {
func discoInfoRoot(iqResp *xmpp.IQ, opts xmpp.ComponentOptions) {
// Higher level discovery
identity := xmpp.Identity{
Name: opts.Name,
@ -117,10 +117,10 @@ func DiscoInfoRoot(iqResp *xmpp.IQ, opts xmpp.ComponentOptions) {
{Var: "urn:xmpp:delegation:1"},
},
}
iqResp.AddPayload(&payload)
iqResp.Payload = &payload
}
func DiscoInfoPubSub(iqResp *xmpp.IQ) {
func discoInfoPubSub(iqResp *xmpp.IQ) {
payload := xmpp.DiscoInfo{
XMLName: xml.Name{
Space: xmpp.NSDiscoInfo,
@ -134,10 +134,10 @@ func DiscoInfoPubSub(iqResp *xmpp.IQ) {
{Var: "http://jabber.org/protocol/pubsub#publish-options"},
},
}
iqResp.AddPayload(&payload)
iqResp.Payload = &payload
}
func DiscoInfoPEP(iqResp *xmpp.IQ) {
func discoInfoPEP(iqResp *xmpp.IQ) {
identity := xmpp.Identity{
Category: "pubsub",
Type: "pep",
@ -163,18 +163,17 @@ func DiscoInfoPEP(iqResp *xmpp.IQ) {
{Var: "http://jabber.org/protocol/pubsub#subscribe"},
},
}
iqResp.AddPayload(&payload)
iqResp.Payload = &payload
}
func HandleDelegation(s xmpp.Sender, p xmpp.Packet) {
func handleDelegation(s xmpp.Sender, p xmpp.Packet) {
// Type conversion & sanity checks
iq, ok := p.(xmpp.IQ)
if !ok {
return
}
payload1 := iq.Payload[0]
delegation, ok := payload1.(*xmpp.Delegation)
delegation, ok := iq.Payload.(*xmpp.Delegation)
if !ok {
return
}
@ -184,12 +183,8 @@ func HandleDelegation(s xmpp.Sender, p xmpp.Packet) {
if !ok {
return
}
payload := forwardedIQ.Payload
if len(payload) == 0 {
return
}
pubsub, ok := payload[0].(*xmpp.PubSub)
pubsub, ok := forwardedIQ.Payload.(*xmpp.PubSub)
if !ok {
// We only support pubsub delegation
return
@ -204,7 +199,7 @@ func HandleDelegation(s xmpp.Sender, p xmpp.Packet) {
Local: "pubsub",
},
}
iqResp.AddPayload(&payload)
iqResp.Payload = &payload
// Wrap the reply in delegation 'forward'
iqForward := xmpp.NewIQ("result", iq.To, iq.From, iq.Id, "en")
delegPayload := xmpp.Delegation{
@ -220,7 +215,7 @@ func HandleDelegation(s xmpp.Sender, p xmpp.Packet) {
Stanza: iqResp,
},
}
iqForward.AddPayload(&delegPayload)
iqForward.Payload = &delegPayload
_ = s.Send(iqForward)
// TODO: The component should actually broadcast the mood to subscribers
}

View File

@ -19,18 +19,18 @@ func main() {
}
router := xmpp.NewRouter()
router.HandleFunc("message", HandleMessage)
router.HandleFunc("message", handleMessage)
router.NewRoute().
IQNamespaces(xmpp.NSDiscoInfo).
HandlerFunc(func(s xmpp.Sender, p xmpp.Packet) {
DiscoInfo(s, p, opts)
discoInfo(s, p, opts)
})
router.NewRoute().
IQNamespaces(xmpp.NSDiscoItems).
HandlerFunc(DiscoItems)
HandlerFunc(discoItems)
router.NewRoute().
IQNamespaces("jabber:iq:version").
HandlerFunc(HandleVersion)
HandlerFunc(handleVersion)
component, err := xmpp.NewComponent(opts, router)
if err != nil {
@ -44,7 +44,7 @@ func main() {
log.Fatal(cm.Run())
}
func HandleMessage(_ xmpp.Sender, p xmpp.Packet) {
func handleMessage(_ xmpp.Sender, p xmpp.Packet) {
msg, ok := p.(xmpp.Message)
if !ok {
return
@ -52,7 +52,7 @@ func HandleMessage(_ xmpp.Sender, p xmpp.Packet) {
fmt.Println("Received message:", msg.Body)
}
func DiscoInfo(c xmpp.Sender, p xmpp.Packet, opts xmpp.ComponentOptions) {
func discoInfo(c xmpp.Sender, p xmpp.Packet, opts xmpp.ComponentOptions) {
// Type conversion & sanity checks
iq, ok := p.(xmpp.IQ)
if !ok {
@ -78,22 +78,19 @@ func DiscoInfo(c xmpp.Sender, p xmpp.Packet, opts xmpp.ComponentOptions) {
{Var: "urn:xmpp:delegation:1"},
},
}
iqResp.AddPayload(&payload)
iqResp.Payload = &payload
_ = c.Send(iqResp)
}
// TODO: Handle iq error responses
func DiscoItems(c xmpp.Sender, p xmpp.Packet) {
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)
discoItems, ok := iq.Payload.(*xmpp.DiscoItems)
if !ok {
return
}
@ -112,11 +109,11 @@ func DiscoItems(c xmpp.Sender, p xmpp.Packet) {
},
}
}
iqResp.AddPayload(&payload)
iqResp.Payload = &payload
_ = c.Send(iqResp)
}
func HandleVersion(c xmpp.Sender, p xmpp.Packet) {
func handleVersion(c xmpp.Sender, p xmpp.Packet) {
// Type conversion & sanity checks
iq, ok := p.(xmpp.IQ)
if !ok {
@ -127,6 +124,6 @@ func HandleVersion(c xmpp.Sender, p xmpp.Packet) {
var payload xmpp.Version
payload.Name = "Fluux XMPP Component"
payload.Version = "0.0.1"
iq.AddPayload(&payload)
iq.Payload = &payload
_ = c.Send(iqResp)
}

View File

@ -22,7 +22,7 @@ func main() {
}
router := xmpp.NewRouter()
router.HandleFunc("message", HandleMessage)
router.HandleFunc("message", handleMessage)
client, err := xmpp.NewClient(config, router)
if err != nil {
@ -35,7 +35,7 @@ func main() {
log.Fatal(cm.Run())
}
func HandleMessage(s xmpp.Sender, p xmpp.Packet) {
func handleMessage(s xmpp.Sender, p xmpp.Packet) {
msg, ok := p.(xmpp.Message)
if !ok {
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)

View File

@ -23,45 +23,61 @@ func main() {
address := flag.String("address", "", "If needed, XMPP server DNSName or IP and optional port (ie myserver:5222)")
flag.Parse()
var client *xmpp.Client
var err error
if client, err = connectXmpp(*jid, *password, *address); err != nil {
log.Fatal("Could not connect to XMPP: ", err)
}
p, err := mpg123.NewPlayer()
// 1. Create mpg player
player, err := mpg123.NewPlayer()
if err != nil {
log.Fatal(err)
}
// Iterator to receive packets coming from our XMPP connection
for packet := range client.Recv() {
switch packet := packet.(type) {
case xmpp.Message:
processMessage(client, p, &packet)
case xmpp.IQ:
processIq(client, p, &packet)
case xmpp.Presence:
// Do nothing with received presence
default:
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", packet)
}
}
// 2. Prepare XMPP client
config := xmpp.Config{
Address: *address,
Jid: *jid,
Password: *password,
Insecure: true,
}
func processMessage(client *xmpp.Client, p *mpg123.Player, packet *xmpp.Message) {
command := strings.Trim(packet.Body, " ")
router := xmpp.NewRouter()
router.NewRoute().
Packet("message").
HandlerFunc(func(s xmpp.Sender, p xmpp.Packet) {
handleMessage(s, p, player)
})
router.NewRoute().
Packet("message").
HandlerFunc(func(s xmpp.Sender, p xmpp.Packet) {
handleIQ(s, p, player)
})
client, err := xmpp.NewClient(config, router)
if err != nil {
log.Fatalf("%+v", err)
}
cm := xmpp.NewStreamManager(client, nil)
log.Fatal(cm.Run())
}
func handleMessage(s xmpp.Sender, p xmpp.Packet, player *mpg123.Player) {
msg, ok := p.(xmpp.Message)
if !ok {
return
}
command := strings.Trim(msg.Body, " ")
if command == "stop" {
p.Stop()
player.Stop()
} else {
playSCURL(p, command)
sendUserTune(client, "Radiohead", "Spectre")
playSCURL(player, command)
sendUserTune(s, "Radiohead", "Spectre")
}
}
func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) {
switch payload := packet.Payload[0].(type) {
func handleIQ(s xmpp.Sender, p xmpp.Packet, player *mpg123.Player) {
iq, ok := p.(xmpp.IQ)
if !ok {
return
}
switch payload := iq.Payload.(type) {
// We support IOT Control IQ
case *xmpp.ControlSet:
var url string
@ -72,24 +88,24 @@ func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) {
}
}
playSCURL(p, url)
playSCURL(player, url)
setResponse := new(xmpp.ControlSetResponse)
// FIXME: Broken
reply := xmpp.IQ{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "result", Id: packet.Id}, Payload: []xmpp.IQPayload{setResponse}}
_ = client.Send(reply)
reply := xmpp.IQ{PacketAttrs: xmpp.PacketAttrs{To: iq.From, Type: "result", Id: iq.Id}, Payload: setResponse}
_ = s.Send(reply)
// TODO add Soundclound artist / title retrieval
sendUserTune(client, "Radiohead", "Spectre")
sendUserTune(s, "Radiohead", "Spectre")
default:
_, _ = fmt.Fprintf(os.Stdout, "Other IQ Payload: %T\n", packet.Payload)
_, _ = fmt.Fprintf(os.Stdout, "Other IQ Payload: %T\n", iq.Payload)
}
}
func sendUserTune(client *xmpp.Client, artist string, title string) {
func sendUserTune(s xmpp.Sender, artist string, title string) {
tune := xmpp.Tune{Artist: artist, Title: title}
iq := xmpp.NewIQ("set", "", "", "usertune-1", "en")
payload := xmpp.PubSub{Publish: &xmpp.Publish{Node: "http://jabber.org/protocol/tune", Item: xmpp.Item{Tune: &tune}}}
iq.AddPayload(&payload)
_ = client.Send(iq)
iq.Payload = &payload
_ = s.Send(iq)
}
func playSCURL(p *mpg123.Player, rawURL string) {
@ -100,20 +116,6 @@ func playSCURL(p *mpg123.Player, rawURL string) {
_ = p.Play(url)
}
func connectXmpp(jid string, password string, address string) (client *xmpp.Client, err error) {
xmppConfig := xmpp.Config{Address: address,
Jid: jid, Password: password, PacketLogger: os.Stdout, Insecure: true}
if client, err = xmpp.NewClient(xmppConfig); err != nil {
return
}
if err = client.Connect(); err != nil {
return
}
return
}
// TODO
// - Have a player API to play, play next, or add to queue
// - Have the ability to parse custom packet to play sound