2018-08-25 23:26:24 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2020-05-08 21:32:22 +02:00
|
|
|
// Copyright (C) 2020 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"
|
|
|
|
"strings"
|
|
|
|
|
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"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2018-08-30 00:10:26 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/types"
|
2018-08-25 23:26:24 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)+?```")
|
2020-07-31 13:30:58 +02:00
|
|
|
|
|
|
|
const mentionedJIDsContextKey = "net.maunium.whatsapp.mentioned_jids"
|
2018-08-25 23:26:24 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
type Formatter struct {
|
|
|
|
bridge *Bridge
|
|
|
|
|
|
|
|
matrixHTMLParser *format.HTMLParser
|
|
|
|
|
|
|
|
waReplString map[*regexp.Regexp]string
|
|
|
|
waReplFunc map[*regexp.Regexp]func(string) string
|
|
|
|
waReplFuncText map[*regexp.Regexp]func(string) string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFormatter(bridge *Bridge) *Formatter {
|
|
|
|
formatter := &Formatter{
|
|
|
|
bridge: bridge,
|
|
|
|
matrixHTMLParser: &format.HTMLParser{
|
|
|
|
TabsToSpaces: 4,
|
|
|
|
Newline: "\n",
|
|
|
|
|
2020-07-31 13:30:58 +02:00
|
|
|
PillConverter: func(mxid, eventID string, ctx format.Context) string {
|
2018-08-28 23:40:54 +02:00
|
|
|
if mxid[0] == '@' {
|
2020-05-08 21:32:22 +02:00
|
|
|
puppet := bridge.GetPuppetByMXID(id.UserID(mxid))
|
2018-08-28 23:40:54 +02:00
|
|
|
if puppet != nil {
|
2020-07-31 13:30:58 +02:00
|
|
|
jids, ok := ctx[mentionedJIDsContextKey].([]types.WhatsAppID)
|
|
|
|
if !ok {
|
|
|
|
ctx[mentionedJIDsContextKey] = []types.WhatsAppID{puppet.JID}
|
|
|
|
} else {
|
|
|
|
ctx[mentionedJIDsContextKey] = append(jids, puppet.JID)
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
return "@" + puppet.PhoneNumber()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mxid
|
|
|
|
},
|
2020-07-31 13:30:58 +02:00
|
|
|
BoldConverter: func(text string, _ format.Context) string {
|
2018-08-28 23:40:54 +02:00
|
|
|
return fmt.Sprintf("*%s*", text)
|
|
|
|
},
|
2020-07-31 13:30:58 +02:00
|
|
|
ItalicConverter: func(text string, _ format.Context) string {
|
2018-08-28 23:40:54 +02:00
|
|
|
return fmt.Sprintf("_%s_", text)
|
|
|
|
},
|
2020-07-31 13:30:58 +02:00
|
|
|
StrikethroughConverter: func(text string, _ format.Context) string {
|
2018-08-28 23:40:54 +02:00
|
|
|
return fmt.Sprintf("~%s~", text)
|
|
|
|
},
|
2020-07-31 13:30:58 +02:00
|
|
|
MonospaceConverter: func(text string, _ format.Context) string {
|
2018-08-28 23:40:54 +02:00
|
|
|
return fmt.Sprintf("```%s```", text)
|
|
|
|
},
|
2020-07-31 13:30:58 +02:00
|
|
|
MonospaceBlockConverter: func(text, language string, _ format.Context) string {
|
2018-08-28 23:40:54 +02:00
|
|
|
return fmt.Sprintf("```%s```", text)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|
|
|
|
formatter.waReplFuncText = map[*regexp.Regexp]func(string) string{
|
2018-08-25 23:26:24 +02:00
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
return formatter
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (formatter *Formatter) getMatrixInfoByJID(jid types.WhatsAppID) (mxid id.UserID, displayname string) {
|
2018-08-28 23:40:54 +02:00
|
|
|
if user := formatter.bridge.GetUserByJID(jid); user != nil {
|
|
|
|
mxid = user.MXID
|
2020-05-08 21:32:22 +02:00
|
|
|
displayname = string(user.MXID)
|
2018-08-28 23:40:54 +02:00
|
|
|
} else if puppet := formatter.bridge.GetPuppetByJID(jid); puppet != nil {
|
|
|
|
mxid = puppet.MXID
|
|
|
|
displayname = puppet.Displayname
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-31 13:30:58 +02:00
|
|
|
func (formatter *Formatter) ParseWhatsApp(content *event.MessageEventContent, mentionedJIDs []types.WhatsAppID) {
|
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)
|
|
|
|
}
|
2020-07-31 13:30:58 +02:00
|
|
|
for _, jid := range mentionedJIDs {
|
|
|
|
mxid, displayname := formatter.getMatrixInfoByJID(jid)
|
|
|
|
number := "@" + strings.Replace(jid, whatsappExt.NewUserSuffix, "", 1)
|
|
|
|
output = strings.Replace(output, number, fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, displayname), -1)
|
|
|
|
content.Body = strings.Replace(content.Body, number, displayname, -1)
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
if output != content.Body {
|
2019-07-17 23:07:44 +02:00
|
|
|
output = strings.Replace(output, "\n", "<br/>", -1)
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 13:30:58 +02:00
|
|
|
func (formatter *Formatter) ParseMatrix(html string) (string, []types.WhatsAppID) {
|
|
|
|
ctx := make(format.Context)
|
|
|
|
result := formatter.matrixHTMLParser.Parse(html, ctx)
|
2020-08-01 01:00:51 +02:00
|
|
|
mentionedJIDs, _ := ctx[mentionedJIDsContextKey].([]types.WhatsAppID)
|
2020-07-31 13:30:58 +02:00
|
|
|
return result, mentionedJIDs
|
2018-08-25 23:26:24 +02:00
|
|
|
}
|