diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-24 20:10:29 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-24 20:10:29 +0300 |
commit | 653b1bde94b89d91862007a18b8730ff58673f59 (patch) | |
tree | 5b0c8674f918eb49dd27e53918ff10529c0ff55c /xmpp/gateway | |
parent | c0c21a35a4cfd326423ad530926a0e96c1b07dcf (diff) |
Telegram authorization
Diffstat (limited to 'xmpp/gateway')
-rw-r--r-- | xmpp/gateway/gateway.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/xmpp/gateway/gateway.go b/xmpp/gateway/gateway.go new file mode 100644 index 0000000..0c22677 --- /dev/null +++ b/xmpp/gateway/gateway.go @@ -0,0 +1,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) +} |