bridgestate.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "net/http"
  20. "maunium.net/go/mautrix/bridge"
  21. "maunium.net/go/mautrix/id"
  22. )
  23. const (
  24. WALoggedOut bridge.StateErrorCode = "wa-logged-out"
  25. WAMainDeviceGone bridge.StateErrorCode = "wa-main-device-gone"
  26. WAUnknownLogout bridge.StateErrorCode = "wa-unknown-logout"
  27. WANotConnected bridge.StateErrorCode = "wa-not-connected"
  28. WAConnecting bridge.StateErrorCode = "wa-connecting"
  29. WAKeepaliveTimeout bridge.StateErrorCode = "wa-keepalive-timeout"
  30. WAPhoneOffline bridge.StateErrorCode = "wa-phone-offline"
  31. WAConnectionFailed bridge.StateErrorCode = "wa-connection-failed"
  32. )
  33. func init() {
  34. bridge.StateHumanErrors.Update(bridge.StateErrorMap{
  35. WALoggedOut: "You were logged out from another device. Relogin to continue using the bridge.",
  36. WAMainDeviceGone: "Your phone was logged out from WhatsApp. Relogin to continue using the bridge.",
  37. WAUnknownLogout: "You were logged out for an unknown reason. Relogin to continue using the bridge.",
  38. WANotConnected: "You're not connected to WhatsApp",
  39. WAConnecting: "Reconnecting to WhatsApp...",
  40. WAKeepaliveTimeout: "The WhatsApp web servers are not responding. The bridge will try to reconnect.",
  41. 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.",
  42. WAConnectionFailed: "Connecting to the WhatsApp web servers failed.",
  43. })
  44. }
  45. func (user *User) GetRemoteID() string {
  46. if user == nil || user.JID.IsEmpty() {
  47. return ""
  48. }
  49. return fmt.Sprintf("%s_a%d_d%d", user.JID.User, user.JID.Agent, user.JID.Device)
  50. }
  51. func (user *User) GetRemoteName() string {
  52. if user == nil || user.JID.IsEmpty() {
  53. return ""
  54. }
  55. return fmt.Sprintf("+%s", user.JID.User)
  56. }
  57. func (prov *ProvisioningAPI) BridgeStatePing(w http.ResponseWriter, r *http.Request) {
  58. if !prov.bridge.AS.CheckServerToken(w, r) {
  59. return
  60. }
  61. userID := r.URL.Query().Get("user_id")
  62. user := prov.bridge.GetUserByMXID(id.UserID(userID))
  63. var global bridge.State
  64. global.StateEvent = bridge.StateRunning
  65. var remote bridge.State
  66. if user.IsConnected() {
  67. if user.Client.IsLoggedIn() {
  68. remote.StateEvent = bridge.StateConnected
  69. } else if user.Session != nil {
  70. remote.StateEvent = bridge.StateConnecting
  71. remote.Error = WAConnecting
  72. } // else: unconfigured
  73. } else if user.Session != nil {
  74. remote.StateEvent = bridge.StateBadCredentials
  75. remote.Error = WANotConnected
  76. } // else: unconfigured
  77. global = global.Fill(nil)
  78. resp := bridge.GlobalState{
  79. BridgeState: global,
  80. RemoteStates: map[string]bridge.State{},
  81. }
  82. if len(remote.StateEvent) > 0 {
  83. remote = remote.Fill(user)
  84. resp.RemoteStates[remote.RemoteID] = remote
  85. }
  86. user.log.Debugfln("Responding bridge state in bridge status endpoint: %+v", resp)
  87. jsonResponse(w, http.StatusOK, &resp)
  88. if len(resp.RemoteStates) > 0 {
  89. user.BridgeState.SetPrev(remote)
  90. }
  91. }