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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. type ChannelRegistry struct {
  62. // this serializes operations of the form (read channel state, synchronously persist it);
  63. // this is enough to guarantee eventual consistency of the database with the
  64. // ChannelManager and Channel objects, which are the source of truth.
  65. // Wwe could use the buntdb RW transaction lock for this purpose but we share
  66. // that with all the other modules, so let's not.
  67. sync.Mutex // tier 2
  68. server *Server
  69. }
  70. func NewChannelRegistry(server *Server) *ChannelRegistry {
  71. return &ChannelRegistry{
  72. server: server,
  73. }
  74. }
  75. // StoreChannel obtains a consistent view of a channel, then persists it to the store.
  76. func (reg *ChannelRegistry) StoreChannel(channel *Channel, includeLists bool) {
  77. if !reg.server.ChannelRegistrationEnabled() {
  78. return
  79. }
  80. reg.Lock()
  81. defer reg.Unlock()
  82. key := channel.NameCasefolded()
  83. info := channel.ExportRegistration(includeLists)
  84. if info.Founder == "" {
  85. // sanity check, don't try to store an unregistered channel
  86. return
  87. }
  88. reg.server.store.Update(func(tx *buntdb.Tx) error {
  89. reg.saveChannel(tx, key, info, includeLists)
  90. return nil
  91. })
  92. }
  93. // LoadChannel loads a channel from the store.
  94. func (reg *ChannelRegistry) LoadChannel(nameCasefolded string) (info *RegisteredChannel) {
  95. if !reg.server.ChannelRegistrationEnabled() {
  96. return nil
  97. }
  98. channelKey := nameCasefolded
  99. // nice to have: do all JSON (de)serialization outside of the buntdb transaction
  100. reg.server.store.View(func(tx *buntdb.Tx) error {
  101. _, err := tx.Get(fmt.Sprintf(keyChannelExists, channelKey))
  102. if err == buntdb.ErrNotFound {
  103. // chan does not already exist, return
  104. return nil
  105. }
  106. // channel exists, load it
  107. name, _ := tx.Get(fmt.Sprintf(keyChannelName, channelKey))
  108. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, channelKey))
  109. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  110. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, channelKey))
  111. topic, _ := tx.Get(fmt.Sprintf(keyChannelTopic, channelKey))
  112. topicSetBy, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetBy, channelKey))
  113. topicSetTime, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetTime, channelKey))
  114. topicSetTimeInt, _ := strconv.ParseInt(topicSetTime, 10, 64)
  115. banlistString, _ := tx.Get(fmt.Sprintf(keyChannelBanlist, channelKey))
  116. exceptlistString, _ := tx.Get(fmt.Sprintf(keyChannelExceptlist, channelKey))
  117. invitelistString, _ := tx.Get(fmt.Sprintf(keyChannelInvitelist, channelKey))
  118. var banlist []string
  119. _ = json.Unmarshal([]byte(banlistString), &banlist)
  120. var exceptlist []string
  121. _ = json.Unmarshal([]byte(exceptlistString), &exceptlist)
  122. var invitelist []string
  123. _ = json.Unmarshal([]byte(invitelistString), &invitelist)
  124. info = &RegisteredChannel{
  125. Name: name,
  126. RegisteredAt: time.Unix(regTimeInt, 0),
  127. Founder: founder,
  128. Topic: topic,
  129. TopicSetBy: topicSetBy,
  130. TopicSetTime: time.Unix(topicSetTimeInt, 0),
  131. Banlist: banlist,
  132. Exceptlist: exceptlist,
  133. Invitelist: invitelist,
  134. }
  135. return nil
  136. })
  137. return info
  138. }
  139. // Rename handles the persistence part of a channel rename: the channel is
  140. // persisted under its new name, and the old name is cleaned up if necessary.
  141. func (reg *ChannelRegistry) Rename(channel *Channel, casefoldedOldName string) {
  142. if !reg.server.ChannelRegistrationEnabled() {
  143. return
  144. }
  145. reg.Lock()
  146. defer reg.Unlock()
  147. includeLists := true
  148. oldKey := casefoldedOldName
  149. key := channel.NameCasefolded()
  150. info := channel.ExportRegistration(includeLists)
  151. if info.Founder == "" {
  152. return
  153. }
  154. reg.server.store.Update(func(tx *buntdb.Tx) error {
  155. reg.deleteChannel(tx, oldKey, info)
  156. reg.saveChannel(tx, key, info, includeLists)
  157. return nil
  158. })
  159. }
  160. // delete a channel, unless it was overwritten by another registration of the same channel
  161. func (reg *ChannelRegistry) deleteChannel(tx *buntdb.Tx, key string, info RegisteredChannel) {
  162. _, err := tx.Get(fmt.Sprintf(keyChannelExists, key))
  163. if err == nil {
  164. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, key))
  165. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  166. registeredAt := time.Unix(regTimeInt, 0)
  167. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, key))
  168. // to see if we're deleting the right channel, confirm the founder and the registration time
  169. if founder == info.Founder && registeredAt == info.RegisteredAt {
  170. for _, keyFmt := range channelKeyStrings {
  171. tx.Delete(fmt.Sprintf(keyFmt, key))
  172. }
  173. }
  174. }
  175. }
  176. // saveChannel saves a channel to the store.
  177. func (reg *ChannelRegistry) saveChannel(tx *buntdb.Tx, channelKey string, channelInfo RegisteredChannel, includeLists bool) {
  178. tx.Set(fmt.Sprintf(keyChannelExists, channelKey), "1", nil)
  179. tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
  180. tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
  181. tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
  182. tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
  183. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
  184. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
  185. if includeLists {
  186. banlistString, _ := json.Marshal(channelInfo.Banlist)
  187. tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
  188. exceptlistString, _ := json.Marshal(channelInfo.Exceptlist)
  189. tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
  190. invitelistString, _ := json.Marshal(channelInfo.Invitelist)
  191. tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
  192. }
  193. }