cryptostore.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2020 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. //go:build cgo && !nocrypto
  17. // +build cgo,!nocrypto
  18. package database
  19. import (
  20. "database/sql"
  21. log "maunium.net/go/maulogger/v2"
  22. "maunium.net/go/mautrix/crypto"
  23. "maunium.net/go/mautrix/id"
  24. )
  25. type SQLCryptoStore struct {
  26. *crypto.SQLCryptoStore
  27. UserID id.UserID
  28. GhostIDFormat string
  29. }
  30. var _ crypto.Store = (*SQLCryptoStore)(nil)
  31. func NewSQLCryptoStore(db *Database, userID id.UserID, ghostIDFormat string) *SQLCryptoStore {
  32. return &SQLCryptoStore{
  33. SQLCryptoStore: crypto.NewSQLCryptoStore(db.DB, db.dialect, "", "",
  34. []byte("maunium.net/go/mautrix-whatsapp"),
  35. &cryptoLogger{db.log.Sub("CryptoStore")}),
  36. UserID: userID,
  37. GhostIDFormat: ghostIDFormat,
  38. }
  39. }
  40. func (store *SQLCryptoStore) FindDeviceID() (deviceID id.DeviceID) {
  41. err := store.DB.QueryRow("SELECT device_id FROM crypto_account WHERE account_id=$1", store.AccountID).Scan(&deviceID)
  42. if err != nil && err != sql.ErrNoRows {
  43. store.Log.Warn("Failed to scan device ID: %v", err)
  44. }
  45. return
  46. }
  47. func (store *SQLCryptoStore) GetRoomMembers(roomID id.RoomID) (members []id.UserID, err error) {
  48. var rows *sql.Rows
  49. rows, err = store.DB.Query(`
  50. SELECT user_id FROM mx_user_profile
  51. WHERE room_id=$1
  52. AND (membership='join' OR membership='invite')
  53. AND user_id<>$2
  54. AND user_id NOT LIKE $3
  55. `, roomID, store.UserID, store.GhostIDFormat)
  56. if err != nil {
  57. return
  58. }
  59. for rows.Next() {
  60. var userID id.UserID
  61. err := rows.Scan(&userID)
  62. if err != nil {
  63. store.Log.Warn("Failed to scan member in %s: %v", roomID, err)
  64. } else {
  65. members = append(members, userID)
  66. }
  67. }
  68. return
  69. }
  70. // TODO merge this with the one in the parent package
  71. type cryptoLogger struct {
  72. int log.Logger
  73. }
  74. var levelTrace = log.Level{
  75. Name: "Trace",
  76. Severity: -10,
  77. Color: -1,
  78. }
  79. func (c *cryptoLogger) Error(message string, args ...interface{}) {
  80. c.int.Errorfln(message, args...)
  81. }
  82. func (c *cryptoLogger) Warn(message string, args ...interface{}) {
  83. c.int.Warnfln(message, args...)
  84. }
  85. func (c *cryptoLogger) Debug(message string, args ...interface{}) {
  86. c.int.Debugfln(message, args...)
  87. }
  88. func (c *cryptoLogger) Trace(message string, args ...interface{}) {
  89. c.int.Logfln(levelTrace, message, args...)
  90. }