logmania/output/xmpp/send.go

80 lines
1.8 KiB
Go
Raw Normal View History

2019-06-21 05:20:34 +02:00
package xmpp
import (
"strings"
"github.com/bdlm/log"
2019-07-17 22:31:15 +02:00
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
2019-06-21 05:20:34 +02:00
"dev.sum7.eu/genofire/logmania/database"
)
func (out *Output) Join(to string) {
2019-07-17 22:31:15 +02:00
toJID, err := xmpp.NewJid(to)
2019-06-21 05:20:34 +02:00
if err != nil {
2019-07-17 22:31:15 +02:00
logger.Errorf("jid not generate to join muc %s : %s", to, err)
return
}
toJID.Resource = nickname
if err = out.client.Send(stanza.Presence{Attrs: stanza.Attrs{To: toJID.Full()},
Extensions: []stanza.PresExtension{
stanza.MucPresence{
History: stanza.History{MaxStanzas: stanza.NewNullableInt(0)},
}},
}); err != nil {
logger.Errorf("muc not join %s : %s", toJID.Full(), err)
2019-06-21 05:20:34 +02:00
} else {
out.channels[to] = true
}
}
func (out *Output) Send(e *log.Entry, to *database.Notify) bool {
2019-07-17 22:31:15 +02:00
if out.client == nil {
logger.Error("xmpp not connected (yet)")
return false
}
2019-06-21 05:20:34 +02:00
html, text := formatLog(e)
if html == "" || text == "" {
logger.Error("during format notify")
return false
}
html = strings.TrimRight(to.RunReplace(html), "\n")
text = strings.TrimRight(to.RunReplace(text), "\n")
2019-07-17 22:31:15 +02:00
msg := stanza.Message{
Attrs: stanza.Attrs{
To: to.To,
},
Body: text,
Extensions: []stanza.MsgExtension{
stanza.HTML{Body: stanza.HTMLBody{InnerXML: html}},
},
}
2019-06-21 05:20:34 +02:00
if to.Protocol == protoGroup {
if _, ok := out.channels[to.To]; ok {
2019-07-17 22:31:15 +02:00
out.Join(to.To)
2019-06-21 05:20:34 +02:00
}
2019-07-17 22:31:15 +02:00
msg.Type = stanza.MessageTypeGroupchat
if err := out.client.Send(msg); err != nil {
logger.WithFields(map[string]interface{}{
"muc": to.To,
"text": text,
}).Errorf("log message not forwarded: %s", err)
2019-06-21 05:20:34 +02:00
}
return true
}
if to.Protocol == proto {
2019-07-17 22:31:15 +02:00
msg.Type = stanza.MessageTypeChat
if err := out.client.Send(msg); err != nil {
logger.WithFields(map[string]interface{}{
"user": to.To,
"text": text,
}).Errorf("log message not forwarded: %s", err)
2019-06-21 05:20:34 +02:00
}
return true
}
return false
}