|
@@ -6,11 +6,13 @@ package irc
|
6
|
6
|
import (
|
7
|
7
|
"fmt"
|
8
|
8
|
"strconv"
|
|
9
|
+ "strings"
|
9
|
10
|
"sync"
|
10
|
11
|
"time"
|
11
|
12
|
|
12
|
13
|
"encoding/json"
|
13
|
14
|
|
|
15
|
+ "github.com/oragono/oragono/irc/modes"
|
14
|
16
|
"github.com/tidwall/buntdb"
|
15
|
17
|
)
|
16
|
18
|
|
|
@@ -18,16 +20,19 @@ import (
|
18
|
20
|
// channel creation/tracking/destruction is in channelmanager.go
|
19
|
21
|
|
20
|
22
|
const (
|
21
|
|
- keyChannelExists = "channel.exists %s"
|
22
|
|
- keyChannelName = "channel.name %s" // stores the 'preferred name' of the channel, not casemapped
|
23
|
|
- keyChannelRegTime = "channel.registered.time %s"
|
24
|
|
- keyChannelFounder = "channel.founder %s"
|
25
|
|
- keyChannelTopic = "channel.topic %s"
|
26
|
|
- keyChannelTopicSetBy = "channel.topic.setby %s"
|
27
|
|
- keyChannelTopicSetTime = "channel.topic.settime %s"
|
28
|
|
- keyChannelBanlist = "channel.banlist %s"
|
29
|
|
- keyChannelExceptlist = "channel.exceptlist %s"
|
30
|
|
- keyChannelInvitelist = "channel.invitelist %s"
|
|
23
|
+ keyChannelExists = "channel.exists %s"
|
|
24
|
+ keyChannelName = "channel.name %s" // stores the 'preferred name' of the channel, not casemapped
|
|
25
|
+ keyChannelRegTime = "channel.registered.time %s"
|
|
26
|
+ keyChannelFounder = "channel.founder %s"
|
|
27
|
+ keyChannelTopic = "channel.topic %s"
|
|
28
|
+ keyChannelTopicSetBy = "channel.topic.setby %s"
|
|
29
|
+ keyChannelTopicSetTime = "channel.topic.settime %s"
|
|
30
|
+ keyChannelBanlist = "channel.banlist %s"
|
|
31
|
+ keyChannelExceptlist = "channel.exceptlist %s"
|
|
32
|
+ keyChannelInvitelist = "channel.invitelist %s"
|
|
33
|
+ keyChannelPassword = "channel.key %s"
|
|
34
|
+ keyChannelModes = "channel.modes %s"
|
|
35
|
+ keyChannelAccountToUMode = "channel.accounttoumode %s"
|
31
|
36
|
)
|
32
|
37
|
|
33
|
38
|
var (
|
|
@@ -42,9 +47,26 @@ var (
|
42
|
47
|
keyChannelBanlist,
|
43
|
48
|
keyChannelExceptlist,
|
44
|
49
|
keyChannelInvitelist,
|
|
50
|
+ keyChannelPassword,
|
|
51
|
+ keyChannelModes,
|
|
52
|
+ keyChannelAccountToUMode,
|
45
|
53
|
}
|
46
|
54
|
)
|
47
|
55
|
|
|
56
|
+// these are bit flags indicating what part of the channel status is "dirty"
|
|
57
|
+// and needs to be read from memory and written to the db
|
|
58
|
+const (
|
|
59
|
+ IncludeInitial uint = 1 << iota
|
|
60
|
+ IncludeTopic
|
|
61
|
+ IncludeModes
|
|
62
|
+ IncludeLists
|
|
63
|
+)
|
|
64
|
+
|
|
65
|
+// this is an OR of all possible flags
|
|
66
|
+const (
|
|
67
|
+ IncludeAllChannelAttrs = ^uint(0)
|
|
68
|
+)
|
|
69
|
+
|
48
|
70
|
// RegisteredChannel holds details about a given registered channel.
|
49
|
71
|
type RegisteredChannel struct {
|
50
|
72
|
// Name of the channel.
|
|
@@ -59,6 +81,12 @@ type RegisteredChannel struct {
|
59
|
81
|
TopicSetBy string
|
60
|
82
|
// TopicSetTime represents the time the topic was set.
|
61
|
83
|
TopicSetTime time.Time
|
|
84
|
+ // Modes represents the channel modes
|
|
85
|
+ Modes []modes.Mode
|
|
86
|
+ // Key represents the channel key / password
|
|
87
|
+ Key string
|
|
88
|
+ // AccountToUMode maps user accounts to their persistent channel modes (e.g., +q, +h)
|
|
89
|
+ AccountToUMode map[string]modes.Mode
|
62
|
90
|
// Banlist represents the bans set on the channel.
|
63
|
91
|
Banlist []string
|
64
|
92
|
// Exceptlist represents the exceptions set on the channel.
|
|
@@ -87,7 +115,7 @@ func NewChannelRegistry(server *Server) *ChannelRegistry {
|
87
|
115
|
}
|
88
|
116
|
|
89
|
117
|
// StoreChannel obtains a consistent view of a channel, then persists it to the store.
|
90
|
|
-func (reg *ChannelRegistry) StoreChannel(channel *Channel, includeLists bool) {
|
|
118
|
+func (reg *ChannelRegistry) StoreChannel(channel *Channel, includeFlags uint) {
|
91
|
119
|
if !reg.server.ChannelRegistrationEnabled() {
|
92
|
120
|
return
|
93
|
121
|
}
|
|
@@ -96,14 +124,14 @@ func (reg *ChannelRegistry) StoreChannel(channel *Channel, includeLists bool) {
|
96
|
124
|
defer reg.Unlock()
|
97
|
125
|
|
98
|
126
|
key := channel.NameCasefolded()
|
99
|
|
- info := channel.ExportRegistration(includeLists)
|
|
127
|
+ info := channel.ExportRegistration(includeFlags)
|
100
|
128
|
if info.Founder == "" {
|
101
|
129
|
// sanity check, don't try to store an unregistered channel
|
102
|
130
|
return
|
103
|
131
|
}
|
104
|
132
|
|
105
|
133
|
reg.server.store.Update(func(tx *buntdb.Tx) error {
|
106
|
|
- reg.saveChannel(tx, key, info, includeLists)
|
|
134
|
+ reg.saveChannel(tx, key, info, includeFlags)
|
107
|
135
|
return nil
|
108
|
136
|
})
|
109
|
137
|
}
|
|
@@ -132,9 +160,17 @@ func (reg *ChannelRegistry) LoadChannel(nameCasefolded string) (info *Registered
|
132
|
160
|
topicSetBy, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetBy, channelKey))
|
133
|
161
|
topicSetTime, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetTime, channelKey))
|
134
|
162
|
topicSetTimeInt, _ := strconv.ParseInt(topicSetTime, 10, 64)
|
|
163
|
+ password, _ := tx.Get(fmt.Sprintf(keyChannelPassword, channelKey))
|
|
164
|
+ modeString, _ := tx.Get(fmt.Sprintf(keyChannelModes, channelKey))
|
135
|
165
|
banlistString, _ := tx.Get(fmt.Sprintf(keyChannelBanlist, channelKey))
|
136
|
166
|
exceptlistString, _ := tx.Get(fmt.Sprintf(keyChannelExceptlist, channelKey))
|
137
|
167
|
invitelistString, _ := tx.Get(fmt.Sprintf(keyChannelInvitelist, channelKey))
|
|
168
|
+ accountToUModeString, _ := tx.Get(fmt.Sprintf(keyChannelAccountToUMode, channelKey))
|
|
169
|
+
|
|
170
|
+ modeSlice := make([]modes.Mode, len(modeString))
|
|
171
|
+ for i, mode := range modeString {
|
|
172
|
+ modeSlice[i] = modes.Mode(mode)
|
|
173
|
+ }
|
138
|
174
|
|
139
|
175
|
var banlist []string
|
140
|
176
|
_ = json.Unmarshal([]byte(banlistString), &banlist)
|
|
@@ -142,17 +178,22 @@ func (reg *ChannelRegistry) LoadChannel(nameCasefolded string) (info *Registered
|
142
|
178
|
_ = json.Unmarshal([]byte(exceptlistString), &exceptlist)
|
143
|
179
|
var invitelist []string
|
144
|
180
|
_ = json.Unmarshal([]byte(invitelistString), &invitelist)
|
|
181
|
+ accountToUMode := make(map[string]modes.Mode)
|
|
182
|
+ _ = json.Unmarshal([]byte(accountToUModeString), &accountToUMode)
|
145
|
183
|
|
146
|
184
|
info = &RegisteredChannel{
|
147
|
|
- Name: name,
|
148
|
|
- RegisteredAt: time.Unix(regTimeInt, 0),
|
149
|
|
- Founder: founder,
|
150
|
|
- Topic: topic,
|
151
|
|
- TopicSetBy: topicSetBy,
|
152
|
|
- TopicSetTime: time.Unix(topicSetTimeInt, 0),
|
153
|
|
- Banlist: banlist,
|
154
|
|
- Exceptlist: exceptlist,
|
155
|
|
- Invitelist: invitelist,
|
|
185
|
+ Name: name,
|
|
186
|
+ RegisteredAt: time.Unix(regTimeInt, 0),
|
|
187
|
+ Founder: founder,
|
|
188
|
+ Topic: topic,
|
|
189
|
+ TopicSetBy: topicSetBy,
|
|
190
|
+ TopicSetTime: time.Unix(topicSetTimeInt, 0),
|
|
191
|
+ Key: password,
|
|
192
|
+ Modes: modeSlice,
|
|
193
|
+ Banlist: banlist,
|
|
194
|
+ Exceptlist: exceptlist,
|
|
195
|
+ Invitelist: invitelist,
|
|
196
|
+ AccountToUMode: accountToUMode,
|
156
|
197
|
}
|
157
|
198
|
return nil
|
158
|
199
|
})
|
|
@@ -170,17 +211,17 @@ func (reg *ChannelRegistry) Rename(channel *Channel, casefoldedOldName string) {
|
170
|
211
|
reg.Lock()
|
171
|
212
|
defer reg.Unlock()
|
172
|
213
|
|
173
|
|
- includeLists := true
|
|
214
|
+ includeFlags := IncludeAllChannelAttrs
|
174
|
215
|
oldKey := casefoldedOldName
|
175
|
216
|
key := channel.NameCasefolded()
|
176
|
|
- info := channel.ExportRegistration(includeLists)
|
|
217
|
+ info := channel.ExportRegistration(includeFlags)
|
177
|
218
|
if info.Founder == "" {
|
178
|
219
|
return
|
179
|
220
|
}
|
180
|
221
|
|
181
|
222
|
reg.server.store.Update(func(tx *buntdb.Tx) error {
|
182
|
223
|
reg.deleteChannel(tx, oldKey, info)
|
183
|
|
- reg.saveChannel(tx, key, info, includeLists)
|
|
224
|
+ reg.saveChannel(tx, key, info, includeFlags)
|
184
|
225
|
return nil
|
185
|
226
|
})
|
186
|
227
|
}
|
|
@@ -204,21 +245,37 @@ func (reg *ChannelRegistry) deleteChannel(tx *buntdb.Tx, key string, info Regist
|
204
|
245
|
}
|
205
|
246
|
|
206
|
247
|
// saveChannel saves a channel to the store.
|
207
|
|
-func (reg *ChannelRegistry) saveChannel(tx *buntdb.Tx, channelKey string, channelInfo RegisteredChannel, includeLists bool) {
|
208
|
|
- tx.Set(fmt.Sprintf(keyChannelExists, channelKey), "1", nil)
|
209
|
|
- tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
|
210
|
|
- tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
|
211
|
|
- tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
|
212
|
|
- tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
|
213
|
|
- tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
|
214
|
|
- tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
|
215
|
|
-
|
216
|
|
- if includeLists {
|
|
248
|
+func (reg *ChannelRegistry) saveChannel(tx *buntdb.Tx, channelKey string, channelInfo RegisteredChannel, includeFlags uint) {
|
|
249
|
+ if includeFlags&IncludeInitial != 0 {
|
|
250
|
+ tx.Set(fmt.Sprintf(keyChannelExists, channelKey), "1", nil)
|
|
251
|
+ tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
|
|
252
|
+ tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
|
|
253
|
+ tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ if includeFlags&IncludeTopic != 0 {
|
|
257
|
+ tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
|
|
258
|
+ tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
|
|
259
|
+ tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
|
|
260
|
+ }
|
|
261
|
+
|
|
262
|
+ if includeFlags&IncludeModes != 0 {
|
|
263
|
+ tx.Set(fmt.Sprintf(keyChannelPassword, channelKey), channelInfo.Key, nil)
|
|
264
|
+ modeStrings := make([]string, len(channelInfo.Modes))
|
|
265
|
+ for i, mode := range channelInfo.Modes {
|
|
266
|
+ modeStrings[i] = string(mode)
|
|
267
|
+ }
|
|
268
|
+ tx.Set(fmt.Sprintf(keyChannelModes, channelKey), strings.Join(modeStrings, ""), nil)
|
|
269
|
+ }
|
|
270
|
+
|
|
271
|
+ if includeFlags&IncludeLists != 0 {
|
217
|
272
|
banlistString, _ := json.Marshal(channelInfo.Banlist)
|
218
|
273
|
tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
|
219
|
274
|
exceptlistString, _ := json.Marshal(channelInfo.Exceptlist)
|
220
|
275
|
tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
|
221
|
276
|
invitelistString, _ := json.Marshal(channelInfo.Invitelist)
|
222
|
277
|
tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
|
|
278
|
+ accountToUModeString, _ := json.Marshal(channelInfo.AccountToUMode)
|
|
279
|
+ tx.Set(fmt.Sprintf(keyChannelAccountToUMode, channelKey), string(accountToUModeString), nil)
|
223
|
280
|
}
|
224
|
281
|
}
|