您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

channelreg.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. )
  30. var (
  31. channelKeyStrings = []string{
  32. keyChannelExists,
  33. keyChannelName,
  34. keyChannelRegTime,
  35. keyChannelFounder,
  36. keyChannelTopic,
  37. keyChannelTopicSetBy,
  38. keyChannelTopicSetTime,
  39. keyChannelBanlist,
  40. keyChannelExceptlist,
  41. keyChannelInvitelist,
  42. keyChannelPassword,
  43. keyChannelModes,
  44. keyChannelAccountToUMode,
  45. }
  46. )
  47. // these are bit flags indicating what part of the channel status is "dirty"
  48. // and needs to be read from memory and written to the db
  49. const (
  50. IncludeInitial uint = 1 << iota
  51. IncludeTopic
  52. IncludeModes
  53. IncludeLists
  54. )
  55. // this is an OR of all possible flags
  56. const (
  57. IncludeAllChannelAttrs = ^uint(0)
  58. )
  59. // RegisteredChannel holds details about a given registered channel.
  60. type RegisteredChannel struct {
  61. // Name of the channel.
  62. Name string
  63. // Casefolded name of the channel.
  64. NameCasefolded string
  65. // RegisteredAt represents the time that the channel was registered.
  66. RegisteredAt time.Time
  67. // Founder indicates the founder of the channel.
  68. Founder string
  69. // Topic represents the channel topic.
  70. Topic string
  71. // TopicSetBy represents the host that set the topic.
  72. TopicSetBy string
  73. // TopicSetTime represents the time the topic was set.
  74. TopicSetTime time.Time
  75. // Modes represents the channel modes
  76. Modes []modes.Mode
  77. // Key represents the channel key / password
  78. Key string
  79. // AccountToUMode maps user accounts to their persistent channel modes (e.g., +q, +h)
  80. AccountToUMode map[string]modes.Mode
  81. // Banlist represents the bans set on the channel.
  82. Banlist []string
  83. // Exceptlist represents the exceptions set on the channel.
  84. Exceptlist []string
  85. // Invitelist represents the invite exceptions set on the channel.
  86. Invitelist []string
  87. }
  88. // ChannelRegistry manages registered channels.
  89. type ChannelRegistry struct {
  90. server *Server
  91. }
  92. // NewChannelRegistry returns a new ChannelRegistry.
  93. func (reg *ChannelRegistry) Initialize(server *Server) {
  94. reg.server = server
  95. }
  96. func (reg *ChannelRegistry) AllChannels() (result map[string]bool) {
  97. result = make(map[string]bool)
  98. prefix := fmt.Sprintf(keyChannelExists, "")
  99. reg.server.store.View(func(tx *buntdb.Tx) error {
  100. return tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
  101. if !strings.HasPrefix(key, prefix) {
  102. return false
  103. }
  104. channel := strings.TrimPrefix(key, prefix)
  105. result[channel] = true
  106. return true
  107. })
  108. })
  109. return
  110. }
  111. // StoreChannel obtains a consistent view of a channel, then persists it to the store.
  112. func (reg *ChannelRegistry) StoreChannel(info RegisteredChannel, includeFlags uint) (err error) {
  113. if !reg.server.ChannelRegistrationEnabled() {
  114. return
  115. }
  116. if info.Founder == "" {
  117. // sanity check, don't try to store an unregistered channel
  118. return
  119. }
  120. reg.server.store.Update(func(tx *buntdb.Tx) error {
  121. reg.saveChannel(tx, info, includeFlags)
  122. return nil
  123. })
  124. return nil
  125. }
  126. // LoadChannel loads a channel from the store.
  127. func (reg *ChannelRegistry) LoadChannel(nameCasefolded string) (info RegisteredChannel, err error) {
  128. if !reg.server.ChannelRegistrationEnabled() {
  129. err = errFeatureDisabled
  130. return
  131. }
  132. channelKey := nameCasefolded
  133. // nice to have: do all JSON (de)serialization outside of the buntdb transaction
  134. err = reg.server.store.View(func(tx *buntdb.Tx) error {
  135. _, dberr := tx.Get(fmt.Sprintf(keyChannelExists, channelKey))
  136. if dberr == buntdb.ErrNotFound {
  137. // chan does not already exist, return
  138. return errNoSuchChannel
  139. }
  140. // channel exists, load it
  141. name, _ := tx.Get(fmt.Sprintf(keyChannelName, channelKey))
  142. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, channelKey))
  143. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  144. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, channelKey))
  145. topic, _ := tx.Get(fmt.Sprintf(keyChannelTopic, channelKey))
  146. topicSetBy, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetBy, channelKey))
  147. topicSetTime, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetTime, channelKey))
  148. topicSetTimeInt, _ := strconv.ParseInt(topicSetTime, 10, 64)
  149. password, _ := tx.Get(fmt.Sprintf(keyChannelPassword, channelKey))
  150. modeString, _ := tx.Get(fmt.Sprintf(keyChannelModes, channelKey))
  151. banlistString, _ := tx.Get(fmt.Sprintf(keyChannelBanlist, channelKey))
  152. exceptlistString, _ := tx.Get(fmt.Sprintf(keyChannelExceptlist, channelKey))
  153. invitelistString, _ := tx.Get(fmt.Sprintf(keyChannelInvitelist, channelKey))
  154. accountToUModeString, _ := tx.Get(fmt.Sprintf(keyChannelAccountToUMode, channelKey))
  155. modeSlice := make([]modes.Mode, len(modeString))
  156. for i, mode := range modeString {
  157. modeSlice[i] = modes.Mode(mode)
  158. }
  159. var banlist []string
  160. _ = json.Unmarshal([]byte(banlistString), &banlist)
  161. var exceptlist []string
  162. _ = json.Unmarshal([]byte(exceptlistString), &exceptlist)
  163. var invitelist []string
  164. _ = json.Unmarshal([]byte(invitelistString), &invitelist)
  165. accountToUMode := make(map[string]modes.Mode)
  166. _ = json.Unmarshal([]byte(accountToUModeString), &accountToUMode)
  167. info = RegisteredChannel{
  168. Name: name,
  169. RegisteredAt: time.Unix(regTimeInt, 0),
  170. Founder: founder,
  171. Topic: topic,
  172. TopicSetBy: topicSetBy,
  173. TopicSetTime: time.Unix(topicSetTimeInt, 0),
  174. Key: password,
  175. Modes: modeSlice,
  176. Banlist: banlist,
  177. Exceptlist: exceptlist,
  178. Invitelist: invitelist,
  179. AccountToUMode: accountToUMode,
  180. }
  181. return nil
  182. })
  183. return
  184. }
  185. // Delete deletes a channel corresponding to `info`. If no such channel
  186. // is present in the database, no error is returned.
  187. func (reg *ChannelRegistry) Delete(info RegisteredChannel) (err error) {
  188. if !reg.server.ChannelRegistrationEnabled() {
  189. return
  190. }
  191. reg.server.store.Update(func(tx *buntdb.Tx) error {
  192. reg.deleteChannel(tx, info.NameCasefolded, info)
  193. return nil
  194. })
  195. return nil
  196. }
  197. // delete a channel, unless it was overwritten by another registration of the same channel
  198. func (reg *ChannelRegistry) deleteChannel(tx *buntdb.Tx, key string, info RegisteredChannel) {
  199. _, err := tx.Get(fmt.Sprintf(keyChannelExists, key))
  200. if err == nil {
  201. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, key))
  202. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  203. registeredAt := time.Unix(regTimeInt, 0)
  204. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, key))
  205. // to see if we're deleting the right channel, confirm the founder and the registration time
  206. if founder == info.Founder && registeredAt.Unix() == info.RegisteredAt.Unix() {
  207. for _, keyFmt := range channelKeyStrings {
  208. tx.Delete(fmt.Sprintf(keyFmt, key))
  209. }
  210. // remove this channel from the client's list of registered channels
  211. channelsKey := fmt.Sprintf(keyAccountChannels, info.Founder)
  212. channelsStr, err := tx.Get(channelsKey)
  213. if err == buntdb.ErrNotFound {
  214. return
  215. }
  216. registeredChannels := unmarshalRegisteredChannels(channelsStr)
  217. var nowRegisteredChannels []string
  218. for _, channel := range registeredChannels {
  219. if channel != key {
  220. nowRegisteredChannels = append(nowRegisteredChannels, channel)
  221. }
  222. }
  223. tx.Set(channelsKey, strings.Join(nowRegisteredChannels, ","), nil)
  224. }
  225. }
  226. }
  227. // saveChannel saves a channel to the store.
  228. func (reg *ChannelRegistry) saveChannel(tx *buntdb.Tx, channelInfo RegisteredChannel, includeFlags uint) {
  229. channelKey := channelInfo.NameCasefolded
  230. // maintain the mapping of account -> registered channels
  231. chanExistsKey := fmt.Sprintf(keyChannelExists, channelKey)
  232. _, existsErr := tx.Get(chanExistsKey)
  233. if existsErr == buntdb.ErrNotFound {
  234. // this is a new registration, need to update account-to-channels
  235. accountChannelsKey := fmt.Sprintf(keyAccountChannels, channelInfo.Founder)
  236. alreadyChannels, _ := tx.Get(accountChannelsKey)
  237. newChannels := channelKey // this is the casefolded channel name
  238. if alreadyChannels != "" {
  239. newChannels = fmt.Sprintf("%s,%s", alreadyChannels, newChannels)
  240. }
  241. tx.Set(accountChannelsKey, newChannels, nil)
  242. }
  243. if includeFlags&IncludeInitial != 0 {
  244. tx.Set(chanExistsKey, "1", nil)
  245. tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
  246. tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
  247. tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
  248. }
  249. if includeFlags&IncludeTopic != 0 {
  250. tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
  251. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
  252. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
  253. }
  254. if includeFlags&IncludeModes != 0 {
  255. tx.Set(fmt.Sprintf(keyChannelPassword, channelKey), channelInfo.Key, nil)
  256. modeStrings := make([]string, len(channelInfo.Modes))
  257. for i, mode := range channelInfo.Modes {
  258. modeStrings[i] = string(mode)
  259. }
  260. tx.Set(fmt.Sprintf(keyChannelModes, channelKey), strings.Join(modeStrings, ""), nil)
  261. }
  262. if includeFlags&IncludeLists != 0 {
  263. banlistString, _ := json.Marshal(channelInfo.Banlist)
  264. tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
  265. exceptlistString, _ := json.Marshal(channelInfo.Exceptlist)
  266. tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
  267. invitelistString, _ := json.Marshal(channelInfo.Invitelist)
  268. tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
  269. accountToUModeString, _ := json.Marshal(channelInfo.AccountToUMode)
  270. tx.Set(fmt.Sprintf(keyChannelAccountToUMode, channelKey), string(accountToUModeString), nil)
  271. }
  272. }