Fix most unexpected logout bugs, handle connection-in-progress states better and send warning message if sending to whatsapp times out
This commit is contained in:
		
							
								
								
									
										22
									
								
								commands.go
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								commands.go
									
									
									
									
									
								
							| @@ -223,6 +223,13 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { | |||||||
| 		} else { | 		} else { | ||||||
| 			ce.Reply("Unknown error while reconnecting: %v", err) | 			ce.Reply("Unknown error while reconnecting: %v", err) | ||||||
| 		} | 		} | ||||||
|  | 		ce.User.log.Debugln("Disconnecting due to failed session restore in reconnect command...") | ||||||
|  | 		sess, err := ce.User.Conn.Disconnect() | ||||||
|  | 		if err != nil { | ||||||
|  | 			ce.User.log.Errorln("Failed to disconnect after failed session restore in reconnect command:", err) | ||||||
|  | 		} else if len(sess.Wid) > 0 { | ||||||
|  | 			ce.User.SetSession(&sess) | ||||||
|  | 		} | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	ce.User.ConnectionErrors = 0 | 	ce.User.ConnectionErrors = 0 | ||||||
| @@ -259,8 +266,9 @@ func (handler *CommandHandler) CommandDisconnect(ce *CommandEvent) { | |||||||
| 		ce.User.log.Warnln("Error while disconnecting:", err) | 		ce.User.log.Warnln("Error while disconnecting:", err) | ||||||
| 		ce.Reply("Unknown error while disconnecting: %v", err) | 		ce.Reply("Unknown error while disconnecting: %v", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} else if len(sess.Wid) > 0 { | ||||||
| 		ce.User.SetSession(&sess) | 		ce.User.SetSession(&sess) | ||||||
|  | 	} | ||||||
| 	ce.Reply("Successfully disconnected. Use the `reconnect` command to reconnect.") | 	ce.Reply("Successfully disconnected. Use the `reconnect` command to reconnect.") | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -268,13 +276,25 @@ const cmdPingHelp = `ping - Check your connection to WhatsApp.` | |||||||
|  |  | ||||||
| func (handler *CommandHandler) CommandPing(ce *CommandEvent) { | func (handler *CommandHandler) CommandPing(ce *CommandEvent) { | ||||||
| 	if ce.User.Session == nil { | 	if ce.User.Session == nil { | ||||||
|  | 		if ce.User.IsLoginInProgress() { | ||||||
|  | 			ce.Reply("You're not logged into WhatsApp, but there's a login in progress.") | ||||||
|  | 		} else { | ||||||
| 			ce.Reply("You're not logged into WhatsApp.") | 			ce.Reply("You're not logged into WhatsApp.") | ||||||
|  | 		} | ||||||
| 	} else if ce.User.Conn == nil { | 	} else if ce.User.Conn == nil { | ||||||
| 		ce.Reply("You don't have a WhatsApp connection.") | 		ce.Reply("You don't have a WhatsApp connection.") | ||||||
| 	} else if ok, err := ce.User.Conn.AdminTest(); err != nil { | 	} else if ok, err := ce.User.Conn.AdminTest(); err != nil { | ||||||
|  | 		if ce.User.IsLoginInProgress() { | ||||||
|  | 			ce.Reply("Connection not OK: %v, but login in progress", err) | ||||||
|  | 		} else { | ||||||
| 			ce.Reply("Connection not OK: %v", err) | 			ce.Reply("Connection not OK: %v", err) | ||||||
|  | 		} | ||||||
| 	} else if !ok { | 	} else if !ok { | ||||||
|  | 		if ce.User.IsLoginInProgress() { | ||||||
|  | 			ce.Reply("Connection not OK, but no error received and login in progress") | ||||||
|  | 		} else { | ||||||
| 			ce.Reply("Connection not OK, but no error received") | 			ce.Reply("Connection not OK, but no error received") | ||||||
|  | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 		ce.Reply("Connection to WhatsApp OK") | 		ce.Reply("Connection to WhatsApp OK") | ||||||
| 	} | 	} | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								main.go
									
									
									
									
									
								
							| @@ -282,7 +282,7 @@ func (bridge *Bridge) Stop() { | |||||||
| 		sess, err := user.Conn.Disconnect() | 		sess, err := user.Conn.Disconnect() | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			bridge.Log.Errorfln("Error while disconnecting %s: %v", user.MXID, err) | 			bridge.Log.Errorfln("Error while disconnecting %s: %v", user.MXID, err) | ||||||
| 		} else { | 		} else if len(sess.Wid) > 0 { | ||||||
| 			user.SetSession(&sess) | 			user.SetSession(&sess) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								portal.go
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								portal.go
									
									
									
									
									
								
							| @@ -1092,8 +1092,11 @@ func (portal *Portal) sendMatrixConnectionError(sender *User, eventID string) bo | |||||||
| 		if portal.IsPrivateChat() { | 		if portal.IsPrivateChat() { | ||||||
| 			inRoom = " in your management room" | 			inRoom = " in your management room" | ||||||
| 		} | 		} | ||||||
| 		msg := format.RenderMarkdown(fmt.Sprintf("\u26a0 You are not connected to WhatsApp, so your message was not bridged. " + | 		reconnect := fmt.Sprintf("Use `%s reconnect`%s to reconnect.", portal.bridge.Config.Bridge.CommandPrefix, inRoom) | ||||||
| 			"Use `%s reconnect`%s to reconnect.", portal.bridge.Config.Bridge.CommandPrefix, inRoom)) | 		if sender.IsLoginInProgress() { | ||||||
|  | 			reconnect = "You have a login attempt in progress, please wait." | ||||||
|  | 		} | ||||||
|  | 		msg := format.RenderMarkdown("\u26a0 You are not connected to WhatsApp, so your message was not bridged. " + reconnect) | ||||||
| 		msg.MsgType = mautrix.MsgNotice | 		msg.MsgType = mautrix.MsgNotice | ||||||
| 		_, err := portal.MainIntent().SendMessageEvent(portal.MXID, mautrix.EventMessage, msg) | 		_, err := portal.MainIntent().SendMessageEvent(portal.MXID, mautrix.EventMessage, msg) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| @@ -1227,6 +1230,12 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *mautrix.Event) { | |||||||
| 	_, err = sender.Conn.Send(info) | 	_, err = sender.Conn.Send(info) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		portal.log.Errorfln("Error handling Matrix event %s: %v", evt.ID, err) | 		portal.log.Errorfln("Error handling Matrix event %s: %v", evt.ID, err) | ||||||
|  | 		msg := format.RenderMarkdown(fmt.Sprintf("\u26a0 Your message may not have been bridged: %v", err)) | ||||||
|  | 		msg.MsgType = mautrix.MsgNotice | ||||||
|  | 		_, err := portal.MainIntent().SendMessageEvent(portal.MXID, mautrix.EventMessage, msg) | ||||||
|  | 		if err != nil { | ||||||
|  | 			portal.log.Errorln("Failed to send bridging failure message:", err) | ||||||
|  | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 		portal.log.Debugln("Handled Matrix event:", evt) | 		portal.log.Debugln("Handled Matrix event:", evt) | ||||||
| 	} | 	} | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								user.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								user.go
									
									
									
									
									
								
							| @@ -233,6 +233,10 @@ func (user *User) IsConnected() bool { | |||||||
| 	return user.Conn != nil && user.Conn.IsConnected() && user.Conn.IsLoggedIn() | 	return user.Conn != nil && user.Conn.IsConnected() && user.Conn.IsLoggedIn() | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (user *User) IsLoginInProgress() bool { | ||||||
|  | 	return user.Conn != nil && user.Conn.IsLoginInProgress() | ||||||
|  | } | ||||||
|  |  | ||||||
| func (user *User) loginQrChannel(ce *CommandEvent, qrChan <-chan string, eventIDChan chan<- string) { | func (user *User) loginQrChannel(ce *CommandEvent, qrChan <-chan string, eventIDChan chan<- string) { | ||||||
| 	var qrEventID string | 	var qrEventID string | ||||||
| 	for code := range qrChan { | 	for code := range qrChan { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user