urlpreview.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bytes"
  19. "context"
  20. "encoding/json"
  21. "image"
  22. "net/http"
  23. "net/url"
  24. "regexp"
  25. "strings"
  26. "time"
  27. "github.com/tidwall/gjson"
  28. "golang.org/x/net/idna"
  29. "google.golang.org/protobuf/proto"
  30. "go.mau.fi/whatsmeow"
  31. waProto "go.mau.fi/whatsmeow/binary/proto"
  32. "maunium.net/go/mautrix"
  33. "maunium.net/go/mautrix/appservice"
  34. "maunium.net/go/mautrix/crypto/attachment"
  35. "maunium.net/go/mautrix/event"
  36. )
  37. type BeeperLinkPreview struct {
  38. mautrix.RespPreviewURL
  39. MatchedURL string `json:"matched_url"`
  40. ImageEncryption *event.EncryptedFileInfo `json:"beeper:image:encryption,omitempty"`
  41. }
  42. func (portal *Portal) convertURLPreviewToBeeper(intent *appservice.IntentAPI, source *User, msg *waProto.ExtendedTextMessage) []*BeeperLinkPreview {
  43. if msg.GetMatchedText() == "" {
  44. return []*BeeperLinkPreview{}
  45. }
  46. output := &BeeperLinkPreview{
  47. MatchedURL: msg.GetMatchedText(),
  48. RespPreviewURL: mautrix.RespPreviewURL{
  49. CanonicalURL: msg.GetCanonicalUrl(),
  50. Title: msg.GetTitle(),
  51. Description: msg.GetDescription(),
  52. },
  53. }
  54. if len(output.CanonicalURL) == 0 {
  55. output.CanonicalURL = output.MatchedURL
  56. }
  57. var thumbnailData []byte
  58. if msg.ThumbnailDirectPath != nil {
  59. var err error
  60. thumbnailData, err = source.Client.DownloadThumbnail(msg)
  61. if err != nil {
  62. portal.log.Warnfln("Failed to download thumbnail for link preview: %v", err)
  63. }
  64. }
  65. if thumbnailData == nil && msg.JpegThumbnail != nil {
  66. thumbnailData = msg.JpegThumbnail
  67. }
  68. if thumbnailData != nil {
  69. output.ImageHeight = int(msg.GetThumbnailHeight())
  70. output.ImageWidth = int(msg.GetThumbnailWidth())
  71. if output.ImageHeight == 0 || output.ImageWidth == 0 {
  72. src, _, err := image.Decode(bytes.NewReader(thumbnailData))
  73. if err == nil {
  74. imageBounds := src.Bounds()
  75. output.ImageWidth, output.ImageHeight = imageBounds.Max.X, imageBounds.Max.Y
  76. }
  77. }
  78. output.ImageSize = len(thumbnailData)
  79. output.ImageType = http.DetectContentType(thumbnailData)
  80. uploadData, uploadMime := thumbnailData, output.ImageType
  81. if portal.Encrypted {
  82. crypto := attachment.NewEncryptedFile()
  83. uploadData = crypto.Encrypt(uploadData)
  84. uploadMime = "application/octet-stream"
  85. output.ImageEncryption = &event.EncryptedFileInfo{EncryptedFile: *crypto}
  86. }
  87. resp, err := intent.UploadBytes(uploadData, uploadMime)
  88. if err != nil {
  89. portal.log.Warnfln("Failed to reupload thumbnail for link preview: %v", err)
  90. } else {
  91. if output.ImageEncryption != nil {
  92. output.ImageEncryption.URL = resp.ContentURI.CUString()
  93. } else {
  94. output.ImageURL = resp.ContentURI.CUString()
  95. }
  96. }
  97. }
  98. if msg.GetPreviewType() == waProto.ExtendedTextMessage_VIDEO {
  99. output.Type = "video.other"
  100. }
  101. return []*BeeperLinkPreview{output}
  102. }
  103. var URLRegex = regexp.MustCompile(`https?://[^\s/_*]+(?:/\S*)?`)
  104. func (portal *Portal) convertURLPreviewToWhatsApp(sender *User, evt *event.Event, dest *waProto.ExtendedTextMessage) bool {
  105. var preview *BeeperLinkPreview
  106. rawPreview := gjson.GetBytes(evt.Content.VeryRaw, `com\.beeper\.linkpreviews`)
  107. if rawPreview.Exists() && rawPreview.IsArray() {
  108. var previews []BeeperLinkPreview
  109. if err := json.Unmarshal([]byte(rawPreview.Raw), &previews); err != nil || len(previews) == 0 {
  110. return false
  111. }
  112. // WhatsApp only supports a single preview.
  113. preview = &previews[0]
  114. } else if portal.bridge.Config.Bridge.URLPreviews {
  115. if matchedURL := URLRegex.FindString(evt.Content.AsMessage().Body); len(matchedURL) == 0 {
  116. return false
  117. } else if parsed, err := url.Parse(matchedURL); err != nil {
  118. return false
  119. } else if parsed.Host, err = idna.ToASCII(parsed.Host); err != nil {
  120. return false
  121. } else if mxPreview, err := portal.MainIntent().GetURLPreview(parsed.String()); err != nil {
  122. portal.log.Warnfln("Failed to fetch preview for %s: %v", matchedURL, err)
  123. return false
  124. } else {
  125. preview = &BeeperLinkPreview{
  126. RespPreviewURL: *mxPreview,
  127. MatchedURL: matchedURL,
  128. }
  129. }
  130. }
  131. if preview == nil || len(preview.MatchedURL) == 0 {
  132. return false
  133. }
  134. dest.MatchedText = &preview.MatchedURL
  135. if len(preview.CanonicalURL) > 0 {
  136. dest.CanonicalUrl = &preview.CanonicalURL
  137. }
  138. if len(preview.Description) > 0 {
  139. dest.Description = &preview.Description
  140. }
  141. if len(preview.Title) > 0 {
  142. dest.Title = &preview.Title
  143. }
  144. if strings.HasPrefix(preview.Type, "video.") {
  145. dest.PreviewType = waProto.ExtendedTextMessage_VIDEO.Enum()
  146. }
  147. imageMXC := preview.ImageURL.ParseOrIgnore()
  148. if preview.ImageEncryption != nil {
  149. imageMXC = preview.ImageEncryption.URL.ParseOrIgnore()
  150. }
  151. if !imageMXC.IsEmpty() {
  152. data, err := portal.MainIntent().DownloadBytes(imageMXC)
  153. if err != nil {
  154. portal.log.Errorfln("Failed to download URL preview image %s in %s: %v", preview.ImageURL, evt.ID, err)
  155. return true
  156. }
  157. if preview.ImageEncryption != nil {
  158. data, err = preview.ImageEncryption.Decrypt(data)
  159. if err != nil {
  160. portal.log.Errorfln("Failed to decrypt URL preview image in %s: %v", evt.ID, err)
  161. return true
  162. }
  163. }
  164. dest.MediaKeyTimestamp = proto.Int64(time.Now().Unix())
  165. uploadResp, err := sender.Client.Upload(context.Background(), data, whatsmeow.MediaLinkThumbnail)
  166. if err != nil {
  167. portal.log.Errorfln("Failed to upload URL preview thumbnail in %s: %v", evt.ID, err)
  168. return true
  169. }
  170. dest.ThumbnailSha256 = uploadResp.FileSHA256
  171. dest.ThumbnailEncSha256 = uploadResp.FileEncSHA256
  172. dest.ThumbnailDirectPath = &uploadResp.DirectPath
  173. dest.MediaKey = uploadResp.MediaKey
  174. var width, height int
  175. dest.JpegThumbnail, width, height, err = createJPEGThumbnailAndGetSize(data)
  176. if err != nil {
  177. portal.log.Warnfln("Failed to create JPEG thumbnail for URL preview in %s: %v", evt.ID, err)
  178. }
  179. if preview.ImageHeight > 0 && preview.ImageWidth > 0 {
  180. dest.ThumbnailWidth = proto.Uint32(uint32(preview.ImageWidth))
  181. dest.ThumbnailHeight = proto.Uint32(uint32(preview.ImageHeight))
  182. } else if width > 0 && height > 0 {
  183. dest.ThumbnailWidth = proto.Uint32(uint32(width))
  184. dest.ThumbnailHeight = proto.Uint32(uint32(height))
  185. }
  186. }
  187. return true
  188. }