|
|
|
@ -8,6 +8,8 @@ import (
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var client = &xmpp.Client{} // @Hacky
|
|
|
|
|
|
|
|
|
|
// box config required for the XmppNotifier (TransportConfig.Options)
|
|
|
|
|
type XmppNotifier struct {
|
|
|
|
|
Recipients []string
|
|
|
|
@ -23,6 +25,16 @@ func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// establish connection with server once, and share it accross instances
|
|
|
|
|
// @Hacky
|
|
|
|
|
if client.JID() == "" {
|
|
|
|
|
c, err := connectXmpp()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
client = c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// assign configuration to the notifier after ensuring the correct type.
|
|
|
|
|
// lesson of this project: golang requires us to fuck around with type
|
|
|
|
|
// assertions, instead of providing us with proper inheritance.
|
|
|
|
@ -53,26 +65,12 @@ func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n XmppNotifier) Submit(notification Notification) error {
|
|
|
|
|
// :TransportConfSourceHack
|
|
|
|
|
xmppOpts := xmpp.Options{
|
|
|
|
|
Host: viper.GetString("xmpp.host"),
|
|
|
|
|
User: viper.GetString("xmpp.user"),
|
|
|
|
|
Password: viper.GetString("xmpp.pass"),
|
|
|
|
|
Resource: "osem_notify",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if viper.GetBool("xmpp.starttls") {
|
|
|
|
|
xmppOpts.NoTLS = true
|
|
|
|
|
xmppOpts.StartTLS = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := xmppOpts.NewClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
if client.JID() == "" {
|
|
|
|
|
return fmt.Errorf("xmpp client not correctly initialized!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, recipient := range n.Recipients {
|
|
|
|
|
_, err = client.Send(xmpp.Chat{
|
|
|
|
|
_, err := client.Send(xmpp.Chat{
|
|
|
|
|
Remote: recipient,
|
|
|
|
|
Subject: notification.Subject,
|
|
|
|
|
Text: fmt.Sprintf("%s\n\n%s", notification.Subject, notification.Body),
|
|
|
|
@ -83,5 +81,22 @@ func (n XmppNotifier) Submit(notification Notification) error {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func connectXmpp() (*xmpp.Client, error) {
|
|
|
|
|
// :TransportConfSourceHack
|
|
|
|
|
xmppOpts := xmpp.Options{
|
|
|
|
|
Host: viper.GetString("xmpp.host"),
|
|
|
|
|
User: viper.GetString("xmpp.user"),
|
|
|
|
|
Password: viper.GetString("xmpp.pass"),
|
|
|
|
|
Resource: "osem_notify",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if viper.GetBool("xmpp.starttls") {
|
|
|
|
|
xmppOpts.NoTLS = true
|
|
|
|
|
xmppOpts.StartTLS = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return xmppOpts.NewClient()
|
|
|
|
|
}
|
|
|
|
|