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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. "sync"
  8. "time"
  9. "encoding/json"
  10. "github.com/tidwall/buntdb"
  11. )
  12. // this is exclusively the *persistence* layer for channel registration;
  13. // channel creation/tracking/destruction is in channelmanager.go
  14. const (
  15. keyChannelExists = "channel.exists %s"
  16. keyChannelName = "channel.name %s" // stores the 'preferred name' of the channel, not casemapped
  17. keyChannelRegTime = "channel.registered.time %s"
  18. keyChannelFounder = "channel.founder %s"
  19. keyChannelTopic = "channel.topic %s"
  20. keyChannelTopicSetBy = "channel.topic.setby %s"
  21. keyChannelTopicSetTime = "channel.topic.settime %s"
  22. keyChannelBanlist = "channel.banlist %s"
  23. keyChannelExceptlist = "channel.exceptlist %s"
  24. keyChannelInvitelist = "channel.invitelist %s"
  25. )
  26. var (
  27. channelKeyStrings = []string{
  28. keyChannelExists,
  29. keyChannelName,
  30. keyChannelRegTime,
  31. keyChannelFounder,
  32. keyChannelTopic,
  33. keyChannelTopicSetBy,
  34. keyChannelTopicSetTime,
  35. keyChannelBanlist,
  36. keyChannelExceptlist,
  37. keyChannelInvitelist,
  38. }
  39. )
  40. // RegisteredChannel holds details about a given registered channel.
  41. type RegisteredChannel struct {
  42. // Name of the channel.
  43. Name string
  44. // RegisteredAt represents the time that the channel was registered.
  45. RegisteredAt time.Time
  46. // Founder indicates the founder of the channel.
  47. Founder string
  48. // Topic represents the channel topic.
  49. Topic string
  50. // TopicSetBy represents the host that set the topic.
  51. TopicSetBy string
  52. // TopicSetTime represents the time the topic was set.
  53. TopicSetTime time.Time
  54. // Banlist represents the bans set on the channel.
  55. Banlist []string
  56. // Exceptlist represents the exceptions set on the channel.
  57. Exceptlist []string
  58. // Invitelist represents the invite exceptions set on the channel.
  59. Invitelist []string
  60. }
  61. // ChannelRegistry manages registered channels.
  62. type ChannelRegistry struct {
  63. // This serializes operations of the form (read channel state, synchronously persist it);
  64. // this is enough to guarantee eventual consistency of the database with the
  65. // ChannelManager and Channel objects, which are the source of truth.
  66. //
  67. // We could use the buntdb RW transaction lock for this purpose but we share
  68. // that with all the other modules, so let's not.
  69. sync.Mutex // tier 2
  70. server *Server
  71. }
  72. // NewChannelRegistry returns a new ChannelRegistry.
  73. func NewChannelRegistry(server *Server) *ChannelRegistry {
  74. return &ChannelRegistry{
  75. server: server,
  76. }
  77. }
  78. // StoreChannel obtains a consistent view of a channel, then persists it to the store.
  79. func (reg *ChannelRegistry) StoreChannel(channel *Channel, includeLists bool) {
  80. if !reg.server.ChannelRegistrationEnabled() {
  81. return
  82. }
  83. reg.Lock()
  84. defer reg.Unlock()
  85. key := channel.NameCasefolded()
  86. info := channel.ExportRegistration(includeLists)
  87. if info.Founder == "" {
  88. // sanity check, don't try to store an unregistered channel
  89. return
  90. }
  91. reg.server.store.Update(func(tx *buntdb.Tx) error {
  92. reg.saveChannel(tx, key, info, includeLists)
  93. return nil
  94. })
  95. }
  96. // LoadChannel loads a channel from the store.
  97. func (reg *ChannelRegistry) LoadChannel(nameCasefolded string) (info *RegisteredChannel) {
  98. if !reg.server.ChannelRegistrationEnabled() {
  99. return nil
  100. }
  101. channelKey := nameCasefolded
  102. // nice to have: do all JSON (de)serialization outside of the buntdb transaction
  103. reg.server.store.View(func(tx *buntdb.Tx) error {
  104. _, err := tx.Get(fmt.Sprintf(keyChannelExists, channelKey))
  105. if err == buntdb.ErrNotFound {
  106. // chan does not already exist, return
  107. return nil
  108. }
  109. // channel exists, load it
  110. name, _ := tx.Get(fmt.Sprintf(keyChannelName, channelKey))
  111. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, channelKey))
  112. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  113. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, channelKey))
  114. topic, _ := tx.Get(fmt.Sprintf(keyChannelTopic, channelKey))
  115. topicSetBy, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetBy, channelKey))
  116. topicSetTime, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetTime, channelKey))
  117. topicSetTimeInt, _ := strconv.ParseInt(topicSetTime, 10, 64)
  118. banlistString, _ := tx.Get(fmt.Sprintf(keyChannelBanlist, channelKey))
  119. exceptlistString, _ := tx.Get(fmt.Sprintf(keyChannelExceptlist, channelKey))
  120. invitelistString, _ := tx.Get(fmt.Sprintf(keyChannelInvitelist, channelKey))
  121. var banlist []string
  122. _ = json.Unmarshal([]byte(banlistString), &banlist)
  123. var exceptlist []string
  124. _ = json.Unmarshal([]byte(exceptlistString), &exceptlist)
  125. var invitelist []string
  126. _ = json.Unmarshal([]byte(invitelistString), &invitelist)
  127. info = &RegisteredChannel{
  128. Name: name,
  129. RegisteredAt: time.Unix(regTimeInt, 0),
  130. Founder: founder,
  131. Topic: topic,
  132. TopicSetBy: topicSetBy,
  133. TopicSetTime: time.Unix(topicSetTimeInt, 0),
  134. Banlist: banlist,
  135. Exceptlist: exceptlist,
  136. Invitelist: invitelist,
  137. }
  138. return nil
  139. })
  140. return info
  141. }
  142. // Rename handles the persistence part of a channel rename: the channel is
  143. // persisted under its new name, and the old name is cleaned up if necessary.
  144. func (reg *ChannelRegistry) Rename(channel *Channel, casefoldedOldName string) {
  145. if !reg.server.ChannelRegistrationEnabled() {
  146. return
  147. }
  148. reg.Lock()
  149. defer reg.Unlock()
  150. includeLists := true
  151. oldKey := casefoldedOldName
  152. key := channel.NameCasefolded()
  153. info := channel.ExportRegistration(includeLists)
  154. if info.Founder == "" {
  155. return
  156. }
  157. reg.server.store.Update(func(tx *buntdb.Tx) error {
  158. reg.deleteChannel(tx, oldKey, info)
  159. reg.saveChannel(tx, key, info, includeLists)
  160. return nil
  161. })
  162. }
  163. // delete a channel, unless it was overwritten by another registration of the same channel
  164. func (reg *ChannelRegistry) deleteChannel(tx *buntdb.Tx, key string, info RegisteredChannel) {
  165. _, err := tx.Get(fmt.Sprintf(keyChannelExists, key))
  166. if err == nil {
  167. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, key))
  168. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  169. registeredAt := time.Unix(regTimeInt, 0)
  170. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, key))
  171. // to see if we're deleting the right channel, confirm the founder and the registration time
  172. if founder == info.Founder && registeredAt == info.RegisteredAt {
  173. for _, keyFmt := range channelKeyStrings {
  174. tx.Delete(fmt.Sprintf(keyFmt, key))
  175. }
  176. }
  177. }
  178. }
  179. // saveChannel saves a channel to the store.
  180. func (reg *ChannelRegistry) saveChannel(tx *buntdb.Tx, channelKey string, channelInfo RegisteredChannel, includeLists bool) {
  181. tx.Set(fmt.Sprintf(keyChannelExists, channelKey), "1", nil)
  182. tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
  183. tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
  184. tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
  185. tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
  186. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
  187. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
  188. if includeLists {
  189. banlistString, _ := json.Marshal(channelInfo.Banlist)
  190. tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
  191. exceptlistString, _ := json.Marshal(channelInfo.Exceptlist)
  192. tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
  193. invitelistString, _ := json.Marshal(channelInfo.Invitelist)
  194. tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
  195. }
  196. }