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
xmpp_client "dev.sum7.eu/genofire/yaja/client"
xmpp "dev.sum7.eu/genofire/yaja/xmpp"
"dev.sum7.eu/genofire/yaja/xmpp/base"
2019-06-20 09:25:43 +02:00
"github.com/bdlm/log"
2018-09-05 01:53:23 +02:00
"github.com/mitchellh/mapstructure"
"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"
)
var historyMaxChars = 0
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
client *xmpp_client.Client
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 {
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
}
jid := xmppbase.NewJID(config.JID)
2019-06-20 09:25:43 +02:00
client, err := xmpp_client.NewClient(jid, config.Password)
2017-06-16 10:33:35 +02:00
if err != nil {
2017-10-25 00:36:16 +02:00
logger.Error(err)
2017-06-16 10:33:35 +02:00
return nil
}
go func() {
2019-06-21 00:46:35 +02:00
client.Start()
log.Panic("closed connection")
}()
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
client: client,
2019-06-21 05:20:34 +02:00
botOut: make(chan interface{}),
logOut: make(chan interface{}),
2018-09-05 21:00:02 +02:00
}
2019-06-21 05:20:34 +02:00
go out.sender()
go out.receiver()
logger.WithField("jid", config.JID).Info("startup")
2018-09-11 20:09:59 +02:00
2018-09-05 01:53:23 +02:00
for to, muc := range config.Defaults {
var def *database.Notify
pro := proto
if muc {
pro = protoGroup
}
if dbNotify, ok := db.NotifiesByAddress[pro+":"+to]; ok {
def = dbNotify
} else {
def = &database.Notify{
Protocol: pro,
To: to,
RegexIn: make(map[string]*regexp.Regexp),
MaxPrioIn: log.DebugLevel,
}
2018-09-11 20:09:59 +02:00
out.Join(to)
}
2018-09-05 21:00:02 +02:00
out.defaults = append(out.defaults, def)
2017-10-25 00:36:16 +02:00
}
2018-09-11 20:09:59 +02:00
for _, toAddresses := range db.NotifiesByAddress {
if toAddresses.Protocol == protoGroup {
out.Join(toAddresses.To)
}
}
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 {
toJID := xmppbase.NewJID(jid)
toJID.Resource = nickname
2018-09-05 01:53:23 +02:00
err := out.client.Send(&xmpp.PresenceClient{
To: toJID,
Type: xmpp.PresenceTypeUnavailable,
})
if err != nil {
logger.Error("xmpp could not leave ", toJID.String(), " error:", err)
}
2017-10-25 00:36:16 +02:00
}
2018-09-05 01:53:23 +02:00
out.client.Close()
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
}