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

114 lines
2.4 KiB
Go
Raw Normal View History

2019-06-01 04:38:35 +02:00
package component
import (
2019-06-20 13:40:22 +02:00
"encoding/xml"
2019-06-01 04:38:35 +02:00
"github.com/bdlm/log"
"gosrc.io/xmpp"
2019-06-28 03:03:38 +02:00
"gosrc.io/xmpp/stanza"
2019-06-01 04:38:35 +02:00
)
2019-06-28 03:03:38 +02:00
func (c *Config) handleDiscoInfo(s xmpp.Sender, p stanza.Packet) {
iq, ok := p.(stanza.IQ)
2019-06-20 13:40:22 +02:00
if !ok || iq.Type != "get" {
return
}
2019-06-28 03:03:38 +02:00
discoInfo, ok := iq.Payload.(*stanza.DiscoInfo)
2019-06-20 13:40:22 +02:00
if !ok {
return
}
2019-06-28 03:03:38 +02:00
attrs := iq.Attrs
iq = stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, To: attrs.From, From: attrs.To, Id: attrs.Id})
2019-06-20 13:40:22 +02:00
2019-06-28 03:03:38 +02:00
payload := stanza.DiscoInfo{
2019-06-20 13:40:22 +02:00
XMLName: xml.Name{
2019-06-28 03:03:38 +02:00
Space: stanza.NSDiscoInfo,
2019-06-20 13:40:22 +02:00
Local: "query",
},
2019-06-28 03:03:38 +02:00
Features: []stanza.Feature{
{Var: stanza.NSDiscoInfo},
{Var: stanza.NSDiscoItems},
{Var: stanza.NSMsgReceipts},
{Var: stanza.NSMsgChatMarkers},
{Var: stanza.NSMsgChatStateNotifications},
2019-06-20 13:40:22 +02:00
},
}
if discoInfo.Node == "" {
2019-06-28 03:03:38 +02:00
payload.Identity = append(payload.Identity, stanza.Identity{
2019-06-20 13:40:22 +02:00
Name: c.Type,
Category: "gateway",
Type: "service",
2019-06-28 03:03:38 +02:00
})
2019-06-01 04:38:35 +02:00
}
2019-06-20 13:40:22 +02:00
iq.Payload = &payload
log.WithFields(map[string]interface{}{
"type": c.Type,
"from": s,
"to": attrs.To,
}).Debug("disco info")
s.Send(iq)
2019-06-01 04:38:35 +02:00
}
2019-06-28 03:03:38 +02:00
func (c *Config) handleDiscoItems(s xmpp.Sender, p stanza.Packet) {
iq, ok := p.(stanza.IQ)
2019-06-20 13:40:22 +02:00
if !ok || iq.Type != "get" {
return
}
2019-06-28 03:03:38 +02:00
discoItems, ok := iq.Payload.(*stanza.DiscoItems)
2019-06-20 13:40:22 +02:00
if !ok {
return
}
2019-06-28 03:03:38 +02:00
attrs := iq.Attrs
iq = stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, To: attrs.From, From: attrs.To, Id: attrs.Id})
2019-06-01 04:38:35 +02:00
2019-06-28 03:03:38 +02:00
payload := stanza.DiscoItems{}
2019-06-20 13:40:22 +02:00
if discoItems.Node == "" {
2019-06-28 03:03:38 +02:00
payload.Items = []stanza.DiscoItem{
2019-06-20 13:40:22 +02:00
{Name: c.Type, JID: c.Host, Node: "node1"},
2019-06-06 20:53:06 +02:00
}
2019-06-20 13:40:22 +02:00
}
iq.Payload = &payload
2019-06-01 04:38:35 +02:00
2019-06-20 13:40:22 +02:00
log.WithFields(map[string]interface{}{
"type": c.Type,
"from": s,
"to": attrs.To,
}).Debug("disco items")
s.Send(iq)
}
2019-06-28 03:03:38 +02:00
func (c *Config) handleIQ(s xmpp.Sender, p stanza.Packet) {
iq, ok := p.(stanza.IQ)
2019-06-20 13:40:22 +02:00
if !ok || iq.Type != "get" {
return
}
2019-06-28 03:03:38 +02:00
xError := stanza.Err{
2019-06-20 13:40:22 +02:00
Code: 501,
Reason: "feature-not-implemented",
Type: "cancel",
}
resp := iq.MakeError(xError)
2019-06-28 03:03:38 +02:00
attrs := iq.Attrs
2019-06-01 04:38:35 +02:00
2019-06-20 13:40:22 +02:00
log.WithFields(map[string]interface{}{
"type": c.Type,
"from": s,
"to": attrs.To,
}).Debugf("ignore: %s", iq.Payload)
s.Send(resp)
}
2019-06-28 03:03:38 +02:00
func (c *Config) handleMessage(s xmpp.Sender, p stanza.Packet) {
msg, ok := p.(stanza.Message)
2019-06-20 13:40:22 +02:00
if !ok {
return
}
if c.XMPPDebug {
log.WithFields(map[string]interface{}{
"type": c.Type,
"from": s,
2019-06-28 03:03:38 +02:00
"to": msg.To,
"id": msg.Id,
2019-06-20 13:40:22 +02:00
}).Debug(msg.XMPPFormat())
2019-06-01 04:38:35 +02:00
}
2019-06-20 13:40:22 +02:00
c.comp.Send(p)
2019-06-01 04:38:35 +02:00
}