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-04-15 23:40:50 +00:00
|
|
|
"strings"
|
2020-03-04 17:22:58 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Username string
|
2020-03-11 18:09:32 +00:00
|
|
|
Password string // hashed
|
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-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
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:22:58 +00:00
|
|
|
type Channel struct {
|
|
|
|
ID int64
|
|
|
|
Name string
|
2020-03-25 10:52:24 +00:00
|
|
|
Key string
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 20:45:02 +00:00
|
|
|
const schema = `
|
|
|
|
CREATE TABLE User (
|
|
|
|
username VARCHAR(255) PRIMARY KEY,
|
|
|
|
password VARCHAR(255) NOT NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE Network (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
name VARCHAR(255),
|
|
|
|
user VARCHAR(255) NOT NULL,
|
|
|
|
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),
|
|
|
|
FOREIGN KEY(user) REFERENCES User(username),
|
|
|
|
UNIQUE(user, addr, nick)
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE Channel (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
|
|
|
name VARCHAR(255) NOT NULL,
|
|
|
|
key VARCHAR(255),
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, name)
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
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-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()
|
|
|
|
return db.Close()
|
|
|
|
}
|
|
|
|
|
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-03-13 14:12:44 +00:00
|
|
|
func fromStringPtr(ptr *string) string {
|
|
|
|
if ptr == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return *ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
func toStringPtr(s string) *string {
|
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
rows, err := db.db.Query("SELECT username, password FROM User")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var users []User
|
|
|
|
for rows.Next() {
|
|
|
|
var user User
|
|
|
|
var password *string
|
|
|
|
if err := rows.Scan(&user.Username, &password); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-13 14:12:44 +00:00
|
|
|
user.Password = fromStringPtr(password)
|
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()
|
|
|
|
|
|
|
|
user := &User{Username: username}
|
|
|
|
|
|
|
|
var password *string
|
|
|
|
row := db.db.QueryRow("SELECT password FROM User WHERE username = ?", username)
|
|
|
|
if err := row.Scan(&password); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
user.Password = fromStringPtr(password)
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 18:01:03 +00:00
|
|
|
func (db *DB) CreateUser(user *User) error {
|
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
2020-03-13 14:12:44 +00:00
|
|
|
password := toStringPtr(user.Password)
|
2020-03-12 17:33:03 +00:00
|
|
|
_, err := db.db.Exec("INSERT INTO User(username, password) VALUES (?, ?)", user.Username, password)
|
|
|
|
return err
|
2020-03-11 18:01:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:59:50 +00:00
|
|
|
func (db *DB) UpdatePassword(user *User) error {
|
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
|
|
|
password := toStringPtr(user.Password)
|
|
|
|
_, err := db.db.Exec(`UPDATE User
|
|
|
|
SET password = ?
|
|
|
|
WHERE username = ?`,
|
|
|
|
password, user.Username)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:22:58 +00:00
|
|
|
func (db *DB) ListNetworks(username string) ([]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-04-15 23:40:50 +00:00
|
|
|
connect_commands, sasl_mechanism, sasl_plain_username, sasl_plain_password
|
2020-03-13 14:12:44 +00:00
|
|
|
FROM Network
|
|
|
|
WHERE user = ?`,
|
|
|
|
username)
|
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-04-15 23:40:50 +00:00
|
|
|
var name, username, realname, pass, connectCommands *string
|
2020-03-13 14:12:44 +00:00
|
|
|
var saslMechanism, saslPlainUsername, saslPlainPassword *string
|
2020-03-18 02:13:26 +00:00
|
|
|
err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname,
|
2020-04-15 23:40:50 +00:00
|
|
|
&pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword)
|
2020-03-13 14:12:44 +00:00
|
|
|
if err != nil {
|
2020-03-04 17:22:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-18 02:13:26 +00:00
|
|
|
net.Name = fromStringPtr(name)
|
2020-03-13 14:12:44 +00:00
|
|
|
net.Username = fromStringPtr(username)
|
|
|
|
net.Realname = fromStringPtr(realname)
|
|
|
|
net.Pass = fromStringPtr(pass)
|
2020-04-15 23:40:50 +00:00
|
|
|
if connectCommands != nil {
|
|
|
|
net.ConnectCommands = strings.Split(*connectCommands, "\r\n")
|
|
|
|
}
|
2020-03-13 14:12:44 +00:00
|
|
|
net.SASL.Mechanism = fromStringPtr(saslMechanism)
|
|
|
|
net.SASL.Plain.Username = fromStringPtr(saslPlainUsername)
|
|
|
|
net.SASL.Plain.Password = fromStringPtr(saslPlainPassword)
|
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-03-12 18:17:06 +00:00
|
|
|
func (db *DB) StoreNetwork(username string, network *Network) error {
|
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
2020-03-18 02:13:26 +00:00
|
|
|
netName := toStringPtr(network.Name)
|
2020-03-13 14:12:44 +00:00
|
|
|
netUsername := toStringPtr(network.Username)
|
|
|
|
realname := toStringPtr(network.Realname)
|
|
|
|
pass := toStringPtr(network.Pass)
|
2020-04-15 23:40:50 +00:00
|
|
|
connectCommands := toStringPtr(strings.Join(network.ConnectCommands, "\r\n"))
|
2020-03-13 14:12:44 +00:00
|
|
|
|
|
|
|
var saslMechanism, saslPlainUsername, saslPlainPassword *string
|
|
|
|
if network.SASL.Mechanism != "" {
|
|
|
|
saslMechanism = &network.SASL.Mechanism
|
|
|
|
switch network.SASL.Mechanism {
|
|
|
|
case "PLAIN":
|
|
|
|
saslPlainUsername = toStringPtr(network.SASL.Plain.Username)
|
|
|
|
saslPlainPassword = toStringPtr(network.SASL.Plain.Password)
|
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-03-13 14:12:44 +00:00
|
|
|
sasl_mechanism = ?, sasl_plain_username = ?, sasl_plain_password = ?
|
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-03-13 14:12:44 +00:00
|
|
|
saslMechanism, saslPlainUsername, saslPlainPassword, 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-03-13 14:12:44 +00:00
|
|
|
sasl_plain_password)
|
2020-04-15 23:40:50 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
|
|
username, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands,
|
2020-03-13 14:12:44 +00:00
|
|
|
saslMechanism, saslPlainUsername, saslPlainPassword)
|
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()
|
|
|
|
|
|
|
|
_, err = tx.Exec("DELETE FROM Network WHERE id = ?", id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec("DELETE FROM Channel WHERE network = ?", id)
|
|
|
|
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
|
|
|
|
2020-03-25 10:52:24 +00:00
|
|
|
rows, err := db.db.Query("SELECT id, name, key 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
|
2020-03-25 10:52:24 +00:00
|
|
|
var key *string
|
|
|
|
if err := rows.Scan(&ch.ID, &ch.Name, &key); err != nil {
|
2020-03-04 17:22:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-25 10:52:24 +00:00
|
|
|
ch.Key = fromStringPtr(key)
|
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-03-25 10:52:24 +00:00
|
|
|
key := toStringPtr(ch.Key)
|
2020-03-25 13:23:41 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if ch.ID != 0 {
|
|
|
|
_, err = db.db.Exec(`UPDATE Channel
|
|
|
|
SET network = ?, name = ?, key = ?
|
|
|
|
WHERE id = ?`, networkID, ch.Name, key, ch.ID)
|
|
|
|
} else {
|
|
|
|
var res sql.Result
|
|
|
|
res, err = db.db.Exec(`INSERT INTO Channel(network, name, key)
|
|
|
|
VALUES (?, ?, ?)`, networkID, ch.Name, key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ch.ID, err = res.LastInsertId()
|
|
|
|
}
|
2020-03-12 17:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) DeleteChannel(networkID int64, name string) error {
|
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
|
|
|
_, err := db.db.Exec("DELETE FROM Channel WHERE network = ? AND name = ?", networkID, name)
|
|
|
|
return err
|
|
|
|
}
|