update xmpp lib
This commit is contained in:
parent
ea9b107109
commit
c38379e37e
|
@ -2,6 +2,7 @@ package component
|
|||
|
||||
import (
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -23,8 +24,8 @@ func (c *Config) Start() (err error) {
|
|||
}
|
||||
|
||||
router := xmpp.NewRouter()
|
||||
router.NewRoute().IQNamespaces(xmpp.NSDiscoInfo).HandlerFunc(c.handleDiscoInfo)
|
||||
router.NewRoute().IQNamespaces(xmpp.NSDiscoItems).HandlerFunc(c.handleDiscoItems)
|
||||
router.NewRoute().IQNamespaces(stanza.NSDiscoInfo).HandlerFunc(c.handleDiscoInfo)
|
||||
router.NewRoute().IQNamespaces(stanza.NSDiscoItems).HandlerFunc(c.handleDiscoItems)
|
||||
router.HandleFunc("iq", c.handleIQ)
|
||||
router.HandleFunc("message", c.handleMessage)
|
||||
|
||||
|
|
|
@ -2,12 +2,12 @@ package component
|
|||
|
||||
import (
|
||||
"github.com/bdlm/log"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
type Component interface {
|
||||
Connect() (chan xmpp.Packet, error)
|
||||
Send(xmpp.Packet)
|
||||
Connect() (chan stanza.Packet, error)
|
||||
Send(stanza.Packet)
|
||||
}
|
||||
|
||||
// Connect function with config to get DB connection interface
|
||||
|
|
|
@ -5,39 +5,40 @@ import (
|
|||
|
||||
"github.com/bdlm/log"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func (c *Config) handleDiscoInfo(s xmpp.Sender, p xmpp.Packet) {
|
||||
iq, ok := p.(xmpp.IQ)
|
||||
func (c *Config) handleDiscoInfo(s xmpp.Sender, p stanza.Packet) {
|
||||
iq, ok := p.(stanza.IQ)
|
||||
if !ok || iq.Type != "get" {
|
||||
return
|
||||
}
|
||||
discoInfo, ok := iq.Payload.(*xmpp.DiscoInfo)
|
||||
discoInfo, ok := iq.Payload.(*stanza.DiscoInfo)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
attrs := iq.PacketAttrs
|
||||
iq = xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
|
||||
attrs := iq.Attrs
|
||||
iq = stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, To: attrs.From, From: attrs.To, Id: attrs.Id})
|
||||
|
||||
payload := xmpp.DiscoInfo{
|
||||
payload := stanza.DiscoInfo{
|
||||
XMLName: xml.Name{
|
||||
Space: xmpp.NSDiscoInfo,
|
||||
Space: stanza.NSDiscoInfo,
|
||||
Local: "query",
|
||||
},
|
||||
Features: []xmpp.Feature{
|
||||
{Var: xmpp.NSDiscoInfo},
|
||||
{Var: xmpp.NSDiscoItems},
|
||||
{Var: xmpp.NSMsgReceipts},
|
||||
{Var: xmpp.NSMsgChatMarkers},
|
||||
{Var: xmpp.NSMsgChatStateNotifications},
|
||||
Features: []stanza.Feature{
|
||||
{Var: stanza.NSDiscoInfo},
|
||||
{Var: stanza.NSDiscoItems},
|
||||
{Var: stanza.NSMsgReceipts},
|
||||
{Var: stanza.NSMsgChatMarkers},
|
||||
{Var: stanza.NSMsgChatStateNotifications},
|
||||
},
|
||||
}
|
||||
if discoInfo.Node == "" {
|
||||
payload.Identity = xmpp.Identity{
|
||||
payload.Identity = append(payload.Identity, stanza.Identity{
|
||||
Name: c.Type,
|
||||
Category: "gateway",
|
||||
Type: "service",
|
||||
}
|
||||
})
|
||||
}
|
||||
iq.Payload = &payload
|
||||
log.WithFields(map[string]interface{}{
|
||||
|
@ -48,21 +49,21 @@ func (c *Config) handleDiscoInfo(s xmpp.Sender, p xmpp.Packet) {
|
|||
s.Send(iq)
|
||||
}
|
||||
|
||||
func (c *Config) handleDiscoItems(s xmpp.Sender, p xmpp.Packet) {
|
||||
iq, ok := p.(xmpp.IQ)
|
||||
func (c *Config) handleDiscoItems(s xmpp.Sender, p stanza.Packet) {
|
||||
iq, ok := p.(stanza.IQ)
|
||||
if !ok || iq.Type != "get" {
|
||||
return
|
||||
}
|
||||
discoItems, ok := iq.Payload.(*xmpp.DiscoItems)
|
||||
discoItems, ok := iq.Payload.(*stanza.DiscoItems)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
attrs := iq.PacketAttrs
|
||||
iq = xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
|
||||
attrs := iq.Attrs
|
||||
iq = stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, To: attrs.From, From: attrs.To, Id: attrs.Id})
|
||||
|
||||
payload := xmpp.DiscoItems{}
|
||||
payload := stanza.DiscoItems{}
|
||||
if discoItems.Node == "" {
|
||||
payload.Items = []xmpp.DiscoItem{
|
||||
payload.Items = []stanza.DiscoItem{
|
||||
{Name: c.Type, JID: c.Host, Node: "node1"},
|
||||
}
|
||||
}
|
||||
|
@ -75,18 +76,18 @@ func (c *Config) handleDiscoItems(s xmpp.Sender, p xmpp.Packet) {
|
|||
}).Debug("disco items")
|
||||
s.Send(iq)
|
||||
}
|
||||
func (c *Config) handleIQ(s xmpp.Sender, p xmpp.Packet) {
|
||||
iq, ok := p.(xmpp.IQ)
|
||||
func (c *Config) handleIQ(s xmpp.Sender, p stanza.Packet) {
|
||||
iq, ok := p.(stanza.IQ)
|
||||
if !ok || iq.Type != "get" {
|
||||
return
|
||||
}
|
||||
xError := xmpp.Err{
|
||||
xError := stanza.Err{
|
||||
Code: 501,
|
||||
Reason: "feature-not-implemented",
|
||||
Type: "cancel",
|
||||
}
|
||||
resp := iq.MakeError(xError)
|
||||
attrs := iq.PacketAttrs
|
||||
attrs := iq.Attrs
|
||||
|
||||
log.WithFields(map[string]interface{}{
|
||||
"type": c.Type,
|
||||
|
@ -95,8 +96,8 @@ func (c *Config) handleIQ(s xmpp.Sender, p xmpp.Packet) {
|
|||
}).Debugf("ignore: %s", iq.Payload)
|
||||
s.Send(resp)
|
||||
}
|
||||
func (c *Config) handleMessage(s xmpp.Sender, p xmpp.Packet) {
|
||||
msg, ok := p.(xmpp.Message)
|
||||
func (c *Config) handleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||
msg, ok := p.(stanza.Message)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
@ -104,8 +105,8 @@ func (c *Config) handleMessage(s xmpp.Sender, p xmpp.Packet) {
|
|||
log.WithFields(map[string]interface{}{
|
||||
"type": c.Type,
|
||||
"from": s,
|
||||
"to": msg.PacketAttrs.To,
|
||||
"id": msg.PacketAttrs.Id,
|
||||
"to": msg.To,
|
||||
"id": msg.Id,
|
||||
}).Debug(msg.XMPPFormat())
|
||||
}
|
||||
c.comp.Send(p)
|
||||
|
|
|
@ -5,26 +5,27 @@ import (
|
|||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
type dummyComp struct {
|
||||
Component
|
||||
LastPacket xmpp.Packet
|
||||
LastPacket stanza.Packet
|
||||
}
|
||||
|
||||
func (d *dummyComp) Connect() (chan xmpp.Packet, error) {
|
||||
func (d *dummyComp) Connect() (chan stanza.Packet, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (d *dummyComp) Send(a xmpp.Packet) {
|
||||
func (d *dummyComp) Send(a stanza.Packet) {
|
||||
d.LastPacket = a
|
||||
}
|
||||
|
||||
type dummyXMPP struct {
|
||||
xmpp.Sender
|
||||
LastPacket xmpp.Packet
|
||||
LastPacket stanza.Packet
|
||||
}
|
||||
|
||||
func (d *dummyXMPP) Send(a xmpp.Packet) error {
|
||||
func (d *dummyXMPP) Send(a stanza.Packet) error {
|
||||
d.LastPacket = a
|
||||
return nil
|
||||
}
|
||||
|
@ -42,67 +43,67 @@ func TestReceive(t *testing.T) {
|
|||
}
|
||||
|
||||
// message
|
||||
c.handleMessage(s, xmpp.IQ{})
|
||||
c.handleMessage(s, stanza.IQ{})
|
||||
assert.Nil(comp.LastPacket)
|
||||
|
||||
c.handleMessage(s, xmpp.Message{})
|
||||
_, ok := comp.LastPacket.(xmpp.Message)
|
||||
c.handleMessage(s, stanza.Message{})
|
||||
_, ok := comp.LastPacket.(stanza.Message)
|
||||
assert.True(ok)
|
||||
|
||||
// unsupported iq
|
||||
c.handleIQ(s, xmpp.IQ{})
|
||||
c.handleIQ(s, stanza.IQ{})
|
||||
assert.Nil(s.LastPacket)
|
||||
|
||||
c.handleIQ(s, xmpp.IQ{
|
||||
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||
c.handleIQ(s, stanza.IQ{
|
||||
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
|
||||
})
|
||||
assert.NotNil(s.LastPacket)
|
||||
iq := s.LastPacket.(xmpp.IQ)
|
||||
assert.Equal("error", iq.Type)
|
||||
iq := s.LastPacket.(stanza.IQ)
|
||||
assert.Equal(stanza.IQTypeError, iq.Type)
|
||||
assert.Equal("feature-not-implemented", iq.Error.Reason)
|
||||
s.LastPacket = nil
|
||||
|
||||
// iq disco info
|
||||
c.handleDiscoInfo(s, xmpp.IQ{
|
||||
Payload: &xmpp.DiscoInfo{},
|
||||
c.handleDiscoInfo(s, stanza.IQ{
|
||||
Payload: &stanza.DiscoInfo{},
|
||||
})
|
||||
assert.Nil(s.LastPacket)
|
||||
|
||||
c.handleDiscoInfo(s, xmpp.IQ{
|
||||
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||
c.handleDiscoInfo(s, stanza.IQ{
|
||||
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
|
||||
})
|
||||
assert.Nil(s.LastPacket)
|
||||
|
||||
c.handleDiscoInfo(s, xmpp.IQ{
|
||||
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||
Payload: &xmpp.DiscoInfo{},
|
||||
c.handleDiscoInfo(s, stanza.IQ{
|
||||
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
|
||||
Payload: &stanza.DiscoInfo{},
|
||||
})
|
||||
assert.NotNil(s.LastPacket)
|
||||
iq = s.LastPacket.(xmpp.IQ)
|
||||
assert.Equal("result", iq.Type)
|
||||
dinfo := iq.Payload.(*xmpp.DiscoInfo)
|
||||
assert.Equal("monkeyservice", dinfo.Identity.Name)
|
||||
iq = s.LastPacket.(stanza.IQ)
|
||||
assert.Equal(stanza.IQTypeResult, iq.Type)
|
||||
dinfo := iq.Payload.(*stanza.DiscoInfo)
|
||||
assert.Equal("monkeyservice", dinfo.Identity[0].Name)
|
||||
s.LastPacket = nil
|
||||
|
||||
// iq disco items
|
||||
c.handleDiscoItems(s, xmpp.IQ{
|
||||
Payload: &xmpp.DiscoItems{},
|
||||
c.handleDiscoItems(s, stanza.IQ{
|
||||
Payload: &stanza.DiscoItems{},
|
||||
})
|
||||
assert.Nil(s.LastPacket)
|
||||
|
||||
c.handleDiscoItems(s, xmpp.IQ{
|
||||
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||
c.handleDiscoItems(s, stanza.IQ{
|
||||
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
|
||||
})
|
||||
assert.Nil(s.LastPacket)
|
||||
|
||||
c.handleDiscoItems(s, xmpp.IQ{
|
||||
PacketAttrs: xmpp.PacketAttrs{Type: "get"},
|
||||
Payload: &xmpp.DiscoItems{},
|
||||
c.handleDiscoItems(s, stanza.IQ{
|
||||
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
|
||||
Payload: &stanza.DiscoItems{},
|
||||
})
|
||||
assert.NotNil(s.LastPacket)
|
||||
iq = s.LastPacket.(xmpp.IQ)
|
||||
assert.Equal("result", iq.Type)
|
||||
ditems := iq.Payload.(*xmpp.DiscoItems)
|
||||
iq = s.LastPacket.(stanza.IQ)
|
||||
assert.Equal(stanza.IQTypeResult, iq.Type)
|
||||
ditems := iq.Payload.(*stanza.DiscoItems)
|
||||
assert.Equal("monkeyservice", ditems.Items[0].Name)
|
||||
s.LastPacket = nil
|
||||
}
|
||||
|
|
|
@ -2,10 +2,10 @@ package component
|
|||
|
||||
import (
|
||||
"github.com/bdlm/log"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func (c *Config) sender(packets chan xmpp.Packet) {
|
||||
func (c *Config) sender(packets chan stanza.Packet) {
|
||||
for packet := range packets {
|
||||
if p := c.sending(packet); p != nil {
|
||||
c.xmpp.Send(p)
|
||||
|
@ -13,20 +13,20 @@ func (c *Config) sender(packets chan xmpp.Packet) {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Config) sending(packet xmpp.Packet) xmpp.Packet {
|
||||
func (c *Config) sending(packet stanza.Packet) stanza.Packet {
|
||||
logger := log.WithField("type", c.Type)
|
||||
switch p := packet.(type) {
|
||||
case xmpp.Message:
|
||||
if p.PacketAttrs.From == "" {
|
||||
p.PacketAttrs.From = c.Host
|
||||
case stanza.Message:
|
||||
if p.From == "" {
|
||||
p.From = c.Host
|
||||
} else {
|
||||
p.PacketAttrs.From += "@" + c.Host
|
||||
p.From += "@" + c.Host
|
||||
}
|
||||
if c.XMPPDebug {
|
||||
logger.WithFields(map[string]interface{}{
|
||||
"from": p.PacketAttrs.From,
|
||||
"to": p.PacketAttrs.To,
|
||||
"id": p.PacketAttrs.Id,
|
||||
"from": p.From,
|
||||
"to": p.To,
|
||||
"id": p.Id,
|
||||
}).Debug(p.XMPPFormat())
|
||||
}
|
||||
return p
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func TestSend(t *testing.T) {
|
||||
|
@ -13,18 +13,18 @@ func TestSend(t *testing.T) {
|
|||
c := Config{Host: "example.org", XMPPDebug: true}
|
||||
|
||||
// ignoring packet
|
||||
p := c.sending(xmpp.IQ{})
|
||||
p := c.sending(stanza.IQ{})
|
||||
assert.Nil(p)
|
||||
|
||||
// send by component host
|
||||
p = c.sending(xmpp.Message{})
|
||||
p = c.sending(stanza.Message{})
|
||||
assert.NotNil(p)
|
||||
msg := p.(xmpp.Message)
|
||||
assert.Equal("example.org", msg.PacketAttrs.From)
|
||||
msg := p.(stanza.Message)
|
||||
assert.Equal("example.org", msg.From)
|
||||
|
||||
// send by a user of component
|
||||
p = c.sending(xmpp.Message{PacketAttrs: xmpp.PacketAttrs{From: "threemaid"}})
|
||||
p = c.sending(stanza.Message{Attrs: stanza.Attrs{From: "threemaid"}})
|
||||
assert.NotNil(p)
|
||||
msg = p.(xmpp.Message)
|
||||
assert.Equal("threemaid@example.org", msg.PacketAttrs.From)
|
||||
msg = p.(stanza.Message)
|
||||
assert.Equal("threemaid@example.org", msg.From)
|
||||
}
|
||||
|
|
|
@ -5,13 +5,13 @@ import (
|
|||
"io/ioutil"
|
||||
"strconv"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func (a *Account) FileToXMPP(from string, msgID uint64, ext string, data []byte) (xmpp.Message, error) {
|
||||
func (a *Account) FileToXMPP(from string, msgID uint64, ext string, data []byte) (stanza.Message, error) {
|
||||
msgIDStr := strconv.FormatUint(msgID, 10)
|
||||
msg := xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{
|
||||
msg := stanza.Message{
|
||||
Attrs: stanza.Attrs{
|
||||
Id: msgIDStr,
|
||||
From: from,
|
||||
To: a.XMPP.String(),
|
||||
|
@ -24,6 +24,6 @@ func (a *Account) FileToXMPP(from string, msgID uint64, ext string, data []byte)
|
|||
return msg, err
|
||||
}
|
||||
msg.Body = url
|
||||
msg.Extensions = append(msg.Extensions, xmpp.OOB{URL: url})
|
||||
msg.Extensions = append(msg.Extensions, stanza.OOB{URL: url})
|
||||
return msg, nil
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package threema
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -19,7 +19,7 @@ func TestFileToXMPP(t *testing.T) {
|
|||
|
||||
msg, err := a.FileToXMPP("", 1, "jpg", []byte("hallo"))
|
||||
assert.NoError(err)
|
||||
oob := msg.Extensions[0].(xmpp.OOB)
|
||||
oob := msg.Extensions[0].(stanza.OOB)
|
||||
assert.Equal("a/1.jpg", oob.URL)
|
||||
|
||||
a.threema.httpUploadPath = "/gibt/es/nicht"
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/bdlm/log"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
|
||||
"dev.sum7.eu/genofire/golang-lib/database"
|
||||
|
||||
|
@ -15,7 +15,7 @@ import (
|
|||
|
||||
type Threema struct {
|
||||
component.Component
|
||||
out chan xmpp.Packet
|
||||
out chan stanza.Packet
|
||||
accountJID map[string]*Account
|
||||
bot map[string]*Bot
|
||||
httpUploadPath string
|
||||
|
@ -24,7 +24,7 @@ type Threema struct {
|
|||
|
||||
func NewThreema(config map[string]interface{}) (component.Component, error) {
|
||||
t := &Threema{
|
||||
out: make(chan xmpp.Packet),
|
||||
out: make(chan stanza.Packet),
|
||||
accountJID: make(map[string]*Account),
|
||||
bot: make(map[string]*Bot),
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ func NewThreema(config map[string]interface{}) (component.Component, error) {
|
|||
return t, nil
|
||||
}
|
||||
|
||||
func (t *Threema) Connect() (chan xmpp.Packet, error) {
|
||||
func (t *Threema) Connect() (chan stanza.Packet, error) {
|
||||
var jids []*models.JID
|
||||
database.Read.Find(&jids)
|
||||
for _, jid := range jids {
|
||||
|
@ -60,37 +60,37 @@ func (t *Threema) Connect() (chan xmpp.Packet, error) {
|
|||
}
|
||||
return t.out, nil
|
||||
}
|
||||
func (t *Threema) Send(packet xmpp.Packet) {
|
||||
func (t *Threema) Send(packet stanza.Packet) {
|
||||
if p := t.send(packet); p != nil {
|
||||
t.out <- p
|
||||
}
|
||||
}
|
||||
func (t *Threema) send(packet xmpp.Packet) xmpp.Packet {
|
||||
func (t *Threema) send(packet stanza.Packet) stanza.Packet {
|
||||
switch p := packet.(type) {
|
||||
case xmpp.Message:
|
||||
from := models.ParseJID(p.PacketAttrs.From)
|
||||
to := models.ParseJID(p.PacketAttrs.To)
|
||||
case stanza.Message:
|
||||
from := models.ParseJID(p.Attrs.From)
|
||||
to := models.ParseJID(p.Attrs.To)
|
||||
|
||||
if to.IsDomain() {
|
||||
if from == nil {
|
||||
log.Warn("receive message without sender")
|
||||
return nil
|
||||
}
|
||||
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
||||
msg := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, To: from.String()})
|
||||
msg.Body = t.getBot(from).Handle(p.Body)
|
||||
return msg
|
||||
}
|
||||
|
||||
account, err := t.getAccount(from)
|
||||
if err != nil {
|
||||
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
||||
msg := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, To: from.String()})
|
||||
msg.Body = "It was not possible to send, because we have no account for you.\nPlease generate one, by sending `generate` to this gateway"
|
||||
return msg
|
||||
}
|
||||
|
||||
threemaID := strings.ToUpper(to.Local)
|
||||
if err := account.Send(threemaID, p); err != nil {
|
||||
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
||||
msg := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, To: from.String()})
|
||||
msg.Body = err.Error()
|
||||
return msg
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
|
||||
"dev.sum7.eu/genofire/golang-lib/database"
|
||||
"dev.sum7.eu/genofire/thrempp/models"
|
||||
|
@ -72,44 +72,44 @@ func TestSend(t *testing.T) {
|
|||
assert := assert.New(t)
|
||||
|
||||
// test channel
|
||||
out := make(chan xmpp.Packet)
|
||||
out := make(chan stanza.Packet)
|
||||
tr := Threema{
|
||||
out: out,
|
||||
accountJID: make(map[string]*Account),
|
||||
bot: make(map[string]*Bot),
|
||||
}
|
||||
go func() {
|
||||
tr.Send(xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
|
||||
tr.Send(stanza.Message{
|
||||
Attrs: stanza.Attrs{From: "a@example.org"},
|
||||
})
|
||||
}()
|
||||
p := <-out
|
||||
assert.NotNil(p)
|
||||
// no account. generate one
|
||||
msg := p.(xmpp.Message)
|
||||
msg := p.(stanza.Message)
|
||||
assert.Contains(msg.Body, "generate")
|
||||
|
||||
// test no answer
|
||||
p = tr.send(xmpp.IQ{})
|
||||
p = tr.send(stanza.IQ{})
|
||||
assert.Nil(p)
|
||||
|
||||
// chat with bot without sender
|
||||
p = tr.send(xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{
|
||||
p = tr.send(stanza.Message{
|
||||
Attrs: stanza.Attrs{
|
||||
To: "example.org",
|
||||
},
|
||||
})
|
||||
assert.Nil(p)
|
||||
|
||||
// chat with bot
|
||||
p = tr.send(xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{
|
||||
p = tr.send(stanza.Message{
|
||||
Attrs: stanza.Attrs{
|
||||
From: "a@example.com",
|
||||
To: "example.org",
|
||||
},
|
||||
})
|
||||
assert.NotNil(p)
|
||||
msg = p.(xmpp.Message)
|
||||
msg = p.(stanza.Message)
|
||||
assert.Contains(msg.Body, "command not found")
|
||||
|
||||
// chat with delivier error
|
||||
|
@ -134,14 +134,14 @@ func TestSend(t *testing.T) {
|
|||
_, err := tr.getAccount(&jid)
|
||||
assert.NoError(err)
|
||||
|
||||
p = tr.send(xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{
|
||||
p = tr.send(stanza.Message{
|
||||
Attrs: stanza.Attrs{
|
||||
From: "a@example.org",
|
||||
To: "12345678@threema.example.org",
|
||||
},
|
||||
})
|
||||
assert.NotNil(p)
|
||||
msg = p.(xmpp.Message)
|
||||
msg = p.(stanza.Message)
|
||||
assert.Equal("command not supported", msg.Body)
|
||||
*/
|
||||
}
|
||||
|
|
|
@ -7,14 +7,14 @@ import (
|
|||
|
||||
"github.com/bdlm/log"
|
||||
"github.com/o3ma/o3"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func (a *Account) receiver(out chan<- xmpp.Packet) {
|
||||
func (a *Account) receiver(out chan<- stanza.Packet) {
|
||||
for receivedMessage := range a.receive {
|
||||
if receivedMessage.Err != nil {
|
||||
log.Warnf("Error Receiving Message: %s\n", receivedMessage.Err)
|
||||
xMSG := xmpp.NewMessage("chat", "", a.XMPP.String(), "", "en")
|
||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, To: a.XMPP.String()})
|
||||
xMSG.Body = fmt.Sprintf("error on decoding message:\n%v", receivedMessage.Err)
|
||||
out <- xMSG
|
||||
continue
|
||||
|
@ -24,7 +24,7 @@ func (a *Account) receiver(out chan<- xmpp.Packet) {
|
|||
continue
|
||||
}
|
||||
if p, err := a.receiving(receivedMessage.Msg); err != nil {
|
||||
xMSG := xmpp.NewMessage("chat", sender, a.XMPP.String(), "", "en")
|
||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String()})
|
||||
xMSG.Body = fmt.Sprintf("error on decoding message: %s\n%v", err, receivedMessage.Msg.Serialize())
|
||||
out <- xMSG
|
||||
} else if p != nil {
|
||||
|
@ -33,13 +33,13 @@ func (a *Account) receiver(out chan<- xmpp.Packet) {
|
|||
}
|
||||
}
|
||||
|
||||
func requestExtensions(xMSG *xmpp.Message) {
|
||||
xMSG.Extensions = append(xMSG.Extensions, xmpp.ReceiptRequest{})
|
||||
xMSG.Extensions = append(xMSG.Extensions, xmpp.Markable{})
|
||||
xMSG.Extensions = append(xMSG.Extensions, xmpp.StateActive{})
|
||||
func requestExtensions(xMSG *stanza.Message) {
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.ReceiptRequest{})
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.Markable{})
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.StateActive{})
|
||||
}
|
||||
|
||||
func (a *Account) receiving(receivedMessage o3.Message) (xmpp.Packet, error) {
|
||||
func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
|
||||
logger := log.WithFields(map[string]interface{}{
|
||||
"from": receivedMessage.Sender().String(),
|
||||
"to": a.XMPP.String(),
|
||||
|
@ -47,7 +47,7 @@ func (a *Account) receiving(receivedMessage o3.Message) (xmpp.Packet, error) {
|
|||
switch msg := receivedMessage.(type) {
|
||||
case o3.TextMessage:
|
||||
sender := msg.Sender().String()
|
||||
xMSG := xmpp.NewMessage("chat", sender, a.XMPP.String(), strconv.FormatUint(msg.ID(), 10), "en")
|
||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(msg.ID(), 10)})
|
||||
xMSG.Body = msg.Text()
|
||||
requestExtensions(&xMSG)
|
||||
logger.WithField("text", xMSG.Body).Debug("send text")
|
||||
|
@ -93,14 +93,14 @@ func (a *Account) receiving(receivedMessage o3.Message) (xmpp.Packet, error) {
|
|||
|
||||
case o3.DeliveryReceiptMessage:
|
||||
msgID := msg.MsgID()
|
||||
xMSG := xmpp.NewMessage("chat", msg.Sender().String(), a.XMPP.String(), "", "en")
|
||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: msg.Sender().String(), To: a.XMPP.String()})
|
||||
state := ""
|
||||
|
||||
if msg.Status() == o3.MSGDELIVERED {
|
||||
state = "delivered"
|
||||
if id, ok := a.deliveredMSG[msgID]; ok {
|
||||
xMSG.Extensions = append(xMSG.Extensions, xmpp.ReceiptReceived{ID: id})
|
||||
xMSG.Extensions = append(xMSG.Extensions, xmpp.MarkReceived{ID: id})
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.ReceiptReceived{ID: id})
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.MarkReceived{ID: id})
|
||||
delete(a.deliveredMSG, msgID)
|
||||
} else {
|
||||
logger.Warnf("found not id in cache to announce received on xmpp side")
|
||||
|
@ -109,7 +109,7 @@ func (a *Account) receiving(receivedMessage o3.Message) (xmpp.Packet, error) {
|
|||
if msg.Status() == o3.MSGREAD {
|
||||
state = "displayed"
|
||||
if id, ok := a.readedMSG[msgID]; ok {
|
||||
xMSG.Extensions = append(xMSG.Extensions, xmpp.MarkDisplayed{ID: id})
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.MarkDisplayed{ID: id})
|
||||
delete(a.readedMSG, msgID)
|
||||
} else {
|
||||
logger.Warnf("found not id in cache to announce readed on xmpp side")
|
||||
|
@ -122,13 +122,13 @@ func (a *Account) receiving(receivedMessage o3.Message) (xmpp.Packet, error) {
|
|||
}
|
||||
return nil, nil
|
||||
case o3.TypingNotificationMessage:
|
||||
xMSG := xmpp.NewMessage("chat", msg.Sender().String(), a.XMPP.String(), strconv.FormatUint(msg.ID(), 10), "en")
|
||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: msg.Sender().String(), To: a.XMPP.String(), Id: strconv.FormatUint(msg.ID(), 10)})
|
||||
if msg.OnOff != 0 {
|
||||
logger.Debug("composing")
|
||||
xMSG.Extensions = append(xMSG.Extensions, xmpp.StateComposing{})
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.StateComposing{})
|
||||
} else {
|
||||
logger.Debug("inactive")
|
||||
xMSG.Extensions = append(xMSG.Extensions, xmpp.StateInactive{})
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.StateInactive{})
|
||||
}
|
||||
return xMSG, nil
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
|
||||
"github.com/o3ma/o3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
const threemaID = "87654321"
|
||||
|
@ -55,7 +55,7 @@ func TestReceiveText(t *testing.T) {
|
|||
assert.NoError(err)
|
||||
p, err := a.receiving(txtMsg)
|
||||
assert.NoError(err)
|
||||
xMSG, ok := p.(xmpp.Message)
|
||||
xMSG, ok := p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
assert.Equal("Oojoh0Ah", xMSG.Body)
|
||||
}
|
||||
|
@ -125,9 +125,9 @@ func TestReceiveDeliveryReceipt(t *testing.T) {
|
|||
assert.NoError(err)
|
||||
p, err := a.receiving(drm)
|
||||
assert.NoError(err)
|
||||
xMSG, ok := p.(xmpp.Message)
|
||||
xMSG, ok := p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
rr := xMSG.Extensions[0].(xmpp.ReceiptReceived)
|
||||
rr := xMSG.Extensions[0].(stanza.ReceiptReceived)
|
||||
assert.Equal("im4aeseeh1IbaQui", rr.ID)
|
||||
|
||||
// receiving delivered -> not in cache
|
||||
|
@ -140,9 +140,9 @@ func TestReceiveDeliveryReceipt(t *testing.T) {
|
|||
assert.NoError(err)
|
||||
p, err = a.receiving(drm)
|
||||
assert.NoError(err)
|
||||
xMSG, ok = p.(xmpp.Message)
|
||||
xMSG, ok = p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
cmdd := xMSG.Extensions[0].(xmpp.MarkDisplayed)
|
||||
cmdd := xMSG.Extensions[0].(stanza.MarkDisplayed)
|
||||
assert.Equal("im4aeseeh1IbaQui", cmdd.ID)
|
||||
|
||||
// receiving delivered -> not in cache
|
||||
|
@ -159,16 +159,16 @@ func TestReceiveTyping(t *testing.T) {
|
|||
tnm := o3.TypingNotificationMessage{}
|
||||
p, err := a.receiving(tnm)
|
||||
assert.NoError(err)
|
||||
xMSG, ok := p.(xmpp.Message)
|
||||
xMSG, ok := p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
assert.IsType(xmpp.StateInactive{}, xMSG.Extensions[0])
|
||||
assert.IsType(stanza.StateInactive{}, xMSG.Extensions[0])
|
||||
|
||||
// receiving composing
|
||||
tnm = o3.TypingNotificationMessage{}
|
||||
tnm.OnOff = 0x1
|
||||
p, err = a.receiving(tnm)
|
||||
assert.NoError(err)
|
||||
xMSG, ok = p.(xmpp.Message)
|
||||
xMSG, ok = p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
assert.IsType(xmpp.StateComposing{}, xMSG.Extensions[0])
|
||||
assert.IsType(stanza.StateComposing{}, xMSG.Extensions[0])
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@ import (
|
|||
|
||||
"github.com/bdlm/log"
|
||||
"github.com/o3ma/o3"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func (a *Account) Send(to string, msg xmpp.Message) error {
|
||||
func (a *Account) Send(to string, msg stanza.Message) error {
|
||||
m, err := a.sending(to, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -18,7 +18,7 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
|
||||
func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
|
||||
logger := log.WithFields(map[string]interface{}{
|
||||
"from": a.XMPP.String(),
|
||||
"to": to,
|
||||
|
@ -33,24 +33,24 @@ func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
|
|||
for _, el := range msg.Extensions {
|
||||
switch ex := el.(type) {
|
||||
|
||||
case *xmpp.StateActive:
|
||||
case *stanza.StateActive:
|
||||
chatState = true
|
||||
case *xmpp.StateComposing:
|
||||
case *stanza.StateComposing:
|
||||
chatState = true
|
||||
chatStateComposing = true
|
||||
case *xmpp.StateGone:
|
||||
case *stanza.StateGone:
|
||||
chatState = true
|
||||
case *xmpp.StateInactive:
|
||||
case *stanza.StateInactive:
|
||||
chatState = true
|
||||
case *xmpp.StatePaused:
|
||||
case *stanza.StatePaused:
|
||||
chatState = true
|
||||
|
||||
case *xmpp.ReceiptReceived:
|
||||
case *stanza.ReceiptReceived:
|
||||
msgStateID = ex.ID
|
||||
case *xmpp.MarkReceived:
|
||||
case *stanza.MarkReceived:
|
||||
msgStateID = ex.ID
|
||||
|
||||
case *xmpp.MarkDisplayed:
|
||||
case *stanza.MarkDisplayed:
|
||||
msgStateRead = true
|
||||
msgStateID = ex.ID
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
|
||||
"github.com/o3ma/o3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func TestAccountSend(t *testing.T) {
|
||||
|
@ -20,9 +20,9 @@ func TestAccountSend(t *testing.T) {
|
|||
}
|
||||
|
||||
go func() {
|
||||
a.Send("a", xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
|
||||
Body: "ohz8kai0ohNgohth",
|
||||
a.Send("a", stanza.Message{
|
||||
Attrs: stanza.Attrs{From: "a@example.org"},
|
||||
Body: "ohz8kai0ohNgohth",
|
||||
})
|
||||
}()
|
||||
p := <-send
|
||||
|
@ -31,10 +31,10 @@ func TestAccountSend(t *testing.T) {
|
|||
assert.Contains(msg.Text(), "ohz8kai0ohNgohth")
|
||||
|
||||
// test error
|
||||
err := a.Send("a", xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
|
||||
Extensions: []xmpp.MsgExtension{
|
||||
&xmpp.ReceiptReceived{ID: "blub"},
|
||||
err := a.Send("a", stanza.Message{
|
||||
Attrs: stanza.Attrs{From: "a@example.org"},
|
||||
Extensions: []stanza.MsgExtension{
|
||||
&stanza.ReceiptReceived{ID: "blub"},
|
||||
},
|
||||
})
|
||||
assert.Error(err)
|
||||
|
@ -48,20 +48,20 @@ func TestAccountSendingDeliviery(t *testing.T) {
|
|||
}
|
||||
|
||||
// test error - threema send only int ids
|
||||
msg, err := a.sending("a", xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
|
||||
Extensions: []xmpp.MsgExtension{
|
||||
&xmpp.ReceiptReceived{ID: "blub"},
|
||||
msg, err := a.sending("a", stanza.Message{
|
||||
Attrs: stanza.Attrs{From: "a@example.org"},
|
||||
Extensions: []stanza.MsgExtension{
|
||||
&stanza.ReceiptReceived{ID: "blub"},
|
||||
},
|
||||
})
|
||||
assert.Error(err)
|
||||
assert.Nil(msg)
|
||||
|
||||
// test delivered
|
||||
msg, err = a.sending("a", xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
|
||||
Extensions: []xmpp.MsgExtension{
|
||||
&xmpp.MarkReceived{ID: "3"},
|
||||
msg, err = a.sending("a", stanza.Message{
|
||||
Attrs: stanza.Attrs{From: "a@example.org"},
|
||||
Extensions: []stanza.MsgExtension{
|
||||
&stanza.MarkReceived{ID: "3"},
|
||||
},
|
||||
})
|
||||
assert.NoError(err)
|
||||
|
@ -70,10 +70,10 @@ func TestAccountSendingDeliviery(t *testing.T) {
|
|||
assert.Equal(o3.MSGDELIVERED, drm.Status())
|
||||
|
||||
// test read
|
||||
msg, err = a.sending("a", xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
|
||||
Extensions: []xmpp.MsgExtension{
|
||||
&xmpp.MarkDisplayed{ID: "5"},
|
||||
msg, err = a.sending("a", stanza.Message{
|
||||
Attrs: stanza.Attrs{From: "a@example.org"},
|
||||
Extensions: []stanza.MsgExtension{
|
||||
&stanza.MarkDisplayed{ID: "5"},
|
||||
},
|
||||
})
|
||||
assert.NoError(err)
|
||||
|
@ -91,37 +91,37 @@ func TestSendTyping(t *testing.T) {
|
|||
}
|
||||
|
||||
// skip typing messae
|
||||
msg, err := a.sending("a", xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
|
||||
Extensions: []xmpp.MsgExtension{
|
||||
&xmpp.StateComposing{},
|
||||
msg, err := a.sending("a", stanza.Message{
|
||||
Attrs: stanza.Attrs{From: "a@example.org"},
|
||||
Extensions: []stanza.MsgExtension{
|
||||
&stanza.StateComposing{},
|
||||
},
|
||||
})
|
||||
assert.NoError(err)
|
||||
assert.Nil(msg)
|
||||
|
||||
// skip gone
|
||||
msg, err = a.sending("a", xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
|
||||
Extensions: []xmpp.MsgExtension{
|
||||
&xmpp.StateActive{},
|
||||
&xmpp.StateGone{},
|
||||
&xmpp.StateInactive{},
|
||||
&xmpp.StatePaused{},
|
||||
msg, err = a.sending("a", stanza.Message{
|
||||
Attrs: stanza.Attrs{From: "a@example.org"},
|
||||
Extensions: []stanza.MsgExtension{
|
||||
&stanza.StateActive{},
|
||||
&stanza.StateGone{},
|
||||
&stanza.StateInactive{},
|
||||
&stanza.StatePaused{},
|
||||
},
|
||||
})
|
||||
assert.NoError(err)
|
||||
assert.Nil(msg)
|
||||
|
||||
// skip gone
|
||||
msg, err = a.sending("a", xmpp.Message{
|
||||
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
|
||||
Extensions: []xmpp.MsgExtension{
|
||||
&xmpp.StateActive{},
|
||||
&xmpp.StateComposing{},
|
||||
&xmpp.StateGone{},
|
||||
&xmpp.StateInactive{},
|
||||
&xmpp.StatePaused{},
|
||||
msg, err = a.sending("a", stanza.Message{
|
||||
Attrs: stanza.Attrs{From: "a@example.org"},
|
||||
Extensions: []stanza.MsgExtension{
|
||||
&stanza.StateActive{},
|
||||
&stanza.StateComposing{},
|
||||
&stanza.StateGone{},
|
||||
&stanza.StateInactive{},
|
||||
&stanza.StatePaused{},
|
||||
},
|
||||
Body: "hi",
|
||||
})
|
||||
|
|
Reference in New Issue