diff --git a/commands.go b/commands.go index aaf075d..f3ff562 100644 --- a/commands.go +++ b/commands.go @@ -251,6 +251,7 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { if ce.User.Conn == nil { return } + _ = ce.User.Conn.GetConversations("", ce.User.bridge.Config.Bridge.InitialChatSync) ce.User.Conn.LoggedIn = false username := "" password := "" @@ -269,7 +270,22 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { Username: username, Password: password, } + portals := ce.User.GetPortals() + newPortals := ce.User.GetPortalsNew() + allPortals := newPortals[0:] + for _, portal := range portals { + var newPortalsHas bool + for _, newPortal := range newPortals { + if portal.Key == newPortal.Key { + newPortalsHas = true + } + } + if !newPortalsHas { + allPortals = append(allPortals, portal) + } + } + leave := func(portal *Portal) { if len(portal.MXID) > 0 { _, _ = portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{ @@ -278,7 +294,7 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { }) } } - for _, portal := range portals { + for _, portal := range allPortals { leave(portal) } ce.Reply("Logged out successfully.") diff --git a/formatting.go b/formatting.go index 5d3c0fa..1ef6bd0 100644 --- a/formatting.go +++ b/formatting.go @@ -186,6 +186,16 @@ func (formatter *Formatter) ParseSkype(content *event.MessageEventContent, RoomM content.Body = content.Body + backStr content.FormattedBody = content.FormattedBody + backStr } + + //filter edit tag + e := regexp.MustCompile(`( 0 { + message.ClientMessageId = message.SkypeEditedId + message.Id + msg := source.bridge.DB.Message.GetByJID(portal.Key, message.SkypeEditedId) + if msg != nil && len(msg.MXID) > 0 { + inRelateTo := &event.RelatesTo{ + Type: event.RelReplace, + EventID: msg.MXID, + } + content.SetRelatesTo(inRelateTo) + content.NewContent = &event.MessageEventContent{ + MsgType: content.MsgType, + Body: content.Body, + FormattedBody: content.FormattedBody, + Format: content.Format, + } + } + } // portal.SetReplySkype(content, message) fmt.Println() @@ -1898,8 +1915,52 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event) Jid: portal.Key.JID,//receiver id(conversation id) Timestamp: time.Now().Unix(), } - //ctxInfo := &waProto.ContextInfo{} + replyToID := content.GetReplyTo() + + // reedit message + if content.NewContent != nil { + a := strings.Replace(sender.JID, skypeExt.NewUserSuffix, "", 1) + a = strings.Replace(a, "8:", "", 1) + tsMs := strconv.FormatInt(time.Now().UnixNano()/1e6, 10) + r := []rune(tsMs) + ts := string(r[:len(r) - 3]) + msg := portal.bridge.DB.Message.GetByMXID(content.RelatesTo.EventID) + if msg != nil && len(msg.JID) > 0 { + info.SkypeEditedId = msg.JID + //info.ClientMessageId = info.ClientMessageId + info.SkypeEditedId + content.Body = content.Body + fmt.Sprintf("", a, tsMs, ts) + content.Body = strings.TrimPrefix(content.Body, " * ") + if len(content.FormattedBody) > 0 { + content.FormattedBody = content.FormattedBody + fmt.Sprintf("", a, tsMs, ts) + content.FormattedBody = strings.TrimPrefix(content.FormattedBody, " * ") + } + } + + // in reedit message we can't obtain the "relayId" from RelatesTo.EventID cause the matrix message doesn't put it in "RelatesTo". + // so i get relayId with use regexp, but it's not a good way, + // now there is no way to get relayId if reedit message with add a mention user + // TODO maybe we can record the relayId to DB + rQuote := regexp.MustCompile(`
In reply to.*
(.*)`) + quoteMatches := rQuote.FindAllStringSubmatch(content.FormattedBody, -1) + if len(replyToID) < 1 { + if len(quoteMatches) > 0 { + if len(quoteMatches[0]) > 0 { + replyToID = id.EventID(quoteMatches[0][1]) + } + + //Filter out the " * " in the matrix editing message (i don't why the matrix need a * in the edit message body) + if len(quoteMatches[0]) > 1 { + needReplace := quoteMatches[0][2] + afterReplace := strings.TrimPrefix(needReplace, " * ") + content.Body = strings.Replace(content.Body, needReplace, afterReplace, 1) + content.FormattedBody = strings.Replace(content.FormattedBody, needReplace, afterReplace, 1) + } + } + } + } + + // reply message var newContent string backStr := "" if len(replyToID) > 0 { @@ -1959,8 +2020,10 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event) if evt.Type == event.EventSticker { content.MsgType = event.MsgImage } + fmt.Println("convertMatrixMessage content.MsgType: ", content.MsgType) fmt.Println("convertMatrixMessage content.Body: ", content.Body) + fmt.Println("convertMatrixMessage content.NewBody: ", content.NewContent) fmt.Println("convertMatrixMessage content.FormattedBody: ", content.FormattedBody) info.Type = string(content.MsgType) switch content.MsgType { @@ -1977,6 +2040,8 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event) if len(backStr) > 0 { matchStr = backStr } + + // mention user message r := regexp.MustCompile(`(?m)]+\bhref="(.*?)://` + portal.bridge.Config.Homeserver.Domain + `/#/@([^"]+):(.*?)">(.*?)`) matches := r.FindAllStringSubmatch(matchStr, -1) fmt.Println("matches: ", matches) @@ -2001,7 +2066,9 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event) content.FormattedBody = content.FormattedBody + backStr } } + } + if len(content.FormattedBody) > 0 { info.SendTextMessage = &skype.SendTextMessage{ Content : content.FormattedBody, } @@ -2010,18 +2077,6 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event) Content : content.Body, } } - //ctxInfo.MentionedJid = mentionRegex.FindAllString(text, -1) - //for index, mention := range ctxInfo.MentionedJid { - // ctxInfo.MentionedJid[index] = mention[1:] + whatsappExt.NewUserSuffix - //} - //if ctxInfo.StanzaId != nil || ctxInfo.MentionedJid != nil { - // info.Message.ExtendedTextMessage = &waProto.ExtendedTextMessage{ - // Text: &text, - // ContextInfo: ctxInfo, - // } - //} else { - // info.Message.Conversation = &text - //} case event.MsgImage: caption, fileSize , data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID) //if media == nil { diff --git a/user.go b/user.go index 25949e9..21f92c5 100644 --- a/user.go +++ b/user.go @@ -156,6 +156,28 @@ func (user *User) GetPortals() []*Portal { return portals } +func (user *User) GetPortalsNew() []*Portal { + keys := make([]database.PortalKey, len(user.Conn.Store.Chats)) + i := 0 + for jid, _ := range user.Conn.Store.Chats { + keys[i] = database.NewPortalKey(jid, user.JID) + i++ + } + + portals := make([]*Portal, len(keys)) + + user.bridge.portalsLock.Lock() + for i, key := range keys { + portal, ok := user.bridge.portalsByJID[key] + if !ok { + portal = user.bridge.loadDBPortal(user.bridge.DB.Portal.GetByJID(key), &key) + } + portals[i] = portal + } + user.bridge.portalsLock.Unlock() + return portals +} + func (bridge *Bridge) NewUser(dbUser *database.User) *User { user := &User{ User: dbUser, @@ -394,9 +416,9 @@ func (user *User) Login(ce *CommandEvent, name string, password string) (err err go user.Conn.Poll() user.ConnectionErrors = 0 - user.SetSession(user.Conn.LoginInfo) user.JID = "8:" + user.Conn.UserProfile.Username + skypeExt.NewUserSuffix user.addToJIDMap() + user.SetSession(user.Conn.LoginInfo) _ = ce.User.Conn.GetConversations("", user.bridge.Config.Bridge.InitialChatSync) user.PostLogin() return