From a9dff6da7391297b64bb5be473b76c3c590f34a1 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 29 Apr 2020 02:45:54 +0300 Subject: Add support for encrypted files --- ui/messages/filemessage.go | 59 +++++++++++++++++++++++++++++----------------- ui/messages/parser.go | 4 ++-- ui/room-view.go | 11 ++++++--- 3 files changed, 48 insertions(+), 26 deletions(-) (limited to 'ui') diff --git a/ui/messages/filemessage.go b/ui/messages/filemessage.go index 7422146..d2455ab 100644 --- a/ui/messages/filemessage.go +++ b/ui/messages/filemessage.go @@ -22,7 +22,7 @@ import ( "image" "image/color" - "maunium.net/go/gomuks/matrix/muksevt" + "maunium.net/go/mautrix/crypto/attachment" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" "maunium.net/go/mauview" @@ -32,14 +32,19 @@ import ( "maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/lib/ansimage" + "maunium.net/go/gomuks/matrix/muksevt" "maunium.net/go/gomuks/ui/messages/tstring" ) type FileMessage struct { - Type event.MessageType - Body string - URL id.ContentURI - Thumbnail id.ContentURI + Type event.MessageType + Body string + + URL id.ContentURI + File *attachment.EncryptedFile + Thumbnail id.ContentURI + ThumbnailFile *attachment.EncryptedFile + imageData []byte buffer []tstring.TString @@ -49,14 +54,23 @@ type FileMessage struct { // NewFileMessage creates a new FileMessage object with the provided values and the default state. func NewFileMessage(matrix ifc.MatrixContainer, evt *muksevt.Event, displayname string) *UIMessage { content := evt.Content.AsMessage() - url, _ := content.URL.Parse() - thumbnail, _ := content.GetInfo().ThumbnailURL.Parse() + var file, thumbnailFile *attachment.EncryptedFile + if content.File != nil { + file = &content.File.EncryptedFile + content.URL = content.File.URL + } + if content.GetInfo().ThumbnailFile != nil { + thumbnailFile = &content.Info.ThumbnailFile.EncryptedFile + content.Info.ThumbnailURL = content.Info.ThumbnailFile.URL + } return newUIMessage(evt, displayname, &FileMessage{ - Type: content.MsgType, - Body: content.Body, - URL: url, - Thumbnail: thumbnail, - matrix: matrix, + Type: content.MsgType, + Body: content.Body, + URL: content.URL, + File: file, + Thumbnail: content.GetInfo().ThumbnailURL, + ThumbnailFile: thumbnailFile, + matrix: matrix, }) } @@ -96,17 +110,20 @@ func (msg *FileMessage) String() string { } func (msg *FileMessage) DownloadPreview() { - url := msg.Thumbnail - if url.IsEmpty() { - if msg.Type == event.MsgImage && !msg.URL.IsEmpty() { - msg.Thumbnail = msg.URL - url = msg.Thumbnail - } else { - return - } + var url id.ContentURI + var file *attachment.EncryptedFile + if !msg.Thumbnail.IsEmpty() { + url = msg.Thumbnail + file = msg.ThumbnailFile + } else if msg.Type == event.MsgImage && !msg.URL.IsEmpty() { + msg.Thumbnail = msg.URL + url = msg.URL + file = msg.File + } else { + return } debug.Print("Loading file:", url) - data, err := msg.matrix.Download(url) + data, err := msg.matrix.Download(url, file) if err != nil { debug.Printf("Failed to download file %s: %v", url, err) return diff --git a/ui/messages/parser.go b/ui/messages/parser.go index 9e44647..8cc6209 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -20,13 +20,13 @@ import ( "fmt" "strings" - "maunium.net/go/gomuks/debug" - "maunium.net/go/gomuks/matrix/muksevt" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" "maunium.net/go/tcell" + "maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/interface" + "maunium.net/go/gomuks/matrix/muksevt" "maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/ui/messages/html" "maunium.net/go/gomuks/ui/messages/tstring" diff --git a/ui/room-view.go b/ui/room-view.go index 5792626..09e1965 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -26,6 +26,7 @@ import ( "github.com/kyokomi/emoji" "github.com/mattn/go-runewidth" + "maunium.net/go/mautrix/crypto/attachment" "maunium.net/go/mauview" "maunium.net/go/tcell" @@ -122,6 +123,10 @@ func NewRoomView(parent *MainView, room *rooms.Room) *RoomView { SetPressKeyUpAtStartFunc(view.EditPrevious). SetPressKeyDownAtEndFunc(view.EditNext) + if room.Encrypted { + view.input.SetPlaceholder("Send an encrypted message...") + } + view.topic. SetTextColor(tcell.ColorWhite). SetBackgroundColor(tcell.ColorDarkGreen) @@ -202,7 +207,7 @@ func (view *RoomView) OnSelect(message *messages.UIMessage) { } else if view.selectReason == SelectDownload { path = msg.Body } - go view.Download(msg.URL, path, view.selectReason == SelectOpen) + go view.Download(msg.URL, msg.File, path, view.selectReason == SelectOpen) } } view.selecting = false @@ -624,8 +629,8 @@ func (view *RoomView) InputSubmit(text string) { view.SetInputText("") } -func (view *RoomView) Download(url id.ContentURI, filename string, openFile bool) { - path, err := view.parent.matrix.DownloadToDisk(url, filename) +func (view *RoomView) Download(url id.ContentURI, file *attachment.EncryptedFile, filename string, openFile bool) { + path, err := view.parent.matrix.DownloadToDisk(url, file, filename) if err != nil { view.AddServiceMessage(fmt.Sprintf("Failed to download media: %v", err)) view.parent.parent.Render() -- cgit v1.2.3 From 22681875f32fa97f65c9a52e2ee666932706ce95 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 5 May 2020 18:39:28 +0300 Subject: Update mautrix-go and give crypto module access to state store --- ui/messages/filemessage.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ui') diff --git a/ui/messages/filemessage.go b/ui/messages/filemessage.go index d2455ab..3d5e554 100644 --- a/ui/messages/filemessage.go +++ b/ui/messages/filemessage.go @@ -66,9 +66,9 @@ func NewFileMessage(matrix ifc.MatrixContainer, evt *muksevt.Event, displayname return newUIMessage(evt, displayname, &FileMessage{ Type: content.MsgType, Body: content.Body, - URL: content.URL, + URL: content.URL.ParseOrIgnore(), File: file, - Thumbnail: content.GetInfo().ThumbnailURL, + Thumbnail: content.GetInfo().ThumbnailURL.ParseOrIgnore(), ThumbnailFile: thumbnailFile, matrix: matrix, }) -- cgit v1.2.3 From 0988b0590cf9c2909131e86a56a1ad9ed1745431 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 5 May 2020 20:38:58 +0300 Subject: Make e2ee optional --- ui/messages/parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ui') diff --git a/ui/messages/parser.go b/ui/messages/parser.go index 8cc6209..4a214f1 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -82,7 +82,7 @@ func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt } return ParseMessage(matrix, room, evt, displayname) case *event.EncryptedEventContent: - return NewExpandedTextMessage(evt, displayname, tstring.NewStyleTString("Encrypted messages are not yet supported", tcell.StyleDefault.Italic(true))) + return NewExpandedTextMessage(evt, displayname, tstring.NewStyleTString("Decryption failed or gomuks not built with encryption support", tcell.StyleDefault.Italic(true))) case *event.TopicEventContent, *event.RoomNameEventContent, *event.CanonicalAliasEventContent: return ParseStateEvent(evt, displayname) case *event.MemberEventContent: -- cgit v1.2.3