2021-06-01 14:19:47 +02:00
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2021 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
2021-06-02 20:43:31 +02:00
"fmt"
2021-06-01 14:19:47 +02:00
"net/http"
2022-08-15 15:36:28 +02:00
"maunium.net/go/mautrix/bridge/status"
2021-06-01 14:19:47 +02:00
"maunium.net/go/mautrix/id"
)
const (
2022-08-15 15:36:28 +02:00
WALoggedOut status . BridgeStateErrorCode = "wa-logged-out"
WAMainDeviceGone status . BridgeStateErrorCode = "wa-main-device-gone"
WAUnknownLogout status . BridgeStateErrorCode = "wa-unknown-logout"
WANotConnected status . BridgeStateErrorCode = "wa-not-connected"
WAConnecting status . BridgeStateErrorCode = "wa-connecting"
WAKeepaliveTimeout status . BridgeStateErrorCode = "wa-keepalive-timeout"
WAPhoneOffline status . BridgeStateErrorCode = "wa-phone-offline"
WAConnectionFailed status . BridgeStateErrorCode = "wa-connection-failed"
2021-06-01 14:19:47 +02:00
)
2022-05-30 23:27:43 +02:00
func init ( ) {
2022-08-15 15:36:28 +02:00
status . BridgeStateHumanErrors . Update ( status . BridgeStateErrorMap {
2022-05-30 23:27:43 +02:00
WALoggedOut : "You were logged out from another device. Relogin to continue using the bridge." ,
WAMainDeviceGone : "Your phone was logged out from WhatsApp. Relogin to continue using the bridge." ,
WAUnknownLogout : "You were logged out for an unknown reason. Relogin to continue using the bridge." ,
WANotConnected : "You're not connected to WhatsApp" ,
WAConnecting : "Reconnecting to WhatsApp..." ,
WAKeepaliveTimeout : "The WhatsApp web servers are not responding. The bridge will try to reconnect." ,
WAPhoneOffline : "Your phone hasn't been seen in over 12 days. The bridge is currently connected, but will get disconnected if you don't open the app soon." ,
WAConnectionFailed : "Connecting to the WhatsApp web servers failed." ,
} )
2022-04-22 12:26:37 +02:00
}
2022-05-30 23:27:43 +02:00
func ( user * User ) GetRemoteID ( ) string {
if user == nil || user . JID . IsEmpty ( ) {
return ""
2021-06-01 14:19:47 +02:00
}
2023-01-05 15:28:15 +01:00
return user . JID . User
2021-06-01 14:19:47 +02:00
}
2022-05-30 23:27:43 +02:00
func ( user * User ) GetRemoteName ( ) string {
if user == nil || user . JID . IsEmpty ( ) {
return ""
2022-01-25 13:26:24 +01:00
}
2022-05-30 23:27:43 +02:00
return fmt . Sprintf ( "+%s" , user . JID . User )
2022-01-25 13:26:24 +01:00
}
2021-06-01 14:19:47 +02:00
func ( prov * ProvisioningAPI ) BridgeStatePing ( w http . ResponseWriter , r * http . Request ) {
if ! prov . bridge . AS . CheckServerToken ( w , r ) {
return
}
userID := r . URL . Query ( ) . Get ( "user_id" )
user := prov . bridge . GetUserByMXID ( id . UserID ( userID ) )
2022-08-15 15:36:28 +02:00
var global status . BridgeState
global . StateEvent = status . StateRunning
var remote status . BridgeState
2021-10-27 14:54:34 +02:00
if user . IsConnected ( ) {
2021-11-30 14:14:56 +01:00
if user . Client . IsLoggedIn ( ) {
2022-08-15 15:36:28 +02:00
remote . StateEvent = status . StateConnected
2021-10-22 19:14:34 +02:00
} else if user . Session != nil {
2022-08-15 15:36:28 +02:00
remote . StateEvent = status . StateConnecting
2021-08-25 14:04:40 +02:00
remote . Error = WAConnecting
2021-09-23 20:04:20 +02:00
} // else: unconfigured
} else if user . Session != nil {
2022-08-15 15:36:28 +02:00
remote . StateEvent = status . StateBadCredentials
2021-09-23 20:04:20 +02:00
remote . Error = WANotConnected
} // else: unconfigured
2022-05-30 23:27:43 +02:00
global = global . Fill ( nil )
2022-08-15 15:36:28 +02:00
resp := status . GlobalBridgeState {
2021-09-23 20:04:20 +02:00
BridgeState : global ,
2022-08-15 15:36:28 +02:00
RemoteStates : map [ string ] status . BridgeState { } ,
2021-09-23 20:04:20 +02:00
}
2021-08-25 14:04:40 +02:00
if len ( remote . StateEvent ) > 0 {
2022-05-30 23:27:43 +02:00
remote = remote . Fill ( user )
2021-09-23 20:04:20 +02:00
resp . RemoteStates [ remote . RemoteID ] = remote
2021-08-25 14:04:40 +02:00
}
2021-06-01 14:57:03 +02:00
user . log . Debugfln ( "Responding bridge state in bridge status endpoint: %+v" , resp )
2021-06-01 14:19:47 +02:00
jsonResponse ( w , http . StatusOK , & resp )
2021-08-25 14:04:40 +02:00
if len ( resp . RemoteStates ) > 0 {
2022-05-30 23:27:43 +02:00
user . BridgeState . SetPrev ( remote )
2021-08-25 14:04:40 +02:00
}
2021-06-01 14:19:47 +02:00
}