fixes after xmpp lib refactored
This commit is contained in:
parent
c6f9405906
commit
ea9b107109
|
@ -21,6 +21,13 @@ func (c *Config) Start() (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
router := xmpp.NewRouter()
|
||||||
|
router.NewRoute().IQNamespaces(xmpp.NSDiscoInfo).HandlerFunc(c.handleDiscoInfo)
|
||||||
|
router.NewRoute().IQNamespaces(xmpp.NSDiscoItems).HandlerFunc(c.handleDiscoItems)
|
||||||
|
router.HandleFunc("iq", c.handleIQ)
|
||||||
|
router.HandleFunc("message", c.handleMessage)
|
||||||
|
|
||||||
c.xmpp, err = xmpp.NewComponent(xmpp.ComponentOptions{
|
c.xmpp, err = xmpp.NewComponent(xmpp.ComponentOptions{
|
||||||
Domain: c.Host,
|
Domain: c.Host,
|
||||||
Secret: c.Secret,
|
Secret: c.Secret,
|
||||||
|
@ -28,18 +35,13 @@ func (c *Config) Start() (err error) {
|
||||||
Name: c.Type,
|
Name: c.Type,
|
||||||
Category: "gateway",
|
Category: "gateway",
|
||||||
Type: "service",
|
Type: "service",
|
||||||
})
|
}, router)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cm := xmpp.NewStreamManager(c.xmpp, nil)
|
cm := xmpp.NewStreamManager(c.xmpp, nil)
|
||||||
err = cm.Start()
|
go cm.Run()
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
go c.sender(out)
|
go c.sender(out)
|
||||||
go c.receiver()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,50 +1,29 @@
|
||||||
package component
|
package component
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
|
||||||
"github.com/bdlm/log"
|
"github.com/bdlm/log"
|
||||||
"gosrc.io/xmpp"
|
"gosrc.io/xmpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Config) receiver() {
|
func (c *Config) handleDiscoInfo(s xmpp.Sender, p xmpp.Packet) {
|
||||||
for packet := range c.xmpp.Recv() {
|
iq, ok := p.(xmpp.IQ)
|
||||||
p, back := c.receiving(packet)
|
if !ok || iq.Type != "get" {
|
||||||
if p == nil {
|
return
|
||||||
continue
|
|
||||||
}
|
|
||||||
if back {
|
|
||||||
c.xmpp.Send(p)
|
|
||||||
} else {
|
|
||||||
c.comp.Send(p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Config) receiving(packet interface{}) (xmpp.Packet, bool) {
|
|
||||||
logger := log.WithField("type", c.Type)
|
|
||||||
|
|
||||||
switch p := packet.(type) {
|
|
||||||
case xmpp.IQ:
|
|
||||||
attrs := p.PacketAttrs
|
|
||||||
loggerIQ := logger.WithFields(map[string]interface{}{
|
|
||||||
"from": attrs.From,
|
|
||||||
"to": attrs.To,
|
|
||||||
})
|
|
||||||
|
|
||||||
switch inner := p.Payload[0].(type) {
|
|
||||||
case *xmpp.DiscoInfo:
|
|
||||||
if p.Type == "get" {
|
|
||||||
iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
|
|
||||||
var identity xmpp.Identity
|
|
||||||
if inner.Node == "" {
|
|
||||||
identity = xmpp.Identity{
|
|
||||||
Name: c.Type,
|
|
||||||
Category: "gateway",
|
|
||||||
Type: "service",
|
|
||||||
}
|
}
|
||||||
|
discoInfo, ok := iq.Payload.(*xmpp.DiscoInfo)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
attrs := iq.PacketAttrs
|
||||||
|
iq = xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
|
||||||
|
|
||||||
payload := xmpp.DiscoInfo{
|
payload := xmpp.DiscoInfo{
|
||||||
Identity: identity,
|
XMLName: xml.Name{
|
||||||
|
Space: xmpp.NSDiscoInfo,
|
||||||
|
Local: "query",
|
||||||
|
},
|
||||||
Features: []xmpp.Feature{
|
Features: []xmpp.Feature{
|
||||||
{Var: xmpp.NSDiscoInfo},
|
{Var: xmpp.NSDiscoInfo},
|
||||||
{Var: xmpp.NSDiscoItems},
|
{Var: xmpp.NSDiscoItems},
|
||||||
|
@ -53,54 +32,81 @@ func (c *Config) receiving(packet interface{}) (xmpp.Packet, bool) {
|
||||||
{Var: xmpp.NSMsgChatStateNotifications},
|
{Var: xmpp.NSMsgChatStateNotifications},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
iq.AddPayload(&payload)
|
if discoInfo.Node == "" {
|
||||||
loggerIQ.Debug("disco info")
|
payload.Identity = xmpp.Identity{
|
||||||
return iq, true
|
Name: c.Type,
|
||||||
|
Category: "gateway",
|
||||||
|
Type: "service",
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
iq.Payload = &payload
|
||||||
|
log.WithFields(map[string]interface{}{
|
||||||
|
"type": c.Type,
|
||||||
|
"from": s,
|
||||||
|
"to": attrs.To,
|
||||||
|
}).Debug("disco info")
|
||||||
|
s.Send(iq)
|
||||||
|
}
|
||||||
|
|
||||||
case *xmpp.DiscoItems:
|
func (c *Config) handleDiscoItems(s xmpp.Sender, p xmpp.Packet) {
|
||||||
if p.Type == "get" {
|
iq, ok := p.(xmpp.IQ)
|
||||||
iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
|
if !ok || iq.Type != "get" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
discoItems, ok := iq.Payload.(*xmpp.DiscoItems)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
attrs := iq.PacketAttrs
|
||||||
|
iq = xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
|
||||||
|
|
||||||
var payload xmpp.DiscoItems
|
payload := xmpp.DiscoItems{}
|
||||||
if inner.Node == "" {
|
if discoItems.Node == "" {
|
||||||
payload = xmpp.DiscoItems{
|
payload.Items = []xmpp.DiscoItem{
|
||||||
Items: []xmpp.DiscoItem{
|
|
||||||
{Name: c.Type, JID: c.Host, Node: "node1"},
|
{Name: c.Type, JID: c.Host, Node: "node1"},
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
iq.AddPayload(&payload)
|
iq.Payload = &payload
|
||||||
loggerIQ.Debug("disco items")
|
|
||||||
return iq, true
|
log.WithFields(map[string]interface{}{
|
||||||
|
"type": c.Type,
|
||||||
|
"from": s,
|
||||||
|
"to": attrs.To,
|
||||||
|
}).Debug("disco items")
|
||||||
|
s.Send(iq)
|
||||||
|
}
|
||||||
|
func (c *Config) handleIQ(s xmpp.Sender, p xmpp.Packet) {
|
||||||
|
iq, ok := p.(xmpp.IQ)
|
||||||
|
if !ok || iq.Type != "get" {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
logger.Debug("ignoring iq packet", inner)
|
|
||||||
xError := xmpp.Err{
|
xError := xmpp.Err{
|
||||||
Code: 501,
|
Code: 501,
|
||||||
Reason: "feature-not-implemented",
|
Reason: "feature-not-implemented",
|
||||||
Type: "cancel",
|
Type: "cancel",
|
||||||
}
|
}
|
||||||
reply := p.MakeError(xError)
|
resp := iq.MakeError(xError)
|
||||||
|
attrs := iq.PacketAttrs
|
||||||
|
|
||||||
return reply, true
|
log.WithFields(map[string]interface{}{
|
||||||
}
|
"type": c.Type,
|
||||||
|
"from": s,
|
||||||
case xmpp.Message:
|
"to": attrs.To,
|
||||||
if c.XMPPDebug {
|
}).Debugf("ignore: %s", iq.Payload)
|
||||||
logger.WithFields(map[string]interface{}{
|
s.Send(resp)
|
||||||
"from": p.PacketAttrs.From,
|
}
|
||||||
"to": p.PacketAttrs.To,
|
func (c *Config) handleMessage(s xmpp.Sender, p xmpp.Packet) {
|
||||||
"id": p.PacketAttrs.Id,
|
msg, ok := p.(xmpp.Message)
|
||||||
}).Debug(p.XMPPFormat())
|
if !ok {
|
||||||
}
|
return
|
||||||
return p, false
|
}
|
||||||
|
if c.XMPPDebug {
|
||||||
case xmpp.Presence:
|
log.WithFields(map[string]interface{}{
|
||||||
logger.Debug("received presence:", p.Type)
|
"type": c.Type,
|
||||||
|
"from": s,
|
||||||
default:
|
"to": msg.PacketAttrs.To,
|
||||||
logger.Debug("ignoring packet:", packet)
|
"id": msg.PacketAttrs.Id,
|
||||||
}
|
}).Debug(msg.XMPPFormat())
|
||||||
return nil, false
|
}
|
||||||
|
c.comp.Send(p)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,59 +7,102 @@ import (
|
||||||
"gosrc.io/xmpp"
|
"gosrc.io/xmpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type dummyComp struct {
|
||||||
|
Component
|
||||||
|
LastPacket xmpp.Packet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *dummyComp) Connect() (chan xmpp.Packet, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
func (d *dummyComp) Send(a xmpp.Packet) {
|
||||||
|
d.LastPacket = a
|
||||||
|
}
|
||||||
|
|
||||||
|
type dummyXMPP struct {
|
||||||
|
xmpp.Sender
|
||||||
|
LastPacket xmpp.Packet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *dummyXMPP) Send(a xmpp.Packet) error {
|
||||||
|
d.LastPacket = a
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestReceive(t *testing.T) {
|
func TestReceive(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
s := &dummyXMPP{}
|
||||||
|
|
||||||
c := Config{Host: "example.org", Type: "monkeyservice", XMPPDebug: true}
|
comp := &dummyComp{}
|
||||||
|
c := Config{
|
||||||
// ignoring packet
|
Host: "example.org",
|
||||||
p, _ := c.receiving(xmpp.Handshake{})
|
Type: "monkeyservice",
|
||||||
assert.Nil(p)
|
XMPPDebug: true,
|
||||||
|
comp: comp,
|
||||||
// receive presence
|
}
|
||||||
p, _ = c.receiving(xmpp.Presence{})
|
|
||||||
assert.Nil(p)
|
|
||||||
|
|
||||||
// message
|
// message
|
||||||
p, back := c.receiving(xmpp.Message{})
|
c.handleMessage(s, xmpp.IQ{})
|
||||||
assert.False(back)
|
assert.Nil(comp.LastPacket)
|
||||||
assert.NotNil(p)
|
|
||||||
|
c.handleMessage(s, xmpp.Message{})
|
||||||
|
_, ok := comp.LastPacket.(xmpp.Message)
|
||||||
|
assert.True(ok)
|
||||||
|
|
||||||
// unsupported iq
|
// unsupported iq
|
||||||
p, back = c.receiving(xmpp.IQ{Payload: []xmpp.IQPayload{
|
c.handleIQ(s, xmpp.IQ{})
|
||||||
&xmpp.Err{},
|
assert.Nil(s.LastPacket)
|
||||||
}})
|
|
||||||
assert.True(back)
|
c.handleIQ(s, xmpp.IQ{
|
||||||
assert.NotNil(p)
|
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||||
iq := p.(xmpp.IQ)
|
})
|
||||||
|
assert.NotNil(s.LastPacket)
|
||||||
|
iq := s.LastPacket.(xmpp.IQ)
|
||||||
assert.Equal("error", iq.Type)
|
assert.Equal("error", iq.Type)
|
||||||
assert.Equal("feature-not-implemented", iq.Error.Reason)
|
assert.Equal("feature-not-implemented", iq.Error.Reason)
|
||||||
|
s.LastPacket = nil
|
||||||
|
|
||||||
// iq disco info
|
// iq disco info
|
||||||
p, back = c.receiving(xmpp.IQ{
|
c.handleDiscoInfo(s, xmpp.IQ{
|
||||||
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
Payload: &xmpp.DiscoInfo{},
|
||||||
Payload: []xmpp.IQPayload{
|
|
||||||
&xmpp.DiscoInfo{},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
assert.True(back)
|
assert.Nil(s.LastPacket)
|
||||||
assert.NotNil(p)
|
|
||||||
iq = p.(xmpp.IQ)
|
c.handleDiscoInfo(s, xmpp.IQ{
|
||||||
|
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||||
|
})
|
||||||
|
assert.Nil(s.LastPacket)
|
||||||
|
|
||||||
|
c.handleDiscoInfo(s, xmpp.IQ{
|
||||||
|
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||||
|
Payload: &xmpp.DiscoInfo{},
|
||||||
|
})
|
||||||
|
assert.NotNil(s.LastPacket)
|
||||||
|
iq = s.LastPacket.(xmpp.IQ)
|
||||||
assert.Equal("result", iq.Type)
|
assert.Equal("result", iq.Type)
|
||||||
dinfo := iq.Payload[0].(*xmpp.DiscoInfo)
|
dinfo := iq.Payload.(*xmpp.DiscoInfo)
|
||||||
assert.Equal("monkeyservice", dinfo.Identity.Name)
|
assert.Equal("monkeyservice", dinfo.Identity.Name)
|
||||||
|
s.LastPacket = nil
|
||||||
|
|
||||||
// iq disco items
|
// iq disco items
|
||||||
p, back = c.receiving(xmpp.IQ{
|
c.handleDiscoItems(s, xmpp.IQ{
|
||||||
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
Payload: &xmpp.DiscoItems{},
|
||||||
Payload: []xmpp.IQPayload{
|
|
||||||
&xmpp.DiscoItems{},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
assert.True(back)
|
assert.Nil(s.LastPacket)
|
||||||
assert.NotNil(p)
|
|
||||||
iq = p.(xmpp.IQ)
|
c.handleDiscoItems(s, xmpp.IQ{
|
||||||
|
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||||
|
})
|
||||||
|
assert.Nil(s.LastPacket)
|
||||||
|
|
||||||
|
c.handleDiscoItems(s, xmpp.IQ{
|
||||||
|
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||||
|
Payload: &xmpp.DiscoItems{},
|
||||||
|
})
|
||||||
|
assert.NotNil(s.LastPacket)
|
||||||
|
iq = s.LastPacket.(xmpp.IQ)
|
||||||
assert.Equal("result", iq.Type)
|
assert.Equal("result", iq.Type)
|
||||||
ditems := iq.Payload[0].(*xmpp.DiscoItems)
|
ditems := iq.Payload.(*xmpp.DiscoItems)
|
||||||
assert.Equal("monkeyservice", ditems.Items[0].Name)
|
assert.Equal("monkeyservice", ditems.Items[0].Name)
|
||||||
|
s.LastPacket = nil
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue