logmania/notify/xmpp/main.go

49 lines
1.0 KiB
Go
Raw Normal View History

2017-06-16 10:33:35 +02:00
package xmpp
import (
"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
state *notify.NotifyState
2017-06-16 10:33:35 +02:00
}
func Init(config *lib.NotifyConfig, state *notify.NotifyState) 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 {
return nil
}
return &Notifier{client: client, state: state}
2017-06-16 10:33:35 +02:00
}
2017-08-09 08:45:45 +02:00
func (n *Notifier) Send(e *log.Entry) {
to := n.state.SendTo(e)
if to == nil {
return
}
for _, to := range to {
n.client.SendHtml(xmpp.Chat{Remote: to, Type: "chat", Text: formatEntry(e)})
}
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
}