diff options
Diffstat (limited to 'bridge/xmpp')
-rw-r--r-- | bridge/xmpp/xmpp.go | 57 |
1 files changed, 51 insertions, 6 deletions
diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 45c5beb0..e92c3d69 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -1,8 +1,11 @@ package bxmpp import ( + "bytes" "crypto/tls" + "encoding/json" "fmt" + "net/http" "strings" "sync" "time" @@ -86,14 +89,21 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) { } // Upload a file (in XMPP case send the upload URL because XMPP has no native upload support). + var err error if msg.Extra != nil { for _, rmsg := range helper.HandleExtra(&msg, b.General) { b.Log.Debugf("=> Sending attachement message %#v", rmsg) - if _, err := b.xc.Send(xmpp.Chat{ - Type: "groupchat", - Remote: rmsg.Channel + "@" + b.GetString("Muc"), - Text: rmsg.Username + rmsg.Text, - }); err != nil { + if b.GetString("WebhookURL") != "" { + err = b.postSlackCompatibleWebhook(msg) + } else { + _, err = b.xc.Send(xmpp.Chat{ + Type: "groupchat", + Remote: rmsg.Channel + "@" + b.GetString("Muc"), + Text: rmsg.Username + rmsg.Text, + }) + } + + if err != nil { b.Log.WithError(err).Error("Unable to send message with share URL.") } } @@ -102,13 +112,24 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) { } } + if b.GetString("WebhookURL") != "" { + b.Log.Debugf("Sending message using Webhook") + err := b.postSlackCompatibleWebhook(msg) + if err != nil { + b.Log.Errorf("Failed to send message using webhook: %s", err) + return "", err + } + + return "", nil + } + + // Post normal message. var msgReplaceID string msgID := xid.New().String() if msg.ID != "" { msgID = msg.ID msgReplaceID = msg.ID } - // Post normal message. b.Log.Debugf("=> Sending message %#v", msg) if _, err := b.xc.Send(xmpp.Chat{ Type: "groupchat", @@ -122,6 +143,25 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) { return msgID, nil } +func (b *Bxmpp) postSlackCompatibleWebhook(msg config.Message) error { + type XMPPWebhook struct { + Username string `json:"username"` + Text string `json:"text"` + } + webhookBody, err := json.Marshal(XMPPWebhook{ + Username: msg.Username, + Text: msg.Text, + }) + if err != nil { + b.Log.Errorf("Failed to marshal webhook: %s", err) + return err + } + + resp, err := http.Post(b.GetString("WebhookURL")+"/"+msg.Channel, "application/json", bytes.NewReader(webhookBody)) + resp.Body.Close() + return err +} + func (b *Bxmpp) createXMPP() error { if !strings.Contains(b.GetString("Jid"), "@") { return fmt.Errorf("the Jid %s doesn't contain an @", b.GetString("Jid")) @@ -378,6 +418,11 @@ func (b *Bxmpp) skipMessage(message xmpp.Chat) bool { return true } + // Ignore messages posted by our webhook + if b.GetString("WebhookURL") != "" && strings.Contains(message.ID, "webhookbot") { + return true + } + // skip delayed messages return !message.Stamp.IsZero() && time.Since(message.Stamp).Minutes() > 5 } |