logmania/notify/xmpp/main.go

206 lines
4.9 KiB
Go
Raw Normal View History

2017-06-16 10:33:35 +02:00
package xmpp
import (
2017-10-25 00:36:16 +02:00
"errors"
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"
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"
"dev.sum7.eu/genofire/logmania/lib"
"dev.sum7.eu/genofire/logmania/notify"
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 logger = log.WithField("notify", proto)
2017-10-25 00:36:16 +02:00
2017-06-16 10:33:35 +02:00
type Notifier struct {
notify.Notifier
client *xmpp_client.Client
channels map[string]bool
db *database.DB
2018-04-13 17:13:09 +02:00
formatter log.Formatter
2017-06-16 10:33:35 +02:00
}
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
channels := make(map[string]bool)
client, err := xmpp_client.NewClient(xmppbase.NewJID(config.XMPP.JID), config.XMPP.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")
client.Connect(config.XMPP.Password)
} else {
log.Warn("closed connection")
return
}
}
}()
go func() {
for {
element, more := client.Recv()
if !more {
log.Warn("could not recieve 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-04-15 01:52:08 +02:00
for toAddr, toAddresses := range db.NotifiesByAddress {
if toAddresses.Protocoll == protoGroup {
toJID := xmppbase.NewJID(toAddresses.To)
toJID.Resource = nickname
err := client.Send(&xmpp.PresenceClient{
To: toJID,
})
if err != nil {
logger.Error("xmpp could not join ", toJID.String(), " error:", err)
} else {
channels[toAddr] = true
}
}
}
2017-10-25 00:36:16 +02:00
logger.Info("startup")
return &Notifier{
channels: channels,
client: client,
db: db,
2017-10-25 00:36:16 +02:00
formatter: &log.TextFormatter{
DisableTimestamp: true,
},
}
2017-06-16 10:33:35 +02:00
}
func (n *Notifier) Send(e *log.Entry) error {
2018-04-15 01:52:08 +02:00
e, _, tos := n.db.SendTo(e)
if tos == nil || len(tos) <= 0 {
2017-10-25 00:36:16 +02:00
return errors.New("no reciever found")
}
text, err := n.formatter.Format(e)
if err != nil {
return err
}
2018-04-15 01:52:08 +02:00
for _, to := range tos {
if to.Protocoll == protoGroup {
if _, ok := n.channels[to.To]; ok {
toJID := xmppbase.NewJID(to.To)
toJID.Resource = nickname
err := n.client.Send(&xmpp.PresenceClient{
To: toJID,
})
if err != nil {
logger.Error("xmpp could not join ", toJID.String(), " error:", err)
} else {
2018-04-15 01:52:08 +02:00
n.channels[to.To] = true
}
}
err := n.client.Send(&xmpp.MessageClient{
Type: xmpp.MessageTypeGroupchat,
2018-04-15 01:52:08 +02:00
To: xmppbase.NewJID(to.To),
Body: string(text),
})
2017-08-11 19:59:19 +02:00
if err != nil {
2018-04-15 01:52:08 +02:00
logger.Error("xmpp to ", to.To, " error:", err)
2017-08-11 19:59:19 +02:00
}
} else {
err := n.client.Send(&xmpp.MessageClient{
Type: xmpp.MessageTypeChat,
2018-04-15 01:52:08 +02:00
To: xmppbase.NewJID(to.To),
Body: string(text),
})
2017-08-11 19:59:19 +02:00
if err != nil {
2017-10-25 00:36:16 +02:00
logger.Error("xmpp to ", to, " error:", err)
2017-08-11 19:59:19 +02:00
}
}
}
2017-10-25 00:36:16 +02:00
return nil
}
func (n *Notifier) Close() {
for jid := range n.channels {
toJID := xmppbase.NewJID(jid)
toJID.Resource = nickname
err := n.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
}
n.client.Close()
2017-06-16 10:33:35 +02:00
}
func init() {
2017-08-09 08:45:45 +02:00
notify.AddNotifier(Init)
2017-06-16 10:33:35 +02:00
}