telegabber/xmpp/gateway/gateway.go

54 lines
1,003 B
Go
Raw Normal View History

2019-11-24 17:10:29 +00:00
package gateway
import (
"encoding/xml"
log "github.com/sirupsen/logrus"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
)
// Jid stores the component's JID object
var Jid *xmpp.Jid
// SendMessage creates and sends a message stanza
func SendMessage(to string, from string, body string, component *xmpp.Component) {
componentJid := Jid.Full()
var logFrom string
var messageFrom string
if from == "" {
logFrom = componentJid
messageFrom = componentJid
} else {
logFrom = from
messageFrom = from + "@" + componentJid
}
log.WithFields(log.Fields{
"from": logFrom,
"to": to,
}).Warn("Got message")
message := stanza.Message{
Attrs: stanza.Attrs{
From: messageFrom,
To: to,
Type: "chat",
},
Body: body,
}
// explicit check, as marshalling is expensive
if log.GetLevel() == log.DebugLevel {
xmlMessage, err := xml.Marshal(message)
if err == nil {
log.Debug(string(xmlMessage))
} else {
log.Debugf("%#v", message)
}
}
_ = component.Send(message)
}