diff --git a/formatting.go b/formatting.go
index 654fe68..c30c3f0 100644
--- a/formatting.go
+++ b/formatting.go
@@ -79,30 +79,8 @@ func NewFormatter(bridge *Bridge) *Formatter {
}
return fmt.Sprintf("%s
", str)
},
- mentionRegex: func(str string) string {
- mxid, displayname := formatter.getMatrixInfoByJID(str[1:] + skypeExt.NewUserSuffix)
- mxid = id.UserID(html.EscapeString(string(mxid)))
- return fmt.Sprintf(`%s:`, bridge.Config.Homeserver.Domain, mxid, displayname)
- },
}
formatter.waReplFuncText = map[*regexp.Regexp]func(string) string{
- mentionRegex: func(str string) string {
- r := regexp.MustCompile(`]+\bid="([^"]+)"(.*?)*`)
- matches := r.FindAllStringSubmatch(str, -1)
- displayname := ""
- var mxid id.UserID
- if len(matches) > 0 {
- for _, match := range matches {
- mxid, displayname = formatter.getMatrixInfoByJID(match[1] + skypeExt.NewUserSuffix)
- }
- }
- //mxid, displayname := formatter.getMatrixInfoByJID(str[1:] + whatsappExt.NewUserSuffix)
- mxid = id.UserID(html.EscapeString(string(mxid)))
- return fmt.Sprintf(`%s:`, bridge.Config.Homeserver.Domain, mxid, displayname)
- // _, displayname = formatter.getMatrixInfoByJID(str[1:] + whatsappExt.NewUserSuffix)
- //fmt.Println("ParseWhatsAp4", displayname)
- //return displayname
- },
}
return formatter
}
@@ -119,29 +97,39 @@ func (formatter *Formatter) getMatrixInfoByJID(jid types.SkypeID) (mxid id.UserI
}
func (formatter *Formatter) ParseSkype(content *event.MessageEventContent) {
- output := html.EscapeString(content.Body)
+ // 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(`]+\bid="([^"]+)"(.*?)*`)
+ matches := r.FindAllStringSubmatch(content.Body, -1)
+ displayname := ""
+ var mxid id.UserID
+ if len(matches) > 0 {
+ for _, match := range matches {
+ mxid, displayname = formatter.getMatrixInfoByJID(match[1] + skypeExt.NewUserSuffix)
+ number := "@" + strings.Replace(match[1], skypeExt.NewUserSuffix, "", 1)
+ output = strings.ReplaceAll(content.Body, match[0], fmt.Sprintf(`%s:`, formatter.bridge.Config.Homeserver.Domain, mxid, displayname))
+ content.Body = strings.Replace(content.Body, number, displayname, -1)
+ }
+ }
+
if output != content.Body {
output = strings.Replace(output, "\n", "
", -1)
- content.Body = html.UnescapeString(content.Body) // skype messages arrive escaped which causes element rendering issues #1
-
- // parse @user message
- r := regexp.MustCompile(`]+\bid="([^"]+)"(.*?)*`)
- matches := r.FindAllStringSubmatch(content.Body, -1)
- displayname := ""
- var mxid id.UserID
- if len(matches) > 0 {
- for _, match := range matches {
- mxid, displayname = formatter.getMatrixInfoByJID(match[1] + skypeExt.NewUserSuffix)
- content.FormattedBody = strings.ReplaceAll(content.Body, match[0], fmt.Sprintf(`%s:`, formatter.bridge.Config.Homeserver.Domain, mxid, displayname))
- content.Body = content.FormattedBody
- }
- }
+ content.FormattedBody = output
// parse quote message
content.Body = strings.ReplaceAll(content.Body, "\n", "")
diff --git a/portal.go b/portal.go
index 718ad5d..3407d40 100644
--- a/portal.go
+++ b/portal.go
@@ -1895,7 +1895,7 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
//replyToID := content.GetReplyTo()
var newContent string
//if len(replyToID) > 0 {
- rQuote := regexp.MustCompile(`]+\bhref="(.*?)://` + portal.bridge.Config.Homeserver.Domain + `/#/@([^"]+):(.*?)">(.*?)
([^"]+)
(.*)`)
+ rQuote := regexp.MustCompile(`]+\bhref="(.*?)://` + portal.bridge.Config.Homeserver.Domain + `/#/@([^"]+):(.*?)">(.*?)
([^"]+)
(.*)`)
quoteMatches := rQuote.FindAllStringSubmatch(content.FormattedBody, -1)
fmt.Println("matches0: ", content.FormattedBody)
fmt.Println("matches1: ", quoteMatches)
@@ -1980,6 +1980,8 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
skyId := patch.ParseLocalPart(html.UnescapeString(match[2]), false)
skyId = strings.ReplaceAll(skyId, "skype&", "")
skyId = strings.ReplaceAll(skyId, "-", ":")
+ // Adapt to the message format sent by the matrix front end
+ content.FormattedBody = strings.ReplaceAll(content.FormattedBody, match[0] + ":", fmt.Sprintf(`%s`, skyId, match[4]))
content.FormattedBody = strings.ReplaceAll(content.FormattedBody, match[0], fmt.Sprintf(`%s`, skyId, match[4]))
}
}
diff --git a/test/main.go b/test/main.go
deleted file mode 100644
index ce4ec5d..0000000
--- a/test/main.go
+++ /dev/null
@@ -1,139 +0,0 @@
-package main
-
-import (
- "encoding/base64"
- "fmt"
- "regexp"
- "strings"
- "time"
-)
-
-func find(htm string, re *regexp.Regexp) [][]string {
- imgs := re.FindAllStringSubmatch(htm, -1)
- //fmt.Println(re.FindAllStringIndex(htm, -1))
- //return imgs
- //out := make([]string, len(imgs))
- for _, img := range imgs {
- for _, img2 := range img {
- fmt.Println(img2)
- }
- }
- return imgs
-}
-func main () {
- a := "2020-07-15T03:47:51.217Z"
- t, _ := time.Parse(time.RFC3339, a)
- fmt.Println(t.Unix())
- return
- //str := `1lyle21211lyle2121`
- // str := `In reply to @skype&8-live-1163765691:oliver.matrix.host
qqqqqqq
9999999`
- //str := `[1594719165] Oliver1 Zhao2↵: 3333333↵↵<<<
1111111`
- str := `[1594808528] Oliver1 Zhao2
-: 00000000
-<<<
1111111111`
- //r,_:=regexp.Compile(".*")
- //r := regexp.MustCompile(`]+\bid="([^"]+)"(.*?)*`)
- //r := regexp.MustCompile(`]+\bhref="(.*?)://matrix\.to/#/@skype&([^"]+):(.*?)">(.*?)*`)
- str = strings.ReplaceAll(str, "\n", "")
- r := regexp.MustCompile(`]+\bauthor="([^"]+)" authorname="([^"]+)" timestamp="([^"]+)".*>.*?(.*?).*?
(.*)`)
- //patten := ``
- find(str, r)
- //fmt.Println(find(str, r))
-}
- //cli, _ := skype.NewConn()
- //_ = cli.Login("1", "3")
- //avatar := &skypeExt.ProfilePicInfo{
- // URL: "https://api.asm.skype.com/v1/objects/0-ea-d6-ee876d1872e567ed85d89efae9b05971/views/swx_avatar",
- // Tag: "https://api.asm.skype.com/v1/objects/0-ea-d6-ee876d1872e567ed85d89efae9b05971/views/swx_avatar",
- // Status: 0,
- // Authorization: "skype_token " + cli.LoginInfo.SkypeToken,
- //}
-
- //data, _ := avatar.DownloadBytes()
- //fmt.Println("DownloadBytes: ", string(data))
- //type user struct {
- // Presences map[string]*skypeExt.Presence
- //}
- //a := user{Presences:make(map[string]*skypeExt.Presence)}
- //a.Presences["1"] = &skypeExt.Presence{
- // Id: "1",
- // Availability: "313",
- // Status: "31312",
- //}
- //fmt.Printf("%+v", a)
- //body := `{"eventMessages":[{"id":1005,"type":"EventMessage","resourceType":"NewMessage","time":"2020-06-19T11:19:57Z","resourceLink":"https://azwcus1-client-s.gateway.messenger.live.com/v1/users/ME/conversations/19:77d9cf34f8d6419fbb3542bd6304ac33@thread.skype/messages/1592565597315","resource":{"contentformat":"FN=MS%20Shell%20Dlg; EF=; CO=0; CS=0; PF=0","messagetype":"ThreadActivity/PictureUpdate","originalarrivaltime":"2020-06-19T11:19:57.315Z","ackrequired":"https://azwcus1-client-s.gateway.messenger.live.com/v1/users/ME/conversations/ALL/messages/1592565597315/ack","type":"Message","version":"1592565597315","contenttype":"text/plain; charset=UTF-8","origincontextid":"8571783950420663070","isactive":false,"from":"https://azwcus1-client-s.gateway.messenger.live.com/v1/users/ME/contacts/19:77d9cf34f8d6419fbb3542bd6304ac33@thread.skype","id":"1592565597315","conversationLink":"https://azwcus1-client-s.gateway.messenger.live.com/v1/users/ME/conversations/19:77d9cf34f8d6419fbb3542bd6304ac33@thread.skype","counterpartymessageid":"1592565597315","threadtopic":"gteat4","content":"15925655974408:live:.cid.d3feb90dceeb51ccURL@https://api.asm.skype.com/v1/objects/0-ea-d1-df4643685906b8826aaf6faddbbd572d/views/avatar_fullsize","composetime":"2020-06-19T11:19:57.315Z"}}]}`
- //var bodyContent struct {
- // EventMessages []skype.Conversation `json:"eventMessages"`
- //}
- //_ = json.Unmarshal([]byte(body), &bodyContent)
- //if len(bodyContent.EventMessages) > 0 {
- // for _, message := range bodyContent.EventMessages {
- // if message.Type == "EventMessage" {
- // messageType := skypeExt.ChatActionType(message.Resource.MessageType)
- // switch messageType {
- // case skypeExt.TopicUpdate:
- // topicContent := skype.TopicContent{}
- // //把xml数据解析成bs对象
- // xml.Unmarshal([]byte(message.Resource.Content), &topicContent)
- // message.Resource.SendId = topicContent.Initiator + skypeExt.NewUserSuffix
- // //go portal.UpdateName(cmd.ThreadTopic, cmd.SendId)
- // case skypeExt.PictureUpdate:
- // topicContent := skype.PictureContent{}
- // //把xml数据解析成bs对象
- // xml.Unmarshal([]byte(message.Resource.Content), &topicContent)
- // message.Resource.SendId = topicContent.Initiator + skypeExt.NewUserSuffix
- // url := strings.TrimPrefix(topicContent.Value, "URL@")
- // fmt.Println(url)
- // //avatar := &skypeExt.ProfilePicInfo{
- // // URL: url,
- // // Tag: topicContent.Value,
- // // Status: 0,
- // //}
- // //go portal.UpdateAvatar(user, avatar)
- // }
- // }
- // }
- //}
-
- //fmt.Println("hello https://tool.lu/")
- //membersStr := "[\"8:live:1163765691\",\"8:live:zhaosl_4\"]"
- //var members []string
- //_ = json.Unmarshal([]byte(membersStr), &members)
- //fmt.Println(members)
- //for _, participant := range members {
- // fmt.Println(participant)
- //}
- //type a string
- //type b struct {
- // C a
- //}
- //v := b{
- // C: a("8:live:1163765691"),
- //}
- //fmt.Println(strings.HasSuffix("28:0d5d6cff-595d-49d7-9cf8-973173f5233b@s.skype.net", skypeExt.NewUserSuffix))
- //return
- //rand.Seed(time.Now().UnixNano())
- //fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(10000))
- //currentTimeNanoStr := strconv.FormatInt(time.Now().UnixNano(), 10)
- //currentTimeNanoStr = currentTimeNanoStr[:len(currentTimeNanoStr)-3]
- ////clientmessageid := currentTimeNanoStr + randomStr
- //fmt.Println(fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(10000)))
- //return
- ////Parse("@whatsapp_8:live:1163765691:oliver.matrix.host")
- //userIDRegex, _ := regexp.Compile("@whatsapp_8:live:1163765691:oliver.matrix.host")
- //match := userIDRegex.FindStringSubmatch(string("@whatsapp_8:live:1163765691:oliver.matrix.host"))
- //fmt.Println(match)
-//}
-
-func Parse(userID string)(localpart, homeserver string, err error) {
- if len(userID) == 0 || userID[0] != '@' || !strings.ContainsRune(string(userID), ':') {
- err = fmt.Errorf("%s is not a valid user id", userID)
- return
- }
- parts := strings.Split(string(userID), ":")
- localpart, homeserver = strings.TrimPrefix(strings.Join(parts[:len(parts)-1], ":"), "@"), parts[len(parts)-1]
- localpart = base64.StdEncoding.EncodeToString([]byte(localpart))
- fmt.Println(localpart, homeserver)
- return
-}