2022-05-09 10:34:43 +00:00
|
|
|
package database
|
2020-03-04 17:22:58 +00:00
|
|
|
|
|
|
|
import (
|
2021-10-18 17:15:15 +00:00
|
|
|
"context"
|
2020-03-25 13:15:25 +00:00
|
|
|
"fmt"
|
2021-03-09 17:54:38 +00:00
|
|
|
"net/url"
|
2020-04-15 23:40:50 +00:00
|
|
|
"strings"
|
2020-11-30 21:01:44 +00:00
|
|
|
"time"
|
2021-11-17 14:40:02 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-06-08 11:27:33 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2020-03-04 17:22:58 +00:00
|
|
|
)
|
|
|
|
|
2021-05-24 19:13:31 +00:00
|
|
|
type Database interface {
|
|
|
|
Close() error
|
2021-10-18 17:15:15 +00:00
|
|
|
Stats(ctx context.Context) (*DatabaseStats, error)
|
|
|
|
|
|
|
|
ListUsers(ctx context.Context) ([]User, error)
|
|
|
|
GetUser(ctx context.Context, username string) (*User, error)
|
|
|
|
StoreUser(ctx context.Context, user *User) error
|
|
|
|
DeleteUser(ctx context.Context, id int64) error
|
|
|
|
|
|
|
|
ListNetworks(ctx context.Context, userID int64) ([]Network, error)
|
|
|
|
StoreNetwork(ctx context.Context, userID int64, network *Network) error
|
|
|
|
DeleteNetwork(ctx context.Context, id int64) error
|
|
|
|
ListChannels(ctx context.Context, networkID int64) ([]Channel, error)
|
|
|
|
StoreChannel(ctx context.Context, networKID int64, ch *Channel) error
|
|
|
|
DeleteChannel(ctx context.Context, id int64) error
|
|
|
|
|
|
|
|
ListDeliveryReceipts(ctx context.Context, networkID int64) ([]DeliveryReceipt, error)
|
|
|
|
StoreClientDeliveryReceipts(ctx context.Context, networkID int64, client string, receipts []DeliveryReceipt) error
|
2021-01-29 15:57:38 +00:00
|
|
|
|
|
|
|
GetReadReceipt(ctx context.Context, networkID int64, name string) (*ReadReceipt, error)
|
|
|
|
StoreReadReceipt(ctx context.Context, networkID int64, receipt *ReadReceipt) error
|
2021-11-27 10:48:10 +00:00
|
|
|
|
|
|
|
ListWebPushConfigs(ctx context.Context) ([]WebPushConfig, error)
|
|
|
|
StoreWebPushConfig(ctx context.Context, config *WebPushConfig) error
|
|
|
|
|
2022-06-16 17:33:39 +00:00
|
|
|
ListWebPushSubscriptions(ctx context.Context, userID, networkID int64) ([]WebPushSubscription, error)
|
|
|
|
StoreWebPushSubscription(ctx context.Context, userID, networkID int64, sub *WebPushSubscription) error
|
2021-11-27 10:48:10 +00:00
|
|
|
DeleteWebPushSubscription(ctx context.Context, id int64) error
|
2021-05-24 19:13:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 14:40:02 +00:00
|
|
|
type MetricsCollectorDatabase interface {
|
|
|
|
Database
|
2022-03-08 09:36:59 +00:00
|
|
|
RegisterMetrics(r prometheus.Registerer) error
|
2021-11-17 14:40:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 10:34:43 +00:00
|
|
|
func Open(driver, source string) (Database, error) {
|
2021-10-08 17:15:56 +00:00
|
|
|
switch driver {
|
|
|
|
case "sqlite3":
|
|
|
|
return OpenSqliteDB(source)
|
|
|
|
case "postgres":
|
|
|
|
return OpenPostgresDB(source)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported database driver: %q", driver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 17:31:06 +00:00
|
|
|
type DatabaseStats struct {
|
|
|
|
Users int64
|
|
|
|
Networks int64
|
|
|
|
Channels int64
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:22:58 +00:00
|
|
|
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
|
2021-06-25 18:33:13 +00:00
|
|
|
Realname string
|
2020-06-06 23:22:54 +00:00
|
|
|
Admin bool
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 11:27:33 +00:00
|
|
|
func (u *User) CheckPassword(password string) error {
|
|
|
|
// Password auth disabled
|
|
|
|
if u.Password == "" {
|
|
|
|
return fmt.Errorf("password auth disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("wrong password: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) SetPassword(password string) error {
|
|
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to hash password: %v", err)
|
|
|
|
}
|
|
|
|
u.Password = string(hashed)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
2021-05-26 08:49:52 +00:00
|
|
|
Enabled bool
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-02 22:33:17 +00:00
|
|
|
func GetNick(user *User, net *Network) string {
|
2022-03-30 12:15:12 +00:00
|
|
|
if net != nil && net.Nick != "" {
|
2021-11-02 22:33:17 +00:00
|
|
|
return net.Nick
|
|
|
|
}
|
|
|
|
return user.Username
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:33:59 +00:00
|
|
|
func GetUsername(user *User, net *Network) string {
|
2022-03-30 12:15:12 +00:00
|
|
|
if net != nil && net.Username != "" {
|
2021-11-07 17:33:59 +00:00
|
|
|
return net.Username
|
|
|
|
}
|
|
|
|
return GetNick(user, net)
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:13 +00:00
|
|
|
func GetRealname(user *User, net *Network) string {
|
2022-03-30 12:15:12 +00:00
|
|
|
if net != nil && net.Realname != "" {
|
2021-03-09 17:54:38 +00:00
|
|
|
return net.Realname
|
|
|
|
}
|
2021-06-25 18:33:13 +00:00
|
|
|
if user.Realname != "" {
|
|
|
|
return user.Realname
|
|
|
|
}
|
2021-11-02 22:33:17 +00:00
|
|
|
return GetNick(user, net)
|
2021-03-09 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
2021-01-29 15:57:38 +00:00
|
|
|
|
|
|
|
type ReadReceipt struct {
|
|
|
|
ID int64
|
|
|
|
Target string // channel or nick
|
|
|
|
Timestamp time.Time
|
|
|
|
}
|
2021-11-27 10:48:10 +00:00
|
|
|
|
|
|
|
type WebPushConfig struct {
|
|
|
|
ID int64
|
|
|
|
VAPIDKeys struct {
|
|
|
|
Public, Private string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type WebPushSubscription struct {
|
|
|
|
ID int64
|
|
|
|
Endpoint string
|
|
|
|
Keys struct {
|
|
|
|
Auth string
|
|
|
|
P256DH string
|
|
|
|
VAPID string
|
|
|
|
}
|
|
|
|
}
|