diff --git a/formatting.go b/formatting.go
index edafb2b..7d04cc0 100644
--- a/formatting.go
+++ b/formatting.go
@@ -64,7 +64,7 @@ var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)
var codeBlockRegex = regexp.MustCompile("```(?:.|\n)+?```")
var mentionRegex = regexp.MustCompile("@[0-9]+")
-func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regexp.Regexp]func(string) string) {
+func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regexp.Regexp]func(string) string, map[*regexp.Regexp]func(string) string) {
return map[*regexp.Regexp]string{
italicRegex: "$1$2$3",
boldRegex: "$1$2$3",
@@ -86,5 +86,10 @@ func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regex
}
return fmt.Sprintf(`%s`, mxid, puppet.Displayname)
},
+ }, map[*regexp.Regexp]func(string)string {
+ mentionRegex: func(str string) string {
+ puppet := user.GetPuppetByJID(str[1:] + whatsappExt.NewUserSuffix)
+ return puppet.Displayname
+ },
}
}
diff --git a/portal.go b/portal.go
index 0bafbb7..1538431 100644
--- a/portal.go
+++ b/portal.go
@@ -421,15 +421,21 @@ func (portal *Portal) SetReply(content *gomatrix.Content, info whatsapp.MessageI
return
}
-func (portal *Portal) ParseWhatsAppFormat(input string) string {
- output := html.EscapeString(input)
+func (portal *Portal) FormatWhatsAppMessage(content *gomatrix.Content) {
+ output := html.EscapeString(content.Body)
for regex, replacement := range portal.user.waReplString {
output = regex.ReplaceAllString(output, replacement)
}
for regex, replacer := range portal.user.waReplFunc {
output = regex.ReplaceAllStringFunc(output, replacer)
}
- return output
+ if output != content.Body {
+ content.FormattedBody = output
+ content.Format = gomatrix.FormatHTML
+ for regex, replacer := range portal.user.waReplFuncText {
+ content.Body = regex.ReplaceAllStringFunc(content.Body, replacer)
+ }
+ }
}
func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
@@ -448,17 +454,13 @@ func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
return
}
- content := gomatrix.Content{
+ 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)
+ portal.FormatWhatsAppMessage(content)
+ 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))
@@ -557,11 +559,7 @@ func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbn
MsgType: gomatrix.MsgNotice,
}
- htmlBody := portal.ParseWhatsAppFormat(captionContent.Body)
- if htmlBody != captionContent.Body {
- captionContent.FormattedBody = htmlBody
- captionContent.Format = gomatrix.FormatHTML
- }
+ portal.FormatWhatsAppMessage(captionContent)
_, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, captionContent, ts)
if err != nil {
diff --git a/user.go b/user.go
index 6acb81b..385e8ba 100644
--- a/user.go
+++ b/user.go
@@ -50,8 +50,9 @@ type User struct {
htmlParser *format.HTMLParser
- waReplString map[*regexp.Regexp]string
- waReplFunc map[*regexp.Regexp]func(string) string
+ waReplString map[*regexp.Regexp]string
+ waReplFunc map[*regexp.Regexp]func(string) string
+ waReplFuncText map[*regexp.Regexp]func(string) string
}
func (bridge *Bridge) GetUser(userID types.MatrixUserID) *User {
@@ -101,7 +102,7 @@ func (bridge *Bridge) NewUser(dbUser *database.User) *User {
user.Whitelisted = user.bridge.Config.Bridge.Permissions.IsWhitelisted(user.ID)
user.Admin = user.bridge.Config.Bridge.Permissions.IsAdmin(user.ID)
user.htmlParser = user.newHTMLParser()
- user.waReplString, user.waReplFunc = user.newWhatsAppFormatMaps()
+ user.waReplString, user.waReplFunc, user.waReplFuncText = user.newWhatsAppFormatMaps()
return user
}