2023-07-02 14:21:55 +00:00
// mautrix-gmessages - A Matrix-Google Messages puppeting bridge.
// Copyright (C) 2023 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 (
2023-07-16 22:13:46 +00:00
"net/http"
2023-07-02 14:21:55 +00:00
"maunium.net/go/mautrix/bridge/status"
2023-07-16 22:13:46 +00:00
"maunium.net/go/mautrix/id"
2023-07-02 14:21:55 +00:00
)
const (
2023-07-03 21:03:36 +00:00
GMListenError status . BridgeStateErrorCode = "gm-listen-error"
GMFatalError status . BridgeStateErrorCode = "gm-listen-fatal-error"
2023-07-15 23:41:34 +00:00
GMUnpaired status . BridgeStateErrorCode = "gm-unpaired"
2023-09-04 11:33:08 +00:00
GMUnpaired404 status . BridgeStateErrorCode = "gm-unpaired-entity-not-found"
2023-07-03 21:03:36 +00:00
GMNotConnected status . BridgeStateErrorCode = "gm-not-connected"
GMConnecting status . BridgeStateErrorCode = "gm-connecting"
GMConnectionFailed status . BridgeStateErrorCode = "gm-connection-failed"
2023-09-04 11:24:45 +00:00
GMPingFailed status . BridgeStateErrorCode = "gm-ping-failed"
2023-09-04 11:40:33 +00:00
GMNotDefaultSMSApp status . BridgeStateErrorCode = "gm-not-default-sms-app"
2023-07-15 12:02:03 +00:00
GMBrowserInactive status . BridgeStateErrorCode = "gm-browser-inactive"
GMBrowserInactiveTimeout status . BridgeStateErrorCode = "gm-browser-inactive-timeout"
GMBrowserInactiveInactivity status . BridgeStateErrorCode = "gm-browser-inactive-inactivity"
2023-07-19 21:58:39 +00:00
GMPhoneNotResponding status . BridgeStateErrorCode = "gm-phone-not-responding"
2023-07-02 14:21:55 +00:00
)
func init ( ) {
2023-07-15 12:02:03 +00:00
status . BridgeStateHumanErrors . Update ( status . BridgeStateErrorMap {
GMListenError : "Error polling messages from Google Messages server, the bridge will try to reconnect" ,
2023-08-15 15:34:55 +00:00
GMFatalError : "Fatal error polling messages from Google Messages server, please re-link the bridge" ,
GMUnpaired : "Unpaired from Google Messages, please re-link the connection to continue using SMS/RCS" ,
2023-09-04 11:33:08 +00:00
GMUnpaired404 : "Unpaired from Google Messages, please re-link the connection to continue using SMS/RCS" ,
2023-09-04 11:40:33 +00:00
GMNotDefaultSMSApp : "Google Messages isn't set as the default SMS app. Please set the default SMS app on your Android phone to Google Messages to continue using SMS/RCS." ,
2023-07-15 12:02:03 +00:00
GMBrowserInactive : "Google Messages opened in another browser" ,
GMBrowserInactiveTimeout : "Google Messages disconnected due to timeout" ,
GMBrowserInactiveInactivity : "Google Messages disconnected due to inactivity" ,
2023-08-15 15:34:55 +00:00
GMPhoneNotResponding : "Your phone is not responding, please check that it is connected to the internet. You may need to open the Messages app on your phone for it to reconnect." ,
2023-07-15 12:02:03 +00:00
} )
2023-07-02 14:21:55 +00:00
}
func ( user * User ) GetRemoteID ( ) string {
if user == nil {
return ""
}
2023-07-19 17:32:01 +00:00
return user . PhoneID
2023-07-02 14:21:55 +00:00
}
func ( user * User ) GetRemoteName ( ) string {
return ""
}
2023-07-16 22:13:46 +00:00
func ( prov * ProvisioningAPI ) BridgeStatePing ( w http . ResponseWriter , r * http . Request ) {
2023-07-02 14:21:55 +00:00
if ! prov . bridge . AS . CheckServerToken ( w , r ) {
return
}
userID := r . URL . Query ( ) . Get ( "user_id" )
user := prov . bridge . GetUserByMXID ( id . UserID ( userID ) )
var global status . BridgeState
global . StateEvent = status . StateRunning
var remote status . BridgeState
if user . IsConnected ( ) {
if user . Client . IsLoggedIn ( ) {
remote . StateEvent = status . StateConnected
} else if user . Session != nil {
remote . StateEvent = status . StateConnecting
2023-07-16 22:13:46 +00:00
//remote.Error = WAConnecting
2023-07-02 14:21:55 +00:00
} // else: unconfigured
} else if user . Session != nil {
remote . StateEvent = status . StateBadCredentials
2023-07-16 22:13:46 +00:00
//remote.Error = WANotConnected
2023-07-02 14:21:55 +00:00
} // else: unconfigured
global = global . Fill ( nil )
resp := status . GlobalBridgeState {
BridgeState : global ,
RemoteStates : map [ string ] status . BridgeState { } ,
}
if len ( remote . StateEvent ) > 0 {
remote = remote . Fill ( user )
resp . RemoteStates [ remote . RemoteID ] = remote
}
user . log . Debugfln ( "Responding bridge state in bridge status endpoint: %+v" , resp )
jsonResponse ( w , http . StatusOK , & resp )
if len ( resp . RemoteStates ) > 0 {
user . BridgeState . SetPrev ( remote )
}
2023-07-16 22:13:46 +00:00
}