bridgestate.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/status"
  21. "maunium.net/go/mautrix/id"
  22. )
  23. const (
  24. WALoggedOut status.BridgeStateErrorCode = "wa-logged-out"
  25. WAMainDeviceGone status.BridgeStateErrorCode = "wa-main-device-gone"
  26. WAUnknownLogout status.BridgeStateErrorCode = "wa-unknown-logout"
  27. WANotConnected status.BridgeStateErrorCode = "wa-not-connected"
  28. WAConnecting status.BridgeStateErrorCode = "wa-connecting"
  29. WAKeepaliveTimeout status.BridgeStateErrorCode = "wa-keepalive-timeout"
  30. WAPhoneOffline status.BridgeStateErrorCode = "wa-phone-offline"
  31. WAConnectionFailed status.BridgeStateErrorCode = "wa-connection-failed"
  32. WADisconnected status.BridgeStateErrorCode = "wa-transient-disconnect"
  33. )
  34. func init() {
  35. status.BridgeStateHumanErrors.Update(status.BridgeStateErrorMap{
  36. WALoggedOut: "You were logged out from another device. Relogin to continue using the bridge.",
  37. WAMainDeviceGone: "Your phone was logged out from WhatsApp. Relogin to continue using the bridge.",
  38. WAUnknownLogout: "You were logged out for an unknown reason. Relogin to continue using the bridge.",
  39. WANotConnected: "You're not connected to WhatsApp",
  40. WAConnecting: "Reconnecting to WhatsApp...",
  41. WAKeepaliveTimeout: "The WhatsApp web servers are not responding. The bridge will try to reconnect.",
  42. 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.",
  43. WAConnectionFailed: "Connecting to the WhatsApp web servers failed.",
  44. WADisconnected: "Disconnected from WhatsApp. Trying to reconnect.",
  45. })
  46. }
  47. func (user *User) GetRemoteID() string {
  48. if user == nil || user.JID.IsEmpty() {
  49. return ""
  50. }
  51. return user.JID.User
  52. }
  53. func (user *User) GetRemoteName() string {
  54. if user == nil || user.JID.IsEmpty() {
  55. return ""
  56. }
  57. return fmt.Sprintf("+%s", user.JID.User)
  58. }
  59. func (prov *ProvisioningAPI) BridgeStatePing(w http.ResponseWriter, r *http.Request) {
  60. if !prov.bridge.AS.CheckServerToken(w, r) {
  61. return
  62. }
  63. userID := r.URL.Query().Get("user_id")
  64. user := prov.bridge.GetUserByMXID(id.UserID(userID))
  65. var global status.BridgeState
  66. global.StateEvent = status.StateRunning
  67. var remote status.BridgeState
  68. if user.IsConnected() {
  69. if user.Client.IsLoggedIn() {
  70. remote.StateEvent = status.StateConnected
  71. } else if user.Session != nil {
  72. remote.StateEvent = status.StateConnecting
  73. remote.Error = WAConnecting
  74. } // else: unconfigured
  75. } else if user.Session != nil {
  76. remote.StateEvent = status.StateBadCredentials
  77. remote.Error = WANotConnected
  78. } // else: unconfigured
  79. global = global.Fill(nil)
  80. resp := status.GlobalBridgeState{
  81. BridgeState: global,
  82. RemoteStates: map[string]status.BridgeState{},
  83. }
  84. if len(remote.StateEvent) > 0 {
  85. remote = remote.Fill(user)
  86. resp.RemoteStates[remote.RemoteID] = remote
  87. }
  88. user.log.Debugfln("Responding bridge state in bridge status endpoint: %+v", resp)
  89. jsonResponse(w, http.StatusOK, &resp)
  90. if len(resp.RemoteStates) > 0 {
  91. user.BridgeState.SetPrev(remote)
  92. }
  93. }