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"
|
2022-09-11 11:57:00 +00:00
|
|
|
"database/sql"
|
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"
|
2022-12-10 23:01:16 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2020-03-04 17:22:58 +00:00
|
|
|
)
|
|
|
|
|
2022-12-10 23:01:16 +00:00
|
|
|
type MessageTarget struct {
|
|
|
|
Name string
|
|
|
|
LatestMessage time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessageOptions struct {
|
|
|
|
AfterID int64
|
|
|
|
AfterTime time.Time
|
|
|
|
BeforeTime time.Time
|
|
|
|
Limit int
|
|
|
|
Events bool
|
|
|
|
Sender string
|
|
|
|
Text string
|
|
|
|
TakeLast bool
|
|
|
|
}
|
|
|
|
|
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
|
2023-01-26 15:57:07 +00:00
|
|
|
ListInactiveUsernames(ctx context.Context, limit time.Time) ([]string, error)
|
2021-10-18 17:15:15 +00:00
|
|
|
|
|
|
|
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
|
2022-12-10 23:01:16 +00:00
|
|
|
|
|
|
|
GetMessageLastID(ctx context.Context, networkID int64, name string) (int64, error)
|
2023-07-10 00:28:00 +00:00
|
|
|
StoreMessages(ctx context.Context, networkID int64, name string, msgs []*irc.Message) ([]int64, error)
|
2022-12-10 23:01:16 +00:00
|
|
|
ListMessageLastPerTarget(ctx context.Context, networkID int64, options *MessageOptions) ([]MessageTarget, error)
|
|
|
|
ListMessages(ctx context.Context, networkID int64, name string, options *MessageOptions) ([]*irc.Message, 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 {
|
2023-01-26 13:02:11 +00:00
|
|
|
ID int64
|
|
|
|
Username string
|
|
|
|
Password string // hashed
|
|
|
|
Nick string
|
|
|
|
Realname string
|
|
|
|
Admin bool
|
|
|
|
Enabled bool
|
|
|
|
DownstreamInteractedAt time.Time
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 22:57:35 +00:00
|
|
|
func NewUser(username string) *User {
|
|
|
|
return &User{
|
|
|
|
Username: username,
|
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 23:56:47 +00:00
|
|
|
func (u *User) CheckPassword(password string) (upgraded bool, err error) {
|
2022-06-08 11:27:33 +00:00
|
|
|
// Password auth disabled
|
|
|
|
if u.Password == "" {
|
2022-07-04 23:56:47 +00:00
|
|
|
return false, fmt.Errorf("password auth disabled")
|
2022-06-08 11:27:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 23:56:47 +00:00
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
2022-06-08 11:27:33 +00:00
|
|
|
if err != nil {
|
2022-07-04 23:56:47 +00:00
|
|
|
return false, fmt.Errorf("wrong password: %v", err)
|
2022-06-08 11:27:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 23:56:47 +00:00
|
|
|
passCost, err := bcrypt.Cost([]byte(u.Password))
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("invalid password cost: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if passCost < bcrypt.DefaultCost {
|
|
|
|
return true, u.SetPassword(password)
|
|
|
|
}
|
|
|
|
return false, nil
|
2022-06-08 11:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-12-10 08:12:46 +00:00
|
|
|
CertFP string
|
2020-04-15 23:40:50 +00:00
|
|
|
SASL SASL
|
2022-09-26 17:49:26 +00:00
|
|
|
AutoAway bool
|
2021-05-26 08:49:52 +00:00
|
|
|
Enabled bool
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 22:51:04 +00:00
|
|
|
func NewNetwork(addr string) *Network {
|
|
|
|
return &Network{
|
2023-10-31 22:51:32 +00:00
|
|
|
Addr: addr,
|
|
|
|
AutoAway: true,
|
|
|
|
Enabled: true,
|
2023-10-31 22:51:04 +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
|
|
|
|
}
|
2022-07-08 16:01:05 +00:00
|
|
|
if user.Nick != "" {
|
|
|
|
return user.Nick
|
|
|
|
}
|
2021-11-02 22:33:17 +00:00
|
|
|
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 {
|
2023-02-18 12:23:02 +00:00
|
|
|
ID int64
|
|
|
|
Endpoint string
|
|
|
|
CreatedAt, UpdatedAt time.Time // read-only
|
|
|
|
|
|
|
|
Keys struct {
|
2021-11-27 10:48:10 +00:00
|
|
|
Auth string
|
|
|
|
P256DH string
|
|
|
|
VAPID string
|
|
|
|
}
|
|
|
|
}
|
2022-09-11 11:57:00 +00:00
|
|
|
|
|
|
|
func toNullString(s string) sql.NullString {
|
|
|
|
return sql.NullString{
|
|
|
|
String: s,
|
|
|
|
Valid: s != "",
|
|
|
|
}
|
|
|
|
}
|
2023-02-13 18:22:15 +00:00
|
|
|
|
|
|
|
func toNullTime(t time.Time) sql.NullTime {
|
|
|
|
return sql.NullTime{
|
|
|
|
Time: t,
|
|
|
|
Valid: !t.IsZero(),
|
|
|
|
}
|
|
|
|
}
|