2017-06-16 10:33:35 +02:00
|
|
|
package xmpp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/genofire/logmania/database"
|
|
|
|
"github.com/genofire/logmania/lib"
|
|
|
|
"github.com/genofire/logmania/log"
|
|
|
|
"github.com/genofire/logmania/notify"
|
|
|
|
xmpp "github.com/mattn/go-xmpp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Notifier struct {
|
|
|
|
notify.Notifier
|
|
|
|
client *xmpp.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotifyInit(config *lib.NotifyConfig) notify.Notifier {
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Notifier{client: client}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) Send(e *database.Entry) {
|
|
|
|
users := database.UserByApplication(e.ApplicationID)
|
|
|
|
for _, user := range users {
|
2017-06-24 11:52:12 +02:00
|
|
|
if user.NotifyXMPP && log.LogLevel(e.Level) >= user.NotifyAfterLoglevel {
|
|
|
|
n.client.SendHtml(xmpp.Chat{Remote: user.XMPP, Type: "chat", Text: formatEntry(e)})
|
2017-06-16 10:33:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
notify.AddNotifier(NotifyInit)
|
|
|
|
}
|