logmania/output/xmpp/send.go

65 lines
1.3 KiB
Go
Raw Normal View History

2019-06-21 05:20:34 +02:00
package xmpp
import (
"strings"
"github.com/bdlm/log"
2019-06-24 14:12:28 +02:00
"gosrc.io/xmpp"
2019-06-21 05:20:34 +02:00
"dev.sum7.eu/genofire/logmania/database"
)
func (out *Output) Join(to string) {
2019-06-24 14:12:28 +02:00
toJID, err := xmpp.NewJid(to)
if err != nil {
logger.Error("xmpp could not generate jid to join ", to, " error:", err)
return
}
2019-06-21 05:20:34 +02:00
toJID.Resource = nickname
2019-06-24 14:12:28 +02:00
err = out.client.Send(xmpp.Presence{Attrs: xmpp.Attrs{To: toJID.Full()},
Extensions: []xmpp.PresExtension{
xmpp.MucPresence{
History: xmpp.History{MaxStanzas: 0},
}},
2019-06-21 05:20:34 +02:00
})
if err != nil {
2019-06-24 14:12:28 +02:00
logger.Error("xmpp could not join ", toJID.Full(), " error:", 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 {
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-06-24 14:12:28 +02:00
msg := xmpp.Message{
Attrs: xmpp.Attrs{
To: to.To,
},
Body: text,
Extensions: []xmpp.MsgExtension{
xmpp.HTML{Body: xmpp.HTMLBody{InnerXML: html}},
},
}
2019-06-21 05:20:34 +02:00
if to.Protocol == protoGroup {
if _, ok := out.channels[to.To]; ok {
2019-06-24 14:12:28 +02:00
out.Join(to.To)
2019-06-21 05:20:34 +02:00
}
2019-06-24 14:12:28 +02:00
msg.Type = xmpp.MessageTypeGroupchat
out.client.Send(msg)
2019-06-21 05:20:34 +02:00
return true
}
if to.Protocol == proto {
2019-06-24 14:12:28 +02:00
msg.Type = xmpp.MessageTypeChat
out.client.Send(msg)
2019-06-21 05:20:34 +02:00
return true
}
return false
}