user.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2019 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 database
  17. import (
  18. "database/sql"
  19. "strings"
  20. "time"
  21. "github.com/Rhymen/go-whatsapp"
  22. log "maunium.net/go/maulogger/v2"
  23. "maunium.net/go/mautrix-whatsapp/types"
  24. "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
  25. )
  26. type UserQuery struct {
  27. db *Database
  28. log log.Logger
  29. }
  30. func (uq *UserQuery) New() *User {
  31. return &User{
  32. db: uq.db,
  33. log: uq.log,
  34. }
  35. }
  36. func (uq *UserQuery) GetAll() (users []*User) {
  37. rows, err := uq.db.Query(`SELECT * FROM "user"`)
  38. if err != nil || rows == nil {
  39. return nil
  40. }
  41. defer rows.Close()
  42. for rows.Next() {
  43. users = append(users, uq.New().Scan(rows))
  44. }
  45. return
  46. }
  47. func (uq *UserQuery) GetByMXID(userID types.MatrixUserID) *User {
  48. row := uq.db.QueryRow(`SELECT * FROM "user" WHERE mxid=$1`, userID)
  49. if row == nil {
  50. return nil
  51. }
  52. return uq.New().Scan(row)
  53. }
  54. func (uq *UserQuery) GetByJID(userID types.WhatsAppID) *User {
  55. row := uq.db.QueryRow(`SELECT * FROM "user" WHERE jid=$1`, stripSuffix(userID))
  56. if row == nil {
  57. return nil
  58. }
  59. return uq.New().Scan(row)
  60. }
  61. type User struct {
  62. db *Database
  63. log log.Logger
  64. MXID types.MatrixUserID
  65. JID types.WhatsAppID
  66. ManagementRoom types.MatrixRoomID
  67. Session *whatsapp.Session
  68. LastConnection uint64
  69. }
  70. func (user *User) Scan(row Scannable) *User {
  71. var jid, clientID, clientToken, serverToken sql.NullString
  72. var encKey, macKey []byte
  73. err := row.Scan(&user.MXID, &jid, &user.ManagementRoom, &clientID, &clientToken, &serverToken, &encKey, &macKey,
  74. &user.LastConnection)
  75. if err != nil {
  76. if err != sql.ErrNoRows {
  77. user.log.Errorln("Database scan failed:", err)
  78. }
  79. return nil
  80. }
  81. if len(jid.String) > 0 && len(clientID.String) > 0 {
  82. user.JID = jid.String + whatsappExt.NewUserSuffix
  83. user.Session = &whatsapp.Session{
  84. ClientId: clientID.String,
  85. ClientToken: clientToken.String,
  86. ServerToken: serverToken.String,
  87. EncKey: encKey,
  88. MacKey: macKey,
  89. Wid: jid.String + whatsappExt.OldUserSuffix,
  90. }
  91. } else {
  92. user.Session = nil
  93. }
  94. return user
  95. }
  96. func stripSuffix(jid types.WhatsAppID) string {
  97. if len(jid) == 0 {
  98. return jid
  99. }
  100. index := strings.IndexRune(jid, '@')
  101. if index < 0 {
  102. return jid
  103. }
  104. return jid[:index]
  105. }
  106. func (user *User) jidPtr() *string {
  107. if len(user.JID) > 0 {
  108. str := stripSuffix(user.JID)
  109. return &str
  110. }
  111. return nil
  112. }
  113. func (user *User) sessionUnptr() (sess whatsapp.Session) {
  114. if user.Session != nil {
  115. sess = *user.Session
  116. }
  117. return
  118. }
  119. func (user *User) Insert() {
  120. sess := user.sessionUnptr()
  121. _, err := user.db.Exec(`INSERT INTO "user" (mxid, jid, management_room, last_connection, client_id, client_token, server_token, enc_key, mac_key) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
  122. user.MXID, user.jidPtr(),
  123. user.ManagementRoom, user.LastConnection,
  124. sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey)
  125. if err != nil {
  126. user.log.Warnfln("Failed to insert %s: %v", user.MXID, err)
  127. }
  128. }
  129. func (user *User) UpdateLastConnection() {
  130. user.LastConnection = uint64(time.Now().Unix())
  131. _, err := user.db.Exec(`UPDATE "user" SET last_connection=$1 WHERE mxid=$2`,
  132. user.LastConnection, user.MXID)
  133. if err != nil {
  134. user.log.Warnfln("Failed to update last connection ts: %v", err)
  135. }
  136. }
  137. func (user *User) Update() {
  138. sess := user.sessionUnptr()
  139. _, err := user.db.Exec(`UPDATE "user" SET jid=$1, management_room=$2, last_connection=$3, client_id=$4, client_token=$5, server_token=$6, enc_key=$7, mac_key=$8 WHERE mxid=$9`,
  140. user.jidPtr(), user.ManagementRoom, user.LastConnection,
  141. sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey,
  142. user.MXID)
  143. if err != nil {
  144. user.log.Warnfln("Failed to update %s: %v", user.MXID, err)
  145. }
  146. }