2022-02-04 21:19:55 +01:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
|
|
|
// Copyright (C) 2022 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"image"
|
|
|
|
"net/http"
|
2022-02-15 15:28:20 +01:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
2022-02-04 22:06:35 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-02-04 21:19:55 +01:00
|
|
|
|
|
|
|
"github.com/tidwall/gjson"
|
2022-02-15 15:28:20 +01:00
|
|
|
"golang.org/x/net/idna"
|
2022-02-04 21:19:55 +01:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
|
|
|
"go.mau.fi/whatsmeow"
|
|
|
|
waProto "go.mau.fi/whatsmeow/binary/proto"
|
2022-04-27 13:31:57 +02:00
|
|
|
|
2022-02-15 15:28:20 +01:00
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/mautrix/appservice"
|
|
|
|
"maunium.net/go/mautrix/crypto/attachment"
|
|
|
|
"maunium.net/go/mautrix/event"
|
2022-02-04 21:19:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type BeeperLinkPreview struct {
|
2022-02-15 15:28:20 +01:00
|
|
|
mautrix.RespPreviewURL
|
|
|
|
MatchedURL string `json:"matched_url"`
|
2022-02-04 22:06:35 +01:00
|
|
|
ImageEncryption *event.EncryptedFileInfo `json:"beeper:image:encryption,omitempty"`
|
2022-02-04 21:19:55 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 06:30:00 +01:00
|
|
|
func (portal *Portal) convertURLPreviewToBeeper(intent *appservice.IntentAPI, source *User, msg *waProto.ExtendedTextMessage) []*BeeperLinkPreview {
|
2022-02-04 21:19:55 +01:00
|
|
|
if msg.GetMatchedText() == "" {
|
2022-02-07 06:30:00 +01:00
|
|
|
return []*BeeperLinkPreview{}
|
2022-02-04 21:19:55 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 06:30:00 +01:00
|
|
|
output := &BeeperLinkPreview{
|
2022-02-15 15:28:20 +01:00
|
|
|
MatchedURL: msg.GetMatchedText(),
|
|
|
|
RespPreviewURL: mautrix.RespPreviewURL{
|
|
|
|
CanonicalURL: msg.GetCanonicalUrl(),
|
|
|
|
Title: msg.GetTitle(),
|
|
|
|
Description: msg.GetDescription(),
|
|
|
|
},
|
2022-02-04 21:19:55 +01:00
|
|
|
}
|
|
|
|
if len(output.CanonicalURL) == 0 {
|
|
|
|
output.CanonicalURL = output.MatchedURL
|
|
|
|
}
|
|
|
|
|
|
|
|
var thumbnailData []byte
|
|
|
|
if msg.ThumbnailDirectPath != nil {
|
|
|
|
var err error
|
|
|
|
thumbnailData, err = source.Client.DownloadThumbnail(msg)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to download thumbnail for link preview: %v", err)
|
|
|
|
}
|
2022-02-15 12:37:05 +01:00
|
|
|
}
|
|
|
|
if thumbnailData == nil && msg.JpegThumbnail != nil {
|
2022-02-04 21:19:55 +01:00
|
|
|
thumbnailData = msg.JpegThumbnail
|
|
|
|
}
|
|
|
|
if thumbnailData != nil {
|
2022-02-04 22:06:35 +01:00
|
|
|
output.ImageHeight = int(msg.GetThumbnailHeight())
|
|
|
|
output.ImageWidth = int(msg.GetThumbnailWidth())
|
|
|
|
if output.ImageHeight == 0 || output.ImageWidth == 0 {
|
|
|
|
src, _, err := image.Decode(bytes.NewReader(thumbnailData))
|
|
|
|
if err == nil {
|
|
|
|
imageBounds := src.Bounds()
|
|
|
|
output.ImageWidth, output.ImageHeight = imageBounds.Max.X, imageBounds.Max.Y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.ImageSize = len(thumbnailData)
|
|
|
|
output.ImageType = http.DetectContentType(thumbnailData)
|
|
|
|
uploadData, uploadMime := thumbnailData, output.ImageType
|
|
|
|
if portal.Encrypted {
|
|
|
|
crypto := attachment.NewEncryptedFile()
|
2022-04-27 18:04:34 +02:00
|
|
|
crypto.EncryptInPlace(uploadData)
|
2022-02-04 22:06:35 +01:00
|
|
|
uploadMime = "application/octet-stream"
|
|
|
|
output.ImageEncryption = &event.EncryptedFileInfo{EncryptedFile: *crypto}
|
|
|
|
}
|
|
|
|
resp, err := intent.UploadBytes(uploadData, uploadMime)
|
2022-02-04 21:19:55 +01:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to reupload thumbnail for link preview: %v", err)
|
|
|
|
} else {
|
2022-02-04 22:06:35 +01:00
|
|
|
if output.ImageEncryption != nil {
|
|
|
|
output.ImageEncryption.URL = resp.ContentURI.CUString()
|
|
|
|
} else {
|
|
|
|
output.ImageURL = resp.ContentURI.CUString()
|
2022-02-04 21:19:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if msg.GetPreviewType() == waProto.ExtendedTextMessage_VIDEO {
|
|
|
|
output.Type = "video.other"
|
|
|
|
}
|
|
|
|
|
2022-02-07 06:30:00 +01:00
|
|
|
return []*BeeperLinkPreview{output}
|
2022-02-04 21:19:55 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 15:28:20 +01:00
|
|
|
var URLRegex = regexp.MustCompile(`https?://[^\s/_*]+(?:/\S*)?`)
|
|
|
|
|
2022-06-29 19:05:55 +02:00
|
|
|
func (portal *Portal) convertURLPreviewToWhatsApp(ctx context.Context, sender *User, evt *event.Event, dest *waProto.ExtendedTextMessage) bool {
|
2022-02-15 15:28:20 +01:00
|
|
|
var preview *BeeperLinkPreview
|
|
|
|
|
2022-02-05 02:17:01 +01:00
|
|
|
rawPreview := gjson.GetBytes(evt.Content.VeryRaw, `com\.beeper\.linkpreviews`)
|
2022-02-15 15:28:20 +01:00
|
|
|
if rawPreview.Exists() && rawPreview.IsArray() {
|
|
|
|
var previews []BeeperLinkPreview
|
|
|
|
if err := json.Unmarshal([]byte(rawPreview.Raw), &previews); err != nil || len(previews) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// WhatsApp only supports a single preview.
|
|
|
|
preview = &previews[0]
|
|
|
|
} else if portal.bridge.Config.Bridge.URLPreviews {
|
|
|
|
if matchedURL := URLRegex.FindString(evt.Content.AsMessage().Body); len(matchedURL) == 0 {
|
|
|
|
return false
|
|
|
|
} else if parsed, err := url.Parse(matchedURL); err != nil {
|
|
|
|
return false
|
|
|
|
} else if parsed.Host, err = idna.ToASCII(parsed.Host); err != nil {
|
|
|
|
return false
|
|
|
|
} else if mxPreview, err := portal.MainIntent().GetURLPreview(parsed.String()); err != nil {
|
|
|
|
portal.log.Warnfln("Failed to fetch preview for %s: %v", matchedURL, err)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
preview = &BeeperLinkPreview{
|
|
|
|
RespPreviewURL: *mxPreview,
|
|
|
|
MatchedURL: matchedURL,
|
|
|
|
}
|
|
|
|
}
|
2022-02-05 02:17:01 +01:00
|
|
|
}
|
2022-02-15 15:28:20 +01:00
|
|
|
if preview == nil || len(preview.MatchedURL) == 0 {
|
|
|
|
return false
|
2022-02-04 21:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dest.MatchedText = &preview.MatchedURL
|
|
|
|
if len(preview.CanonicalURL) > 0 {
|
|
|
|
dest.CanonicalUrl = &preview.CanonicalURL
|
|
|
|
}
|
|
|
|
if len(preview.Description) > 0 {
|
|
|
|
dest.Description = &preview.Description
|
|
|
|
}
|
|
|
|
if len(preview.Title) > 0 {
|
|
|
|
dest.Title = &preview.Title
|
|
|
|
}
|
2022-02-04 22:06:35 +01:00
|
|
|
if strings.HasPrefix(preview.Type, "video.") {
|
|
|
|
dest.PreviewType = waProto.ExtendedTextMessage_VIDEO.Enum()
|
|
|
|
}
|
|
|
|
imageMXC := preview.ImageURL.ParseOrIgnore()
|
|
|
|
if preview.ImageEncryption != nil {
|
|
|
|
imageMXC = preview.ImageEncryption.URL.ParseOrIgnore()
|
|
|
|
}
|
2022-02-04 21:19:55 +01:00
|
|
|
if !imageMXC.IsEmpty() {
|
2022-06-29 19:05:55 +02:00
|
|
|
data, err := portal.MainIntent().DownloadBytesContext(ctx, imageMXC)
|
2022-02-04 21:19:55 +01:00
|
|
|
if err != nil {
|
2022-02-04 22:06:35 +01:00
|
|
|
portal.log.Errorfln("Failed to download URL preview image %s in %s: %v", preview.ImageURL, evt.ID, err)
|
2022-02-15 15:28:20 +01:00
|
|
|
return true
|
2022-02-04 21:19:55 +01:00
|
|
|
}
|
2022-02-04 22:06:35 +01:00
|
|
|
if preview.ImageEncryption != nil {
|
2022-04-27 18:04:34 +02:00
|
|
|
err = preview.ImageEncryption.DecryptInPlace(data)
|
2022-02-04 22:06:35 +01:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to decrypt URL preview image in %s: %v", evt.ID, err)
|
2022-02-15 15:28:20 +01:00
|
|
|
return true
|
2022-02-04 22:06:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dest.MediaKeyTimestamp = proto.Int64(time.Now().Unix())
|
2022-06-29 19:05:55 +02:00
|
|
|
uploadResp, err := sender.Client.Upload(ctx, data, whatsmeow.MediaLinkThumbnail)
|
2022-02-04 21:19:55 +01:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to upload URL preview thumbnail in %s: %v", evt.ID, err)
|
2022-02-15 15:28:20 +01:00
|
|
|
return true
|
2022-02-04 21:19:55 +01:00
|
|
|
}
|
|
|
|
dest.ThumbnailSha256 = uploadResp.FileSHA256
|
|
|
|
dest.ThumbnailEncSha256 = uploadResp.FileEncSHA256
|
|
|
|
dest.ThumbnailDirectPath = &uploadResp.DirectPath
|
|
|
|
dest.MediaKey = uploadResp.MediaKey
|
|
|
|
var width, height int
|
2022-08-22 14:00:01 +02:00
|
|
|
dest.JpegThumbnail, width, height, err = createThumbnailAndGetSize(data, false)
|
2022-02-04 21:19:55 +01:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to create JPEG thumbnail for URL preview in %s: %v", evt.ID, err)
|
|
|
|
}
|
|
|
|
if preview.ImageHeight > 0 && preview.ImageWidth > 0 {
|
|
|
|
dest.ThumbnailWidth = proto.Uint32(uint32(preview.ImageWidth))
|
|
|
|
dest.ThumbnailHeight = proto.Uint32(uint32(preview.ImageHeight))
|
|
|
|
} else if width > 0 && height > 0 {
|
|
|
|
dest.ThumbnailWidth = proto.Uint32(uint32(width))
|
|
|
|
dest.ThumbnailHeight = proto.Uint32(uint32(height))
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 15:28:20 +01:00
|
|
|
return true
|
2022-02-04 21:19:55 +01:00
|
|
|
}
|