package main
import (
"fmt"
"html"
"regexp"
"strings"
skypeExt "github.com/0ranki/matrix-skype/skype-ext"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
"github.com/0ranki/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, RoomMXID id.RoomID) {
// parse '' 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)
var backStr string
if output != content.Body {
output = strings.Replace(output, "\n", "]+\bauthor="([^"]+)" authorname="([^"]+)" timestamp="([^"]+)" conversation="([^"]+)" messageid="([^"]+)".*>.*?(.*?)(.*)`) quoteMatches := quoteReg.FindAllStringSubmatch(content.Body, -1) if len(quoteMatches) > 0 { for _, match := range quoteMatches { for index, a := range match { fmt.Println("index: ", index) fmt.Println("ParseSkype quoteMatches a:", a) fmt.Println() } portal := formatter.bridge.GetPortalByMXID(RoomMXID) if portal.Key.JID != match[4] { content.FormattedBody = match[6] content.Body = fmt.Sprintf("%s\n\n", match[6]) // this means that there are forwarding messages across groups if strings.HasSuffix(match[4], skypeExt.GroupSuffix) || strings.HasSuffix(portal.Key.JID, skypeExt.GroupSuffix) { continue } } msgMXID := "" msg := formatter.bridge.DB.Message.GetByID(match[5]) if msg != nil { msgMXID = string(msg.MXID) } mxid, _ = formatter.getMatrixInfoByJID("8:" + match[1] + skypeExt.NewUserSuffix) href1 := fmt.Sprintf(`https://%s/#/room/%s/%s?via=%s`, formatter.bridge.Config.Homeserver.ServerName, RoomMXID, msgMXID, formatter.bridge.Config.Homeserver.Domain) href2 := fmt.Sprintf(`https://%s/#/user/%s`, formatter.bridge.Config.Homeserver.ServerName, mxid) newContent := fmt.Sprintf(`.*?
In reply to %s
%s