Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

channelreg.go 14KB

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