diff --git a/database/message.go b/database/message.go index fc7ebdc..528629b 100644 --- a/database/message.go +++ b/database/message.go @@ -4,8 +4,6 @@ import ( "bytes" "database/sql" "encoding/json" - skype "github.com/kelaresg/go-skypeapi" - log "maunium.net/go/maulogger/v2" "github.com/kelaresg/matrix-skype/types" @@ -84,7 +82,7 @@ type Message struct { MXID id.EventID Sender types.SkypeID Timestamp uint64 - Content *skype.Resource + Content string } func (msg *Message) Scan(row Scannable) *Message { @@ -103,7 +101,8 @@ func (msg *Message) Scan(row Scannable) *Message { } func (msg *Message) decodeBinaryContent(content []byte) { - msg.Content = &skype.Resource{} + //msg.Content = &skype.Resource{} + msg.Content = "" reader := bytes.NewReader(content) dec := json.NewDecoder(reader) err := dec.Decode(&msg.Content) diff --git a/formatting.go b/formatting.go index 94d55aa..5572247 100644 --- a/formatting.go +++ b/formatting.go @@ -113,55 +113,65 @@ func (formatter *Formatter) ParseSkype(content *event.MessageEventContent, RoomM } content.Body = html.UnescapeString(content.Body) - // parse mention 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) - } - } - + var backStr string if output != content.Body { output = strings.Replace(output, "\n", "
", -1) content.FormattedBody = output content.Format = event.FormatHTML + var mxid id.UserID // parse quote message(set reply) content.Body = strings.ReplaceAll(content.Body, "\n", "") quoteReg := regexp.MustCompile(`]+\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() + } msgMXID := "" msg := formatter.bridge.DB.Message.GetByID(match[4]) if msg != nil { msgMXID = string(msg.MXID) } - mxid, displayname = formatter.getMatrixInfoByJID("8:" + match[1] + skypeExt.NewUserSuffix) + mxid, _ = formatter.getMatrixInfoByJID("8:" + match[1] + skypeExt.NewUserSuffix) href1 := fmt.Sprintf(`https://%s/#/room/%s/%s?via=%s`, formatter.bridge.Config.Homeserver.Domain, RoomMXID, msgMXID, formatter.bridge.Config.Homeserver.Domain) href2 := fmt.Sprintf(`https://%s/#/user/%s`, formatter.bridge.Config.Homeserver.Domain, mxid) - newContent := fmt.Sprintf(`
In reply to %s
%s
%s`, + newContent := fmt.Sprintf(`
In reply to %s
%s
`, href1, href2, mxid, - match[5], - match[6]) + match[5]) content.FormattedBody = newContent - content.Body = fmt.Sprintf("> <%s> %s\n\n%s", mxid, match[5], match[6]) + content.Body = fmt.Sprintf("> <%s> %s\n\n", mxid, match[5]) inRelateTo := &event.RelatesTo{ Type: event.RelReply, EventID: id.EventID(msgMXID), } content.SetRelatesTo(inRelateTo) + backStr = match[6] } } } + + // parse mention user message + r := regexp.MustCompile(`]+\bid="([^"]+)"(.*?)*`) + matches := r.FindAllStringSubmatch(backStr, -1) + 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(backStr, match[0], fmt.Sprintf(`%s:`, formatter.bridge.Config.Homeserver.Domain, mxid, displayname)) + content.FormattedBody = content.FormattedBody + output + content.Body = content.Body + strings.Replace(backStr, number, displayname, -1) + } + } else { + content.Body = content.Body + backStr + content.FormattedBody = content.FormattedBody + backStr + } } func (formatter *Formatter) ParseMatrix(html string) string { diff --git a/portal.go b/portal.go index 901f003..4b89ffd 100644 --- a/portal.go +++ b/portal.go @@ -5,7 +5,6 @@ import ( "maunium.net/go/mautrix/patch" "encoding/hex" - "encoding/json" "encoding/xml" "fmt" skype "github.com/kelaresg/go-skypeapi" @@ -293,7 +292,8 @@ func (portal *Portal) markHandledSkype(source *User, message *skype.Resource, mx // msg.Sender = message.Jid //} } - msg.Content = message + + msg.Content = message.Content if len(message.Id)>0 { msg.ID = message.Id } @@ -1890,9 +1890,6 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event) return nil, sender, content } - //ts := uint64(evt.Timestamp / 1000) - //status := waProto.WebMessageInfo_ERROR - //fromMe := true currentTimeNanoStr := strconv.FormatInt(time.Now().UnixNano(), 10) currentTimeNanoStr = currentTimeNanoStr[:len(currentTimeNanoStr)-3] clientMessageId := currentTimeNanoStr + fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(10000)) @@ -1902,53 +1899,51 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event) Timestamp: time.Now().Unix(), } //ctxInfo := &waProto.ContextInfo{} - //replyToID := content.GetReplyTo() + replyToID := content.GetReplyTo() var newContent string - //if len(replyToID) > 0 { - rQuote := regexp.MustCompile(`
]+\bhref="(.*?)://` + portal.bridge.Config.Homeserver.Domain + `/#/@([^"]+):(.*?)">(.*?)
([^"]+)
(.*)`) - quoteMatches := rQuote.FindAllStringSubmatch(content.FormattedBody, -1) - fmt.Println("matches0: ", content.FormattedBody) - fmt.Println("matches1: ", quoteMatches) - if len(quoteMatches) > 0 { - for _, match := range quoteMatches { - if len(match) > 2 { - var skyId string - if strings.Index(match[4], "@skype") > -1 { - skyId = patch.ParseLocalPart(html.UnescapeString(match[2]), false) - skyId = strings.ReplaceAll(skyId, "skype&", "") - skyId = strings.ReplaceAll(skyId, "-", ":") - } else { - skyId = strings.ReplaceAll(sender.JID, skypeExt.NewUserSuffix, "") - } - if len(skyId) < 2 { - continue - } - skypeUsername := strings.Replace(skyId, "8:", "", 1) - puppet := sender.bridge.GetPuppetByJID(skyId + skypeExt.NewUserSuffix) - time.Now().Unix() - newContent = fmt.Sprintf(`[%s] %s↵: %s↵↵<<< %s`, - skypeUsername, - puppet.Displayname, - strconv.Itoa(int(time.Now().Unix())), - portal.Key.JID, - time.Now().UnixNano() / 1e6, - strconv.Itoa(int(time.Now().UnixNano())) + "1", - time.Now().Unix(), - puppet.Displayname, - match[5], - match[6]) - content.FormattedBody = newContent - } + if len(replyToID) > 0 { + rQuote := regexp.MustCompile(`(.*?)(.*)`) + quoteMatches := rQuote.FindAllStringSubmatch(content.FormattedBody, -1) + backStr := "" + if len(quoteMatches) > 0 { + if len(quoteMatches[0]) > 2 { + backStr = quoteMatches[0][2] } } + content.RemoveReplyFallback() - //if len(newContent) > 0 { - // newContent = content.Body - //} - //content.FormattedBody = newContent - //msg := portal.bridge.DB.Message.GetByMXID(replyToID) - //} + msg := portal.bridge.DB.Message.GetByMXID(replyToID) + if msg != nil && len(msg.Content) > 0 { + messageId := msg.ID + if len(messageId) < 1 { + messageId = strconv.FormatInt(time.Now().UnixNano()/1e6, 10) + } + author := strings.Replace(msg.Sender, skypeExt.NewUserSuffix, "", 1) + author = strings.Replace(author, "8:", "", 1) + conversation := msg.Chat.Receiver + cuid := msg.JID + r := []rune(messageId) + timestamp := string(r[:len(r) - 3]) + quoteMessage := msg.Content + + puppet := sender.bridge.GetPuppetByJID(msg.Sender) + + newContent = fmt.Sprintf(`[%s] %s: %s\n\n<<< %s`, + author, + puppet.Displayname, + timestamp, + conversation, + messageId, + cuid, + timestamp, + puppet.Displayname, + quoteMessage, + backStr) + content.FormattedBody = newContent + } + } + relaybotFormatted := false if sender.NeedsRelaybot(portal) { if !portal.HasRelaybot() { @@ -2115,24 +2110,20 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) { fmt.Println("portal HandleMatrixMessage info is nil: ") return } - infoRaw, err := json.Marshal(info) - if err != nil { - fmt.Println("portal HandleMatrixMessage Marshal info err: ", err) - return - } + fmt.Println("portal HandleMatrixMessage start markHandledSkype: ") portal.markHandledSkype(sender, &skype.Resource{ ClientMessageId: info.ClientMessageId, Jid: portal.Key.JID,//receiver id(conversation id) Timestamp: time.Now().Unix(), - Content: string(infoRaw), + Content: info.Content, }, evt.ID) portal.log.Debugln("Sending event", evt.ID, "to Skype") errChan := make(chan error, 1) //go sender.Conn.Conn.SendMsg(portal.Key.JID, info.Content, info.ClientMessageId, errChan) go SendMsg(sender, portal.Key.JID, info, errChan) - + var err error var errorEventID id.EventID select { case err = <-errChan: