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
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-08-11 17:45:42 +02:00
|
|
|
xmpp "github.com/mattn/go-xmpp"
|
2017-10-25 00:36:16 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-11 17:45:42 +02:00
|
|
|
|
|
|
|
"github.com/genofire/logmania/bot"
|
2017-06-16 10:33:35 +02:00
|
|
|
"github.com/genofire/logmania/lib"
|
|
|
|
"github.com/genofire/logmania/notify"
|
2017-08-11 17:45:42 +02:00
|
|
|
configNotify "github.com/genofire/logmania/notify/config"
|
2017-06-16 10:33:35 +02:00
|
|
|
)
|
|
|
|
|
2017-10-25 00:36:16 +02:00
|
|
|
var logger = log.WithField("notify", "xmpp")
|
|
|
|
|
2017-06-16 10:33:35 +02:00
|
|
|
type Notifier struct {
|
|
|
|
notify.Notifier
|
2017-10-25 00:36:16 +02:00
|
|
|
client *xmpp.Client
|
|
|
|
state *configNotify.NotifyState
|
|
|
|
formatter *log.TextFormatter
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 17:45:42 +02:00
|
|
|
func Init(config *lib.NotifyConfig, state *configNotify.NotifyState, bot *bot.Bot) notify.Notifier {
|
2017-06-16 10:33:35 +02:00
|
|
|
options := xmpp.Options{
|
|
|
|
Host: config.XMPP.Host,
|
|
|
|
User: config.XMPP.Username,
|
|
|
|
Password: config.XMPP.Password,
|
|
|
|
NoTLS: config.XMPP.NoTLS,
|
|
|
|
Debug: config.XMPP.Debug,
|
|
|
|
Session: config.XMPP.Session,
|
|
|
|
Status: config.XMPP.Status,
|
|
|
|
StatusMessage: config.XMPP.StatusMessage,
|
|
|
|
}
|
|
|
|
client, err := options.NewClient()
|
|
|
|
if err != nil {
|
2017-10-25 00:36:16 +02:00
|
|
|
logger.Error(err)
|
2017-06-16 10:33:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-11 17:45:42 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
chat, err := client.Recv()
|
|
|
|
if err != nil {
|
2017-10-25 00:36:16 +02:00
|
|
|
logger.Warn(err)
|
2017-08-11 17:45:42 +02:00
|
|
|
}
|
|
|
|
switch v := chat.(type) {
|
|
|
|
case xmpp.Chat:
|
|
|
|
bot.Handle(func(answer string) {
|
|
|
|
client.SendHtml(xmpp.Chat{Remote: v.Remote, Type: "chat", Text: answer})
|
2017-08-11 19:59:19 +02:00
|
|
|
}, fmt.Sprintf("xmpp:%s", strings.Split(v.Remote, "/")[0]), v.Text)
|
2017-08-11 17:45:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-10-25 00:36:16 +02:00
|
|
|
logger.Info("startup")
|
|
|
|
return &Notifier{
|
|
|
|
client: client,
|
|
|
|
state: state,
|
|
|
|
formatter: &log.TextFormatter{
|
|
|
|
DisableTimestamp: true,
|
|
|
|
},
|
|
|
|
}
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 00:36:16 +02:00
|
|
|
func (n *Notifier) Fire(e *log.Entry) error {
|
2017-08-10 20:11:35 +02:00
|
|
|
to := n.state.SendTo(e)
|
|
|
|
if to == nil {
|
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
|
2017-08-10 20:11:35 +02:00
|
|
|
}
|
2017-08-11 19:59:19 +02:00
|
|
|
for _, toAddr := range to {
|
|
|
|
to := strings.TrimPrefix(toAddr, "xmpp:")
|
|
|
|
if strings.Contains(toAddr, "conference") || strings.Contains(toAddr, "irc") {
|
|
|
|
n.client.JoinMUCNoHistory(to, "logmania")
|
2017-10-25 00:36:16 +02:00
|
|
|
_, err = n.client.SendHtml(xmpp.Chat{Remote: to, Type: "groupchat", Text: 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
|
|
|
}
|
|
|
|
} else {
|
2017-10-25 00:36:16 +02:00
|
|
|
_, err := n.client.SendHtml(xmpp.Chat{Remote: to, Type: "chat", Text: 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-08-10 20:11:35 +02:00
|
|
|
}
|
2017-10-25 00:36:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) Levels() []log.Level {
|
|
|
|
return []log.Level{
|
|
|
|
log.DebugLevel,
|
|
|
|
log.InfoLevel,
|
|
|
|
log.WarnLevel,
|
|
|
|
log.ErrorLevel,
|
|
|
|
log.FatalLevel,
|
|
|
|
log.PanicLevel,
|
|
|
|
}
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 08:45:45 +02:00
|
|
|
func (n *Notifier) 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
|
|
|
}
|