Merge pull request #148 from remicorniere/ResultSetsRework

- Changed IQ stanzas to pointer semantics
- Fixed commands from v 0.4.0 and tests
- Added primitive Result Sets support (XEP-0059)
- Tests for Result sets are not implemented yet. Result sets seem to be fairly unused across servers and is a little weird to test without a specific implementing XEP like XEP-0313; because the implementations are different across XEPs. Therefore, as 313 is coming, I'll update the tests for XEP-0059 with it.
This commit is contained in:
remicorniere
2020-01-31 11:18:36 +00:00
committed by GitHub
54 changed files with 673 additions and 229 deletions

View File

@@ -9,7 +9,10 @@ import (
)
func main() {
iq := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeGet, To: "service.localhost", Id: "custom-pl-1"})
iq, err := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeGet, To: "service.localhost", Id: "custom-pl-1"})
if err != nil {
log.Fatalf("failed to create IQ: %v", err)
}
payload := CustomPayload{XMLName: xml.Name{Space: "my:custom:payload", Local: "query"}, Node: "test"}
iq.Payload = payload
@@ -45,5 +48,5 @@ func (c CustomPayload) Namespace() string {
}
func init() {
stanza.TypeRegistry.MapExtension(stanza.PKTIQ, xml.Name{"my:custom:payload", "query"}, CustomPayload{})
stanza.TypeRegistry.MapExtension(stanza.PKTIQ, xml.Name{Space: "my:custom:payload", Local: "query"}, CustomPayload{})
}

View File

@@ -35,7 +35,9 @@ func main() {
IQNamespaces("urn:xmpp:delegation:1").
HandlerFunc(handleDelegation)
component, err := xmpp.NewComponent(opts, router)
component, err := xmpp.NewComponent(opts, router, func(err error) {
log.Println(err)
})
if err != nil {
log.Fatalf("%+v", err)
}
@@ -78,7 +80,7 @@ const (
// ctx.Opts
func discoInfo(c xmpp.Sender, p stanza.Packet, opts xmpp.ComponentOptions) {
// Type conversion & sanity checks
iq, ok := p.(stanza.IQ)
iq, ok := p.(*stanza.IQ)
if !ok {
return
}
@@ -87,15 +89,18 @@ func discoInfo(c xmpp.Sender, p stanza.Packet, opts xmpp.ComponentOptions) {
return
}
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id})
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)
}
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)
@@ -155,7 +160,7 @@ func discoInfoPEP(iqResp *stanza.IQ) {
func handleDelegation(s xmpp.Sender, p stanza.Packet) {
// Type conversion & sanity checks
iq, ok := p.(stanza.IQ)
iq, ok := p.(*stanza.IQ)
if !ok {
return
}
@@ -166,7 +171,7 @@ func handleDelegation(s xmpp.Sender, p stanza.Packet) {
}
forwardedPacket := delegation.Forwarded.Stanza
fmt.Println(forwardedPacket)
forwardedIQ, ok := forwardedPacket.(stanza.IQ)
forwardedIQ, ok := forwardedPacket.(*stanza.IQ)
if !ok {
return
}
@@ -179,7 +184,10 @@ func handleDelegation(s xmpp.Sender, p stanza.Packet) {
if pubsub.Publish.XMLName.Local == "publish" {
// Prepare pubsub IQ reply
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: forwardedIQ.To, To: forwardedIQ.From, Id: forwardedIQ.Id})
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)
}
payload := stanza.PubSubGeneric{
XMLName: xml.Name{
Space: "http://jabber.org/protocol/pubsub",
@@ -188,7 +196,10 @@ func handleDelegation(s xmpp.Sender, p stanza.Packet) {
}
iqResp.Payload = &payload
// Wrap the reply in delegation 'forward'
iqForward := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id})
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)
}
delegPayload := stanza.Delegation{
XMLName: xml.Name{
Space: "urn:xmpp:delegation:1",

View File

@@ -5,7 +5,7 @@ go 1.13
require (
github.com/processone/mpg123 v1.0.0
github.com/processone/soundcloud v1.0.0
gosrc.io/xmpp v0.1.1
gosrc.io/xmpp v0.4.0
)
replace gosrc.io/xmpp => ./../

View File

@@ -6,5 +6,5 @@ require (
github.com/awesome-gocui/gocui v0.6.1-0.20191115151952-a34ffb055986
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.1
gosrc.io/xmpp v0.3.1-0.20191223080939-f8f820170e08
gosrc.io/xmpp v0.4.0
)

View File

@@ -186,7 +186,7 @@ func startClient(g *gocui.Gui, config *config) {
// ==========================
// Start working
updateRosterFromConfig(g, config)
updateRosterFromConfig(config)
// Sending the default contact in a channel. Default value is the first contact in the list from the config.
viewState.currentContact = strings.Split(config.Contacts, configContactSep)[0]
// Informing user of the default contact
@@ -283,7 +283,7 @@ func errorHandler(err error) {
// Read the client roster from the config. This does not check with the server that the roster is correct.
// If user tries to send a message to someone not registered with the server, the server will return an error.
func updateRosterFromConfig(g *gocui.Gui, config *config) {
func updateRosterFromConfig(config *config) {
viewState.contacts = append(strings.Split(config.Contacts, configContactSep), backFromContacts)
// Put a "go back" button at the end of the list
viewState.contacts = append(viewState.contacts, backFromContacts)

View File

@@ -61,12 +61,16 @@ func handleMessage(_ xmpp.Sender, p stanza.Packet) {
func discoInfo(c xmpp.Sender, p stanza.Packet, opts xmpp.ComponentOptions) {
// Type conversion & sanity checks
iq, ok := p.(stanza.IQ)
iq, ok := p.(*stanza.IQ)
if !ok || iq.Type != stanza.IQTypeGet {
return
}
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
iqResp, err := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
// TODO: fix this...
if err != nil {
return
}
disco := iqResp.DiscoInfo()
disco.AddIdentity(opts.Name, opts.Category, opts.Type)
disco.AddFeatures(stanza.NSDiscoInfo, stanza.NSDiscoItems, "jabber:iq:version", "urn:xmpp:delegation:1")
@@ -76,7 +80,7 @@ func discoInfo(c xmpp.Sender, p stanza.Packet, opts xmpp.ComponentOptions) {
// TODO: Handle iq error responses
func discoItems(c xmpp.Sender, p stanza.Packet) {
// Type conversion & sanity checks
iq, ok := p.(stanza.IQ)
iq, ok := p.(*stanza.IQ)
if !ok || iq.Type != stanza.IQTypeGet {
return
}
@@ -86,7 +90,11 @@ func discoItems(c xmpp.Sender, p stanza.Packet) {
return
}
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
// TODO: fix this...
iqResp, err := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
if err != nil {
return
}
items := iqResp.DiscoItems()
if discoItems.Node == "" {
@@ -97,12 +105,15 @@ func discoItems(c xmpp.Sender, p stanza.Packet) {
func handleVersion(c xmpp.Sender, p stanza.Packet) {
// Type conversion & sanity checks
iq, ok := p.(stanza.IQ)
iq, ok := p.(*stanza.IQ)
if !ok {
return
}
iqResp := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
iqResp, err := stanza.NewIQ(stanza.Attrs{Type: "result", From: iq.To, To: iq.From, Id: iq.Id, Lang: "en"})
if err != nil {
return
}
iqResp.Version().SetInfo("Fluux XMPP Component", "0.0.1", "")
_ = c.Send(iqResp)
}

View File

@@ -9,9 +9,10 @@ Connect to an XMPP server using XEP 114 protocol, perform a discovery query on t
import (
"context"
"fmt"
"log"
"time"
xmpp "gosrc.io/xmpp"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
)
@@ -53,10 +54,13 @@ func main() {
}
// make a disco iq
iqReq := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeGet,
iqReq, err := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeGet,
From: domain,
To: "localhost",
Id: "my-iq1"})
if err != nil {
log.Fatalf("failed to create IQ: %v", err)
}
disco := iqReq.DiscoInfo()
iqReq.Payload = disco

View File

@@ -81,7 +81,7 @@ func handleMessage(s xmpp.Sender, p stanza.Packet, player *mpg123.Player) {
}
func handleIQ(s xmpp.Sender, p stanza.Packet, player *mpg123.Player) {
iq, ok := p.(stanza.IQ)
iq, ok := p.(*stanza.IQ)
if !ok {
return
}
@@ -100,7 +100,7 @@ func handleIQ(s xmpp.Sender, p stanza.Packet, player *mpg123.Player) {
setResponse := new(stanza.ControlSetResponse)
// FIXME: Broken
reply := stanza.IQ{Attrs: stanza.Attrs{To: iq.From, Type: "result", Id: iq.Id}, Payload: setResponse}
_ = s.Send(reply)
_ = s.Send(&reply)
// TODO add Soundclound artist / title retrieval
sendUserTune(s, "Radiohead", "Spectre")
default:

View File

@@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/xml"
"errors"
"fmt"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
@@ -17,6 +18,8 @@ const (
serviceName = "pubsub.localhost"
)
var invalidResp = errors.New("invalid response")
func main() {
config := xmpp.Config{
@@ -52,7 +55,7 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
createNode(ctx, cancel, client)
// =============================
// ================================================================================
// Configure the node. This can also be done in a single message with the creation
configureNode(ctx, cancel, client)
@@ -68,10 +71,17 @@ func main() {
// Let's purge the node :
purgeRq, _ := stanza.NewPurgeAllItems(serviceName, nodeName)
purgeCh, err := client.SendIQ(ctx, purgeRq)
if err != nil {
log.Fatalf("could not send purge request: %v", err)
}
select {
case purgeResp := <-purgeCh:
if purgeResp.Error != nil {
if purgeResp.Type == stanza.IQTypeError {
cancel()
if vld, err := purgeResp.IsValid(); !vld {
log.Fatalf(invalidResp.Error()+" %v"+" reason: %v", purgeResp, err)
}
log.Fatalf("error while purging node : %s", purgeResp.Error.Text)
}
log.Println("node successfully purged")
@@ -97,7 +107,10 @@ func createNode(ctx context.Context, cancel context.CancelFunc, client *xmpp.Cli
select {
case respCr := <-createCh:
// Got response from server
if respCr.Error != nil {
if respCr.Type == stanza.IQTypeError {
if vld, err := respCr.IsValid(); !vld {
log.Fatalf(invalidResp.Error()+" %+v"+" reason: %s", respCr, err)
}
if respCr.Error.Reason != "conflict" {
log.Fatalf("%+v", respCr.Error.Text)
}
@@ -148,8 +161,11 @@ func configureNode(ctx context.Context, cancel context.CancelFunc, client *xmpp.
c, _ := client.SendIQ(ctx, submitConf)
select {
case confResp := <-c:
if confResp.Error != nil {
if confResp.Type == stanza.IQTypeError {
cancel()
if vld, err := confResp.IsValid(); !vld {
log.Fatalf(invalidResp.Error()+" %v"+" reason: %v", confResp, err)
}
log.Fatalf("node configuration failed : %s", confResp.Error.Text)
}
log.Println("node configuration was successful")