Initial desegregation of users and automatic config updating

This commit is contained in:
Tulir Asokan
2018-08-29 00:40:54 +03:00
parent 55c3ab2d4f
commit c7348f29b0
24 changed files with 806 additions and 475 deletions

View File

@ -18,58 +18,71 @@ package main
import (
"fmt"
"html"
"regexp"
"strings"
"maunium.net/go/gomatrix"
"maunium.net/go/gomatrix/format"
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
)
func (user *User) newHTMLParser() *format.HTMLParser {
return &format.HTMLParser{
TabsToSpaces: 4,
Newline: "\n",
PillConverter: func(mxid, eventID string) string {
if mxid[0] == '@' {
puppet := user.GetPuppetByMXID(mxid)
fmt.Println(mxid, puppet)
if puppet != nil {
return "@" + puppet.PhoneNumber()
}
}
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)
},
}
}
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)+?```")
var mentionRegex = regexp.MustCompile("@[0-9]+")
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<em>$2</em>$3",
boldRegex: "$1<strong>$2</strong>$3",
strikethroughRegex: "$1<del>$2</del>$3",
}, map[*regexp.Regexp]func(string) string{
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",
PillConverter: func(mxid, eventID string) string {
if mxid[0] == '@' {
puppet := bridge.GetPuppetByMXID(mxid)
fmt.Println(mxid, puppet)
if puppet != nil {
return "@" + puppet.PhoneNumber()
}
}
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)
},
},
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{
codeBlockRegex: func(str string) string {
str = str[3 : len(str)-3]
if strings.ContainsRune(str, '\n') {
@ -78,18 +91,47 @@ func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regex
return fmt.Sprintf("<code>%s</code>", str)
},
mentionRegex: func(str string) string {
jid := str[1:] + whatsappExt.NewUserSuffix
puppet := user.GetPuppetByJID(jid)
mxid := puppet.MXID
if jid == user.JID() {
mxid = user.ID
}
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
mxid, displayname := formatter.getMatrixInfoByJID(str[1:] + whatsappExt.NewUserSuffix)
return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, displayname)
},
}
formatter.waReplFuncText = map[*regexp.Regexp]func(string) string{
mentionRegex: func(str string) string {
_, displayname := formatter.getMatrixInfoByJID(str[1:] + whatsappExt.NewUserSuffix)
return displayname
},
}
return formatter
}
func (formatter *Formatter) getMatrixInfoByJID(jid string) (mxid, displayname string) {
if user := formatter.bridge.GetUserByJID(jid); user != nil {
mxid = user.MXID
displayname = user.MXID
} else if puppet := formatter.bridge.GetPuppetByJID(jid); puppet != nil {
mxid = puppet.MXID
displayname = puppet.Displayname
}
return
}
func (formatter *Formatter) ParseWhatsApp(content *gomatrix.Content) {
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)
}
if output != content.Body {
content.FormattedBody = output
content.Format = gomatrix.FormatHTML
for regex, replacer := range formatter.waReplFuncText {
content.Body = regex.ReplaceAllStringFunc(content.Body, replacer)
}
}
}
func (formatter *Formatter) ParseMatrix(html string) string {
return formatter.matrixHTMLParser.Parse(html)
}