[98] | 1 | package soju
|
---|
[77] | 2 |
|
---|
| 3 | import (
|
---|
[148] | 4 | "fmt"
|
---|
[457] | 5 | "net/url"
|
---|
[263] | 6 | "strings"
|
---|
[434] | 7 | "time"
|
---|
[77] | 8 | )
|
---|
| 9 |
|
---|
[531] | 10 | type Database interface {
|
---|
| 11 | Close() error
|
---|
| 12 |
|
---|
| 13 | ListUsers() ([]User, error)
|
---|
| 14 | GetUser(username string) (*User, error)
|
---|
| 15 | StoreUser(user *User) error
|
---|
| 16 | DeleteUser(id int64) error
|
---|
| 17 |
|
---|
| 18 | ListNetworks(userID int64) ([]Network, error)
|
---|
| 19 | StoreNetwork(userID int64, network *Network) error
|
---|
| 20 | DeleteNetwork(id int64) error
|
---|
| 21 | ListChannels(networkID int64) ([]Channel, error)
|
---|
| 22 | StoreChannel(networKID int64, ch *Channel) error
|
---|
| 23 | DeleteChannel(id int64) error
|
---|
| 24 |
|
---|
| 25 | ListDeliveryReceipts(networkID int64) ([]DeliveryReceipt, error)
|
---|
| 26 | StoreClientDeliveryReceipts(networkID int64, client string, receipts []DeliveryReceipt) error
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[77] | 29 | type User struct {
|
---|
[382] | 30 | ID int64
|
---|
[77] | 31 | Username string
|
---|
[85] | 32 | Password string // hashed
|
---|
[327] | 33 | Admin bool
|
---|
[77] | 34 | }
|
---|
| 35 |
|
---|
[95] | 36 | type SASL struct {
|
---|
| 37 | Mechanism string
|
---|
| 38 |
|
---|
| 39 | Plain struct {
|
---|
| 40 | Username string
|
---|
| 41 | Password string
|
---|
| 42 | }
|
---|
[307] | 43 |
|
---|
| 44 | // TLS client certificate authentication.
|
---|
| 45 | External struct {
|
---|
| 46 | // X.509 certificate in DER form.
|
---|
| 47 | CertBlob []byte
|
---|
| 48 | // PKCS#8 private key in DER form.
|
---|
| 49 | PrivKeyBlob []byte
|
---|
| 50 | }
|
---|
[95] | 51 | }
|
---|
| 52 |
|
---|
[77] | 53 | type Network struct {
|
---|
[263] | 54 | ID int64
|
---|
| 55 | Name string
|
---|
| 56 | Addr string
|
---|
| 57 | Nick string
|
---|
| 58 | Username string
|
---|
| 59 | Realname string
|
---|
| 60 | Pass string
|
---|
| 61 | ConnectCommands []string
|
---|
| 62 | SASL SASL
|
---|
[542] | 63 | Enabled bool
|
---|
[77] | 64 | }
|
---|
| 65 |
|
---|
[149] | 66 | func (net *Network) GetName() string {
|
---|
| 67 | if net.Name != "" {
|
---|
| 68 | return net.Name
|
---|
| 69 | }
|
---|
| 70 | return net.Addr
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[457] | 73 | func (net *Network) URL() (*url.URL, error) {
|
---|
| 74 | s := net.Addr
|
---|
| 75 | if !strings.Contains(s, "://") {
|
---|
| 76 | // This is a raw domain name, make it an URL with the default scheme
|
---|
| 77 | s = "ircs://" + s
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | u, err := url.Parse(s)
|
---|
| 81 | if err != nil {
|
---|
| 82 | return nil, fmt.Errorf("failed to parse upstream server URL: %v", err)
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | return u, nil
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | func (net *Network) GetUsername() string {
|
---|
| 89 | if net.Username != "" {
|
---|
| 90 | return net.Username
|
---|
| 91 | }
|
---|
| 92 | return net.Nick
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | func (net *Network) GetRealname() string {
|
---|
| 96 | if net.Realname != "" {
|
---|
| 97 | return net.Realname
|
---|
| 98 | }
|
---|
| 99 | return net.Nick
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[434] | 102 | type MessageFilter int
|
---|
| 103 |
|
---|
| 104 | const (
|
---|
| 105 | // TODO: use customizable user defaults for FilterDefault
|
---|
| 106 | FilterDefault MessageFilter = iota
|
---|
| 107 | FilterNone
|
---|
| 108 | FilterHighlight
|
---|
| 109 | FilterMessage
|
---|
| 110 | )
|
---|
| 111 |
|
---|
| 112 | func parseFilter(filter string) (MessageFilter, error) {
|
---|
| 113 | switch filter {
|
---|
| 114 | case "default":
|
---|
| 115 | return FilterDefault, nil
|
---|
| 116 | case "none":
|
---|
| 117 | return FilterNone, nil
|
---|
| 118 | case "highlight":
|
---|
| 119 | return FilterHighlight, nil
|
---|
| 120 | case "message":
|
---|
| 121 | return FilterMessage, nil
|
---|
| 122 | }
|
---|
| 123 | return 0, fmt.Errorf("unknown filter: %q", filter)
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[77] | 126 | type Channel struct {
|
---|
[497] | 127 | ID int64
|
---|
| 128 | Name string
|
---|
| 129 | Key string
|
---|
[434] | 130 |
|
---|
[497] | 131 | Detached bool
|
---|
| 132 | DetachedInternalMsgID string
|
---|
| 133 |
|
---|
[434] | 134 | RelayDetached MessageFilter
|
---|
| 135 | ReattachOn MessageFilter
|
---|
| 136 | DetachAfter time.Duration
|
---|
| 137 | DetachOn MessageFilter
|
---|
[77] | 138 | }
|
---|
| 139 |
|
---|
[489] | 140 | type DeliveryReceipt struct {
|
---|
| 141 | ID int64
|
---|
| 142 | Target string // channel or nick
|
---|
| 143 | Client string
|
---|
| 144 | InternalMsgID string
|
---|
| 145 | }
|
---|