IQ error management

This commit is contained in:
Mickael Remond
2018-01-20 18:56:07 +01:00
parent 8470c01c09
commit fb8d050a00
3 changed files with 56 additions and 62 deletions

View File

@@ -1,7 +1,6 @@
package main
import (
"encoding/xml"
"fmt"
"fluux.io/xmpp"
@@ -22,28 +21,28 @@ func main() {
switch p := packet.(type) {
case xmpp.IQ:
switch inner := p.Payload[0].(type) {
case *xmpp.Node:
fmt.Printf("%q\n", inner)
data, err := xml.Marshal(inner)
if err != nil {
fmt.Println("cannot marshall payload")
case *xmpp.DiscoInfo:
fmt.Println("Disco Info")
if p.Type == "get" {
DiscoResult(component, p.From, p.To, p.Id)
}
fmt.Println("data=", string(data))
component.processIQ(p.Type, p.Id, p.From, inner)
default:
fmt.Println("default")
fmt.Println("ignoring iq packet", inner)
xerror := xmpp.Err{
Code: 501,
Reason: "feature-not-implemented",
Type: "cancel",
}
reply := p.MakeError(xerror)
component.xmpp.Send(&reply)
}
default:
fmt.Println("Packet unhandled packet:", packet)
fmt.Println("ignoring packet:", packet)
}
}
}
const (
NSDiscoInfo = "http://jabber.org/protocol/disco#info"
)
type MyComponent struct {
Name string
// Typical categories and types: https://xmpp.org/registrar/disco-categories.html
@@ -53,35 +52,19 @@ type MyComponent struct {
xmpp *xmpp.Component
}
func (c MyComponent) processIQ(iqType, id, from string, inner *xmpp.Node) {
fmt.Println("Node:", inner.XMLName.Space, inner.XMLName.Local)
switch inner.XMLName.Space + " " + iqType {
case NSDiscoInfo + " get":
fmt.Println("Send Disco Info")
iq := xmpp.NewIQ("result", "admin@localhost", "test@localhost", "1", "en")
payload := xmpp.DiscoInfo{
Identity: xmpp.Identity{
Name: "Test Gateway",
Category: "gateway",
Type: "mqtt",
},
Features: []xmpp.Feature{
{Var: "http://jabber.org/protocol/disco#info"},
{Var: "http://jabber.org/protocol/disco#item"},
},
}
iq.AddPayload(&payload)
c.xmpp.Send(iq)
default:
iqErr := fmt.Sprintf(`<iq type='error'
from='%s'
to='%s'
id='%s'>
<error type="cancel" code="501">
<feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
</iq>`, c.xmpp.Host, from, id)
c.xmpp.SendOld(iqErr) // FIXME Remove that method
func DiscoResult(c MyComponent, from, to, id string) {
iq := xmpp.NewIQ("result", to, from, id, "en")
payload := xmpp.DiscoInfo{
Identity: xmpp.Identity{
Name: c.Name,
Category: c.Category,
Type: c.Type,
},
Features: []xmpp.Feature{
{Var: "http://jabber.org/protocol/disco#info"},
{Var: "http://jabber.org/protocol/disco#item"},
},
}
iq.AddPayload(&payload)
c.xmpp.Send(iq)
}