Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

channelreg.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. func (reg *ChannelRegistry) Delete(casefoldedName string, info RegisteredChannel) {
  181. if !reg.server.ChannelRegistrationEnabled() {
  182. return
  183. }
  184. reg.Lock()
  185. defer reg.Unlock()
  186. reg.server.store.Update(func(tx *buntdb.Tx) error {
  187. reg.deleteChannel(tx, casefoldedName, info)
  188. return nil
  189. })
  190. }
  191. // Rename handles the persistence part of a channel rename: the channel is
  192. // persisted under its new name, and the old name is cleaned up if necessary.
  193. func (reg *ChannelRegistry) Rename(channel *Channel, casefoldedOldName string) {
  194. if !reg.server.ChannelRegistrationEnabled() {
  195. return
  196. }
  197. reg.Lock()
  198. defer reg.Unlock()
  199. includeFlags := IncludeAllChannelAttrs
  200. oldKey := casefoldedOldName
  201. key := channel.NameCasefolded()
  202. info := channel.ExportRegistration(includeFlags)
  203. if info.Founder == "" {
  204. return
  205. }
  206. reg.server.store.Update(func(tx *buntdb.Tx) error {
  207. reg.deleteChannel(tx, oldKey, info)
  208. reg.saveChannel(tx, key, info, includeFlags)
  209. return nil
  210. })
  211. }
  212. // delete a channel, unless it was overwritten by another registration of the same channel
  213. func (reg *ChannelRegistry) deleteChannel(tx *buntdb.Tx, key string, info RegisteredChannel) {
  214. _, err := tx.Get(fmt.Sprintf(keyChannelExists, key))
  215. if err == nil {
  216. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, key))
  217. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  218. registeredAt := time.Unix(regTimeInt, 0)
  219. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, key))
  220. // to see if we're deleting the right channel, confirm the founder and the registration time
  221. if founder == info.Founder && registeredAt.Unix() == info.RegisteredAt.Unix() {
  222. for _, keyFmt := range channelKeyStrings {
  223. tx.Delete(fmt.Sprintf(keyFmt, key))
  224. }
  225. // remove this channel from the client's list of registered channels
  226. channelsKey := fmt.Sprintf(keyAccountChannels, info.Founder)
  227. channelsStr, err := tx.Get(channelsKey)
  228. if err == buntdb.ErrNotFound {
  229. return
  230. }
  231. registeredChannels := unmarshalRegisteredChannels(channelsStr)
  232. var nowRegisteredChannels []string
  233. for _, channel := range registeredChannels {
  234. if channel != key {
  235. nowRegisteredChannels = append(nowRegisteredChannels, channel)
  236. }
  237. }
  238. tx.Set(channelsKey, strings.Join(nowRegisteredChannels, ","), nil)
  239. }
  240. }
  241. }
  242. // saveChannel saves a channel to the store.
  243. func (reg *ChannelRegistry) saveChannel(tx *buntdb.Tx, channelKey string, channelInfo RegisteredChannel, includeFlags uint) {
  244. // maintain the mapping of account -> registered channels
  245. chanExistsKey := fmt.Sprintf(keyChannelExists, channelKey)
  246. _, existsErr := tx.Get(chanExistsKey)
  247. if existsErr == buntdb.ErrNotFound {
  248. // this is a new registration, need to update account-to-channels
  249. accountChannelsKey := fmt.Sprintf(keyAccountChannels, channelInfo.Founder)
  250. alreadyChannels, _ := tx.Get(accountChannelsKey)
  251. newChannels := channelKey // this is the casefolded channel name
  252. if alreadyChannels != "" {
  253. newChannels = fmt.Sprintf("%s,%s", alreadyChannels, newChannels)
  254. }
  255. tx.Set(accountChannelsKey, newChannels, nil)
  256. }
  257. if includeFlags&IncludeInitial != 0 {
  258. tx.Set(chanExistsKey, "1", nil)
  259. tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
  260. tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
  261. tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
  262. }
  263. if includeFlags&IncludeTopic != 0 {
  264. tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
  265. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
  266. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
  267. }
  268. if includeFlags&IncludeModes != 0 {
  269. tx.Set(fmt.Sprintf(keyChannelPassword, channelKey), channelInfo.Key, nil)
  270. modeStrings := make([]string, len(channelInfo.Modes))
  271. for i, mode := range channelInfo.Modes {
  272. modeStrings[i] = string(mode)
  273. }
  274. tx.Set(fmt.Sprintf(keyChannelModes, channelKey), strings.Join(modeStrings, ""), nil)
  275. }
  276. if includeFlags&IncludeLists != 0 {
  277. banlistString, _ := json.Marshal(channelInfo.Banlist)
  278. tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
  279. exceptlistString, _ := json.Marshal(channelInfo.Exceptlist)
  280. tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
  281. invitelistString, _ := json.Marshal(channelInfo.Invitelist)
  282. tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
  283. accountToUModeString, _ := json.Marshal(channelInfo.AccountToUMode)
  284. tx.Set(fmt.Sprintf(keyChannelAccountToUMode, channelKey), string(accountToUModeString), nil)
  285. }
  286. }