2018-08-25 23:26:24 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2023-03-02 12:54:27 +01:00
|
|
|
// Copyright (C) 2023 Tulir Asokan
|
2018-08-25 23:26:24 +02:00
|
|
|
//
|
|
|
|
// 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 (
|
|
|
|
"fmt"
|
2018-08-28 23:40:54 +02:00
|
|
|
"html"
|
2018-08-25 23:26:24 +02:00
|
|
|
"regexp"
|
2023-05-24 11:41:54 +02:00
|
|
|
"sort"
|
2018-08-25 23:26:24 +02:00
|
|
|
"strings"
|
|
|
|
|
2021-10-22 19:14:34 +02:00
|
|
|
"go.mau.fi/whatsmeow/types"
|
2023-05-24 11:41:54 +02:00
|
|
|
"golang.org/x/exp/slices"
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/event"
|
2019-01-11 20:17:31 +01:00
|
|
|
"maunium.net/go/mautrix/format"
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
2018-08-25 23:26:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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 codeBlockRegex = regexp.MustCompile("```(?:.|\n)+?```")
|
2022-06-24 20:50:58 +02:00
|
|
|
var inlineURLRegex = regexp.MustCompile(`\[(.+?)]\((.+?)\)`)
|
2020-07-31 13:30:58 +02:00
|
|
|
|
2022-12-23 14:17:57 +01:00
|
|
|
const mentionedJIDsContextKey = "fi.mau.whatsapp.mentioned_jids"
|
2023-05-24 11:41:54 +02:00
|
|
|
const allowedMentionsContextKey = "fi.mau.whatsapp.allowed_mentions"
|
2018-08-25 23:26:24 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
type Formatter struct {
|
2022-05-22 00:06:30 +02:00
|
|
|
bridge *WABridge
|
2018-08-28 23:40:54 +02:00
|
|
|
|
|
|
|
matrixHTMLParser *format.HTMLParser
|
|
|
|
|
|
|
|
waReplString map[*regexp.Regexp]string
|
|
|
|
waReplFunc map[*regexp.Regexp]func(string) string
|
|
|
|
waReplFuncText map[*regexp.Regexp]func(string) string
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func NewFormatter(bridge *WABridge) *Formatter {
|
2018-08-28 23:40:54 +02:00
|
|
|
formatter := &Formatter{
|
|
|
|
bridge: bridge,
|
|
|
|
matrixHTMLParser: &format.HTMLParser{
|
|
|
|
TabsToSpaces: 4,
|
|
|
|
Newline: "\n",
|
|
|
|
|
2021-06-17 11:38:04 +02:00
|
|
|
PillConverter: func(displayname, mxid, eventID string, ctx format.Context) string {
|
2023-05-24 11:41:54 +02:00
|
|
|
allowedMentions, _ := ctx.ReturnData[allowedMentionsContextKey].(map[types.JID]bool)
|
|
|
|
if mxid[0] == '@' {
|
|
|
|
var jid types.JID
|
|
|
|
if puppet := bridge.GetPuppetByMXID(id.UserID(mxid)); puppet != nil {
|
|
|
|
jid = puppet.JID
|
|
|
|
} else if user := bridge.GetUserByMXIDIfExists(id.UserID(mxid)); user != nil {
|
|
|
|
jid = user.JID.ToNonAD()
|
|
|
|
}
|
|
|
|
if !jid.IsEmpty() && (allowedMentions == nil || allowedMentions[jid]) {
|
|
|
|
if allowedMentions == nil {
|
|
|
|
jids, ok := ctx.ReturnData[mentionedJIDsContextKey].([]string)
|
|
|
|
if !ok {
|
|
|
|
ctx.ReturnData[mentionedJIDsContextKey] = []string{jid.String()}
|
|
|
|
} else {
|
|
|
|
ctx.ReturnData[mentionedJIDsContextKey] = append(jids, jid.String())
|
|
|
|
}
|
2020-07-31 13:30:58 +02:00
|
|
|
}
|
2023-05-24 11:41:54 +02:00
|
|
|
return "@" + jid.User
|
2018-08-28 23:40:54 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-23 14:17:57 +01:00
|
|
|
return displayname
|
2018-08-28 23:40:54 +02:00
|
|
|
},
|
2021-10-22 19:14:34 +02:00
|
|
|
BoldConverter: func(text string, _ format.Context) string { return fmt.Sprintf("*%s*", text) },
|
|
|
|
ItalicConverter: func(text string, _ format.Context) string { return fmt.Sprintf("_%s_", text) },
|
|
|
|
StrikethroughConverter: func(text string, _ format.Context) string { return fmt.Sprintf("~%s~", text) },
|
|
|
|
MonospaceConverter: func(text string, _ format.Context) string { return fmt.Sprintf("```%s```", text) },
|
|
|
|
MonospaceBlockConverter: func(text, language string, _ format.Context) string { return fmt.Sprintf("```%s```", text) },
|
2018-08-28 23:40:54 +02:00
|
|
|
},
|
|
|
|
waReplString: map[*regexp.Regexp]string{
|
|
|
|
italicRegex: "$1<em>$2</em>$3",
|
|
|
|
boldRegex: "$1<strong>$2</strong>$3",
|
|
|
|
strikethroughRegex: "$1<del>$2</del>$3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
formatter.waReplFunc = map[*regexp.Regexp]func(string) string{
|
2018-08-25 23:26:24 +02:00
|
|
|
codeBlockRegex: 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)
|
|
|
|
},
|
2018-08-28 23:40:54 +02:00
|
|
|
}
|
2021-10-22 19:14:34 +02:00
|
|
|
formatter.waReplFuncText = map[*regexp.Regexp]func(string) string{}
|
2018-08-28 23:40:54 +02:00
|
|
|
return formatter
|
|
|
|
}
|
|
|
|
|
2022-02-16 00:00:49 +01:00
|
|
|
func (formatter *Formatter) getMatrixInfoByJID(roomID id.RoomID, jid types.JID) (mxid id.UserID, displayname string) {
|
|
|
|
if puppet := formatter.bridge.GetPuppetByJID(jid); puppet != nil {
|
2018-08-28 23:40:54 +02:00
|
|
|
mxid = puppet.MXID
|
|
|
|
displayname = puppet.Displayname
|
|
|
|
}
|
2022-02-16 00:00:49 +01:00
|
|
|
if user := formatter.bridge.GetUserByJID(jid); user != nil {
|
|
|
|
mxid = user.MXID
|
|
|
|
member := formatter.bridge.StateStore.GetMember(roomID, user.MXID)
|
|
|
|
if len(member.Displayname) > 0 {
|
|
|
|
displayname = member.Displayname
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-24 22:25:37 +02:00
|
|
|
func (formatter *Formatter) ParseWhatsApp(roomID id.RoomID, content *event.MessageEventContent, mentionedJIDs []string, allowInlineURL, forceHTML bool) {
|
2018-08-28 23:40:54 +02:00
|
|
|
output := html.EscapeString(content.Body)
|
|
|
|
for regex, replacement := range formatter.waReplString {
|
|
|
|
output = regex.ReplaceAllString(output, replacement)
|
|
|
|
}
|
|
|
|
for regex, replacer := range formatter.waReplFunc {
|
|
|
|
output = regex.ReplaceAllStringFunc(output, replacer)
|
|
|
|
}
|
2022-06-24 20:50:58 +02:00
|
|
|
if allowInlineURL {
|
|
|
|
output = inlineURLRegex.ReplaceAllStringFunc(output, func(s string) string {
|
|
|
|
groups := inlineURLRegex.FindStringSubmatch(s)
|
|
|
|
return fmt.Sprintf(`<a href="%s">%s</a>`, groups[2], groups[1])
|
|
|
|
})
|
|
|
|
}
|
2023-03-02 12:54:27 +01:00
|
|
|
alreadyMentioned := make(map[id.UserID]struct{})
|
|
|
|
content.Mentions = &event.Mentions{}
|
2021-10-22 19:14:34 +02:00
|
|
|
for _, rawJID := range mentionedJIDs {
|
|
|
|
jid, err := types.ParseJID(rawJID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
} else if jid.Server == types.LegacyUserServer {
|
|
|
|
jid.Server = types.DefaultUserServer
|
|
|
|
}
|
2022-02-16 00:00:49 +01:00
|
|
|
mxid, displayname := formatter.getMatrixInfoByJID(roomID, jid)
|
2021-10-22 19:14:34 +02:00
|
|
|
number := "@" + jid.User
|
|
|
|
output = strings.ReplaceAll(output, number, fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, displayname))
|
|
|
|
content.Body = strings.ReplaceAll(content.Body, number, displayname)
|
2023-03-02 12:54:27 +01:00
|
|
|
if _, ok := alreadyMentioned[mxid]; !ok {
|
|
|
|
alreadyMentioned[mxid] = struct{}{}
|
|
|
|
content.Mentions.UserIDs = append(content.Mentions.UserIDs, mxid)
|
|
|
|
}
|
2020-07-31 13:30:58 +02:00
|
|
|
}
|
2022-06-24 22:25:37 +02:00
|
|
|
if output != content.Body || forceHTML {
|
2021-10-22 19:14:34 +02:00
|
|
|
output = strings.ReplaceAll(output, "\n", "<br/>")
|
2018-08-28 23:40:54 +02:00
|
|
|
content.FormattedBody = output
|
2020-05-08 21:32:22 +02:00
|
|
|
content.Format = event.FormatHTML
|
2018-08-28 23:40:54 +02:00
|
|
|
for regex, replacer := range formatter.waReplFuncText {
|
|
|
|
content.Body = regex.ReplaceAllStringFunc(content.Body, replacer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 11:41:54 +02:00
|
|
|
func (formatter *Formatter) ParseMatrix(html string, mentions *event.Mentions) (string, []string) {
|
2023-02-04 19:14:23 +01:00
|
|
|
ctx := format.NewContext()
|
2023-05-24 11:41:54 +02:00
|
|
|
var mentionedJIDs []string
|
|
|
|
if mentions != nil {
|
|
|
|
var allowedMentions = make(map[types.JID]bool)
|
|
|
|
mentionedJIDs = make([]string, 0, len(mentions.UserIDs))
|
|
|
|
for _, userID := range mentions.UserIDs {
|
|
|
|
var jid types.JID
|
|
|
|
if puppet := formatter.bridge.GetPuppetByMXID(userID); puppet != nil {
|
|
|
|
jid = puppet.JID
|
|
|
|
mentionedJIDs = append(mentionedJIDs, puppet.JID.String())
|
|
|
|
} else if user := formatter.bridge.GetUserByMXIDIfExists(userID); user != nil {
|
|
|
|
jid = user.JID.ToNonAD()
|
|
|
|
}
|
|
|
|
if !jid.IsEmpty() && !allowedMentions[jid] {
|
|
|
|
allowedMentions[jid] = true
|
|
|
|
mentionedJIDs = append(mentionedJIDs, jid.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.ReturnData[allowedMentionsContextKey] = allowedMentions
|
|
|
|
}
|
2020-07-31 13:30:58 +02:00
|
|
|
result := formatter.matrixHTMLParser.Parse(html, ctx)
|
2023-05-24 11:41:54 +02:00
|
|
|
if mentions == nil {
|
|
|
|
mentionedJIDs, _ = ctx.ReturnData[mentionedJIDsContextKey].([]string)
|
|
|
|
sort.Strings(mentionedJIDs)
|
|
|
|
mentionedJIDs = slices.Compact(mentionedJIDs)
|
|
|
|
}
|
2020-07-31 13:30:58 +02:00
|
|
|
return result, mentionedJIDs
|
2018-08-25 23:26:24 +02:00
|
|
|
}
|
2022-12-23 14:17:57 +01:00
|
|
|
|
|
|
|
func (formatter *Formatter) ParseMatrixWithoutMentions(html string) string {
|
2023-02-04 19:14:23 +01:00
|
|
|
ctx := format.NewContext()
|
2023-05-24 11:41:54 +02:00
|
|
|
ctx.ReturnData[allowedMentionsContextKey] = map[types.JID]struct{}{}
|
2022-12-23 14:17:57 +01:00
|
|
|
return formatter.matrixHTMLParser.Parse(html, ctx)
|
|
|
|
}
|