123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
- // Copyright (C) 2018 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"
- "encoding/hex"
- "fmt"
- "html"
- "image"
- "io"
- "math/rand"
- "mime"
- "net/http"
- "regexp"
- "strings"
- "sync"
- "github.com/Rhymen/go-whatsapp"
- "maunium.net/go/gomatrix"
- "maunium.net/go/gomatrix/format"
- log "maunium.net/go/maulogger"
- "maunium.net/go/mautrix-appservice"
- "maunium.net/go/mautrix-whatsapp/database"
- "maunium.net/go/mautrix-whatsapp/types"
- "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
- )
- func (user *User) GetPortalByMXID(mxid types.MatrixRoomID) *Portal {
- user.portalsLock.Lock()
- defer user.portalsLock.Unlock()
- portal, ok := user.portalsByMXID[mxid]
- if !ok {
- dbPortal := user.bridge.DB.Portal.GetByMXID(mxid)
- if dbPortal == nil || dbPortal.Owner != user.ID {
- return nil
- }
- portal = user.NewPortal(dbPortal)
- user.portalsByJID[portal.JID] = portal
- if len(portal.MXID) > 0 {
- user.portalsByMXID[portal.MXID] = portal
- }
- }
- return portal
- }
- func (user *User) GetPortalByJID(jid types.WhatsAppID) *Portal {
- user.portalsLock.Lock()
- defer user.portalsLock.Unlock()
- portal, ok := user.portalsByJID[jid]
- if !ok {
- dbPortal := user.bridge.DB.Portal.GetByJID(user.ID, jid)
- if dbPortal == nil {
- dbPortal = user.bridge.DB.Portal.New()
- dbPortal.JID = jid
- dbPortal.Owner = user.ID
- dbPortal.Insert()
- }
- portal = user.NewPortal(dbPortal)
- user.portalsByJID[portal.JID] = portal
- if len(portal.MXID) > 0 {
- user.portalsByMXID[portal.MXID] = portal
- }
- }
- return portal
- }
- func (user *User) GetAllPortals() []*Portal {
- user.portalsLock.Lock()
- defer user.portalsLock.Unlock()
- dbPortals := user.bridge.DB.Portal.GetAll(user.ID)
- output := make([]*Portal, len(dbPortals))
- for index, dbPortal := range dbPortals {
- portal, ok := user.portalsByJID[dbPortal.JID]
- if !ok {
- portal = user.NewPortal(dbPortal)
- user.portalsByJID[dbPortal.JID] = portal
- if len(dbPortal.MXID) > 0 {
- user.portalsByMXID[dbPortal.MXID] = portal
- }
- }
- output[index] = portal
- }
- return output
- }
- func (user *User) NewPortal(dbPortal *database.Portal) *Portal {
- return &Portal{
- Portal: dbPortal,
- user: user,
- bridge: user.bridge,
- log: user.log.Sub(fmt.Sprintf("Portal/%s", dbPortal.JID)),
- }
- }
- type Portal struct {
- *database.Portal
- user *User
- bridge *Bridge
- log log.Logger
- roomCreateLock sync.Mutex
- }
- func (portal *Portal) SyncParticipants(metadata *whatsapp_ext.GroupInfo) {
- for _, participant := range metadata.Participants {
- intent := portal.user.GetPuppetByJID(participant.JID).Intent()
- intent.EnsureJoined(portal.MXID)
- }
- }
- func (portal *Portal) UpdateAvatar() bool {
- avatar, err := portal.user.Conn.GetProfilePicThumb(portal.JID)
- if err != nil {
- portal.log.Errorln(err)
- return false
- }
- if portal.Avatar == avatar.Tag {
- return false
- }
- data, err := avatar.DownloadBytes()
- if err != nil {
- portal.log.Errorln("Failed to download avatar:", err)
- return false
- }
- mimeType := http.DetectContentType(data)
- resp, err := portal.MainIntent().UploadBytes(data, mimeType)
- if err != nil {
- portal.log.Errorln("Failed to upload avatar:", err)
- return false
- }
- _, err = portal.MainIntent().SetRoomAvatar(portal.MXID, resp.ContentURI)
- if err != nil {
- portal.log.Warnln("Failed to set room topic:", err)
- return false
- }
- portal.Avatar = avatar.Tag
- return true
- }
- func (portal *Portal) UpdateName(metadata *whatsapp_ext.GroupInfo) bool {
- if portal.Name != metadata.Name {
- _, err := portal.MainIntent().SetRoomName(portal.MXID, metadata.Name)
- if err == nil {
- portal.Name = metadata.Name
- return true
- }
- portal.log.Warnln("Failed to set room name:", err)
- }
- return false
- }
- func (portal *Portal) UpdateTopic(metadata *whatsapp_ext.GroupInfo) bool {
- if portal.Topic != metadata.Topic {
- _, err := portal.MainIntent().SetRoomTopic(portal.MXID, metadata.Topic)
- if err == nil {
- portal.Topic = metadata.Topic
- return true
- }
- portal.log.Warnln("Failed to set room topic:", err)
- }
- return false
- }
- func (portal *Portal) UpdateMetadata() bool {
- metadata, err := portal.user.Conn.GetGroupMetaData(portal.JID)
- if err != nil {
- portal.log.Errorln(err)
- return false
- }
- portal.SyncParticipants(metadata)
- update := false
- update = portal.UpdateName(metadata) || update
- update = portal.UpdateTopic(metadata) || update
- return update
- }
- func (portal *Portal) Sync(contact whatsapp.Contact) {
- if len(portal.MXID) == 0 {
- if !portal.IsPrivateChat() {
- portal.Name = contact.Name
- }
- err := portal.CreateMatrixRoom()
- if err != nil {
- portal.log.Errorln("Failed to create portal room:", err)
- return
- }
- }
- if portal.IsPrivateChat() {
- return
- }
- update := false
- update = portal.UpdateMetadata() || update
- update = portal.UpdateAvatar() || update
- if update {
- portal.Update()
- }
- }
- func (portal *Portal) CreateMatrixRoom() error {
- portal.roomCreateLock.Lock()
- defer portal.roomCreateLock.Unlock()
- if len(portal.MXID) > 0 {
- return nil
- }
- name := portal.Name
- topic := portal.Topic
- isPrivateChat := false
- invite := []string{portal.user.ID}
- if portal.IsPrivateChat() {
- name = ""
- topic = "WhatsApp private chat"
- isPrivateChat = true
- }
- resp, err := portal.MainIntent().CreateRoom(&gomatrix.ReqCreateRoom{
- Visibility: "private",
- Name: name,
- Topic: topic,
- Invite: invite,
- Preset: "private_chat",
- IsDirect: isPrivateChat,
- })
- if err != nil {
- return err
- }
- portal.MXID = resp.RoomID
- portal.Update()
- return nil
- }
- func (portal *Portal) IsPrivateChat() bool {
- return strings.HasSuffix(portal.JID, whatsapp_ext.NewUserSuffix)
- }
- func (portal *Portal) MainIntent() *appservice.IntentAPI {
- if portal.IsPrivateChat() {
- return portal.user.GetPuppetByJID(portal.JID).Intent()
- }
- return portal.bridge.AppService.BotIntent()
- }
- func (portal *Portal) IsDuplicate(id types.WhatsAppMessageID) bool {
- msg := portal.bridge.DB.Message.GetByJID(portal.Owner, id)
- if msg != nil {
- return true
- }
- return false
- }
- func (portal *Portal) MarkHandled(jid types.WhatsAppMessageID, mxid types.MatrixEventID) {
- msg := portal.bridge.DB.Message.New()
- msg.Owner = portal.Owner
- msg.JID = jid
- msg.MXID = mxid
- msg.Insert()
- }
- func (portal *Portal) GetMessageIntent(info whatsapp.MessageInfo) *appservice.IntentAPI {
- if info.FromMe {
- portal.log.Debugln("Unhandled message from me:", info.Id)
- return nil
- } else if portal.IsPrivateChat() {
- return portal.MainIntent()
- }
- puppet := portal.user.GetPuppetByJID(info.SenderJid)
- return puppet.Intent()
- }
- func (portal *Portal) SetReply(content *gomatrix.Content, info whatsapp.MessageInfo) {
- if len(info.QuotedMessageID) == 0 {
- return
- }
- message := portal.bridge.DB.Message.GetByJID(portal.Owner, info.QuotedMessageID)
- if message != nil {
- event, err := portal.MainIntent().GetEvent(portal.MXID, message.MXID)
- if err != nil {
- portal.log.Warnln("Failed to get reply target:", err)
- return
- }
- content.SetReply(event)
- }
- return
- }
- var codeBlockRegex = regexp.MustCompile("```((?:.|\n)+?)```")
- var italicRegex = regexp.MustCompile("([\\s>~*]|^)_(.+?)_([^a-zA-Z\\d]|$)")
- var boldRegex = regexp.MustCompile("([\\s>_~]|^)\\*(.+?)\\*([^a-zA-Z\\d]|$)")
- var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)")
- var whatsAppFormat = map[*regexp.Regexp]string{
- italicRegex: "$1<em>$2</em>$3",
- boldRegex: "$1<strong>$2</strong>$3",
- strikethroughRegex: "$1<del>$2</del>$3",
- }
- func (portal *Portal) ParseWhatsAppFormat(input string) string {
- output := html.EscapeString(input)
- for regex, replacement := range whatsAppFormat {
- output = regex.ReplaceAllString(output, replacement)
- }
- output = codeBlockRegex.ReplaceAllStringFunc(output, func(str string) string {
- str = str[3 : len(str)-3]
- if strings.ContainsRune(str, '\n') {
- return fmt.Sprintf("<pre><code>%s</code></pre>", str)
- }
- return fmt.Sprintf("<code>%s</code>", str)
- })
- return output
- }
- func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
- if portal.IsDuplicate(message.Info.Id) {
- return
- }
- err := portal.CreateMatrixRoom()
- if err != nil {
- portal.log.Errorln("Failed to create portal room:", err)
- return
- }
- intent := portal.GetMessageIntent(message.Info)
- if intent == nil {
- return
- }
- content := gomatrix.Content{
- Body: message.Text,
- MsgType: gomatrix.MsgText,
- }
- htmlBody := portal.ParseWhatsAppFormat(message.Text)
- if htmlBody != message.Text {
- content.FormattedBody = htmlBody
- content.Format = gomatrix.FormatHTML
- }
- portal.SetReply(&content, message.Info)
- intent.UserTyping(portal.MXID, false, 0)
- resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(message.Info.Timestamp*1000))
- if err != nil {
- portal.log.Errorfln("Failed to handle message %s: %v", message.Info.Id, err)
- return
- }
- portal.MarkHandled(message.Info.Id, resp.EventID)
- portal.log.Debugln("Handled message", message.Info.Id, "->", resp.EventID)
- }
- func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbnail []byte, info whatsapp.MessageInfo, mimeType, caption string) {
- if portal.IsDuplicate(info.Id) {
- return
- }
- err := portal.CreateMatrixRoom()
- if err != nil {
- portal.log.Errorln("Failed to create portal room:", err)
- return
- }
- intent := portal.GetMessageIntent(info)
- if intent == nil {
- return
- }
- data, err := download()
- if err != nil {
- portal.log.Errorln("Failed to download media:", err)
- return
- }
- uploaded, err := intent.UploadBytes(data, mimeType)
- if err != nil {
- portal.log.Errorln("Failed to upload media:", err)
- return
- }
- if len(caption) == 0 {
- caption = info.Id
- exts, _ := mime.ExtensionsByType(mimeType)
- if exts != nil && len(exts) > 0 {
- caption += exts[0]
- }
- }
- content := gomatrix.Content{
- Body: caption,
- URL: uploaded.ContentURI,
- Info: &gomatrix.FileInfo{
- Size: len(data),
- MimeType: mimeType,
- },
- }
- portal.SetReply(&content, info)
- if thumbnail != nil {
- thumbnailMime := http.DetectContentType(thumbnail)
- uploadedThumbnail, _ := intent.UploadBytes(thumbnail, thumbnailMime)
- if uploadedThumbnail != nil {
- content.Info.ThumbnailURL = uploadedThumbnail.ContentURI
- cfg, _, _ := image.DecodeConfig(bytes.NewReader(data))
- content.Info.ThumbnailInfo = &gomatrix.FileInfo{
- Size: len(thumbnail),
- Width: cfg.Width,
- Height: cfg.Height,
- MimeType: thumbnailMime,
- }
- }
- }
- switch strings.ToLower(strings.Split(mimeType, "/")[0]) {
- case "image":
- content.MsgType = gomatrix.MsgImage
- cfg, _, _ := image.DecodeConfig(bytes.NewReader(data))
- content.Info.Width = cfg.Width
- content.Info.Height = cfg.Height
- case "video":
- content.MsgType = gomatrix.MsgVideo
- case "audio":
- content.MsgType = gomatrix.MsgAudio
- default:
- content.MsgType = gomatrix.MsgFile
- }
- intent.UserTyping(portal.MXID, false, 0)
- resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(info.Timestamp*1000))
- if err != nil {
- portal.log.Errorfln("Failed to handle message %s: %v", info.Id, err)
- return
- }
- portal.MarkHandled(info.Id, resp.EventID)
- portal.log.Debugln("Handled message", info.Id, "->", resp.EventID)
- }
- var htmlParser = format.HTMLParser{
- TabsToSpaces: 4,
- Newline: "\n",
- PillConverter: func(mxid, eventID string) string {
- return mxid
- },
- BoldConverter: func(text string) string {
- return fmt.Sprintf("*%s*", text)
- },
- ItalicConverter: func(text string) string {
- return fmt.Sprintf("_%s_", text)
- },
- StrikethroughConverter: func(text string) string {
- return fmt.Sprintf("~%s~", text)
- },
- MonospaceConverter: func(text string) string {
- return fmt.Sprintf("```%s```", text)
- },
- MonospaceBlockConverter: func(text string) string {
- return fmt.Sprintf("```%s```", text)
- },
- }
- func makeMessageID() string {
- b := make([]byte, 10)
- rand.Read(b)
- return strings.ToUpper(hex.EncodeToString(b))
- }
- func (portal *Portal) PreprocessMatrixMedia(evt *gomatrix.Event) (string, io.ReadCloser, []byte) {
- if evt.Content.Info == nil {
- evt.Content.Info = &gomatrix.FileInfo{}
- }
- caption := evt.Content.Body
- exts, err := mime.ExtensionsByType(evt.Content.Info.MimeType)
- for _, ext := range exts {
- if strings.HasSuffix(caption, ext) {
- caption = ""
- break
- }
- }
- content, err := portal.MainIntent().Download(evt.Content.URL)
- if err != nil {
- portal.log.Errorln("Failed to download media in %s: %v", evt.ID, err)
- return "", nil, nil
- }
- thumbnail, err := portal.MainIntent().DownloadBytes(evt.Content.Info.ThumbnailURL)
- return caption, content, thumbnail
- }
- func (portal *Portal) HandleMatrixMessage(evt *gomatrix.Event) {
- info := whatsapp.MessageInfo{
- Id: makeMessageID(),
- RemoteJid: portal.JID,
- }
- var err error
- switch evt.Content.MsgType {
- case gomatrix.MsgText, gomatrix.MsgEmote:
- text := evt.Content.Body
- if evt.Content.Format == gomatrix.FormatHTML {
- text = htmlParser.Parse(evt.Content.FormattedBody)
- }
- if evt.Content.MsgType == gomatrix.MsgEmote {
- text = "/me " + text
- }
- err = portal.user.Conn.Send(whatsapp.TextMessage{
- Text: text,
- Info: info,
- })
- case gomatrix.MsgImage:
- caption, content, thumbnail := portal.PreprocessMatrixMedia(evt)
- if content == nil {
- return
- }
- err = portal.user.Conn.Send(whatsapp.ImageMessage{
- Caption: caption,
- Content: content,
- Thumbnail: thumbnail,
- Type: evt.Content.Info.MimeType,
- Info: info,
- })
- case gomatrix.MsgVideo:
- caption, content, thumbnail := portal.PreprocessMatrixMedia(evt)
- if content == nil {
- return
- }
- err = portal.user.Conn.Send(whatsapp.VideoMessage{
- Caption: caption,
- Content: content,
- Thumbnail: thumbnail,
- Type: evt.Content.Info.MimeType,
- Info: info,
- })
- case gomatrix.MsgAudio:
- _, content, _ := portal.PreprocessMatrixMedia(evt)
- if content == nil {
- return
- }
- err = portal.user.Conn.Send(whatsapp.AudioMessage{
- Content: content,
- Type: evt.Content.Info.MimeType,
- Info: info,
- })
- case gomatrix.MsgFile:
- _, content, thumbnail := portal.PreprocessMatrixMedia(evt)
- if content == nil {
- return
- }
- err = portal.user.Conn.Send(whatsapp.DocumentMessage{
- Content: content,
- Thumbnail: thumbnail,
- Type: evt.Content.Info.MimeType,
- Info: info,
- })
- default:
- portal.log.Debugln("Unhandled Matrix event:", evt)
- return
- }
- portal.MarkHandled(info.Id, evt.ID)
- if err != nil {
- portal.log.Errorfln("Error handling Matrix event %s: %v", evt.ID, err)
- } else {
- portal.log.Debugln("Handled Matrix event:", evt)
- }
- }
|