2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2019-01-12 14:54:04 +01:00
|
|
|
// Copyright (C) 2019 Tulir Asokan
|
2018-08-13 22:24:44 +02:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2018-08-26 21:53:13 +02:00
|
|
|
"database/sql"
|
2018-08-30 00:10:26 +02:00
|
|
|
"strings"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
"github.com/Rhymen/go-whatsapp"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
|
|
|
log "maunium.net/go/maulogger/v2"
|
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/types"
|
2018-08-30 00:10:26 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
2018-08-13 22:24:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserQuery struct {
|
|
|
|
db *Database
|
2018-08-16 18:20:07 +02:00
|
|
|
log log.Logger
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2019-03-06 22:23:15 +01:00
|
|
|
func (uq *UserQuery) CreateTable(dbType string) error {
|
2019-03-06 22:26:55 +01:00
|
|
|
if strings.ToLower(dbType) == "postgres" {
|
|
|
|
_, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user (
|
|
|
|
mxid VARCHAR(255) PRIMARY KEY,
|
|
|
|
jid VARCHAR(255) UNIQUE,
|
|
|
|
|
|
|
|
management_room VARCHAR(255),
|
|
|
|
|
|
|
|
client_id VARCHAR(255),
|
|
|
|
client_token VARCHAR(255),
|
|
|
|
server_token VARCHAR(255),
|
|
|
|
enc_key bytea,
|
|
|
|
mac_key bytea
|
|
|
|
)`)
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
_, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user (
|
|
|
|
mxid VARCHAR(255) PRIMARY KEY,
|
|
|
|
jid VARCHAR(255) UNIQUE,
|
|
|
|
|
|
|
|
management_room VARCHAR(255),
|
|
|
|
|
|
|
|
client_id VARCHAR(255),
|
|
|
|
client_token VARCHAR(255),
|
|
|
|
server_token VARCHAR(255),
|
|
|
|
enc_key BLOB,
|
|
|
|
mac_key BLOB
|
|
|
|
)`)
|
|
|
|
return err
|
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (uq *UserQuery) New() *User {
|
|
|
|
return &User{
|
2018-08-26 21:53:13 +02:00
|
|
|
db: uq.db,
|
|
|
|
log: uq.log,
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uq *UserQuery) GetAll() (users []*User) {
|
2019-03-06 10:59:52 +01:00
|
|
|
rows, err := uq.db.Query("SELECT * FROM whatsapp_user")
|
2018-08-13 22:24:44 +02:00
|
|
|
if err != nil || rows == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
users = append(users, uq.New().Scan(rows))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (uq *UserQuery) GetByMXID(userID types.MatrixUserID) *User {
|
2019-03-06 16:33:42 +01:00
|
|
|
row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE mxid=$1", userID)
|
2018-08-13 22:24:44 +02:00
|
|
|
if row == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return uq.New().Scan(row)
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (uq *UserQuery) GetByJID(userID types.WhatsAppID) *User {
|
2019-03-06 16:33:42 +01:00
|
|
|
row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE jid=$1", stripSuffix(userID))
|
2018-08-28 23:40:54 +02:00
|
|
|
if row == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return uq.New().Scan(row)
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
type User struct {
|
|
|
|
db *Database
|
2018-08-16 18:20:07 +02:00
|
|
|
log log.Logger
|
2018-08-13 22:24:44 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
MXID types.MatrixUserID
|
|
|
|
JID types.WhatsAppID
|
2018-08-16 23:11:28 +02:00
|
|
|
ManagementRoom types.MatrixRoomID
|
2018-08-16 14:59:18 +02:00
|
|
|
Session *whatsapp.Session
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) Scan(row Scannable) *User {
|
2018-10-16 21:36:16 +02:00
|
|
|
var jid, clientID, clientToken, serverToken sql.NullString
|
2018-08-30 00:10:26 +02:00
|
|
|
var encKey, macKey []byte
|
2018-10-16 21:36:16 +02:00
|
|
|
err := row.Scan(&user.MXID, &jid, &user.ManagementRoom, &clientID, &clientToken, &serverToken, &encKey, &macKey)
|
2018-08-13 22:24:44 +02:00
|
|
|
if err != nil {
|
2018-08-18 21:57:08 +02:00
|
|
|
if err != sql.ErrNoRows {
|
2018-08-26 00:55:21 +02:00
|
|
|
user.log.Errorln("Database scan failed:", err)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
return nil
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
2018-08-30 00:10:26 +02:00
|
|
|
if len(jid.String) > 0 && len(clientID.String) > 0 {
|
|
|
|
user.JID = jid.String + whatsappExt.NewUserSuffix
|
|
|
|
user.Session = &whatsapp.Session{
|
|
|
|
ClientId: clientID.String,
|
|
|
|
ClientToken: clientToken.String,
|
|
|
|
ServerToken: serverToken.String,
|
|
|
|
EncKey: encKey,
|
|
|
|
MacKey: macKey,
|
|
|
|
Wid: jid.String + whatsappExt.OldUserSuffix,
|
|
|
|
}
|
2018-08-16 14:59:18 +02:00
|
|
|
} else {
|
|
|
|
user.Session = nil
|
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2018-08-30 00:10:26 +02:00
|
|
|
func stripSuffix(jid types.WhatsAppID) string {
|
|
|
|
if len(jid) == 0 {
|
|
|
|
return jid
|
|
|
|
}
|
|
|
|
|
|
|
|
index := strings.IndexRune(jid, '@')
|
|
|
|
if index < 0 {
|
|
|
|
return jid
|
2018-08-28 23:40:54 +02:00
|
|
|
}
|
2018-08-30 00:10:26 +02:00
|
|
|
|
|
|
|
return jid[:index]
|
2018-08-28 23:40:54 +02:00
|
|
|
}
|
|
|
|
|
2018-09-01 22:38:03 +02:00
|
|
|
func (user *User) jidPtr() *string {
|
|
|
|
if len(user.JID) > 0 {
|
|
|
|
str := stripSuffix(user.JID)
|
|
|
|
return &str
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (user *User) sessionUnptr() (sess whatsapp.Session) {
|
2018-08-16 14:59:18 +02:00
|
|
|
if user.Session != nil {
|
|
|
|
sess = *user.Session
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-21 22:55:16 +01:00
|
|
|
func (user *User) Insert() {
|
2018-08-28 23:40:54 +02:00
|
|
|
sess := user.sessionUnptr()
|
2019-03-06 16:33:42 +01:00
|
|
|
_, err := user.db.Exec("INSERT INTO whatsapp_user VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", user.MXID, user.jidPtr(),
|
2018-08-30 00:10:26 +02:00
|
|
|
user.ManagementRoom,
|
|
|
|
sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey)
|
2018-09-01 22:38:03 +02:00
|
|
|
if err != nil {
|
|
|
|
user.log.Warnfln("Failed to insert %s: %v", user.MXID, err)
|
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:55:16 +01:00
|
|
|
func (user *User) Update() {
|
2018-08-28 23:40:54 +02:00
|
|
|
sess := user.sessionUnptr()
|
2019-03-06 16:33:42 +01:00
|
|
|
_, err := user.db.Exec("UPDATE whatsapp_user SET jid=$1, management_room=$2, client_id=$3, client_token=$4, server_token=$5, enc_key=$6, mac_key=$7 WHERE mxid=$8",
|
2018-09-01 22:38:03 +02:00
|
|
|
user.jidPtr(), user.ManagementRoom,
|
2018-08-30 00:10:26 +02:00
|
|
|
sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey,
|
2018-08-28 23:40:54 +02:00
|
|
|
user.MXID)
|
2018-09-01 22:38:03 +02:00
|
|
|
if err != nil {
|
|
|
|
user.log.Warnfln("Failed to update %s: %v", user.MXID, err)
|
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|