package main
import (
"fmt"
skypeExt "github.com/kelaresg/matrix-skype/skype-ext"
"html"
"regexp"
"strings"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
"github.com/kelaresg/matrix-skype/types"
)
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]+")
//var mentionRegex = regexp.MustCompile("@(.*)")
var mentionRegex = regexp.MustCompile("$2$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') {
return fmt.Sprintf("
%s
", str)
}
return fmt.Sprintf("%s
", str)
},
}
formatter.waReplFuncText = map[*regexp.Regexp]func(string) string{
}
return formatter
}
func (formatter *Formatter) getMatrixInfoByJID(jid types.SkypeID) (mxid id.UserID, displayname string) {
if user := formatter.bridge.GetUserByJID(jid); user != nil {
mxid = user.MXID
displayname = string(user.MXID)
} else if puppet := formatter.bridge.GetPuppetByJID(jid); puppet != nil {
mxid = puppet.MXID
displayname = puppet.Displayname
}
return
}
func (formatter *Formatter) ParseSkype(content *event.MessageEventContent) {
// parse a tag
reg:= regexp.MustCompile(`(?U)((.*))`)
bodyMatch := reg.FindAllStringSubmatch(content.Body, -1)
for _, match := range bodyMatch {
content.Body = strings.ReplaceAll(content.Body, match[1], match[2])
}
output := content.Body
for regex, replacement := range formatter.waReplString {
output = regex.ReplaceAllString(output, replacement)
}
for regex, replacer := range formatter.waReplFunc {
output = regex.ReplaceAllStringFunc(output, replacer)
}
content.Body = html.UnescapeString(content.Body)
// parse @user message
r := regexp.MustCompile(`]+\bauthor="([^"]+)" authorname="([^"]+)" timestamp="([^"]+)".*>.*?(.*?)(.*)`) quoteMatches := quoteReg.FindAllStringSubmatch(content.Body, -1) if len(quoteMatches) > 0 { for _, match := range quoteMatches { mxid, displayname = formatter.getMatrixInfoByJID("8:" + match[1] + skypeExt.NewUserSuffix) //href1 := fmt.Sprintf(`https://matrix.to/#/!kpouCkfhzvXgbIJmkP:oliver.matrix.host/$fHQNRydqqqAVS8usHRmXn0nIBM_FC-lo2wI2Uol7wu8?via=oliver.matrix.host`) href1 := "" //mxid `@skype&8-live-xxxxxx:name.matrix.server` href2 := fmt.Sprintf(`https://%s/#/%s`, formatter.bridge.Config.Homeserver.Domain, mxid) newContent := fmt.Sprintf(`.*?
%s
%s