From 7609b9d8ae85c53b103dcc8e16ad3676e05650d2 Mon Sep 17 00:00:00 2001 From: zhaoYangguang <1163765691@qq.com> Date: Thu, 3 Jun 2021 19:37:52 +0800 Subject: [PATCH] fix can't auto exit skype room even if user relogin skype and logout. fix cannot sync room to matrix(isn't recently room but synced before) --- commands.go | 64 ++++++++++++----------------------------------------- portal.go | 21 ++++++++++++++++-- 2 files changed, 33 insertions(+), 52 deletions(-) diff --git a/commands.go b/commands.go index 4e4ae8f..394fbcb 100644 --- a/commands.go +++ b/commands.go @@ -235,7 +235,7 @@ func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { ce.Reply("**Usage:** `login username password`") return } - + leavePortals(ce) if !ce.User.Connect(true) { ce.User.log.Debugln("Connect() returned false, assuming error was logged elsewhere and canceling login.") return @@ -271,6 +271,19 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { Username: username, Password: password, } + + ce.User.Conn.Store = &skype.Store{ + Contacts: make(map[string]skype.Contact), + Chats: make(map[string]skype.Conversation), + } + ce.Reply("Logged out successfully.") + leavePortals(ce) + if ce.User.Conn.Refresh != nil { + ce.User.Conn.Refresh <- -1 + } +} + +func leavePortals(ce *CommandEvent) { portals := ce.User.GetPortals() //newPortals := ce.User.GetPortalsNew() //allPortals := newPortals[0:] @@ -297,57 +310,8 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { for _, portal := range portals { leave(portal) } - ce.User.Conn.Store = &skype.Store{ - Contacts: make(map[string]skype.Contact), - Chats: make(map[string]skype.Conversation), - } - ce.Reply("Logged out successfully.") - - //defer func() { - // if r := recover(); r != nil { - // fmt.Printf("close ReRefresh:%s\n", r) - // } - //}() - if ce.User.Conn.Refresh != nil { - ce.User.Conn.Refresh <- -1 - } } -// CommandLogout handles !logout command -//func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { -// if ce.User.Session == nil { -// ce.Reply("You're not logged in.") -// return -// } else if !ce.User.IsConnected() { -// ce.Reply("You are not connected to WhatsApp. Use the `reconnect` command to reconnect, or `delete-session` to forget all login information.") -// return -// } -// puppet := handler.bridge.GetPuppetByJID(ce.User.JID) -// if puppet.CustomMXID != "" { -// err := puppet.SwitchCustomMXID("", "") -// if err != nil { -// ce.User.log.Warnln("Failed to logout-matrix while logging out of WhatsApp:", err) -// } -// } -// err := ce.User.Conn.Logout() -// if err != nil { -// ce.User.log.Warnln("Error while logging out:", err) -// ce.Reply("Unknown error while logging out: %v", err) -// return -// } -// _, err = ce.User.Conn.Disconnect() -// if err != nil { -// ce.User.log.Warnln("Error while disconnecting after logout:", err) -// } -// ce.User.Conn.RemoveHandlers() -// ce.User.Conn = nil -// ce.User.removeFromJIDMap() -// // TODO this causes a foreign key violation, which should be fixed -// //ce.User.JID = "" -// ce.User.SetSession(nil) -// ce.Reply("Logged out successfully.") -//} - const cmdDeleteSessionHelp = `delete-session - Delete session information and disconnect from WhatsApp without sending a logout request` //func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) { diff --git a/portal.go b/portal.go index b27c42b..53d2b9c 100644 --- a/portal.go +++ b/portal.go @@ -176,6 +176,11 @@ func (portal *Portal) handleMessageLoop() { fmt.Printf("portal handleMessageLoop2: %+v", msg) return } + } else { + if !msg.source.IsInPortal(portal.Key) { + fmt.Println("portal handleMessageLoop InPortal:") + msg.source.CreateUserPortal(database.PortalKeyWithMeta{PortalKey: portal.Key, InCommunity: false}) + } } fmt.Println() fmt.Printf("portal handleMessageLoop3: %+v", msg) @@ -1423,7 +1428,7 @@ func (portal *Portal) sendMessage(intent *appservice.IntentAPI, eventType event. } } fmt.Println("portal sendMessage timestamp:", timestamp) - fmt.Printf("portal sendMessage: %+v", content) + fmt.Printf("portal sendMessage: %+v\n", content) if portal.Encrypted && portal.bridge.Crypto != nil { encrypted, err := portal.bridge.Crypto.Encrypt(portal.MXID, eventType, wrappedContent) if err != nil { @@ -1499,6 +1504,15 @@ func (portal *Portal) trySendMessage(intent *appservice.IntentAPI, eventType eve portal.log.Errorfln("Failed to handle message %s: %v", message.Id, err) } } + } else if strings.Index(err.Error(), "M_FORBIDDEN (HTTP 403)") > -1 { + puppet := source.bridge.GetPuppetByJID(message.Jid) + intentP := puppet.IntentFor(portal) + _, err = intentP.InviteUser(portal.MXID, &mautrix.ReqInviteUser{ + UserID: intent.UserID, + }) + if err == nil { + resp, err = portal.sendMessage(intent, eventType, content, message.Timestamp * 1000) + } } } return @@ -2233,6 +2247,9 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) { } if err != nil { portal.log.Errorfln("Error handling Matrix event %s: %v", evt.ID, err) + if !sender.Conn.LoggedIn { + err = errors.New("Skype account has been logged out.") + } portal.sendErrorMessage(err) } else { portal.log.Debugfln("Handled Matrix event %s", evt.ID) @@ -2248,7 +2265,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) { func SendMsg(sender *User, chatThreadId string, content *skype.SendMessage, output chan<- error) (err error) { fmt.Println("message SendMsg type: ", content.Type) - if sender.Conn.LoginInfo != nil { + if sender.Conn.LoginInfo != nil && sender.Conn.LoggedIn != false { switch event.MessageType(content.Type) { case event.MsgText, event.MsgEmote, event.MsgNotice: err = sender.Conn.SendText(chatThreadId, content)