2019-06-01 04:38:35 +02:00
|
|
|
package component
|
|
|
|
|
|
|
|
import (
|
2019-09-06 03:08:58 +02:00
|
|
|
"github.com/bdlm/log"
|
2019-06-01 04:38:35 +02:00
|
|
|
"gosrc.io/xmpp"
|
2019-06-28 03:03:38 +02:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-06-01 04:38:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Type string
|
|
|
|
Host string
|
|
|
|
Connection string
|
|
|
|
Secret string
|
2019-06-06 21:20:51 +02:00
|
|
|
XMPPDebug bool `toml:"xmpp_debug"`
|
2019-06-01 04:38:35 +02:00
|
|
|
Special map[string]interface{}
|
|
|
|
|
|
|
|
xmpp *xmpp.Component
|
|
|
|
comp Component
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:08:58 +02:00
|
|
|
func (c *Config) Start() error {
|
2019-06-10 01:00:36 +02:00
|
|
|
out, err := c.comp.Connect()
|
2019-06-01 04:38:35 +02:00
|
|
|
if err != nil {
|
2019-09-06 03:08:58 +02:00
|
|
|
return err
|
2019-06-01 04:38:35 +02:00
|
|
|
}
|
2019-06-20 13:40:22 +02:00
|
|
|
|
|
|
|
router := xmpp.NewRouter()
|
2019-06-28 03:03:38 +02:00
|
|
|
router.NewRoute().IQNamespaces(stanza.NSDiscoInfo).HandlerFunc(c.handleDiscoInfo)
|
|
|
|
router.NewRoute().IQNamespaces(stanza.NSDiscoItems).HandlerFunc(c.handleDiscoItems)
|
2019-06-20 13:40:22 +02:00
|
|
|
router.HandleFunc("iq", c.handleIQ)
|
|
|
|
router.HandleFunc("message", c.handleMessage)
|
2019-09-06 04:34:13 +02:00
|
|
|
router.HandleFunc("presence", c.handleMessage)
|
2019-06-20 13:40:22 +02:00
|
|
|
|
2019-06-10 01:00:36 +02:00
|
|
|
c.xmpp, err = xmpp.NewComponent(xmpp.ComponentOptions{
|
|
|
|
Domain: c.Host,
|
|
|
|
Secret: c.Secret,
|
|
|
|
Address: c.Connection,
|
|
|
|
Name: c.Type,
|
|
|
|
Category: "gateway",
|
|
|
|
Type: "service",
|
2019-06-20 13:40:22 +02:00
|
|
|
}, router)
|
2019-06-10 01:00:36 +02:00
|
|
|
if err != nil {
|
2019-09-06 03:08:58 +02:00
|
|
|
return err
|
2019-06-10 01:00:36 +02:00
|
|
|
}
|
|
|
|
cm := xmpp.NewStreamManager(c.xmpp, nil)
|
2019-09-06 03:08:58 +02:00
|
|
|
go func() {
|
|
|
|
if err := cm.Run(); err != nil {
|
|
|
|
log.WithField("host", c.Host).Panicf("xmpp stream not started: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-06-01 04:38:35 +02:00
|
|
|
go c.sender(out)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|