Convert mentions to names in plaintext body. Fixes #3
This commit is contained in:
parent
eca9bc7887
commit
6ad224a58b
@ -64,7 +64,7 @@ var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)
|
|||||||
var codeBlockRegex = regexp.MustCompile("```(?:.|\n)+?```")
|
var codeBlockRegex = regexp.MustCompile("```(?:.|\n)+?```")
|
||||||
var mentionRegex = regexp.MustCompile("@[0-9]+")
|
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{
|
return map[*regexp.Regexp]string{
|
||||||
italicRegex: "$1<em>$2</em>$3",
|
italicRegex: "$1<em>$2</em>$3",
|
||||||
boldRegex: "$1<strong>$2</strong>$3",
|
boldRegex: "$1<strong>$2</strong>$3",
|
||||||
@ -86,5 +86,10 @@ func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regex
|
|||||||
}
|
}
|
||||||
return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, puppet.Displayname)
|
return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, puppet.Displayname)
|
||||||
},
|
},
|
||||||
|
}, map[*regexp.Regexp]func(string)string {
|
||||||
|
mentionRegex: func(str string) string {
|
||||||
|
puppet := user.GetPuppetByJID(str[1:] + whatsappExt.NewUserSuffix)
|
||||||
|
return puppet.Displayname
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
28
portal.go
28
portal.go
@ -421,15 +421,21 @@ func (portal *Portal) SetReply(content *gomatrix.Content, info whatsapp.MessageI
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (portal *Portal) ParseWhatsAppFormat(input string) string {
|
func (portal *Portal) FormatWhatsAppMessage(content *gomatrix.Content) {
|
||||||
output := html.EscapeString(input)
|
output := html.EscapeString(content.Body)
|
||||||
for regex, replacement := range portal.user.waReplString {
|
for regex, replacement := range portal.user.waReplString {
|
||||||
output = regex.ReplaceAllString(output, replacement)
|
output = regex.ReplaceAllString(output, replacement)
|
||||||
}
|
}
|
||||||
for regex, replacer := range portal.user.waReplFunc {
|
for regex, replacer := range portal.user.waReplFunc {
|
||||||
output = regex.ReplaceAllStringFunc(output, replacer)
|
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) {
|
func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
|
||||||
@ -448,17 +454,13 @@ func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content := gomatrix.Content{
|
content := &gomatrix.Content{
|
||||||
Body: message.Text,
|
Body: message.Text,
|
||||||
MsgType: gomatrix.MsgText,
|
MsgType: gomatrix.MsgText,
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlBody := portal.ParseWhatsAppFormat(message.Text)
|
portal.FormatWhatsAppMessage(content)
|
||||||
if htmlBody != message.Text {
|
portal.SetReply(content, message.Info)
|
||||||
content.FormattedBody = htmlBody
|
|
||||||
content.Format = gomatrix.FormatHTML
|
|
||||||
}
|
|
||||||
portal.SetReply(&content, message.Info)
|
|
||||||
|
|
||||||
intent.UserTyping(portal.MXID, false, 0)
|
intent.UserTyping(portal.MXID, false, 0)
|
||||||
resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(message.Info.Timestamp*1000))
|
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,
|
MsgType: gomatrix.MsgNotice,
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlBody := portal.ParseWhatsAppFormat(captionContent.Body)
|
portal.FormatWhatsAppMessage(captionContent)
|
||||||
if htmlBody != captionContent.Body {
|
|
||||||
captionContent.FormattedBody = htmlBody
|
|
||||||
captionContent.Format = gomatrix.FormatHTML
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, captionContent, ts)
|
_, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, captionContent, ts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
7
user.go
7
user.go
@ -50,8 +50,9 @@ type User struct {
|
|||||||
|
|
||||||
htmlParser *format.HTMLParser
|
htmlParser *format.HTMLParser
|
||||||
|
|
||||||
waReplString map[*regexp.Regexp]string
|
waReplString map[*regexp.Regexp]string
|
||||||
waReplFunc map[*regexp.Regexp]func(string) string
|
waReplFunc map[*regexp.Regexp]func(string) string
|
||||||
|
waReplFuncText map[*regexp.Regexp]func(string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bridge *Bridge) GetUser(userID types.MatrixUserID) *User {
|
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.Whitelisted = user.bridge.Config.Bridge.Permissions.IsWhitelisted(user.ID)
|
||||||
user.Admin = user.bridge.Config.Bridge.Permissions.IsAdmin(user.ID)
|
user.Admin = user.bridge.Config.Bridge.Permissions.IsAdmin(user.ID)
|
||||||
user.htmlParser = user.newHTMLParser()
|
user.htmlParser = user.newHTMLParser()
|
||||||
user.waReplString, user.waReplFunc = user.newWhatsAppFormatMaps()
|
user.waReplString, user.waReplFunc, user.waReplFuncText = user.newWhatsAppFormatMaps()
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user