|
@@ -76,6 +76,8 @@ type User struct {
|
|
mgmtCreateLock sync.Mutex
|
|
mgmtCreateLock sync.Mutex
|
|
connLock sync.Mutex
|
|
connLock sync.Mutex
|
|
cancelReconnect func()
|
|
cancelReconnect func()
|
|
|
|
+
|
|
|
|
+ prevBridgeStatus *AsmuxPong
|
|
}
|
|
}
|
|
|
|
|
|
func (bridge *Bridge) GetUserByMXID(userID id.UserID) *User {
|
|
func (bridge *Bridge) GetUserByMXID(userID id.UserID) *User {
|
|
@@ -116,6 +118,7 @@ func (user *User) removeFromJIDMap() {
|
|
}
|
|
}
|
|
user.bridge.usersLock.Unlock()
|
|
user.bridge.usersLock.Unlock()
|
|
user.bridge.Metrics.TrackLoginState(user.JID, false)
|
|
user.bridge.Metrics.TrackLoginState(user.JID, false)
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWANotLoggedIn})
|
|
}
|
|
}
|
|
|
|
|
|
func (bridge *Bridge) GetAllUsers() []*User {
|
|
func (bridge *Bridge) GetAllUsers() []*User {
|
|
@@ -249,6 +252,9 @@ func (user *User) Connect(evenIfNoSession bool) bool {
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
user.log.Debugln("Connecting to WhatsApp")
|
|
user.log.Debugln("Connecting to WhatsApp")
|
|
|
|
+ if user.Session != nil {
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWAConnecting})
|
|
|
|
+ }
|
|
timeout := time.Duration(user.bridge.Config.Bridge.ConnectionTimeout)
|
|
timeout := time.Duration(user.bridge.Config.Bridge.ConnectionTimeout)
|
|
if timeout == 0 {
|
|
if timeout == 0 {
|
|
timeout = 20
|
|
timeout = 20
|
|
@@ -261,6 +267,7 @@ func (user *User) Connect(evenIfNoSession bool) bool {
|
|
Log: user.log.Sub("Conn"),
|
|
Log: user.log.Sub("Conn"),
|
|
Handler: []whatsapp.Handler{user},
|
|
Handler: []whatsapp.Handler{user},
|
|
})
|
|
})
|
|
|
|
+ user.setupAdminTestHooks()
|
|
user.connLock.Unlock()
|
|
user.connLock.Unlock()
|
|
return user.RestoreSession()
|
|
return user.RestoreSession()
|
|
}
|
|
}
|
|
@@ -278,6 +285,7 @@ func (user *User) DeleteConnection() {
|
|
user.Conn.RemoveHandlers()
|
|
user.Conn.RemoveHandlers()
|
|
user.Conn = nil
|
|
user.Conn = nil
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, false)
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, false)
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWANotConnected})
|
|
user.connLock.Unlock()
|
|
user.connLock.Unlock()
|
|
}
|
|
}
|
|
|
|
|
|
@@ -300,6 +308,7 @@ func (user *User) RestoreSession() bool {
|
|
user.DeleteConnection()
|
|
user.DeleteConnection()
|
|
return false
|
|
return false
|
|
} else {
|
|
} else {
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWANotConnected})
|
|
user.sendMarkdownBridgeAlert("\u26a0 Failed to connect to WhatsApp. Make sure WhatsApp " +
|
|
user.sendMarkdownBridgeAlert("\u26a0 Failed to connect to WhatsApp. Make sure WhatsApp " +
|
|
"on your phone is reachable and use `reconnect` to try connecting again.")
|
|
"on your phone is reachable and use `reconnect` to try connecting again.")
|
|
}
|
|
}
|
|
@@ -449,6 +458,7 @@ func (cl ChatList) Swap(i, j int) {
|
|
}
|
|
}
|
|
|
|
|
|
func (user *User) PostLogin() {
|
|
func (user *User) PostLogin() {
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{OK: true})
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, true)
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, true)
|
|
user.bridge.Metrics.TrackLoginState(user.JID, true)
|
|
user.bridge.Metrics.TrackLoginState(user.JID, true)
|
|
user.bridge.Metrics.TrackBufferLength(user.MXID, 0)
|
|
user.bridge.Metrics.TrackBufferLength(user.MXID, 0)
|
|
@@ -522,6 +532,7 @@ func (user *User) postConnPing() bool {
|
|
if disconnectErr != nil {
|
|
if disconnectErr != nil {
|
|
user.log.Warnln("Error while disconnecting after failed post-connection ping:", disconnectErr)
|
|
user.log.Warnln("Error while disconnecting after failed post-connection ping:", disconnectErr)
|
|
}
|
|
}
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWANotConnected})
|
|
user.bridge.Metrics.TrackDisconnection(user.MXID)
|
|
user.bridge.Metrics.TrackDisconnection(user.MXID)
|
|
go func() {
|
|
go func() {
|
|
time.Sleep(1 * time.Second)
|
|
time.Sleep(1 * time.Second)
|
|
@@ -831,6 +842,7 @@ func (user *User) HandleError(err error) {
|
|
if closed.Code == 1000 && user.cleanDisconnection {
|
|
if closed.Code == 1000 && user.cleanDisconnection {
|
|
user.cleanDisconnection = false
|
|
user.cleanDisconnection = false
|
|
if !user.bridge.Config.Bridge.AggressiveReconnect {
|
|
if !user.bridge.Config.Bridge.AggressiveReconnect {
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWANotConnected})
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, false)
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, false)
|
|
user.log.Infoln("Clean disconnection by server")
|
|
user.log.Infoln("Clean disconnection by server")
|
|
return
|
|
return
|
|
@@ -863,6 +875,7 @@ func (user *User) tryReconnect(msg string) {
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, false)
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, false)
|
|
if user.ConnectionErrors > user.bridge.Config.Bridge.MaxConnectionAttempts {
|
|
if user.ConnectionErrors > user.bridge.Config.Bridge.MaxConnectionAttempts {
|
|
user.sendMarkdownBridgeAlert("%s. Use the `reconnect` command to reconnect.", msg)
|
|
user.sendMarkdownBridgeAlert("%s. Use the `reconnect` command to reconnect.", msg)
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWANotConnected})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
|
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
|
@@ -888,6 +901,7 @@ func (user *User) tryReconnect(msg string) {
|
|
return
|
|
return
|
|
default:
|
|
default:
|
|
}
|
|
}
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWAConnecting})
|
|
err := user.Conn.Restore(true, ctx)
|
|
err := user.Conn.Restore(true, ctx)
|
|
if err == nil {
|
|
if err == nil {
|
|
user.ConnectionErrors = 0
|
|
user.ConnectionErrors = 0
|
|
@@ -910,6 +924,7 @@ func (user *User) tryReconnect(msg string) {
|
|
user.DeleteConnection()
|
|
user.DeleteConnection()
|
|
user.sendMarkdownBridgeAlert("\u26a0 Failed to reconnect to WhatsApp: unpaired from phone. " +
|
|
user.sendMarkdownBridgeAlert("\u26a0 Failed to reconnect to WhatsApp: unpaired from phone. " +
|
|
"To re-pair your phone, log in again.")
|
|
"To re-pair your phone, log in again.")
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWANotLoggedIn})
|
|
return
|
|
return
|
|
} else if errors.Is(err, whatsapp.ErrAlreadyLoggedIn) {
|
|
} else if errors.Is(err, whatsapp.ErrAlreadyLoggedIn) {
|
|
user.log.Warnln("Reconnection said we're already logged in, not trying anymore")
|
|
user.log.Warnln("Reconnection said we're already logged in, not trying anymore")
|
|
@@ -930,6 +945,7 @@ func (user *User) tryReconnect(msg string) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ user.sendBridgeStatus(AsmuxPong{Error: AsmuxWANotConnected})
|
|
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
|
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
|
user.sendMarkdownBridgeAlert("%d reconnection attempts failed. Use the `reconnect` command to try to reconnect manually.", tries)
|
|
user.sendMarkdownBridgeAlert("%d reconnection attempts failed. Use the `reconnect` command to try to reconnect manually.", tries)
|
|
} else {
|
|
} else {
|