logmania/output/xmpp/main.go

122 lines
2.6 KiB
Go
Raw Normal View History

2017-06-16 10:33:35 +02:00
package xmpp
import (
2018-09-11 20:20:58 +02:00
"regexp"
2017-08-11 19:59:19 +02:00
2019-06-20 09:25:43 +02:00
"github.com/bdlm/log"
2018-09-05 01:53:23 +02:00
"github.com/mitchellh/mapstructure"
2019-07-17 22:31:15 +02:00
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
"dev.sum7.eu/genofire/logmania/bot"
"dev.sum7.eu/genofire/logmania/database"
2018-09-05 01:53:23 +02:00
"dev.sum7.eu/genofire/logmania/output"
2017-06-16 10:33:35 +02:00
)
const (
2018-04-15 01:52:08 +02:00
proto = "xmpp"
protoGroup = "xmpp-muc"
nickname = "logmania"
)
2018-09-05 01:53:23 +02:00
var logger = log.WithField("output", proto)
2017-10-25 00:36:16 +02:00
2018-09-05 01:53:23 +02:00
type Output struct {
output.Output
2019-06-21 05:20:34 +02:00
defaults []*database.Notify
channels map[string]bool
bot *bot.Bot
2019-07-17 22:31:15 +02:00
client xmpp.Sender
2019-06-21 05:20:34 +02:00
botOut chan interface{}
logOut chan interface{}
2017-06-16 10:33:35 +02:00
}
2018-09-05 01:53:23 +02:00
type OutputConfig struct {
2019-07-17 22:31:15 +02:00
Address string `mapstructure:"address"`
2018-09-05 01:53:23 +02:00
JID string `mapstructure:"jid"`
Password string `mapstructure:"password"`
Defaults map[string]bool `mapstructure:"default"`
}
func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Output {
var config OutputConfig
if err := mapstructure.Decode(configInterface, &config); err != nil {
logger.Warnf("not able to decode data: %s", err)
return nil
}
2018-09-05 21:00:02 +02:00
out := &Output{
2019-06-21 05:20:34 +02:00
channels: make(map[string]bool),
bot: bot,
2018-09-05 21:00:02 +02:00
}
2019-06-21 05:20:34 +02:00
2019-07-17 22:31:15 +02:00
router := xmpp.NewRouter()
router.HandleFunc("message", out.recvMessage)
router.HandleFunc("presence", out.recvPresence)
2018-09-11 20:09:59 +02:00
2019-07-17 22:31:15 +02:00
client, err := xmpp.NewClient(xmpp.Config{
Address: config.Address,
Jid: config.JID,
Password: config.Password,
}, router)
if err != nil {
logger.Error(err)
return nil
}
cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) {
out.client = c
for to, muc := range config.Defaults {
def := &database.Notify{
Protocol: proto,
To: to,
RegexIn: make(map[string]*regexp.Regexp),
MaxPrioIn: log.DebugLevel,
}
2019-07-17 22:31:15 +02:00
if muc {
def.Protocol = protoGroup
out.Join(to)
}
out.defaults = append(out.defaults, def)
}
2019-07-17 22:31:15 +02:00
for _, toAddresses := range db.NotifiesByAddress {
if toAddresses.Protocol == protoGroup {
out.Join(toAddresses.To)
}
2018-09-11 20:09:59 +02:00
}
2019-07-17 22:31:15 +02:00
logger.Info("join muc after connect")
})
go func() {
cm.Run()
log.Panic("closed connection")
}()
logger.WithField("jid", config.JID).Info("startup")
2018-09-05 21:00:02 +02:00
return out
2017-06-16 10:33:35 +02:00
}
2018-09-05 01:53:23 +02:00
func (out *Output) Default() []*database.Notify {
return out.defaults
}
2018-09-05 01:53:23 +02:00
func (out *Output) Close() {
for jid := range out.channels {
2019-07-17 22:31:15 +02:00
toJID, err := xmpp.NewJid(jid)
if err != nil {
2019-07-17 22:31:15 +02:00
logger.Error("xmpp could generate jid to leave ", jid, " error:", err)
}
toJID.Resource = nickname
if err = out.client.Send(stanza.Presence{Attrs: stanza.Attrs{
To: toJID.Full(),
Type: stanza.PresenceTypeUnavailable,
}}); err != nil {
logger.Error("xmpp could not leave ", toJID.Full(), " error:", err)
}
2017-10-25 00:36:16 +02:00
}
2017-06-16 10:33:35 +02:00
}
func init() {
2018-09-05 01:53:23 +02:00
output.Add(proto, Init)
2017-06-16 10:33:35 +02:00
}