2020-03-13 17:13:03 +00:00
|
|
|
package soju
|
2020-03-04 17:22:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-03-25 13:15:25 +00:00
|
|
|
"fmt"
|
2020-11-30 21:01:44 +00:00
|
|
|
"math"
|
2021-03-09 17:54:38 +00:00
|
|
|
"net/url"
|
2020-04-15 23:40:50 +00:00
|
|
|
"strings"
|
2020-03-04 17:22:58 +00:00
|
|
|
"sync"
|
2020-11-30 21:01:44 +00:00
|
|
|
"time"
|
2020-03-04 17:22:58 +00:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
2020-08-11 08:26:42 +00:00
|
|
|
ID int64
|
2020-03-04 17:22:58 +00:00
|
|
|
Username string
|
2020-03-11 18:09:32 +00:00
|
|
|
Password string // hashed
|
2020-06-06 23:22:54 +00:00
|
|
|
Admin bool
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 14:12:44 +00:00
|
|
|
type SASL struct {
|
|
|
|
Mechanism string
|
|
|
|
|
|
|
|
Plain struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
2020-05-29 11:10:54 +00:00
|
|
|
|
|
|
|
// TLS client certificate authentication.
|
|
|
|
External struct {
|
|
|
|
// X.509 certificate in DER form.
|
|
|
|
CertBlob []byte
|
|
|
|
// PKCS#8 private key in DER form.
|
|
|
|
PrivKeyBlob []byte
|
|
|
|
}
|
2020-03-13 14:12:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 17:22:58 +00:00
|
|
|
type Network struct {
|
2020-04-15 23:40:50 +00:00
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
Addr string
|
|
|
|
Nick string
|
|
|
|
Username string
|
|
|
|
Realname string
|
|
|
|
Pass string
|
|
|
|
ConnectCommands []string
|
|
|
|
SASL SASL
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 13:23:41 +00:00
|
|
|
func (net *Network) GetName() string {
|
|
|
|
if net.Name != "" {
|
|
|
|
return net.Name
|
|
|
|
}
|
|
|
|
return net.Addr
|
|
|
|
}
|
|
|
|
|
2021-03-09 17:54:38 +00:00
|
|
|
func (net *Network) URL() (*url.URL, error) {
|
|
|
|
s := net.Addr
|
|
|
|
if !strings.Contains(s, "://") {
|
|
|
|
// This is a raw domain name, make it an URL with the default scheme
|
|
|
|
s = "ircs://" + s
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse upstream server URL: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (net *Network) GetUsername() string {
|
|
|
|
if net.Username != "" {
|
|
|
|
return net.Username
|
|
|
|
}
|
|
|
|
return net.Nick
|
|
|
|
}
|
|
|
|
|
|
|
|
func (net *Network) GetRealname() string {
|
|
|
|
if net.Realname != "" {
|
|
|
|
return net.Realname
|
|
|
|
}
|
|
|
|
return net.Nick
|
|
|
|
}
|
|
|
|
|
2020-11-30 21:01:44 +00:00
|
|
|
type MessageFilter int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TODO: use customizable user defaults for FilterDefault
|
|
|
|
FilterDefault MessageFilter = iota
|
|
|
|
FilterNone
|
|
|
|
FilterHighlight
|
|
|
|
FilterMessage
|
|
|
|
)
|
|
|
|
|
|
|
|
func parseFilter(filter string) (MessageFilter, error) {
|
|
|
|
switch filter {
|
|
|
|
case "default":
|
|
|
|
return FilterDefault, nil
|
|
|
|
case "none":
|
|
|
|
return FilterNone, nil
|
|
|
|
case "highlight":
|
|
|
|
return FilterHighlight, nil
|
|
|
|
case "message":
|
|
|
|
return FilterMessage, nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("unknown filter: %q", filter)
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:22:58 +00:00
|
|
|
type Channel struct {
|
2021-04-13 16:15:30 +00:00
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
Key string
|
|
|
|
|
|
|
|
Detached bool
|
|
|
|
DetachedInternalMsgID string
|
2020-11-30 21:01:44 +00:00
|
|
|
|
|
|
|
RelayDetached MessageFilter
|
|
|
|
ReattachOn MessageFilter
|
|
|
|
DetachAfter time.Duration
|
|
|
|
DetachOn MessageFilter
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:16:08 +00:00
|
|
|
type DeliveryReceipt struct {
|
|
|
|
ID int64
|
|
|
|
Target string // channel or nick
|
|
|
|
Client string
|
|
|
|
InternalMsgID string
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:45:02 +00:00
|
|
|
const schema = `
|
|
|
|
CREATE TABLE User (
|
2020-10-24 12:52:55 +00:00
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
username VARCHAR(255) NOT NULL UNIQUE,
|
|
|
|
password VARCHAR(255),
|
2020-06-06 23:22:54 +00:00
|
|
|
admin INTEGER NOT NULL DEFAULT 0
|
2020-04-10 20:45:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE Network (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
name VARCHAR(255),
|
2020-10-24 13:14:23 +00:00
|
|
|
user INTEGER NOT NULL,
|
2020-04-10 20:45:02 +00:00
|
|
|
addr VARCHAR(255) NOT NULL,
|
|
|
|
nick VARCHAR(255) NOT NULL,
|
|
|
|
username VARCHAR(255),
|
|
|
|
realname VARCHAR(255),
|
|
|
|
pass VARCHAR(255),
|
2020-04-15 23:40:50 +00:00
|
|
|
connect_commands VARCHAR(1023),
|
2020-04-10 20:45:02 +00:00
|
|
|
sasl_mechanism VARCHAR(255),
|
|
|
|
sasl_plain_username VARCHAR(255),
|
|
|
|
sasl_plain_password VARCHAR(255),
|
2020-05-29 11:10:54 +00:00
|
|
|
sasl_external_cert BLOB DEFAULT NULL,
|
|
|
|
sasl_external_key BLOB DEFAULT NULL,
|
2020-10-24 13:14:23 +00:00
|
|
|
FOREIGN KEY(user) REFERENCES User(id),
|
|
|
|
UNIQUE(user, addr, nick),
|
|
|
|
UNIQUE(user, name)
|
2020-04-10 20:45:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE Channel (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
|
|
|
name VARCHAR(255) NOT NULL,
|
|
|
|
key VARCHAR(255),
|
2020-04-28 13:27:41 +00:00
|
|
|
detached INTEGER NOT NULL DEFAULT 0,
|
2021-04-13 16:15:30 +00:00
|
|
|
detached_internal_msgid VARCHAR(255),
|
2020-11-30 21:01:44 +00:00
|
|
|
relay_detached INTEGER NOT NULL DEFAULT 0,
|
|
|
|
reattach_on INTEGER NOT NULL DEFAULT 0,
|
|
|
|
detach_after INTEGER NOT NULL DEFAULT 0,
|
|
|
|
detach_on INTEGER NOT NULL DEFAULT 0,
|
2020-04-10 20:45:02 +00:00
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, name)
|
|
|
|
);
|
2021-02-10 17:16:08 +00:00
|
|
|
|
|
|
|
CREATE TABLE DeliveryReceipt (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
|
|
|
target VARCHAR(255) NOT NULL,
|
|
|
|
client VARCHAR(255),
|
|
|
|
internal_msgid VARCHAR(255) NOT NULL,
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, target, client)
|
|
|
|
);
|
2020-04-10 20:45:02 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
var migrations = []string{
|
|
|
|
"", // migration #0 is reserved for schema initialization
|
2020-04-15 23:40:50 +00:00
|
|
|
"ALTER TABLE Network ADD COLUMN connect_commands VARCHAR(1023)",
|
2020-04-28 13:27:41 +00:00
|
|
|
"ALTER TABLE Channel ADD COLUMN detached INTEGER NOT NULL DEFAULT 0",
|
2020-05-29 11:10:54 +00:00
|
|
|
"ALTER TABLE Network ADD COLUMN sasl_external_cert BLOB DEFAULT NULL",
|
|
|
|
"ALTER TABLE Network ADD COLUMN sasl_external_key BLOB DEFAULT NULL",
|
2020-06-06 23:22:54 +00:00
|
|
|
"ALTER TABLE User ADD COLUMN admin INTEGER NOT NULL DEFAULT 0",
|
2020-10-24 12:52:55 +00:00
|
|
|
`
|
|
|
|
CREATE TABLE UserNew (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
username VARCHAR(255) NOT NULL UNIQUE,
|
|
|
|
password VARCHAR(255),
|
|
|
|
admin INTEGER NOT NULL DEFAULT 0
|
|
|
|
);
|
|
|
|
INSERT INTO UserNew SELECT rowid, username, password, admin FROM User;
|
|
|
|
DROP TABLE User;
|
|
|
|
ALTER TABLE UserNew RENAME TO User;
|
|
|
|
`,
|
2020-10-24 13:14:23 +00:00
|
|
|
`
|
|
|
|
CREATE TABLE NetworkNew (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
name VARCHAR(255),
|
|
|
|
user INTEGER NOT NULL,
|
|
|
|
addr VARCHAR(255) NOT NULL,
|
|
|
|
nick VARCHAR(255) NOT NULL,
|
|
|
|
username VARCHAR(255),
|
|
|
|
realname VARCHAR(255),
|
|
|
|
pass VARCHAR(255),
|
|
|
|
connect_commands VARCHAR(1023),
|
|
|
|
sasl_mechanism VARCHAR(255),
|
|
|
|
sasl_plain_username VARCHAR(255),
|
|
|
|
sasl_plain_password VARCHAR(255),
|
|
|
|
sasl_external_cert BLOB DEFAULT NULL,
|
|
|
|
sasl_external_key BLOB DEFAULT NULL,
|
|
|
|
FOREIGN KEY(user) REFERENCES User(id),
|
|
|
|
UNIQUE(user, addr, nick),
|
|
|
|
UNIQUE(user, name)
|
|
|
|
);
|
|
|
|
INSERT INTO NetworkNew
|
|
|
|
SELECT Network.id, name, User.id as user, addr, nick,
|
|
|
|
Network.username, realname, pass, connect_commands,
|
|
|
|
sasl_mechanism, sasl_plain_username, sasl_plain_password,
|
|
|
|
sasl_external_cert, sasl_external_key
|
|
|
|
FROM Network
|
|
|
|
JOIN User ON Network.user = User.username;
|
|
|
|
DROP TABLE Network;
|
|
|
|
ALTER TABLE NetworkNew RENAME TO Network;
|
|
|
|
`,
|
2020-11-30 21:01:44 +00:00
|
|
|
`
|
|
|
|
ALTER TABLE Channel ADD COLUMN relay_detached INTEGER NOT NULL DEFAULT 0;
|
|
|
|
ALTER TABLE Channel ADD COLUMN reattach_on INTEGER NOT NULL DEFAULT 0;
|
|
|
|
ALTER TABLE Channel ADD COLUMN detach_after INTEGER NOT NULL DEFAULT 0;
|
|
|
|
ALTER TABLE Channel ADD COLUMN detach_on INTEGER NOT NULL DEFAULT 0;
|
|
|
|
`,
|
2021-02-10 17:16:08 +00:00
|
|
|
`
|
|
|
|
CREATE TABLE DeliveryReceipt (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
|
|
|
target VARCHAR(255) NOT NULL,
|
|
|
|
client VARCHAR(255),
|
|
|
|
internal_msgid VARCHAR(255) NOT NULL,
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, target, client)
|
|
|
|
);
|
|
|
|
`,
|
2021-04-13 16:15:30 +00:00
|
|
|
"ALTER TABLE Channel ADD COLUMN detached_internal_msgid VARCHAR(255)",
|
2020-04-10 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 17:22:58 +00:00
|
|
|
type DB struct {
|
2020-03-04 19:15:38 +00:00
|
|
|
lock sync.RWMutex
|
2020-03-04 17:22:58 +00:00
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenSQLDB(driver, source string) (*DB, error) {
|
2020-04-10 20:45:02 +00:00
|
|
|
sqlDB, err := sql.Open(driver, source)
|
2020-03-04 17:22:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-10 20:45:02 +00:00
|
|
|
|
|
|
|
db := &DB{db: sqlDB}
|
|
|
|
if err := db.upgrade(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
2020-07-09 09:23:51 +00:00
|
|
|
return db.db.Close()
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 20:45:02 +00:00
|
|
|
func (db *DB) upgrade() error {
|
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
|
|
|
var version int
|
|
|
|
if err := db.db.QueryRow("PRAGMA user_version").Scan(&version); err != nil {
|
|
|
|
return fmt.Errorf("failed to query schema version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if version == len(migrations) {
|
|
|
|
return nil
|
|
|
|
} else if version > len(migrations) {
|
|
|
|
return fmt.Errorf("soju (version %d) older than schema (version %d)", len(migrations), version)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
if version == 0 {
|
|
|
|
if _, err := tx.Exec(schema); err != nil {
|
|
|
|
return fmt.Errorf("failed to initialize schema: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := version; i < len(migrations); i++ {
|
|
|
|
if _, err := tx.Exec(migrations[i]); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute migration #%v: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For some reason prepared statements don't work here
|
|
|
|
_, err = tx.Exec(fmt.Sprintf("PRAGMA user_version = %d", len(migrations)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to bump schema version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2020-10-24 13:47:53 +00:00
|
|
|
func toNullString(s string) sql.NullString {
|
|
|
|
return sql.NullString{
|
|
|
|
String: s,
|
|
|
|
Valid: s != "",
|
2020-03-13 14:12:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:22:58 +00:00
|
|
|
func (db *DB) ListUsers() ([]User, error) {
|
2020-03-04 19:15:38 +00:00
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
2020-03-04 17:22:58 +00:00
|
|
|
|
2020-10-24 12:52:55 +00:00
|
|
|
rows, err := db.db.Query("SELECT id, username, password, admin FROM User")
|
2020-03-04 17:22:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var users []User
|
|
|
|
for rows.Next() {
|
|
|
|
var user User
|
2020-10-24 13:47:53 +00:00
|
|
|
var password sql.NullString
|
2020-08-11 08:26:42 +00:00
|
|
|
if err := rows.Scan(&user.ID, &user.Username, &password, &user.Admin); err != nil {
|
2020-03-04 17:22:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-24 13:47:53 +00:00
|
|
|
user.Password = password.String
|
2020-03-04 17:22:58 +00:00
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:38:38 +00:00
|
|
|
func (db *DB) GetUser(username string) (*User, error) {
|
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
|
|
|
|
2020-08-11 08:26:42 +00:00
|
|
|
user := &User{Username: username}
|
2020-03-27 21:38:38 +00:00
|
|
|
|
2020-10-24 13:47:53 +00:00
|
|
|
var password sql.NullString
|
2020-10-24 12:52:55 +00:00
|
|
|
row := db.db.QueryRow("SELECT id, password, admin FROM User WHERE username = ?", username)
|
2020-08-11 08:26:42 +00:00
|
|
|
if err := row.Scan(&user.ID, &password, &user.Admin); err != nil {
|
2020-03-27 21:38:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-24 13:47:53 +00:00
|
|
|
user.Password = password.String
|
2020-03-27 21:38:38 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2020-06-08 09:59:03 +00:00
|
|
|
func (db *DB) StoreUser(user *User) error {
|
2020-03-11 18:01:03 +00:00
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
2020-10-24 13:47:53 +00:00
|
|
|
password := toNullString(user.Password)
|
2020-03-11 18:01:03 +00:00
|
|
|
|
2020-06-08 09:59:03 +00:00
|
|
|
var err error
|
2020-08-11 08:26:42 +00:00
|
|
|
if user.ID != 0 {
|
2020-06-06 23:22:54 +00:00
|
|
|
_, err = db.db.Exec("UPDATE User SET password = ?, admin = ? WHERE username = ?",
|
|
|
|
password, user.Admin, user.Username)
|
2020-06-08 09:59:03 +00:00
|
|
|
} else {
|
2020-08-11 08:26:42 +00:00
|
|
|
var res sql.Result
|
2020-08-17 22:22:57 +00:00
|
|
|
res, err = db.db.Exec("INSERT INTO User(username, password, admin) VALUES (?, ?, ?)",
|
2020-06-06 23:22:54 +00:00
|
|
|
user.Username, password, user.Admin)
|
2020-08-11 08:26:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-08 09:59:03 +00:00
|
|
|
}
|
2020-08-11 08:26:42 +00:00
|
|
|
user.ID, err = res.LastInsertId()
|
2020-06-08 09:59:03 +00:00
|
|
|
}
|
2020-04-08 10:59:50 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-24 13:14:23 +00:00
|
|
|
func (db *DB) DeleteUser(id int64) error {
|
2020-08-03 16:43:15 +00:00
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
_, err = tx.Exec(`DELETE FROM Channel
|
|
|
|
WHERE id IN (
|
|
|
|
SELECT Channel.id
|
|
|
|
FROM Channel
|
|
|
|
JOIN Network ON Channel.network = Network.id
|
|
|
|
WHERE Network.user = ?
|
2020-10-24 13:14:23 +00:00
|
|
|
)`, id)
|
2020-08-03 16:43:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-24 13:14:23 +00:00
|
|
|
_, err = tx.Exec("DELETE FROM Network WHERE user = ?", id)
|
2020-08-03 16:43:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-24 13:14:23 +00:00
|
|
|
_, err = tx.Exec("DELETE FROM User WHERE id = ?", id)
|
2020-08-03 16:43:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2020-10-24 13:14:23 +00:00
|
|
|
func (db *DB) ListNetworks(userID int64) ([]Network, error) {
|
2020-03-04 19:15:38 +00:00
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
2020-03-04 17:22:58 +00:00
|
|
|
|
2020-03-18 02:13:26 +00:00
|
|
|
rows, err := db.db.Query(`SELECT id, name, addr, nick, username, realname, pass,
|
2020-05-29 11:10:54 +00:00
|
|
|
connect_commands, sasl_mechanism, sasl_plain_username, sasl_plain_password,
|
|
|
|
sasl_external_cert, sasl_external_key
|
2020-03-13 14:12:44 +00:00
|
|
|
FROM Network
|
|
|
|
WHERE user = ?`,
|
2020-10-24 13:14:23 +00:00
|
|
|
userID)
|
2020-03-04 17:22:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var networks []Network
|
|
|
|
for rows.Next() {
|
|
|
|
var net Network
|
2020-10-24 13:47:53 +00:00
|
|
|
var name, username, realname, pass, connectCommands sql.NullString
|
|
|
|
var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
|
2020-03-18 02:13:26 +00:00
|
|
|
err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname,
|
2020-05-29 11:10:54 +00:00
|
|
|
&pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword,
|
|
|
|
&net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob)
|
2020-03-13 14:12:44 +00:00
|
|
|
if err != nil {
|
2020-03-04 17:22:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-24 13:47:53 +00:00
|
|
|
net.Name = name.String
|
|
|
|
net.Username = username.String
|
|
|
|
net.Realname = realname.String
|
|
|
|
net.Pass = pass.String
|
|
|
|
if connectCommands.Valid {
|
|
|
|
net.ConnectCommands = strings.Split(connectCommands.String, "\r\n")
|
2020-04-15 23:40:50 +00:00
|
|
|
}
|
2020-10-24 13:47:53 +00:00
|
|
|
net.SASL.Mechanism = saslMechanism.String
|
|
|
|
net.SASL.Plain.Username = saslPlainUsername.String
|
|
|
|
net.SASL.Plain.Password = saslPlainPassword.String
|
2020-03-04 17:22:58 +00:00
|
|
|
networks = append(networks, net)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return networks, nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 13:14:23 +00:00
|
|
|
func (db *DB) StoreNetwork(userID int64, network *Network) error {
|
2020-03-12 18:17:06 +00:00
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
2020-10-24 13:47:53 +00:00
|
|
|
netName := toNullString(network.Name)
|
|
|
|
netUsername := toNullString(network.Username)
|
|
|
|
realname := toNullString(network.Realname)
|
|
|
|
pass := toNullString(network.Pass)
|
|
|
|
connectCommands := toNullString(strings.Join(network.ConnectCommands, "\r\n"))
|
2020-03-13 14:12:44 +00:00
|
|
|
|
2020-10-24 13:47:53 +00:00
|
|
|
var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
|
2020-03-13 14:12:44 +00:00
|
|
|
if network.SASL.Mechanism != "" {
|
2020-10-24 13:47:53 +00:00
|
|
|
saslMechanism = toNullString(network.SASL.Mechanism)
|
2020-03-13 14:12:44 +00:00
|
|
|
switch network.SASL.Mechanism {
|
|
|
|
case "PLAIN":
|
2020-10-24 13:47:53 +00:00
|
|
|
saslPlainUsername = toNullString(network.SASL.Plain.Username)
|
|
|
|
saslPlainPassword = toNullString(network.SASL.Plain.Password)
|
2020-05-29 11:10:54 +00:00
|
|
|
network.SASL.External.CertBlob = nil
|
|
|
|
network.SASL.External.PrivKeyBlob = nil
|
|
|
|
case "EXTERNAL":
|
|
|
|
// keep saslPlain* nil
|
2020-03-25 13:15:25 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("soju: cannot store network: unsupported SASL mechanism %q", network.SASL.Mechanism)
|
2020-03-13 14:12:44 +00:00
|
|
|
}
|
2020-03-12 18:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if network.ID != 0 {
|
2020-03-13 11:06:02 +00:00
|
|
|
_, err = db.db.Exec(`UPDATE Network
|
2020-04-15 23:40:50 +00:00
|
|
|
SET name = ?, addr = ?, nick = ?, username = ?, realname = ?, pass = ?, connect_commands = ?,
|
2020-05-29 11:10:54 +00:00
|
|
|
sasl_mechanism = ?, sasl_plain_username = ?, sasl_plain_password = ?,
|
|
|
|
sasl_external_cert = ?, sasl_external_key = ?
|
2020-03-13 11:06:02 +00:00
|
|
|
WHERE id = ?`,
|
2020-04-15 23:40:50 +00:00
|
|
|
netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands,
|
2020-05-29 11:10:54 +00:00
|
|
|
saslMechanism, saslPlainUsername, saslPlainPassword,
|
|
|
|
network.SASL.External.CertBlob, network.SASL.External.PrivKeyBlob,
|
|
|
|
network.ID)
|
2020-03-12 18:17:06 +00:00
|
|
|
} else {
|
|
|
|
var res sql.Result
|
2020-03-18 02:13:26 +00:00
|
|
|
res, err = db.db.Exec(`INSERT INTO Network(user, name, addr, nick, username,
|
2020-04-15 23:40:50 +00:00
|
|
|
realname, pass, connect_commands, sasl_mechanism, sasl_plain_username,
|
2020-05-29 11:10:54 +00:00
|
|
|
sasl_plain_password, sasl_external_cert, sasl_external_key)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
2020-10-24 13:14:23 +00:00
|
|
|
userID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands,
|
2020-05-29 11:10:54 +00:00
|
|
|
saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob,
|
|
|
|
network.SASL.External.PrivKeyBlob)
|
2020-03-12 18:17:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
network.ID, err = res.LastInsertId()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:40:20 +00:00
|
|
|
func (db *DB) DeleteNetwork(id int64) error {
|
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2020-08-03 16:43:15 +00:00
|
|
|
_, err = tx.Exec("DELETE FROM Channel WHERE network = ?", id)
|
2020-04-01 13:40:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-03 16:43:15 +00:00
|
|
|
_, err = tx.Exec("DELETE FROM Network WHERE id = ?", id)
|
2020-04-01 13:40:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:22:58 +00:00
|
|
|
func (db *DB) ListChannels(networkID int64) ([]Channel, error) {
|
2020-03-04 19:15:38 +00:00
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
2020-03-04 17:22:58 +00:00
|
|
|
|
2021-04-13 16:15:30 +00:00
|
|
|
rows, err := db.db.Query(`SELECT
|
|
|
|
id, name, key, detached, detached_internal_msgid,
|
|
|
|
relay_detached, reattach_on, detach_after, detach_on
|
2020-04-28 13:27:41 +00:00
|
|
|
FROM Channel
|
|
|
|
WHERE network = ?`, networkID)
|
2020-03-04 17:22:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var channels []Channel
|
|
|
|
for rows.Next() {
|
|
|
|
var ch Channel
|
2021-04-13 16:15:30 +00:00
|
|
|
var key, detachedInternalMsgID sql.NullString
|
2020-11-30 21:01:44 +00:00
|
|
|
var detachAfter int64
|
2021-04-13 16:15:30 +00:00
|
|
|
if err := rows.Scan(&ch.ID, &ch.Name, &key, &ch.Detached, &detachedInternalMsgID, &ch.RelayDetached, &ch.ReattachOn, &detachAfter, &ch.DetachOn); err != nil {
|
2020-03-04 17:22:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-24 13:47:53 +00:00
|
|
|
ch.Key = key.String
|
2021-04-13 16:15:30 +00:00
|
|
|
ch.DetachedInternalMsgID = detachedInternalMsgID.String
|
2020-11-30 21:01:44 +00:00
|
|
|
ch.DetachAfter = time.Duration(detachAfter) * time.Second
|
2020-03-04 17:22:58 +00:00
|
|
|
channels = append(channels, ch)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return channels, nil
|
|
|
|
}
|
2020-03-12 17:33:03 +00:00
|
|
|
|
|
|
|
func (db *DB) StoreChannel(networkID int64, ch *Channel) error {
|
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
2020-10-24 13:47:53 +00:00
|
|
|
key := toNullString(ch.Key)
|
2020-11-30 21:01:44 +00:00
|
|
|
detachAfter := int64(math.Ceil(ch.DetachAfter.Seconds()))
|
2020-03-25 13:23:41 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if ch.ID != 0 {
|
|
|
|
_, err = db.db.Exec(`UPDATE Channel
|
2021-04-13 16:15:30 +00:00
|
|
|
SET network = ?, name = ?, key = ?, detached = ?, detached_internal_msgid = ?, relay_detached = ?, reattach_on = ?, detach_after = ?, detach_on = ?
|
2020-04-28 13:27:41 +00:00
|
|
|
WHERE id = ?`,
|
2021-04-13 16:15:30 +00:00
|
|
|
networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID), ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn, ch.ID)
|
2020-03-25 13:23:41 +00:00
|
|
|
} else {
|
|
|
|
var res sql.Result
|
2021-04-13 16:15:30 +00:00
|
|
|
res, err = db.db.Exec(`INSERT INTO Channel(network, name, key, detached, detached_internal_msgid, relay_detached, reattach_on, detach_after, detach_on)
|
2020-11-30 21:01:44 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
2021-04-13 16:15:30 +00:00
|
|
|
networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID), ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn)
|
2020-03-25 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ch.ID, err = res.LastInsertId()
|
|
|
|
}
|
2020-03-12 17:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-06 15:00:42 +00:00
|
|
|
func (db *DB) DeleteChannel(id int64) error {
|
2020-03-12 17:33:03 +00:00
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
2020-09-06 15:00:42 +00:00
|
|
|
_, err := db.db.Exec("DELETE FROM Channel WHERE id = ?", id)
|
2020-03-12 17:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-10 17:16:08 +00:00
|
|
|
|
|
|
|
func (db *DB) ListDeliveryReceipts(networkID int64) ([]DeliveryReceipt, error) {
|
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
|
|
|
|
|
|
|
rows, err := db.db.Query(`SELECT id, target, client, internal_msgid
|
|
|
|
FROM DeliveryReceipt
|
|
|
|
WHERE network = ?`, networkID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var receipts []DeliveryReceipt
|
|
|
|
for rows.Next() {
|
|
|
|
var rcpt DeliveryReceipt
|
|
|
|
var client sql.NullString
|
|
|
|
if err := rows.Scan(&rcpt.ID, &rcpt.Target, &client, &rcpt.InternalMsgID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rcpt.Client = client.String
|
|
|
|
receipts = append(receipts, rcpt)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return receipts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) StoreClientDeliveryReceipts(networkID int64, client string, receipts []DeliveryReceipt) error {
|
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
_, err = tx.Exec("DELETE FROM DeliveryReceipt WHERE network = ? AND client = ?",
|
|
|
|
networkID, toNullString(client))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range receipts {
|
|
|
|
rcpt := &receipts[i]
|
|
|
|
|
|
|
|
res, err := tx.Exec("INSERT INTO DeliveryReceipt(network, target, client, internal_msgid) VALUES (?, ?, ?, ?)",
|
|
|
|
networkID, rcpt.Target, toNullString(client), rcpt.InternalMsgID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rcpt.ID, err = res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|