logmania/output/xmpp/main.go

260 lines
6.0 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"
2018-04-26 21:05:27 +02:00
"strings"
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"
"dev.sum7.eu/genofire/yaja/xmpp/x/muc"
2018-09-05 01:53:23 +02:00
"github.com/mitchellh/mapstructure"
2017-10-25 00:36:16 +02:00
log "github.com/sirupsen/logrus"
"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
defaults []*database.Notify
client *xmpp_client.Client
channels map[string]bool
2018-04-13 17:13:09 +02:00
formatter log.Formatter
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
}
channels := make(map[string]bool)
jid := xmppbase.NewJID(config.JID)
client := &xmpp_client.Client{
JID: jid,
Logging: logger.WithField("jid", jid.String()),
}
err := client.Connect(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() {
for {
if err := client.Start(); err != nil {
log.Warn("close connection, try reconnect")
2018-09-05 01:53:23 +02:00
client.Connect(config.Password)
} else {
log.Warn("closed connection")
return
}
}
}()
go func() {
for {
element, more := client.Recv()
if !more {
2018-04-18 14:32:28 +02:00
log.Warn("could not receive new message, try later")
continue
}
switch element.(type) {
case *xmpp.PresenceClient:
pres := element.(*xmpp.PresenceClient)
sender := pres.From
logPres := logger.WithField("from", sender.Full())
switch pres.Type {
case xmpp.PresenceTypeSubscribe:
logPres.Debugf("recv presence subscribe")
pres.Type = xmpp.PresenceTypeSubscribed
pres.To = sender
pres.From = nil
client.Send(pres)
logPres.Debugf("accept new subscribe")
pres.Type = xmpp.PresenceTypeSubscribe
pres.ID = ""
client.Send(pres)
logPres.Info("request also subscribe")
case xmpp.PresenceTypeSubscribed:
logPres.Info("recv presence accepted subscribe")
case xmpp.PresenceTypeUnsubscribe:
logPres.Info("recv presence remove subscribe")
case xmpp.PresenceTypeUnsubscribed:
logPres.Info("recv presence removed subscribe")
case xmpp.PresenceTypeUnavailable:
logPres.Debug("recv presence unavailable")
case "":
logPres.Debug("recv empty presence, maybe from joining muc")
continue
default:
logPres.Warnf("recv presence unsupported: %s -> %s", pres.Type, xmpp.XMLChildrenString(pres))
}
case *xmpp.MessageClient:
msg := element.(*xmpp.MessageClient)
from := msg.From.Bare().String()
if msg.Type == xmpp.MessageTypeGroupchat {
2018-04-15 01:52:08 +02:00
from = protoGroup + ":" + from
} else {
2018-04-15 01:52:08 +02:00
from = proto + ":" + from
}
bot.Handle(func(answer string) {
2018-04-13 22:11:38 +02:00
to := msg.From
if msg.Type == xmpp.MessageTypeGroupchat && !to.IsBare() {
to = to.Bare()
}
err := client.Send(&xmpp.MessageClient{
Type: msg.Type,
2018-04-13 22:11:38 +02:00
To: to,
Body: answer,
})
if err != nil {
logger.Error("xmpp to ", msg.From.String(), " error:", err)
}
}, from, msg.Body)
}
}
}()
2018-09-05 01:53:23 +02:00
logger.WithField("jid", config.JID).Info("startup")
2018-09-05 21:00:02 +02:00
out := &Output{
channels: channels,
client: client,
formatter: &log.TextFormatter{
DisableTimestamp: true,
},
}
2018-09-11 20:09:59 +02:00
2018-09-05 01:53:23 +02:00
for to, muc := range config.Defaults {
def := &database.Notify{
2018-09-11 20:20:58 +02:00
Protocol: proto,
To: to,
RegexIn: make(map[string]*regexp.Regexp),
MaxPrioIn: log.DebugLevel,
}
if muc {
def.Protocol = protoGroup
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-11 20:09:59 +02:00
func (out *Output) Join(to string) {
toJID := xmppbase.NewJID(to)
toJID.Resource = nickname
err := out.client.Send(&xmpp.PresenceClient{
To: toJID,
MUC: &xmuc.Base{
History: &xmuc.History{
MaxChars: &historyMaxChars,
},
},
})
if err != nil {
logger.Error("xmpp could not join ", toJID.String(), " error:", err)
} else {
out.channels[to] = true
}
}
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) Send(e *log.Entry, to *database.Notify) bool {
textByte, err := out.formatter.Format(e)
2017-10-25 00:36:16 +02:00
if err != nil {
2018-04-26 21:05:27 +02:00
logger.Error("during format notify", err)
return false
}
2018-04-26 21:05:27 +02:00
text := strings.TrimRight(to.RunReplace(string(textByte)), "\n")
if to.Protocol == protoGroup {
2018-09-05 01:53:23 +02:00
if _, ok := out.channels[to.To]; ok {
2018-04-26 21:05:27 +02:00
toJID := xmppbase.NewJID(to.To)
toJID.Resource = nickname
2018-09-05 01:53:23 +02:00
err := out.client.Send(&xmpp.PresenceClient{
2018-04-26 21:05:27 +02:00
To: toJID,
MUC: &xmuc.Base{
History: &xmuc.History{
MaxChars: &historyMaxChars,
},
},
})
2017-08-11 19:59:19 +02:00
if err != nil {
2018-04-26 21:05:27 +02:00
logger.Error("xmpp could not join ", toJID.String(), " error:", err)
} else {
2018-09-05 01:53:23 +02:00
out.channels[to.To] = true
2017-08-11 19:59:19 +02:00
}
}
2018-09-05 01:53:23 +02:00
err := out.client.Send(&xmpp.MessageClient{
2018-04-26 21:05:27 +02:00
Type: xmpp.MessageTypeGroupchat,
To: xmppbase.NewJID(to.To),
Body: text,
})
if err != nil {
logger.Error("xmpp to ", to.To, " error:", err)
}
return true
}
if to.Protocol == proto {
2018-09-05 01:53:23 +02:00
err := out.client.Send(&xmpp.MessageClient{
2018-04-26 21:05:27 +02:00
Type: xmpp.MessageTypeChat,
To: xmppbase.NewJID(to.To),
Body: text,
})
if err != nil {
logger.Error("xmpp to ", to, " error:", err)
}
return true
}
2018-04-26 21:05:27 +02:00
return false
2017-10-25 00:36:16 +02:00
}
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
}