aboutsummaryrefslogblamecommitdiff
path: root/xmpp/gateway/gateway.go
blob: 0c2267771fd66fd27e3db8c9b1f53d5553d8376f (plain) (tree)




















































                                                                                  
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)
}