handle xmpp component

This commit is contained in:
Martin/Geno 2019-05-31 13:53:44 +02:00
parent 651c6c1404
commit 3787c86fbf
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
3 changed files with 90 additions and 1 deletions

View File

@ -31,6 +31,10 @@ func Load(configs []Config) {
log.WithField("type", config.Type).Panic(err) log.WithField("type", config.Type).Panic(err)
} }
config.comp = comp config.comp = comp
err = config.Start()
if err != nil {
log.WithField("type", config.Type).Panic(err)
}
log.WithField("type", config.Type).Infof("component for %s started", config.Host) log.WithField("type", config.Type).Infof("component for %s started", config.Host)
} }
} }

View File

@ -1,6 +1,9 @@
package threema package threema
import "dev.sum7.eu/genofire/thrempp/component" import (
"dev.sum7.eu/genofire/thrempp/component"
"gosrc.io/xmpp"
)
type Threema struct { type Threema struct {
component.Component component.Component
@ -10,6 +13,11 @@ func NewThreema(config map[string]interface{}) (component.Component, error) {
return &Threema{}, nil return &Threema{}, nil
} }
func (t *Threema) Connect() (chan xmpp.Packet, error) {
c := make(chan xmpp.Packet)
return c, nil
}
func init() { func init() {
component.AddComponent("threema", NewThreema) component.AddComponent("threema", NewThreema)
} }

View File

@ -1,6 +1,7 @@
package component package component
import ( import (
"github.com/bdlm/log"
"gosrc.io/xmpp" "gosrc.io/xmpp"
) )
@ -35,4 +36,80 @@ func (c *Config) Start() error {
func (c *Config) recieve(chan xmpp.Packet) { func (c *Config) recieve(chan xmpp.Packet) {
} }
func (c *Config) sender() { func (c *Config) sender() {
logger := log.WithField("type", c.Type)
for {
logger.Debug("wait fo recieve")
packet, err := c.xmpp.ReadPacket()
if err != nil {
logger.Panicf("connection closed%s", err)
return
}
logger.Debug("recieve")
switch p := packet.(type) {
case xmpp.IQ:
attrs := p.PacketAttrs
switch inner := p.Payload[0].(type) {
case *xmpp.DiscoInfo:
logger.Debug("Disco Info")
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"},
},
}
iq.AddPayload(&payload)
_ = c.xmpp.Send(iq)
}
case *xmpp.DiscoItems:
logger.Debug("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)
_ = c.xmpp.Send(iq)
}
default:
logger.Warn("ignoring iq packet", inner)
xError := xmpp.Err{
Code: 501,
Reason: "feature-not-implemented",
Type: "cancel",
}
reply := p.MakeError(xError)
_ = c.xmpp.Send(&reply)
}
case xmpp.Message:
logger.Info("Received message:", p.Body)
case xmpp.Presence:
logger.Info("Received presence:", p.Type)
default:
logger.Warn("ignoring packet:", packet)
}
}
} }