source: code/trunk/db.go@ 654

Last change on this file since 654 was 652, checked in by contact, 4 years ago

Add context args to Database interface

This is a mecanical change, which just lifts up the context.TODO()
calls from inside the DB implementations to the callers.

Future work involves properly wiring up the contexts when it makes
sense.

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