aboutsummaryrefslogtreecommitdiff
path: root/xmpp/gateway/gateway.go
blob: 0c2267771fd66fd27e3db8c9b1f53d5553d8376f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)
}