Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. "sync"
  9. "time"
  10. "encoding/json"
  11. "github.com/oragono/oragono/irc/modes"
  12. "github.com/tidwall/buntdb"
  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. )
  31. var (
  32. channelKeyStrings = []string{
  33. keyChannelExists,
  34. keyChannelName,
  35. keyChannelRegTime,
  36. keyChannelFounder,
  37. keyChannelTopic,
  38. keyChannelTopicSetBy,
  39. keyChannelTopicSetTime,
  40. keyChannelBanlist,
  41. keyChannelExceptlist,
  42. keyChannelInvitelist,
  43. keyChannelPassword,
  44. keyChannelModes,
  45. keyChannelAccountToUMode,
  46. }
  47. )
  48. // these are bit flags indicating what part of the channel status is "dirty"
  49. // and needs to be read from memory and written to the db
  50. const (
  51. IncludeInitial uint = 1 << iota
  52. IncludeTopic
  53. IncludeModes
  54. IncludeLists
  55. )
  56. // this is an OR of all possible flags
  57. const (
  58. IncludeAllChannelAttrs = ^uint(0)
  59. )
  60. // RegisteredChannel holds details about a given registered channel.
  61. type RegisteredChannel struct {
  62. // Name of the channel.
  63. Name string
  64. // RegisteredAt represents the time that the channel was registered.
  65. RegisteredAt time.Time
  66. // Founder indicates the founder of the channel.
  67. Founder string
  68. // Topic represents the channel topic.
  69. Topic string
  70. // TopicSetBy represents the host that set the topic.
  71. TopicSetBy string
  72. // TopicSetTime represents the time the topic was set.
  73. TopicSetTime time.Time
  74. // Modes represents the channel modes
  75. Modes []modes.Mode
  76. // Key represents the channel key / password
  77. Key string
  78. // AccountToUMode maps user accounts to their persistent channel modes (e.g., +q, +h)
  79. AccountToUMode map[string]modes.Mode
  80. // Banlist represents the bans set on the channel.
  81. Banlist []string
  82. // Exceptlist represents the exceptions set on the channel.
  83. Exceptlist []string
  84. // Invitelist represents the invite exceptions set on the channel.
  85. Invitelist []string
  86. }
  87. // ChannelRegistry manages registered channels.
  88. type ChannelRegistry struct {
  89. // This serializes operations of the form (read channel state, synchronously persist it);
  90. // this is enough to guarantee eventual consistency of the database with the
  91. // ChannelManager and Channel objects, which are the source of truth.
  92. //
  93. // We could use the buntdb RW transaction lock for this purpose but we share
  94. // that with all the other modules, so let's not.
  95. sync.Mutex // tier 2
  96. server *Server
  97. }
  98. // NewChannelRegistry returns a new ChannelRegistry.
  99. func NewChannelRegistry(server *Server) *ChannelRegistry {
  100. return &ChannelRegistry{
  101. server: server,
  102. }
  103. }
  104. // StoreChannel obtains a consistent view of a channel, then persists it to the store.
  105. func (reg *ChannelRegistry) StoreChannel(channel *Channel, includeFlags uint) {
  106. if !reg.server.ChannelRegistrationEnabled() {
  107. return
  108. }
  109. reg.Lock()
  110. defer reg.Unlock()
  111. key := channel.NameCasefolded()
  112. info := channel.ExportRegistration(includeFlags)
  113. if info.Founder == "" {
  114. // sanity check, don't try to store an unregistered channel
  115. return
  116. }
  117. reg.server.store.Update(func(tx *buntdb.Tx) error {
  118. reg.saveChannel(tx, key, info, includeFlags)
  119. return nil
  120. })
  121. }
  122. // LoadChannel loads a channel from the store.
  123. func (reg *ChannelRegistry) LoadChannel(nameCasefolded string) (info *RegisteredChannel) {
  124. if !reg.server.ChannelRegistrationEnabled() {
  125. return nil
  126. }
  127. channelKey := nameCasefolded
  128. // nice to have: do all JSON (de)serialization outside of the buntdb transaction
  129. reg.server.store.View(func(tx *buntdb.Tx) error {
  130. _, err := tx.Get(fmt.Sprintf(keyChannelExists, channelKey))
  131. if err == buntdb.ErrNotFound {
  132. // chan does not already exist, return
  133. return nil
  134. }
  135. // channel exists, load it
  136. name, _ := tx.Get(fmt.Sprintf(keyChannelName, channelKey))
  137. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, channelKey))
  138. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  139. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, channelKey))
  140. topic, _ := tx.Get(fmt.Sprintf(keyChannelTopic, channelKey))
  141. topicSetBy, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetBy, channelKey))
  142. topicSetTime, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetTime, channelKey))
  143. topicSetTimeInt, _ := strconv.ParseInt(topicSetTime, 10, 64)
  144. password, _ := tx.Get(fmt.Sprintf(keyChannelPassword, channelKey))
  145. modeString, _ := tx.Get(fmt.Sprintf(keyChannelModes, channelKey))
  146. banlistString, _ := tx.Get(fmt.Sprintf(keyChannelBanlist, channelKey))
  147. exceptlistString, _ := tx.Get(fmt.Sprintf(keyChannelExceptlist, channelKey))
  148. invitelistString, _ := tx.Get(fmt.Sprintf(keyChannelInvitelist, channelKey))
  149. accountToUModeString, _ := tx.Get(fmt.Sprintf(keyChannelAccountToUMode, channelKey))
  150. modeSlice := make([]modes.Mode, len(modeString))
  151. for i, mode := range modeString {
  152. modeSlice[i] = modes.Mode(mode)
  153. }
  154. var banlist []string
  155. _ = json.Unmarshal([]byte(banlistString), &banlist)
  156. var exceptlist []string
  157. _ = json.Unmarshal([]byte(exceptlistString), &exceptlist)
  158. var invitelist []string
  159. _ = json.Unmarshal([]byte(invitelistString), &invitelist)
  160. accountToUMode := make(map[string]modes.Mode)
  161. _ = json.Unmarshal([]byte(accountToUModeString), &accountToUMode)
  162. info = &RegisteredChannel{
  163. Name: name,
  164. RegisteredAt: time.Unix(regTimeInt, 0),
  165. Founder: founder,
  166. Topic: topic,
  167. TopicSetBy: topicSetBy,
  168. TopicSetTime: time.Unix(topicSetTimeInt, 0),
  169. Key: password,
  170. Modes: modeSlice,
  171. Banlist: banlist,
  172. Exceptlist: exceptlist,
  173. Invitelist: invitelist,
  174. AccountToUMode: accountToUMode,
  175. }
  176. return nil
  177. })
  178. return info
  179. }
  180. // Rename handles the persistence part of a channel rename: the channel is
  181. // persisted under its new name, and the old name is cleaned up if necessary.
  182. func (reg *ChannelRegistry) Rename(channel *Channel, casefoldedOldName string) {
  183. if !reg.server.ChannelRegistrationEnabled() {
  184. return
  185. }
  186. reg.Lock()
  187. defer reg.Unlock()
  188. includeFlags := IncludeAllChannelAttrs
  189. oldKey := casefoldedOldName
  190. key := channel.NameCasefolded()
  191. info := channel.ExportRegistration(includeFlags)
  192. if info.Founder == "" {
  193. return
  194. }
  195. reg.server.store.Update(func(tx *buntdb.Tx) error {
  196. reg.deleteChannel(tx, oldKey, info)
  197. reg.saveChannel(tx, key, info, includeFlags)
  198. return nil
  199. })
  200. }
  201. // delete a channel, unless it was overwritten by another registration of the same channel
  202. func (reg *ChannelRegistry) deleteChannel(tx *buntdb.Tx, key string, info RegisteredChannel) {
  203. _, err := tx.Get(fmt.Sprintf(keyChannelExists, key))
  204. if err == nil {
  205. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, key))
  206. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  207. registeredAt := time.Unix(regTimeInt, 0)
  208. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, key))
  209. // to see if we're deleting the right channel, confirm the founder and the registration time
  210. if founder == info.Founder && registeredAt == info.RegisteredAt {
  211. for _, keyFmt := range channelKeyStrings {
  212. tx.Delete(fmt.Sprintf(keyFmt, key))
  213. }
  214. }
  215. }
  216. }
  217. // saveChannel saves a channel to the store.
  218. func (reg *ChannelRegistry) saveChannel(tx *buntdb.Tx, channelKey string, channelInfo RegisteredChannel, includeFlags uint) {
  219. if includeFlags&IncludeInitial != 0 {
  220. tx.Set(fmt.Sprintf(keyChannelExists, channelKey), "1", nil)
  221. tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
  222. tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
  223. tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
  224. }
  225. if includeFlags&IncludeTopic != 0 {
  226. tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
  227. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
  228. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
  229. }
  230. if includeFlags&IncludeModes != 0 {
  231. tx.Set(fmt.Sprintf(keyChannelPassword, channelKey), channelInfo.Key, nil)
  232. modeStrings := make([]string, len(channelInfo.Modes))
  233. for i, mode := range channelInfo.Modes {
  234. modeStrings[i] = string(mode)
  235. }
  236. tx.Set(fmt.Sprintf(keyChannelModes, channelKey), strings.Join(modeStrings, ""), nil)
  237. }
  238. if includeFlags&IncludeLists != 0 {
  239. banlistString, _ := json.Marshal(channelInfo.Banlist)
  240. tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
  241. exceptlistString, _ := json.Marshal(channelInfo.Exceptlist)
  242. tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
  243. invitelistString, _ := json.Marshal(channelInfo.Invitelist)
  244. tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
  245. accountToUModeString, _ := json.Marshal(channelInfo.AccountToUMode)
  246. tx.Set(fmt.Sprintf(keyChannelAccountToUMode, channelKey), string(accountToUModeString), nil)
  247. }
  248. }