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/xmpp.go

144 lines
3.1 KiB
Go
Raw Normal View History

2019-05-31 10:07:40 +02:00
package component
import (
2019-05-31 13:53:44 +02:00
"github.com/bdlm/log"
2019-05-31 10:07:40 +02:00
"gosrc.io/xmpp"
)
type Config struct {
Type string
Host string
Connection string
Secret string
Special map[string]interface{}
xmpp *xmpp.Component
comp Component
}
func (c *Config) Start() error {
c.xmpp = &xmpp.Component{Host: c.Host, Secret: c.Secret}
err := c.xmpp.Connect(c.Connection)
if err != nil {
return err
}
out, err := c.comp.Connect()
if err != nil {
return err
}
go c.recieve(out)
go c.sender()
return nil
}
2019-05-31 14:36:18 +02:00
func (c *Config) recieve(packets chan xmpp.Packet) {
logger := log.WithField("type", c.Type)
for packet := range packets {
switch p := packet.(type) {
case xmpp.Message:
if p.PacketAttrs.From == "" {
p.PacketAttrs.From = c.Host
} else {
p.PacketAttrs.From += "@" + c.Host
}
2019-06-01 01:43:48 +02:00
logger.WithFields(map[string]interface{}{
2019-05-31 14:36:18 +02:00
"from": p.PacketAttrs.From,
"to": p.PacketAttrs.To,
2019-06-01 01:43:48 +02:00
"id": p.PacketAttrs.Id,
}).Debug(p.XMPPFormat())
2019-05-31 14:36:18 +02:00
c.xmpp.Send(p)
default:
log.Warn("ignoring packet:", packet)
}
}
2019-05-31 10:07:40 +02:00
}
func (c *Config) sender() {
2019-05-31 13:53:44 +02:00
logger := log.WithField("type", c.Type)
for {
packet, err := c.xmpp.ReadPacket()
if err != nil {
logger.Panicf("connection closed%s", err)
return
}
switch p := packet.(type) {
case xmpp.IQ:
attrs := p.PacketAttrs
2019-05-31 14:36:18 +02:00
loggerIQ := logger.WithFields(map[string]interface{}{
"from": attrs.From,
"to": attrs.To,
})
2019-05-31 13:53:44 +02:00
switch inner := p.Payload[0].(type) {
case *xmpp.DiscoInfo:
2019-05-31 14:36:18 +02:00
loggerIQ.Debug("Disco Info")
2019-05-31 13:53:44 +02:00
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{
{Var: "http://jabber.org/protocol/disco#info"},
{Var: "http://jabber.org/protocol/disco#item"},
2019-06-01 01:43:48 +02:00
{Var: xmpp.NSSpaceXEP0184Receipt},
2019-06-01 02:32:34 +02:00
{Var: xmpp.NSSpaceXEP0333ChatMarkers},
2019-05-31 13:53:44 +02:00
},
}
iq.AddPayload(&payload)
_ = c.xmpp.Send(iq)
}
case *xmpp.DiscoItems:
2019-05-31 14:36:18 +02:00
loggerIQ.Debug("DiscoItems")
2019-05-31 13:53:44 +02:00
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)
_ = c.xmpp.Send(iq)
}
default:
2019-05-31 19:39:29 +02:00
logger.Debug("ignoring iq packet", inner)
2019-05-31 13:53:44 +02:00
xError := xmpp.Err{
Code: 501,
Reason: "feature-not-implemented",
Type: "cancel",
}
reply := p.MakeError(xError)
_ = c.xmpp.Send(&reply)
}
case xmpp.Message:
2019-06-01 01:43:48 +02:00
logger.WithFields(map[string]interface{}{
"from": p.PacketAttrs.From,
"to": p.PacketAttrs.To,
"id": p.PacketAttrs.Id,
}).Debug(p.XMPPFormat())
2019-05-31 14:36:18 +02:00
c.comp.Send(packet)
2019-05-31 13:53:44 +02:00
case xmpp.Presence:
2019-05-31 19:39:29 +02:00
logger.Debug("Received presence:", p.Type)
2019-05-31 13:53:44 +02:00
default:
2019-05-31 19:39:29 +02:00
logger.Debug("ignoring packet:", packet)
2019-05-31 13:53:44 +02:00
}
}
2019-05-31 10:07:40 +02:00
}