You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

channelreg.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "encoding/json"
  10. "github.com/oragono/oragono/irc/modes"
  11. "github.com/tidwall/buntdb"
  12. )
  13. // this is exclusively the *persistence* layer for channel registration;
  14. // channel creation/tracking/destruction is in channelmanager.go
  15. const (
  16. keyChannelExists = "channel.exists %s"
  17. keyChannelName = "channel.name %s" // stores the 'preferred name' of the channel, not casemapped
  18. keyChannelRegTime = "channel.registered.time %s"
  19. keyChannelFounder = "channel.founder %s"
  20. keyChannelTopic = "channel.topic %s"
  21. keyChannelTopicSetBy = "channel.topic.setby %s"
  22. keyChannelTopicSetTime = "channel.topic.settime %s"
  23. keyChannelBanlist = "channel.banlist %s"
  24. keyChannelExceptlist = "channel.exceptlist %s"
  25. keyChannelInvitelist = "channel.invitelist %s"
  26. keyChannelPassword = "channel.key %s"
  27. keyChannelModes = "channel.modes %s"
  28. keyChannelAccountToUMode = "channel.accounttoumode %s"
  29. keyChannelUserLimit = "channel.userlimit %s"
  30. keyChannelPurged = "channel.purged %s"
  31. )
  32. var (
  33. channelKeyStrings = []string{
  34. keyChannelExists,
  35. keyChannelName,
  36. keyChannelRegTime,
  37. keyChannelFounder,
  38. keyChannelTopic,
  39. keyChannelTopicSetBy,
  40. keyChannelTopicSetTime,
  41. keyChannelBanlist,
  42. keyChannelExceptlist,
  43. keyChannelInvitelist,
  44. keyChannelPassword,
  45. keyChannelModes,
  46. keyChannelAccountToUMode,
  47. keyChannelUserLimit,
  48. }
  49. )
  50. // these are bit flags indicating what part of the channel status is "dirty"
  51. // and needs to be read from memory and written to the db
  52. const (
  53. IncludeInitial uint = 1 << iota
  54. IncludeTopic
  55. IncludeModes
  56. IncludeLists
  57. )
  58. // this is an OR of all possible flags
  59. const (
  60. IncludeAllChannelAttrs = ^uint(0)
  61. )
  62. // RegisteredChannel holds details about a given registered channel.
  63. type RegisteredChannel struct {
  64. // Name of the channel.
  65. Name string
  66. // Casefolded name of the channel.
  67. NameCasefolded string
  68. // RegisteredAt represents the time that the channel was registered.
  69. RegisteredAt time.Time
  70. // Founder indicates the founder of the channel.
  71. Founder string
  72. // Topic represents the channel topic.
  73. Topic string
  74. // TopicSetBy represents the host that set the topic.
  75. TopicSetBy string
  76. // TopicSetTime represents the time the topic was set.
  77. TopicSetTime time.Time
  78. // Modes represents the channel modes
  79. Modes []modes.Mode
  80. // Key represents the channel key / password
  81. Key string
  82. // UserLimit is the user limit (0 for no limit)
  83. UserLimit int
  84. // AccountToUMode maps user accounts to their persistent channel modes (e.g., +q, +h)
  85. AccountToUMode map[string]modes.Mode
  86. // Bans represents the bans set on the channel.
  87. Bans map[string]MaskInfo
  88. // Excepts represents the exceptions set on the channel.
  89. Excepts map[string]MaskInfo
  90. // Invites represents the invite exceptions set on the channel.
  91. Invites map[string]MaskInfo
  92. }
  93. type ChannelPurgeRecord struct {
  94. Oper string
  95. PurgedAt time.Time
  96. Reason string
  97. }
  98. // ChannelRegistry manages registered channels.
  99. type ChannelRegistry struct {
  100. server *Server
  101. }
  102. // NewChannelRegistry returns a new ChannelRegistry.
  103. func (reg *ChannelRegistry) Initialize(server *Server) {
  104. reg.server = server
  105. }
  106. // AllChannels returns the uncasefolded names of all registered channels.
  107. func (reg *ChannelRegistry) AllChannels() (result []string) {
  108. prefix := fmt.Sprintf(keyChannelName, "")
  109. reg.server.store.View(func(tx *buntdb.Tx) error {
  110. return tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
  111. if !strings.HasPrefix(key, prefix) {
  112. return false
  113. }
  114. result = append(result, value)
  115. return true
  116. })
  117. })
  118. return
  119. }
  120. // PurgedChannels returns the set of all casefolded channel names that have been purged
  121. func (reg *ChannelRegistry) PurgedChannels() (result map[string]empty) {
  122. result = make(map[string]empty)
  123. prefix := fmt.Sprintf(keyChannelPurged, "")
  124. reg.server.store.View(func(tx *buntdb.Tx) error {
  125. return tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
  126. if !strings.HasPrefix(key, prefix) {
  127. return false
  128. }
  129. channel := strings.TrimPrefix(key, prefix)
  130. result[channel] = empty{}
  131. return true
  132. })
  133. })
  134. return
  135. }
  136. // StoreChannel obtains a consistent view of a channel, then persists it to the store.
  137. func (reg *ChannelRegistry) StoreChannel(info RegisteredChannel, includeFlags uint) (err error) {
  138. if !reg.server.ChannelRegistrationEnabled() {
  139. return
  140. }
  141. if info.Founder == "" {
  142. // sanity check, don't try to store an unregistered channel
  143. return
  144. }
  145. reg.server.store.Update(func(tx *buntdb.Tx) error {
  146. reg.saveChannel(tx, info, includeFlags)
  147. return nil
  148. })
  149. return nil
  150. }
  151. // LoadChannel loads a channel from the store.
  152. func (reg *ChannelRegistry) LoadChannel(nameCasefolded string) (info RegisteredChannel, err error) {
  153. if !reg.server.ChannelRegistrationEnabled() {
  154. err = errFeatureDisabled
  155. return
  156. }
  157. channelKey := nameCasefolded
  158. // nice to have: do all JSON (de)serialization outside of the buntdb transaction
  159. err = reg.server.store.View(func(tx *buntdb.Tx) error {
  160. _, dberr := tx.Get(fmt.Sprintf(keyChannelExists, channelKey))
  161. if dberr == buntdb.ErrNotFound {
  162. // chan does not already exist, return
  163. return errNoSuchChannel
  164. }
  165. // channel exists, load it
  166. name, _ := tx.Get(fmt.Sprintf(keyChannelName, channelKey))
  167. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, channelKey))
  168. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  169. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, channelKey))
  170. topic, _ := tx.Get(fmt.Sprintf(keyChannelTopic, channelKey))
  171. topicSetBy, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetBy, channelKey))
  172. topicSetTime, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetTime, channelKey))
  173. topicSetTimeInt, _ := strconv.ParseInt(topicSetTime, 10, 64)
  174. password, _ := tx.Get(fmt.Sprintf(keyChannelPassword, channelKey))
  175. modeString, _ := tx.Get(fmt.Sprintf(keyChannelModes, channelKey))
  176. userLimitString, _ := tx.Get(fmt.Sprintf(keyChannelUserLimit, channelKey))
  177. banlistString, _ := tx.Get(fmt.Sprintf(keyChannelBanlist, channelKey))
  178. exceptlistString, _ := tx.Get(fmt.Sprintf(keyChannelExceptlist, channelKey))
  179. invitelistString, _ := tx.Get(fmt.Sprintf(keyChannelInvitelist, channelKey))
  180. accountToUModeString, _ := tx.Get(fmt.Sprintf(keyChannelAccountToUMode, channelKey))
  181. modeSlice := make([]modes.Mode, len(modeString))
  182. for i, mode := range modeString {
  183. modeSlice[i] = modes.Mode(mode)
  184. }
  185. userLimit, _ := strconv.Atoi(userLimitString)
  186. var banlist map[string]MaskInfo
  187. _ = json.Unmarshal([]byte(banlistString), &banlist)
  188. var exceptlist map[string]MaskInfo
  189. _ = json.Unmarshal([]byte(exceptlistString), &exceptlist)
  190. var invitelist map[string]MaskInfo
  191. _ = json.Unmarshal([]byte(invitelistString), &invitelist)
  192. accountToUMode := make(map[string]modes.Mode)
  193. _ = json.Unmarshal([]byte(accountToUModeString), &accountToUMode)
  194. info = RegisteredChannel{
  195. Name: name,
  196. RegisteredAt: time.Unix(regTimeInt, 0).UTC(),
  197. Founder: founder,
  198. Topic: topic,
  199. TopicSetBy: topicSetBy,
  200. TopicSetTime: time.Unix(topicSetTimeInt, 0).UTC(),
  201. Key: password,
  202. Modes: modeSlice,
  203. Bans: banlist,
  204. Excepts: exceptlist,
  205. Invites: invitelist,
  206. AccountToUMode: accountToUMode,
  207. UserLimit: int(userLimit),
  208. }
  209. return nil
  210. })
  211. return
  212. }
  213. // Delete deletes a channel corresponding to `info`. If no such channel
  214. // is present in the database, no error is returned.
  215. func (reg *ChannelRegistry) Delete(info RegisteredChannel) (err error) {
  216. if !reg.server.ChannelRegistrationEnabled() {
  217. return
  218. }
  219. reg.server.store.Update(func(tx *buntdb.Tx) error {
  220. reg.deleteChannel(tx, info.NameCasefolded, info)
  221. return nil
  222. })
  223. return nil
  224. }
  225. // delete a channel, unless it was overwritten by another registration of the same channel
  226. func (reg *ChannelRegistry) deleteChannel(tx *buntdb.Tx, key string, info RegisteredChannel) {
  227. _, err := tx.Get(fmt.Sprintf(keyChannelExists, key))
  228. if err == nil {
  229. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, key))
  230. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  231. registeredAt := time.Unix(regTimeInt, 0)
  232. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, key))
  233. // to see if we're deleting the right channel, confirm the founder and the registration time
  234. if founder == info.Founder && registeredAt.Unix() == info.RegisteredAt.Unix() {
  235. for _, keyFmt := range channelKeyStrings {
  236. tx.Delete(fmt.Sprintf(keyFmt, key))
  237. }
  238. // remove this channel from the client's list of registered channels
  239. channelsKey := fmt.Sprintf(keyAccountChannels, info.Founder)
  240. channelsStr, err := tx.Get(channelsKey)
  241. if err == buntdb.ErrNotFound {
  242. return
  243. }
  244. registeredChannels := unmarshalRegisteredChannels(channelsStr)
  245. var nowRegisteredChannels []string
  246. for _, channel := range registeredChannels {
  247. if channel != key {
  248. nowRegisteredChannels = append(nowRegisteredChannels, channel)
  249. }
  250. }
  251. tx.Set(channelsKey, strings.Join(nowRegisteredChannels, ","), nil)
  252. }
  253. }
  254. }
  255. func (reg *ChannelRegistry) updateAccountToChannelMapping(tx *buntdb.Tx, channelInfo RegisteredChannel) {
  256. channelKey := channelInfo.NameCasefolded
  257. chanFounderKey := fmt.Sprintf(keyChannelFounder, channelKey)
  258. founder, existsErr := tx.Get(chanFounderKey)
  259. if existsErr == buntdb.ErrNotFound || founder != channelInfo.Founder {
  260. // add to new founder's list
  261. accountChannelsKey := fmt.Sprintf(keyAccountChannels, channelInfo.Founder)
  262. alreadyChannels, _ := tx.Get(accountChannelsKey)
  263. newChannels := channelKey // this is the casefolded channel name
  264. if alreadyChannels != "" {
  265. newChannels = fmt.Sprintf("%s,%s", alreadyChannels, newChannels)
  266. }
  267. tx.Set(accountChannelsKey, newChannels, nil)
  268. }
  269. if existsErr == nil && founder != channelInfo.Founder {
  270. // remove from old founder's list
  271. accountChannelsKey := fmt.Sprintf(keyAccountChannels, founder)
  272. alreadyChannelsRaw, _ := tx.Get(accountChannelsKey)
  273. var newChannels []string
  274. if alreadyChannelsRaw != "" {
  275. for _, chname := range strings.Split(alreadyChannelsRaw, ",") {
  276. if chname != channelInfo.NameCasefolded {
  277. newChannels = append(newChannels, chname)
  278. }
  279. }
  280. }
  281. tx.Set(accountChannelsKey, strings.Join(newChannels, ","), nil)
  282. }
  283. }
  284. // saveChannel saves a channel to the store.
  285. func (reg *ChannelRegistry) saveChannel(tx *buntdb.Tx, channelInfo RegisteredChannel, includeFlags uint) {
  286. channelKey := channelInfo.NameCasefolded
  287. // maintain the mapping of account -> registered channels
  288. reg.updateAccountToChannelMapping(tx, channelInfo)
  289. if includeFlags&IncludeInitial != 0 {
  290. tx.Set(fmt.Sprintf(keyChannelExists, channelKey), "1", nil)
  291. tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
  292. tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
  293. tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
  294. }
  295. if includeFlags&IncludeTopic != 0 {
  296. tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
  297. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
  298. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
  299. }
  300. if includeFlags&IncludeModes != 0 {
  301. tx.Set(fmt.Sprintf(keyChannelPassword, channelKey), channelInfo.Key, nil)
  302. modeStrings := make([]string, len(channelInfo.Modes))
  303. for i, mode := range channelInfo.Modes {
  304. modeStrings[i] = string(mode)
  305. }
  306. tx.Set(fmt.Sprintf(keyChannelModes, channelKey), strings.Join(modeStrings, ""), nil)
  307. tx.Set(fmt.Sprintf(keyChannelUserLimit, channelKey), strconv.Itoa(channelInfo.UserLimit), nil)
  308. }
  309. if includeFlags&IncludeLists != 0 {
  310. banlistString, _ := json.Marshal(channelInfo.Bans)
  311. tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
  312. exceptlistString, _ := json.Marshal(channelInfo.Excepts)
  313. tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
  314. invitelistString, _ := json.Marshal(channelInfo.Invites)
  315. tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
  316. accountToUModeString, _ := json.Marshal(channelInfo.AccountToUMode)
  317. tx.Set(fmt.Sprintf(keyChannelAccountToUMode, channelKey), string(accountToUModeString), nil)
  318. }
  319. }
  320. // PurgeChannel records a channel purge.
  321. func (reg *ChannelRegistry) PurgeChannel(chname string, record ChannelPurgeRecord) (err error) {
  322. serialized, err := json.Marshal(record)
  323. if err != nil {
  324. return err
  325. }
  326. serializedStr := string(serialized)
  327. key := fmt.Sprintf(keyChannelPurged, chname)
  328. return reg.server.store.Update(func(tx *buntdb.Tx) error {
  329. tx.Set(key, serializedStr, nil)
  330. return nil
  331. })
  332. }
  333. // LoadPurgeRecord retrieves information about whether and how a channel was purged.
  334. func (reg *ChannelRegistry) LoadPurgeRecord(chname string) (record ChannelPurgeRecord, err error) {
  335. var rawRecord string
  336. key := fmt.Sprintf(keyChannelPurged, chname)
  337. reg.server.store.View(func(tx *buntdb.Tx) error {
  338. rawRecord, _ = tx.Get(key)
  339. return nil
  340. })
  341. if rawRecord == "" {
  342. err = errNoSuchChannel
  343. return
  344. }
  345. err = json.Unmarshal([]byte(rawRecord), &record)
  346. if err != nil {
  347. reg.server.logger.Error("internal", "corrupt purge record", chname, err.Error())
  348. err = errNoSuchChannel
  349. return
  350. }
  351. return
  352. }
  353. // UnpurgeChannel deletes the record of a channel purge.
  354. func (reg *ChannelRegistry) UnpurgeChannel(chname string) (err error) {
  355. key := fmt.Sprintf(keyChannelPurged, chname)
  356. return reg.server.store.Update(func(tx *buntdb.Tx) error {
  357. tx.Delete(key)
  358. return nil
  359. })
  360. }