This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
thrempp/component/receiver.go

111 lines
2.3 KiB
Go
Raw Normal View History

2019-06-01 04:38:35 +02:00
package component
import (
"github.com/bdlm/log"
"gosrc.io/xmpp"
)
func (c *Config) receiver() {
for {
packet, err := c.xmpp.ReadPacket()
if err != nil {
log.WithField("type", c.Type).Panicf("connection closed%s", err)
return
}
p, back := c.receiving(packet)
2019-06-01 04:38:35 +02:00
if p == nil {
continue
}
if back {
c.xmpp.Send(p)
} else {
c.comp.Send(p)
}
}
}
func (c *Config) receiving(packet xmpp.Packet) (xmpp.Packet, bool) {
2019-06-01 04:38:35 +02:00
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",
}
}
payload := xmpp.DiscoInfo{
Identity: identity,
Features: []xmpp.Feature{
2019-06-05 04:30:00 +02:00
{Var: xmpp.NSDiscoInfo},
{Var: xmpp.NSDiscoItems},
{Var: xmpp.NSMsgReceipts},
{Var: xmpp.NSMsgChatMarkers},
2019-06-06 20:53:06 +02:00
{Var: xmpp.NSMsgChatStateNotifications},
2019-06-01 04:38:35 +02:00
},
}
iq.AddPayload(&payload)
loggerIQ.Debug("disco info")
return iq, true
}
case *xmpp.DiscoItems:
if p.Type == "get" {
iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
var payload xmpp.DiscoItems
if inner.Node == "" {
payload = xmpp.DiscoItems{
Items: []xmpp.DiscoItem{
{Name: c.Type, JID: c.Host, Node: "node1"},
},
}
}
iq.AddPayload(&payload)
loggerIQ.Debug("disco items")
return iq, true
}
default:
logger.Debug("ignoring iq packet", inner)
xError := xmpp.Err{
Code: 501,
Reason: "feature-not-implemented",
Type: "cancel",
}
reply := p.MakeError(xError)
return reply, true
}
case xmpp.Message:
2019-06-06 21:20:51 +02:00
if c.XMPPDebug {
2019-06-06 20:53:06 +02:00
logger.WithFields(map[string]interface{}{
"from": p.PacketAttrs.From,
"to": p.PacketAttrs.To,
"id": p.PacketAttrs.Id,
}).Debug(p.XMPPFormat())
}
2019-06-01 04:38:35 +02:00
return packet, false
case xmpp.Presence:
logger.Debug("received presence:", p.Type)
default:
logger.Debug("ignoring packet:", packet)
}
return nil, false
}