cryptostore.go 2.9 KB

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