Add support for file URLs
This commit is contained in:
@@ -2,9 +2,9 @@ package bmumble
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"layeh.com/gumble/gumble"
|
"layeh.com/gumble/gumble"
|
||||||
"layeh.com/gumble/gumbleutil"
|
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
@@ -20,6 +20,7 @@ func (b *Bmumble) handleTextMessage(event *gumble.TextMessageEvent) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
b.Log.Error(err)
|
b.Log.Error(err)
|
||||||
}
|
}
|
||||||
|
now := time.Now().UTC()
|
||||||
for i, part := range parts {
|
for i, part := range parts {
|
||||||
// Construct matterbridge message and pass on to the gateway
|
// Construct matterbridge message and pass on to the gateway
|
||||||
rmsg := config.Message{
|
rmsg := config.Message{
|
||||||
@@ -31,14 +32,14 @@ func (b *Bmumble) handleTextMessage(event *gumble.TextMessageEvent) {
|
|||||||
if part.Image == nil {
|
if part.Image == nil {
|
||||||
rmsg.Text = part.Text
|
rmsg.Text = part.Text
|
||||||
} else {
|
} else {
|
||||||
|
fname := b.Account + "_" + strconv.FormatInt(now.UnixNano(), 10) + "_" + strconv.Itoa(i) + part.FileExtension
|
||||||
rmsg.Extra = make(map[string][]interface{})
|
rmsg.Extra = make(map[string][]interface{})
|
||||||
if err = helper.HandleDownloadSize(b.Log, &rmsg, "image"+strconv.Itoa(i)+part.FileExtension, int64(len(part.Image)), b.General); err != nil {
|
if err = helper.HandleDownloadSize(b.Log, &rmsg, fname, int64(len(part.Image)), b.General); err != nil {
|
||||||
b.Log.WithError(err).Warn("not including image in message")
|
b.Log.WithError(err).Warn("not including image in message")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
helper.HandleDownloadData(b.Log, &rmsg, "image"+strconv.Itoa(i)+part.FileExtension, "", "", &part.Image, b.General)
|
helper.HandleDownloadData(b.Log, &rmsg, fname, "", "", &part.Image, b.General)
|
||||||
}
|
}
|
||||||
b.Log.Debugf("<= Remote message is %+v", rmsg)
|
|
||||||
b.Remote <- rmsg
|
b.Remote <- rmsg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,20 +83,3 @@ func (b *Bmumble) handleUserChange(event *gumble.UserChangeEvent) {
|
|||||||
func (b *Bmumble) handleDisconnect(event *gumble.DisconnectEvent) {
|
func (b *Bmumble) handleDisconnect(event *gumble.DisconnectEvent) {
|
||||||
b.connected <- *event
|
b.connected <- *event
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bmumble) makeDebugHandler() *gumbleutil.Listener {
|
|
||||||
handler := gumbleutil.Listener{
|
|
||||||
Connect: func(e *gumble.ConnectEvent) { b.Log.Debugf("Received connect event: %+v", e) },
|
|
||||||
Disconnect: func(e *gumble.DisconnectEvent) { b.Log.Debugf("Received disconnect event: %+v", e) },
|
|
||||||
TextMessage: func(e *gumble.TextMessageEvent) { b.Log.Debugf("Received textmessage event: %+v", e) },
|
|
||||||
UserChange: func(e *gumble.UserChangeEvent) { b.Log.Debugf("Received userchange event: %+v", e) },
|
|
||||||
ChannelChange: func(e *gumble.ChannelChangeEvent) { b.Log.Debugf("Received channelchange event: %+v", e) },
|
|
||||||
PermissionDenied: func(e *gumble.PermissionDeniedEvent) { b.Log.Debugf("Received permissiondenied event: %+v", e) },
|
|
||||||
UserList: func(e *gumble.UserListEvent) { b.Log.Debugf("Received userlist event: %+v", e) },
|
|
||||||
ACL: func(e *gumble.ACLEvent) { b.Log.Debugf("Received acl event: %+v", e) },
|
|
||||||
BanList: func(e *gumble.BanListEvent) { b.Log.Debugf("Received banlist event: %+v", e) },
|
|
||||||
ContextActionChange: func(e *gumble.ContextActionChangeEvent) { b.Log.Debugf("Received contextactionchange event: %+v", e) },
|
|
||||||
ServerConfig: func(e *gumble.ServerConfigEvent) { b.Log.Debugf("Received serverconfig event: %+v", e) },
|
|
||||||
}
|
|
||||||
return &handler
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ func (b *Bmumble) tokenize(t *string) ([]MessagePart, error) {
|
|||||||
tokens := p.FindStringSubmatch(remaining)
|
tokens := p.FindStringSubmatch(remaining)
|
||||||
|
|
||||||
if tokens == nil {
|
if tokens == nil {
|
||||||
b.Log.Debugf("Last text token: %s", remaining)
|
|
||||||
// no match -> remaining string is non-image text
|
// no match -> remaining string is non-image text
|
||||||
if len(remaining) > 0 {
|
if len(remaining) > 0 {
|
||||||
parts = append(parts, MessagePart{remaining, "", nil})
|
parts = append(parts, MessagePart{remaining, "", nil})
|
||||||
@@ -67,7 +66,6 @@ func (b *Bmumble) tokenize(t *string) ([]MessagePart, error) {
|
|||||||
remaining = tokens[3]
|
remaining = tokens[3]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
b.Log.Debugf("Raw data: URL: %s", uri)
|
|
||||||
err = b.decodeImage(uri, &parts)
|
err = b.decodeImage(uri, &parts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Log.WithError(err).Info("Decoding the image failed")
|
b.Log.WithError(err).Info("Decoding the image failed")
|
||||||
@@ -94,14 +92,43 @@ func (b *Bmumble) extractFiles(msg *config.Message) []config.Message {
|
|||||||
for _, f := range msg.Extra["file"] {
|
for _, f := range msg.Extra["file"] {
|
||||||
fi := f.(config.FileInfo)
|
fi := f.(config.FileInfo)
|
||||||
if fi.Data == nil || len(*fi.Data) == 0 {
|
if fi.Data == nil || len(*fi.Data) == 0 {
|
||||||
|
if len(fi.URL) > 0 {
|
||||||
|
// no data, send link instead
|
||||||
|
imsg := config.Message{
|
||||||
|
Text: fmt.Sprintf(`<a href="%s">%s</a>`, fi.URL, fi.URL),
|
||||||
|
Channel: msg.Channel,
|
||||||
|
Username: msg.Username,
|
||||||
|
UserID: msg.UserID,
|
||||||
|
Account: msg.Account,
|
||||||
|
Protocol: msg.Protocol,
|
||||||
|
Timestamp: msg.Timestamp,
|
||||||
|
Event: "mumble_image",
|
||||||
|
}
|
||||||
|
messages = append(messages, imsg)
|
||||||
|
} else {
|
||||||
// Mumble needs the raw data
|
// Mumble needs the raw data
|
||||||
b.Log.Info("Not forwarding file without local data")
|
b.Log.Info("Not forwarding file without local data")
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mimeType := http.DetectContentType(*fi.Data)
|
mimeType := http.DetectContentType(*fi.Data)
|
||||||
if !strings.HasPrefix(mimeType, "image/") {
|
if !strings.HasPrefix(mimeType, "image/") {
|
||||||
// Mumble only supports images
|
// Mumble only supports images, send link instead
|
||||||
|
if len(fi.URL) > 0 {
|
||||||
|
imsg := config.Message{
|
||||||
|
Text: fmt.Sprintf(`<a href="%s">%s</a>`, fi.URL, fi.URL),
|
||||||
|
Channel: msg.Channel,
|
||||||
|
Username: msg.Username,
|
||||||
|
UserID: msg.UserID,
|
||||||
|
Account: msg.Account,
|
||||||
|
Protocol: msg.Protocol,
|
||||||
|
Timestamp: msg.Timestamp,
|
||||||
|
Event: "mumble_image",
|
||||||
|
}
|
||||||
|
messages = append(messages, imsg)
|
||||||
|
} else {
|
||||||
b.Log.Infof("Not forwarding file of type %s", mimeType)
|
b.Log.Infof("Not forwarding file of type %s", mimeType)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mimeType = strings.TrimSpace(strings.Split(mimeType, ";")[0])
|
mimeType = strings.TrimSpace(strings.Split(mimeType, ";")[0])
|
||||||
@@ -120,7 +147,6 @@ func (b *Bmumble) extractFiles(msg *config.Message) []config.Message {
|
|||||||
Account: msg.Account,
|
Account: msg.Account,
|
||||||
Protocol: msg.Protocol,
|
Protocol: msg.Protocol,
|
||||||
Timestamp: msg.Timestamp,
|
Timestamp: msg.Timestamp,
|
||||||
Extra: make(map[string][]interface{}),
|
|
||||||
Event: "mumble_image",
|
Event: "mumble_image",
|
||||||
}
|
}
|
||||||
messages = append(messages, imsg)
|
messages = append(messages, imsg)
|
||||||
|
|||||||
@@ -173,9 +173,6 @@ func (b *Bmumble) doConnect() error {
|
|||||||
Disconnect: b.handleDisconnect,
|
Disconnect: b.handleDisconnect,
|
||||||
UserChange: b.handleUserChange,
|
UserChange: b.handleUserChange,
|
||||||
})
|
})
|
||||||
if b.GetInt("DebugLevel") == 0 {
|
|
||||||
gumbleConfig.Attach(b.makeDebugHandler())
|
|
||||||
}
|
|
||||||
gumbleConfig.Username = b.GetString("Nick")
|
gumbleConfig.Username = b.GetString("Nick")
|
||||||
if password := b.GetString("Password"); password != "" {
|
if password := b.GetString("Password"); password != "" {
|
||||||
gumbleConfig.Password = password
|
gumbleConfig.Password = password
|
||||||
@@ -225,7 +222,6 @@ func (b *Bmumble) processMessage(msg *config.Message) {
|
|||||||
// If this is a specially generated image message, send it unmodified
|
// If this is a specially generated image message, send it unmodified
|
||||||
if msg.Event == "mumble_image" {
|
if msg.Event == "mumble_image" {
|
||||||
if allowHTML {
|
if allowHTML {
|
||||||
b.Log.Debugf("Sending image message: %s%s", msg.Username, msg.Text)
|
|
||||||
b.client.Self.Channel.Send(msg.Username+msg.Text, false)
|
b.client.Self.Channel.Send(msg.Username+msg.Text, false)
|
||||||
} else {
|
} else {
|
||||||
b.Log.Info("Can't send image, server does not allow HTML messages")
|
b.Log.Info("Can't send image, server does not allow HTML messages")
|
||||||
@@ -253,7 +249,6 @@ func (b *Bmumble) processMessage(msg *config.Message) {
|
|||||||
}
|
}
|
||||||
// Send the individual lindes
|
// Send the individual lindes
|
||||||
for i := range msgLines {
|
for i := range msgLines {
|
||||||
b.Log.Debugf("Sending line: %s", msgLines[i])
|
|
||||||
b.client.Self.Channel.Send(msg.Username+msgLines[i], false)
|
b.client.Self.Channel.Send(msg.Username+msgLines[i], false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user