From ffa87ebcfc25f5fe1ba25789589e0bc58459fd38 Mon Sep 17 00:00:00 2001 From: zhaoYangguang <1163765691@qq.com> Date: Mon, 12 Oct 2020 20:13:19 +0800 Subject: [PATCH] Solve the problem of switching the matrix client account to synchronize the same Skype account --- commands.go | 6 ++++-- matrix.go | 2 +- portal.go | 40 ++++++++++++++++++++++++---------------- user.go | 21 +++++++-------------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/commands.go b/commands.go index 8d4add5..612409d 100644 --- a/commands.go +++ b/commands.go @@ -239,8 +239,10 @@ func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { ce.User.log.Debugln("Connect() returned false, assuming error was logged elsewhere and canceling login.") return } - ce.User.Login(ce, ce.Args[0], ce.Args[1]) - syncAll(ce.User, true) + err := ce.User.Login(ce, ce.Args[0], ce.Args[1]) + if err == nil { + syncAll(ce.User, true) + } } const cmdLogoutHelp = `logout - Logout from WhatsApp` diff --git a/matrix.go b/matrix.go index bc5795e..347598a 100644 --- a/matrix.go +++ b/matrix.go @@ -161,7 +161,7 @@ func (mx *MatrixHandler) handlePrivatePortal(roomID id.RoomID, inviter *User, pu func (mx *MatrixHandler) createPrivatePortalFromInvite(roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) { portal.MXID = roomID - portal.Topic = "WhatsApp private chat" + portal.Topic = "Skype private chat" _, _ = portal.MainIntent().SetRoomTopic(portal.MXID, portal.Topic) if portal.bridge.Config.Bridge.PrivateChatPortalMeta { portal.Name = puppet.Displayname diff --git a/portal.go b/portal.go index efc9bf2..75234a7 100644 --- a/portal.go +++ b/portal.go @@ -620,10 +620,20 @@ func (portal *Portal) SyncSkype(user *User, chat skype.Conversation) { } else { fmt.Println("SyncSkype ensureUserInvited", portal.MXID) portal.ensureUserInvited(user) - rep, err := portal.MainIntent().SetPowerLevel(portal.MXID, user.MXID, 100) + rep, err := portal.MainIntent().SetPowerLevel(portal.MXID, user.MXID, 95) if err != nil { portal.log.Warnfln("SyncSkype: SetPowerLevel err: ", err, rep) } + + preUserIds,_ := portal.GetMatrixUsers() + for _,userId := range preUserIds { + if user.MXID != userId { + err := portal.tryKickUser(userId, portal.MainIntent()) + if err != nil { + portal.log.Errorln("Failed to try kick user:", err) + } + } + } } if portal.IsPrivateChat() { @@ -683,7 +693,7 @@ func (portal *Portal) SyncSkype(user *User, chat skype.Conversation) { func (portal *Portal) GetBasePowerLevels() *event.PowerLevelsEventContent { anyone := 0 - nope := 99 + nope := 95 invite := 50 if portal.bridge.Config.Bridge.AllowUserInvite { invite = 0 @@ -1127,7 +1137,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { // content.Users[user.MXID] = 100 //} // When creating a room, make user self the highest level of authority - content.Users[user.MXID] = 99 + content.Users[user.MXID] = 95 initialState := []*event.Event{{ Type: event.StatePowerLevels, Content: event.Content{ @@ -1506,19 +1516,6 @@ func (portal *Portal) sendMediaBridgeFailureSkype(source *User, intent *appservi } } -func (portal *Portal) sendMediaBridgeFailure(source *User, intent *appservice.IntentAPI, info whatsapp.MessageInfo, downloadErr error) { - portal.log.Errorfln("Failed to download media for %s: %v", info.Id, downloadErr) - resp, err := portal.sendMessage(intent, event.EventMessage, &event.MessageEventContent{ - MsgType: event.MsgNotice, - Body: "Failed to bridge media", - }, int64(info.Timestamp*1000)) - if err != nil { - portal.log.Errorfln("Failed to send media download error message for %s: %v", info.Id, err) - } else { - portal.finishHandling(source, info.Source, resp.EventID) - } -} - func (portal *Portal) encryptFile(data []byte, mimeType string) ([]byte, string, *event.EncryptedFileInfo) { if !portal.Encrypted { return data, mimeType, nil @@ -1532,6 +1529,17 @@ func (portal *Portal) encryptFile(data []byte, mimeType string) ([]byte, string, } +func (portal *Portal) tryKickUser(userID id.UserID, intent *appservice.IntentAPI) error { + _, err := intent.KickUser(portal.MXID, &mautrix.ReqKickUser{UserID: userID}) + if err != nil { + httpErr, ok := err.(mautrix.HTTPError) + if ok && httpErr.RespError != nil && httpErr.RespError.ErrCode == "M_FORBIDDEN" { + _, err = portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{UserID: userID}) + } + } + return err +} + func (portal *Portal) HandleMediaMessageSkype(source *User, download func(conn *skype.Conn, mediaType string) (data []byte, mediaMessage *skype.MediaMessageContent, err error), mediaType string, thumbnail []byte, info skype.Resource, sendAsSticker bool) { intent, endHandlePrivateChatFromMe := portal.startHandlingSkype(source, info) if endHandlePrivateChatFromMe != nil { diff --git a/user.go b/user.go index 1ea6870..337d7da 100644 --- a/user.go +++ b/user.go @@ -361,17 +361,16 @@ func (user *User) loginQrChannel(ce *CommandEvent, qrChan <-chan string, eventID } } -func (user *User) Login(ce *CommandEvent, name string, password string) { +func (user *User) Login(ce *CommandEvent, name string, password string) (err error) { if user.contactsPresence == nil { user.contactsPresence = make(map[string]*skypeExt.Presence) } - session, err := user.Conn.Login(name, password) + err = user.Conn.Login(name, password) if err != nil { user.log.Errorln("Failed to login:", err) - ce.Reply("Please confirm that your account password is entered correctly") - return + ce.Reply(err.Error()) + return err } - fmt.Println("user.MXID", user.MXID) ce.Reply("Successfully logged in") // subscribe basic @@ -381,7 +380,6 @@ func (user *User) Login(ce *CommandEvent, name string, password string) { if err == nil{ var userIds []string for _, contact := range user.Conn.Store.Contacts { - //fmt.Println(contact.PersonId) if strings.Index(contact.PersonId, "28:") > -1 { continue } @@ -389,24 +387,19 @@ func (user *User) Login(ce *CommandEvent, name string, password string) { userIds = append(userIds, userId) } ce.User.Conn.SubscribeUsers(userIds) - //fmt.Println("Login user0: ", user.JID) go loopPresence(user) } go user.Conn.Poll() user.ConnectionErrors = 0 - user.SetSession(&session) + user.SetSession(user.Conn.LoginInfo) user.JID = "8:" + user.Conn.UserProfile.Username + skypeExt.NewUserSuffix user.addToJIDMap() - //user.PostLogin() ce.User.Conn.GetConversations("", user.bridge.Config.Bridge.InitialChatSync) + return } func loopPresence(user *User) { -Loop: - for i := 0; i <= 1000; i++ { - if i > 1000 { - goto Loop - } + for { for cid, contact := range user.contactsPresence { puppet := user.bridge.GetPuppetByJID(cid) _ = puppet.DefaultIntent().SetPresence(event.Presence(strings.ToLower(contact.Availability)))