diff --git a/bridge/config/config.go b/bridge/config/config.go index fdb6e55c..91e2b4b9 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -10,12 +10,13 @@ import ( ) const ( - EVENT_JOIN_LEAVE = "join_leave" - EVENT_TOPIC_CHANGE = "topic_change" - EVENT_FAILURE = "failure" - EVENT_REJOIN_CHANNELS = "rejoin_channels" - EVENT_USER_ACTION = "user_action" - EVENT_MSG_DELETE = "msg_delete" + EVENT_JOIN_LEAVE = "join_leave" + EVENT_TOPIC_CHANGE = "topic_change" + EVENT_FAILURE = "failure" + EVENT_FILE_FAILURE_SIZE = "file_failure_size" + EVENT_REJOIN_CHANNELS = "rejoin_channels" + EVENT_USER_ACTION = "user_action" + EVENT_MSG_DELETE = "msg_delete" ) type Message struct { @@ -38,6 +39,7 @@ type FileInfo struct { Data *[]byte Comment string URL string + Size int64 } type ChannelInfo struct { diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index a5e20758..d57f9122 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -3,6 +3,7 @@ package bdiscord import ( "bytes" "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" log "github.com/Sirupsen/logrus" "github.com/bwmarrin/discordgo" "regexp" @@ -139,6 +140,9 @@ func (b *bdiscord) Send(msg config.Message) (string, error) { } if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text) + } // check if we have files to upload (from slack, telegram or mattermost) if len(msg.Extra["file"]) > 0 { var err error diff --git a/bridge/gitter/gitter.go b/bridge/gitter/gitter.go index 6b1c2122..0d41c778 100644 --- a/bridge/gitter/gitter.go +++ b/bridge/gitter/gitter.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/42wim/go-gitter" "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" log "github.com/Sirupsen/logrus" "strings" ) @@ -121,6 +122,9 @@ func (b *Bgitter) Send(msg config.Message) (string, error) { } if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + b.c.SendMessage(roomID, rmsg.Username+rmsg.Text) + } if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) diff --git a/bridge/helper/helper.go b/bridge/helper/helper.go index 0b6be4f4..bfa9e64b 100644 --- a/bridge/helper/helper.go +++ b/bridge/helper/helper.go @@ -2,6 +2,8 @@ package helper import ( "bytes" + "fmt" + "github.com/42wim/matterbridge/bridge/config" "io" "net/http" "time" @@ -38,3 +40,18 @@ func SplitStringLength(input string, length int) string { } return str } + +// handle all the stuff we put into extra +func HandleExtra(msg *config.Message, general *config.Protocol) []config.Message { + extra := msg.Extra + rmsg := []config.Message{} + if len(extra[config.EVENT_FILE_FAILURE_SIZE]) > 0 { + for _, f := range extra[config.EVENT_FILE_FAILURE_SIZE] { + fi := f.(config.FileInfo) + text := fmt.Sprintf("file %s too big to download (%#v > allowed size: %#v)", fi.Name, fi.Size, general.MediaDownloadSize) + rmsg = append(rmsg, config.Message{Text: text, Username: " ", Channel: msg.Channel}) + } + return rmsg + } + return rmsg +} diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index f35efd26..0e90aaf9 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -177,6 +177,9 @@ func (b *Birc) Send(msg config.Message) (string, error) { } if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + b.Local <- rmsg + } if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index 109d56c8..0be7fc9b 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -98,6 +98,9 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) { } if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + b.mc.SendText(channel, rmsg.Username+rmsg.Text) + } // check if we have files to upload (from slack, telegram or mattermost) if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { @@ -234,6 +237,10 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) { flog.Debugf("download OK %#v %#v %#v", name, len(*data), len(url)) rmsg.Extra["file"] = append(rmsg.Extra["file"], config.FileInfo{Name: name, Data: data}) } + } else { + flog.Errorf("File %#v to large to download (%#v). MediaDownloadSize is %#v", name, size, b.General.MediaDownloadSize) + rmsg.Event = config.EVENT_FILE_FAILURE_SIZE + rmsg.Extra[rmsg.Event] = append(rmsg.Extra[rmsg.Event], config.FileInfo{Name: name, Size: int64(size)}) } rmsg.Text = "" } diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index 7f5b6f8e..1f42b437 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/matterclient" "github.com/42wim/matterbridge/matterhook" log "github.com/Sirupsen/logrus" @@ -154,6 +155,12 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { if b.Config.WebhookURL != "" { if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL, Channel: channel, UserName: rmsg.Username, + Text: rmsg.Text, Props: make(map[string]interface{})} + matterMessage.Props["matterbridge"] = true + b.mh.Send(matterMessage) + } if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) @@ -186,6 +193,9 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { return msg.ID, b.mc.DeleteMessage(msg.ID) } if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + b.mc.PostMessage(b.mc.GetChannelId(channel, ""), rmsg.Username+rmsg.Text) + } if len(msg.Extra["file"]) > 0 { var err error var res, id string @@ -296,6 +306,8 @@ func (b *Bmattermost) handleMatterClient(mchan chan *MMMessage) { flog.Debugf("trying to download %#v fileid %#v with size %#v", finfo.Name, finfo.Id, finfo.Size) if int(finfo.Size) > b.General.MediaDownloadSize { flog.Errorf("File %#v to large to download (%#v). MediaDownloadSize is %#v", finfo.Name, finfo.Size, b.General.MediaDownloadSize) + m.Event = config.EVENT_FILE_FAILURE_SIZE + m.Extra[m.Event] = append(m.Extra[m.Event], config.FileInfo{Name: finfo.Name, Comment: message.Text, Size: int64(finfo.Size)}) continue } data, resp := b.mc.Client.DownloadFile(id, true) diff --git a/bridge/rocketchat/rocketchat.go b/bridge/rocketchat/rocketchat.go index 05238539..0164612e 100644 --- a/bridge/rocketchat/rocketchat.go +++ b/bridge/rocketchat/rocketchat.go @@ -2,6 +2,7 @@ package brocketchat import ( "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/hook/rockethook" "github.com/42wim/matterbridge/matterhook" log "github.com/Sirupsen/logrus" @@ -57,6 +58,22 @@ func (b *Brocketchat) Send(msg config.Message) (string, error) { return "", nil } flog.Debugf("Receiving %#v", msg) + if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL, Channel: rmsg.Channel, UserName: rmsg.Username, + Text: rmsg.Text} + b.mh.Send(matterMessage) + } + if len(msg.Extra["file"]) > 0 { + for _, f := range msg.Extra["file"] { + fi := f.(config.FileInfo) + if fi.URL != "" { + msg.Text += fi.URL + } + } + } + } + matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL} matterMessage.Channel = msg.Channel matterMessage.UserName = msg.Username diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index e040e9dd..8e5e2606 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/matterhook" log "github.com/Sirupsen/logrus" "github.com/nlopes/slack" @@ -134,6 +135,22 @@ func (b *Bslack) Send(msg config.Message) (string, error) { message = nick + " " + message } if b.Config.WebhookURL != "" { + if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL, Channel: channel, UserName: rmsg.Username, + Text: rmsg.Text} + b.mh.Send(matterMessage) + } + if len(msg.Extra["file"]) > 0 { + for _, f := range msg.Extra["file"] { + fi := f.(config.FileInfo) + if fi.URL != "" { + message += fi.URL + } + } + } + } + matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL} matterMessage.Channel = channel matterMessage.UserName = nick @@ -183,6 +200,9 @@ func (b *Bslack) Send(msg config.Message) (string, error) { } if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + b.sc.PostMessage(schannel.ID, rmsg.Username+rmsg.Text, np) + } // check if we have files to upload (from slack, telegram or mattermost) if len(msg.Extra["file"]) > 0 { var err error @@ -291,16 +311,21 @@ func (b *Bslack) handleSlack() { // if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra if message.Raw.File != nil { // limit to 1MB for now - if message.Raw.File.Size <= b.General.MediaDownloadSize { - comment := "" + comment := "" + results := regexp.MustCompile(`.*?commented: (.*)`).FindAllStringSubmatch(msg.Text, -1) + if len(results) > 0 { + comment = results[0][1] + } + + if message.Raw.File.Size > b.General.MediaDownloadSize { + flog.Errorf("File %#v to large to download (%#v). MediaDownloadSize is %#v", message.Raw.File.Name, message.Raw.File.Size, b.General.MediaDownloadSize) + msg.Event = config.EVENT_FILE_FAILURE_SIZE + msg.Extra[msg.Event] = append(msg.Extra[msg.Event], config.FileInfo{Name: message.Raw.File.Name, Comment: comment, Size: int64(message.Raw.File.Size)}) + } else { data, err := b.downloadFile(message.Raw.File.URLPrivateDownload) if err != nil { flog.Errorf("download %s failed %#v", message.Raw.File.URLPrivateDownload, err) } else { - results := regexp.MustCompile(`.*?commented: (.*)`).FindAllStringSubmatch(msg.Text, -1) - if len(results) > 0 { - comment = results[0][1] - } msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: message.Raw.File.Name, Data: data, Comment: comment}) } } diff --git a/bridge/sshchat/sshchat.go b/bridge/sshchat/sshchat.go index 1682b42e..e25cdbd9 100644 --- a/bridge/sshchat/sshchat.go +++ b/bridge/sshchat/sshchat.go @@ -3,6 +3,7 @@ package bsshchat import ( "bufio" "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" log "github.com/Sirupsen/logrus" "github.com/shazow/ssh-chat/sshd" "io" @@ -62,6 +63,9 @@ func (b *Bsshchat) Send(msg config.Message) (string, error) { } flog.Debugf("Receiving %#v", msg) if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + b.w.Write([]byte(rmsg.Username + rmsg.Text + "\r\n")) + } if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 99968ec9..6e78e819 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -97,6 +97,9 @@ func (b *Btelegram) Send(msg config.Message) (string, error) { } if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + b.sendMessage(chatid, rmsg.Username+rmsg.Text) + } // check if we have files to upload (from slack, telegram or mattermost) if len(msg.Extra["file"]) > 0 { var c tgbotapi.Chattable @@ -309,6 +312,10 @@ func (b *Btelegram) handleDownload(file interface{}, comment string, msg *config flog.Debugf("download OK %#v %#v %#v", name, len(*data), len(url)) msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: name, Data: data, Comment: comment}) } + } else { + flog.Errorf("File %#v to large to download (%#v). MediaDownloadSize is %#v", name, size, b.General.MediaDownloadSize) + msg.Event = config.EVENT_FILE_FAILURE_SIZE + msg.Extra[msg.Event] = append(msg.Extra[msg.Event], config.FileInfo{Name: name, Comment: comment, Size: int64(size)}) } } diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 6cc9413d..1f18f74b 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -3,6 +3,7 @@ package bxmpp import ( "crypto/tls" "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" log "github.com/Sirupsen/logrus" "github.com/jpillora/backoff" "github.com/mattn/go-xmpp" @@ -81,6 +82,9 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) { } flog.Debugf("Receiving %#v", msg) if msg.Extra != nil { + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: rmsg.Channel + "@" + b.Config.Muc, Text: rmsg.Username + rmsg.Text}) + } if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) diff --git a/gateway/gateway.go b/gateway/gateway.go index 93e5b5be..3d0a61ed 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -171,7 +171,8 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM dest.Protocol != "mattermost" && dest.Protocol != "telegram" && dest.Protocol != "matrix" && - dest.Protocol != "xmpp" { + dest.Protocol != "xmpp" && + len(msg.Extra[config.EVENT_FILE_FAILURE_SIZE]) == 0 { if msg.Text == "" { return brMsgIDs } @@ -235,7 +236,10 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool { } if msg.Text == "" { // we have an attachment or actual bytes - if msg.Extra != nil && (msg.Extra["attachments"] != nil || len(msg.Extra["file"]) > 0) { + if msg.Extra != nil && + (msg.Extra["attachments"] != nil || + len(msg.Extra["file"]) > 0 || + len(msg.Extra[config.EVENT_FILE_FAILURE_SIZE]) > 0) { return false } log.Debugf("ignoring empty message %#v from %s", msg, msg.Account)