Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

channelmanager.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // Copyright (c) 2017 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "sort"
  6. "sync"
  7. "github.com/oragono/oragono/irc/utils"
  8. )
  9. type channelManagerEntry struct {
  10. channel *Channel
  11. // this is a refcount for joins, so we can avoid a race where we incorrectly
  12. // think the channel is empty (without holding a lock across the entire Channel.Join()
  13. // call)
  14. pendingJoins int
  15. skeleton string
  16. }
  17. // ChannelManager keeps track of all the channels on the server,
  18. // providing synchronization for creation of new channels on first join,
  19. // cleanup of empty channels on last part, and renames.
  20. type ChannelManager struct {
  21. sync.RWMutex // tier 2
  22. // chans is the main data structure, mapping casefolded name -> *Channel
  23. chans map[string]*channelManagerEntry
  24. chansSkeletons utils.StringSet // skeletons of *unregistered* chans
  25. registeredChannels utils.StringSet // casefolds of registered chans
  26. registeredSkeletons utils.StringSet // skeletons of registered chans
  27. purgedChannels utils.StringSet // casefolds of purged chans
  28. server *Server
  29. }
  30. // NewChannelManager returns a new ChannelManager.
  31. func (cm *ChannelManager) Initialize(server *Server) {
  32. cm.chans = make(map[string]*channelManagerEntry)
  33. cm.chansSkeletons = make(utils.StringSet)
  34. cm.server = server
  35. // purging should work even if registration is disabled
  36. cm.purgedChannels = cm.server.channelRegistry.PurgedChannels()
  37. cm.loadRegisteredChannels(server.Config())
  38. }
  39. func (cm *ChannelManager) loadRegisteredChannels(config *Config) {
  40. if !config.Channels.Registration.Enabled {
  41. return
  42. }
  43. var newChannels []*Channel
  44. var collisions []string
  45. defer func() {
  46. for _, ch := range newChannels {
  47. ch.EnsureLoaded()
  48. cm.server.logger.Debug("channels", "initialized registered channel", ch.Name())
  49. }
  50. for _, collision := range collisions {
  51. cm.server.logger.Warning("channels", "registered channel collides with existing channel", collision)
  52. }
  53. }()
  54. rawNames := cm.server.channelRegistry.AllChannels()
  55. cm.Lock()
  56. defer cm.Unlock()
  57. cm.registeredChannels = make(utils.StringSet, len(rawNames))
  58. cm.registeredSkeletons = make(utils.StringSet, len(rawNames))
  59. for _, name := range rawNames {
  60. cfname, err := CasefoldChannel(name)
  61. if err == nil {
  62. cm.registeredChannels.Add(cfname)
  63. }
  64. skeleton, err := Skeleton(name)
  65. if err == nil {
  66. cm.registeredSkeletons.Add(skeleton)
  67. }
  68. if !cm.purgedChannels.Has(cfname) {
  69. if _, ok := cm.chans[cfname]; !ok {
  70. ch := NewChannel(cm.server, name, cfname, true)
  71. cm.chans[cfname] = &channelManagerEntry{
  72. channel: ch,
  73. pendingJoins: 0,
  74. }
  75. newChannels = append(newChannels, ch)
  76. } else {
  77. collisions = append(collisions, name)
  78. }
  79. }
  80. }
  81. }
  82. // Get returns an existing channel with name equivalent to `name`, or nil
  83. func (cm *ChannelManager) Get(name string) (channel *Channel) {
  84. name, err := CasefoldChannel(name)
  85. if err == nil {
  86. cm.RLock()
  87. defer cm.RUnlock()
  88. entry := cm.chans[name]
  89. // if the channel is still loading, pretend we don't have it
  90. if entry != nil && entry.channel.IsLoaded() {
  91. return entry.channel
  92. }
  93. }
  94. return nil
  95. }
  96. // Join causes `client` to join the channel named `name`, creating it if necessary.
  97. func (cm *ChannelManager) Join(client *Client, name string, key string, isSajoin bool, rb *ResponseBuffer) (err error, forward string) {
  98. server := client.server
  99. casefoldedName, err := CasefoldChannel(name)
  100. skeleton, skerr := Skeleton(name)
  101. if err != nil || skerr != nil || len(casefoldedName) > server.Config().Limits.ChannelLen {
  102. return errNoSuchChannel, ""
  103. }
  104. channel, err := func() (*Channel, error) {
  105. cm.Lock()
  106. defer cm.Unlock()
  107. if cm.purgedChannels.Has(casefoldedName) {
  108. return nil, errChannelPurged
  109. }
  110. entry := cm.chans[casefoldedName]
  111. if entry == nil {
  112. registered := cm.registeredChannels.Has(casefoldedName)
  113. // enforce OpOnlyCreation
  114. if !registered && server.Config().Channels.OpOnlyCreation && !client.HasRoleCapabs("chanreg") {
  115. return nil, errInsufficientPrivs
  116. }
  117. // enforce confusables
  118. if !registered && (cm.chansSkeletons.Has(skeleton) || cm.registeredSkeletons.Has(skeleton)) {
  119. return nil, errConfusableIdentifier
  120. }
  121. entry = &channelManagerEntry{
  122. channel: NewChannel(server, name, casefoldedName, registered),
  123. pendingJoins: 0,
  124. }
  125. if !registered {
  126. // for an unregistered channel, we already have the correct unfolded name
  127. // and therefore the final skeleton. for a registered channel, we don't have
  128. // the unfolded name yet (it needs to be loaded from the db), but we already
  129. // have the final skeleton in `registeredSkeletons` so we don't need to track it
  130. cm.chansSkeletons.Add(skeleton)
  131. entry.skeleton = skeleton
  132. }
  133. cm.chans[casefoldedName] = entry
  134. }
  135. entry.pendingJoins += 1
  136. return entry.channel, nil
  137. }()
  138. if err != nil {
  139. return err, ""
  140. }
  141. channel.EnsureLoaded()
  142. err, forward = channel.Join(client, key, isSajoin, rb)
  143. cm.maybeCleanup(channel, true)
  144. return
  145. }
  146. func (cm *ChannelManager) maybeCleanup(channel *Channel, afterJoin bool) {
  147. cm.Lock()
  148. defer cm.Unlock()
  149. cfname := channel.NameCasefolded()
  150. entry := cm.chans[cfname]
  151. if entry == nil || entry.channel != channel {
  152. return
  153. }
  154. if afterJoin {
  155. entry.pendingJoins -= 1
  156. }
  157. if entry.pendingJoins == 0 && entry.channel.IsClean() {
  158. delete(cm.chans, cfname)
  159. if entry.skeleton != "" {
  160. delete(cm.chansSkeletons, entry.skeleton)
  161. }
  162. }
  163. }
  164. // Part parts `client` from the channel named `name`, deleting it if it's empty.
  165. func (cm *ChannelManager) Part(client *Client, name string, message string, rb *ResponseBuffer) error {
  166. var channel *Channel
  167. casefoldedName, err := CasefoldChannel(name)
  168. if err != nil {
  169. return errNoSuchChannel
  170. }
  171. cm.RLock()
  172. entry := cm.chans[casefoldedName]
  173. if entry != nil {
  174. channel = entry.channel
  175. }
  176. cm.RUnlock()
  177. if channel == nil {
  178. return errNoSuchChannel
  179. }
  180. channel.Part(client, message, rb)
  181. return nil
  182. }
  183. func (cm *ChannelManager) Cleanup(channel *Channel) {
  184. cm.maybeCleanup(channel, false)
  185. }
  186. func (cm *ChannelManager) SetRegistered(channelName string, account string) (err error) {
  187. if cm.server.Defcon() <= 4 {
  188. return errFeatureDisabled
  189. }
  190. var channel *Channel
  191. cfname, err := CasefoldChannel(channelName)
  192. if err != nil {
  193. return err
  194. }
  195. var entry *channelManagerEntry
  196. defer func() {
  197. if err == nil && channel != nil {
  198. // registration was successful: make the database reflect it
  199. err = channel.Store(IncludeAllAttrs)
  200. }
  201. }()
  202. cm.Lock()
  203. defer cm.Unlock()
  204. entry = cm.chans[cfname]
  205. if entry == nil {
  206. return errNoSuchChannel
  207. }
  208. channel = entry.channel
  209. err = channel.SetRegistered(account)
  210. if err != nil {
  211. return err
  212. }
  213. // transfer the skeleton from chansSkeletons to registeredSkeletons
  214. skeleton := entry.skeleton
  215. delete(cm.chansSkeletons, skeleton)
  216. entry.skeleton = ""
  217. cm.chans[cfname] = entry
  218. cm.registeredChannels.Add(cfname)
  219. cm.registeredSkeletons.Add(skeleton)
  220. return nil
  221. }
  222. func (cm *ChannelManager) SetUnregistered(channelName string, account string) (err error) {
  223. cfname, err := CasefoldChannel(channelName)
  224. if err != nil {
  225. return err
  226. }
  227. info, err := cm.server.channelRegistry.LoadChannel(cfname)
  228. if err != nil {
  229. return err
  230. }
  231. if info.Founder != account {
  232. return errChannelNotOwnedByAccount
  233. }
  234. defer func() {
  235. if err == nil {
  236. err = cm.server.channelRegistry.Delete(info)
  237. }
  238. }()
  239. cm.Lock()
  240. defer cm.Unlock()
  241. entry := cm.chans[cfname]
  242. if entry != nil {
  243. entry.channel.SetUnregistered(account)
  244. delete(cm.registeredChannels, cfname)
  245. // transfer the skeleton from registeredSkeletons to chansSkeletons
  246. if skel, err := Skeleton(entry.channel.Name()); err == nil {
  247. delete(cm.registeredSkeletons, skel)
  248. cm.chansSkeletons.Add(skel)
  249. entry.skeleton = skel
  250. cm.chans[cfname] = entry
  251. }
  252. }
  253. return nil
  254. }
  255. // Rename renames a channel (but does not notify the members)
  256. func (cm *ChannelManager) Rename(name string, newName string) (err error) {
  257. oldCfname, err := CasefoldChannel(name)
  258. if err != nil {
  259. return errNoSuchChannel
  260. }
  261. newCfname, err := CasefoldChannel(newName)
  262. if err != nil {
  263. return errInvalidChannelName
  264. }
  265. newSkeleton, err := Skeleton(newName)
  266. if err != nil {
  267. return errInvalidChannelName
  268. }
  269. var channel *Channel
  270. var info RegisteredChannel
  271. defer func() {
  272. if channel != nil && info.Founder != "" {
  273. channel.Store(IncludeAllAttrs)
  274. // we just flushed the channel under its new name, therefore this delete
  275. // cannot be overwritten by a write to the old name:
  276. cm.server.channelRegistry.Delete(info)
  277. }
  278. }()
  279. cm.Lock()
  280. defer cm.Unlock()
  281. entry := cm.chans[oldCfname]
  282. if entry == nil || !entry.channel.IsLoaded() {
  283. return errNoSuchChannel
  284. }
  285. channel = entry.channel
  286. info = channel.ExportRegistration(IncludeInitial)
  287. registered := info.Founder != ""
  288. oldSkeleton, err := Skeleton(info.Name)
  289. if err != nil {
  290. return errNoSuchChannel // ugh
  291. }
  292. if newCfname != oldCfname {
  293. if cm.chans[newCfname] != nil || cm.registeredChannels.Has(newCfname) {
  294. return errChannelNameInUse
  295. }
  296. }
  297. if oldSkeleton != newSkeleton {
  298. if cm.chansSkeletons.Has(newSkeleton) || cm.registeredSkeletons.Has(newSkeleton) {
  299. return errConfusableIdentifier
  300. }
  301. }
  302. delete(cm.chans, oldCfname)
  303. if !registered {
  304. entry.skeleton = newSkeleton
  305. }
  306. cm.chans[newCfname] = entry
  307. if registered {
  308. delete(cm.registeredChannels, oldCfname)
  309. cm.registeredChannels.Add(newCfname)
  310. delete(cm.registeredSkeletons, oldSkeleton)
  311. cm.registeredSkeletons.Add(newSkeleton)
  312. } else {
  313. delete(cm.chansSkeletons, oldSkeleton)
  314. cm.chansSkeletons.Add(newSkeleton)
  315. }
  316. entry.channel.Rename(newName, newCfname)
  317. return nil
  318. }
  319. // Len returns the number of channels
  320. func (cm *ChannelManager) Len() int {
  321. cm.RLock()
  322. defer cm.RUnlock()
  323. return len(cm.chans)
  324. }
  325. // Channels returns a slice containing all current channels
  326. func (cm *ChannelManager) Channels() (result []*Channel) {
  327. cm.RLock()
  328. defer cm.RUnlock()
  329. result = make([]*Channel, 0, len(cm.chans))
  330. for _, entry := range cm.chans {
  331. if entry.channel.IsLoaded() {
  332. result = append(result, entry.channel)
  333. }
  334. }
  335. return
  336. }
  337. // Purge marks a channel as purged.
  338. func (cm *ChannelManager) Purge(chname string, record ChannelPurgeRecord) (err error) {
  339. chname, err = CasefoldChannel(chname)
  340. if err != nil {
  341. return errInvalidChannelName
  342. }
  343. skel, err := Skeleton(chname)
  344. if err != nil {
  345. return errInvalidChannelName
  346. }
  347. cm.Lock()
  348. cm.purgedChannels.Add(chname)
  349. entry := cm.chans[chname]
  350. if entry != nil {
  351. delete(cm.chans, chname)
  352. if entry.channel.Founder() != "" {
  353. delete(cm.registeredSkeletons, skel)
  354. } else {
  355. delete(cm.chansSkeletons, skel)
  356. }
  357. }
  358. cm.Unlock()
  359. cm.server.channelRegistry.PurgeChannel(chname, record)
  360. if entry != nil {
  361. entry.channel.Purge("")
  362. }
  363. return nil
  364. }
  365. // IsPurged queries whether a channel is purged.
  366. func (cm *ChannelManager) IsPurged(chname string) (result bool) {
  367. chname, err := CasefoldChannel(chname)
  368. if err != nil {
  369. return false
  370. }
  371. cm.RLock()
  372. result = cm.purgedChannels.Has(chname)
  373. cm.RUnlock()
  374. return
  375. }
  376. // Unpurge deletes a channel's purged status.
  377. func (cm *ChannelManager) Unpurge(chname string) (err error) {
  378. chname, err = CasefoldChannel(chname)
  379. if err != nil {
  380. return errNoSuchChannel
  381. }
  382. cm.Lock()
  383. found := cm.purgedChannels.Has(chname)
  384. delete(cm.purgedChannels, chname)
  385. cm.Unlock()
  386. cm.server.channelRegistry.UnpurgeChannel(chname)
  387. if !found {
  388. return errNoSuchChannel
  389. }
  390. return nil
  391. }
  392. func (cm *ChannelManager) ListPurged() (result []string) {
  393. cm.RLock()
  394. result = make([]string, 0, len(cm.purgedChannels))
  395. for c := range cm.purgedChannels {
  396. result = append(result, c)
  397. }
  398. cm.RUnlock()
  399. sort.Strings(result)
  400. return
  401. }