2020-03-13 17:13:03 +00:00
|
|
|
package soju
|
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"
|
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-05-24 19:13:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 17:15:56 +00:00
|
|
|
func OpenDB(driver, source string) (Database, error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if net.Nick != "" {
|
|
|
|
return net.Nick
|
|
|
|
}
|
|
|
|
return user.Username
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:33:59 +00:00
|
|
|
func GetUsername(user *User, net *Network) string {
|
|
|
|
if net.Username != "" {
|
|
|
|
return net.Username
|
|
|
|
}
|
|
|
|
return GetNick(user, net)
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:13 +00:00
|
|
|
func GetRealname(user *User, net *Network) string {
|
2021-03-09 17:54:38 +00:00
|
|
|
if net.Realname != "" {
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|