2020-05-09 19:07:21 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-08-02 12:55:02 +02:00
|
|
|
// +build cgo,!nocrypto
|
2020-05-09 19:25:48 +02:00
|
|
|
|
2020-05-09 19:07:21 +02:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
log "maunium.net/go/maulogger/v2"
|
|
|
|
|
|
|
|
"maunium.net/go/mautrix/crypto"
|
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SQLCryptoStore struct {
|
2020-06-23 22:22:30 +02:00
|
|
|
*crypto.SQLCryptoStore
|
|
|
|
UserID id.UserID
|
2020-05-09 19:07:21 +02:00
|
|
|
GhostIDFormat string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ crypto.Store = (*SQLCryptoStore)(nil)
|
|
|
|
|
2020-07-10 14:56:45 +02:00
|
|
|
func NewSQLCryptoStore(db *Database, userID id.UserID, ghostIDFormat string) *SQLCryptoStore {
|
2020-05-09 19:07:21 +02:00
|
|
|
return &SQLCryptoStore{
|
2020-07-10 14:56:45 +02:00
|
|
|
SQLCryptoStore: crypto.NewSQLCryptoStore(db.DB, db.dialect, "", "",
|
2020-06-23 22:22:30 +02:00
|
|
|
[]byte("maunium.net/go/mautrix-whatsapp"),
|
|
|
|
&cryptoLogger{db.log.Sub("CryptoStore")}),
|
|
|
|
UserID: userID,
|
|
|
|
GhostIDFormat: ghostIDFormat,
|
2020-05-09 19:07:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:56:45 +02:00
|
|
|
func (store *SQLCryptoStore) FindDeviceID() (deviceID id.DeviceID) {
|
|
|
|
err := store.DB.QueryRow("SELECT device_id FROM crypto_account WHERE account_id=$1", store.AccountID).Scan(&deviceID)
|
2020-05-09 19:07:21 +02:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2020-07-10 14:56:45 +02:00
|
|
|
store.Log.Warn("Failed to scan device ID: %v", err)
|
2020-05-09 19:07:21 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *SQLCryptoStore) GetRoomMembers(roomID id.RoomID) (members []id.UserID, err error) {
|
|
|
|
var rows *sql.Rows
|
2020-06-23 22:22:30 +02:00
|
|
|
rows, err = store.DB.Query(`
|
2020-05-09 19:07:21 +02:00
|
|
|
SELECT user_id FROM mx_user_profile
|
|
|
|
WHERE room_id=$1
|
|
|
|
AND (membership='join' OR membership='invite')
|
|
|
|
AND user_id<>$2
|
|
|
|
AND user_id NOT LIKE $3
|
|
|
|
`, roomID, store.UserID, store.GhostIDFormat)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
var userID id.UserID
|
|
|
|
err := rows.Scan(&userID)
|
|
|
|
if err != nil {
|
2020-06-23 22:22:30 +02:00
|
|
|
store.Log.Warn("Failed to scan member in %s: %v", roomID, err)
|
2020-05-09 19:07:21 +02:00
|
|
|
} else {
|
|
|
|
members = append(members, userID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:22:30 +02:00
|
|
|
// TODO merge this with the one in the parent package
|
|
|
|
type cryptoLogger struct {
|
|
|
|
int log.Logger
|
2020-05-09 19:07:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:22:30 +02:00
|
|
|
var levelTrace = log.Level{
|
|
|
|
Name: "Trace",
|
|
|
|
Severity: -10,
|
|
|
|
Color: -1,
|
2020-05-12 21:25:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:22:30 +02:00
|
|
|
func (c *cryptoLogger) Error(message string, args ...interface{}) {
|
|
|
|
c.int.Errorfln(message, args...)
|
2020-05-09 19:07:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:22:30 +02:00
|
|
|
func (c *cryptoLogger) Warn(message string, args ...interface{}) {
|
|
|
|
c.int.Warnfln(message, args...)
|
2020-05-12 22:16:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:22:30 +02:00
|
|
|
func (c *cryptoLogger) Debug(message string, args ...interface{}) {
|
|
|
|
c.int.Debugfln(message, args...)
|
2020-05-09 19:07:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:22:30 +02:00
|
|
|
func (c *cryptoLogger) Trace(message string, args ...interface{}) {
|
|
|
|
c.int.Logfln(levelTrace, message, args...)
|
2020-05-09 19:07:21 +02:00
|
|
|
}
|