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-05-30 23:27:43 +02:00
"maunium.net/go/mautrix/bridge"
2021-06-01 14:19:47 +02:00
"maunium.net/go/mautrix/id"
)
const (
2022-05-30 23:27:43 +02:00
WALoggedOut bridge . StateErrorCode = "wa-logged-out"
WAMainDeviceGone bridge . StateErrorCode = "wa-main-device-gone"
WAUnknownLogout bridge . StateErrorCode = "wa-unknown-logout"
WANotConnected bridge . StateErrorCode = "wa-not-connected"
WAConnecting bridge . StateErrorCode = "wa-connecting"
WAKeepaliveTimeout bridge . StateErrorCode = "wa-keepalive-timeout"
WAPhoneOffline bridge . StateErrorCode = "wa-phone-offline"
WAConnectionFailed bridge . StateErrorCode = "wa-connection-failed"
2021-06-01 14:19:47 +02:00
)
2022-05-30 23:27:43 +02:00
func init ( ) {
bridge . StateHumanErrors . Update ( bridge . StateErrorMap {
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
}
2022-05-30 23:27:43 +02:00
return fmt . Sprintf ( "%s_a%d_d%d" , user . JID . User , user . JID . Agent , user . JID . Device )
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-05-30 23:27:43 +02:00
var global bridge . State
global . StateEvent = bridge . StateRunning
var remote bridge . State
2021-10-27 14:54:34 +02:00
if user . IsConnected ( ) {
2021-11-30 14:14:56 +01:00
if user . Client . IsLoggedIn ( ) {
2022-05-30 23:27:43 +02:00
remote . StateEvent = bridge . StateConnected
2021-10-22 19:14:34 +02:00
} else if user . Session != nil {
2022-05-30 23:27:43 +02:00
remote . StateEvent = bridge . 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-05-30 23:27:43 +02:00
remote . StateEvent = bridge . 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 )
resp := bridge . GlobalState {
2021-09-23 20:04:20 +02:00
BridgeState : global ,
2022-05-30 23:27:43 +02:00
RemoteStates : map [ string ] bridge . State { } ,
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
}