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.

types.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2014-2015 Edmund Huber
  3. // Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
  4. // released under the MIT license
  5. package irc
  6. import (
  7. "fmt"
  8. "strings"
  9. "sync"
  10. )
  11. // ChannelNameMap is a map that converts channel names to actual channel objects.
  12. type ChannelNameMap struct {
  13. ChansLock sync.RWMutex
  14. Chans map[string]*Channel
  15. }
  16. // NewChannelNameMap returns a new ChannelNameMap.
  17. func NewChannelNameMap() *ChannelNameMap {
  18. var channels ChannelNameMap
  19. channels.Chans = make(map[string]*Channel)
  20. return &channels
  21. }
  22. // Get returns the given channel if it exists.
  23. func (channels *ChannelNameMap) Get(name string) *Channel {
  24. name, err := CasefoldChannel(name)
  25. if err == nil {
  26. channels.ChansLock.RLock()
  27. defer channels.ChansLock.RUnlock()
  28. return channels.Chans[name]
  29. }
  30. return nil
  31. }
  32. // Add adds the given channel to our map.
  33. func (channels *ChannelNameMap) Add(channel *Channel) error {
  34. channels.ChansLock.Lock()
  35. defer channels.ChansLock.Unlock()
  36. if channels.Chans[channel.nameCasefolded] != nil {
  37. return fmt.Errorf("%s: already set", channel.name)
  38. }
  39. channels.Chans[channel.nameCasefolded] = channel
  40. return nil
  41. }
  42. // Remove removes the given channel from our map.
  43. func (channels *ChannelNameMap) Remove(channel *Channel) error {
  44. channels.ChansLock.Lock()
  45. defer channels.ChansLock.Unlock()
  46. if channel != channels.Chans[channel.nameCasefolded] {
  47. return fmt.Errorf("%s: mismatch", channel.name)
  48. }
  49. delete(channels.Chans, channel.nameCasefolded)
  50. return nil
  51. }
  52. // Len returns how many channels we have.
  53. func (channels *ChannelNameMap) Len() int {
  54. channels.ChansLock.RLock()
  55. defer channels.ChansLock.RUnlock()
  56. return len(channels.Chans)
  57. }
  58. // ModeSet holds a set of modes.
  59. type ModeSet map[Mode]bool
  60. // String returns the modes in this set.
  61. func (set ModeSet) String() string {
  62. if len(set) == 0 {
  63. return ""
  64. }
  65. strs := make([]string, len(set))
  66. index := 0
  67. for mode := range set {
  68. strs[index] = mode.String()
  69. index++
  70. }
  71. return strings.Join(strs, "")
  72. }
  73. // ClientSet is a set of clients.
  74. type ClientSet map[*Client]bool
  75. // Add adds the given client to this set.
  76. func (clients ClientSet) Add(client *Client) {
  77. clients[client] = true
  78. }
  79. // Remove removes the given client from this set.
  80. func (clients ClientSet) Remove(client *Client) {
  81. delete(clients, client)
  82. }
  83. // Has returns true if the given client is in this set.
  84. func (clients ClientSet) Has(client *Client) bool {
  85. return clients[client]
  86. }
  87. // MemberSet is a set of members with modes.
  88. type MemberSet map[*Client]ModeSet
  89. // Add adds the given client to this set.
  90. func (members MemberSet) Add(member *Client) {
  91. members[member] = make(ModeSet)
  92. }
  93. // Remove removes the given client from this set.
  94. func (members MemberSet) Remove(member *Client) {
  95. delete(members, member)
  96. }
  97. // Has returns true if the given client is in this set.
  98. func (members MemberSet) Has(member *Client) bool {
  99. _, ok := members[member]
  100. return ok
  101. }
  102. // HasMode returns true if the given client is in this set with the given mode.
  103. func (members MemberSet) HasMode(member *Client, mode Mode) bool {
  104. modes, ok := members[member]
  105. if !ok {
  106. return false
  107. }
  108. return modes[mode]
  109. }
  110. // AnyHasMode returns true if any of our clients has the given mode.
  111. func (members MemberSet) AnyHasMode(mode Mode) bool {
  112. for _, modes := range members {
  113. if modes[mode] {
  114. return true
  115. }
  116. }
  117. return false
  118. }
  119. // ChannelSet is a set of channels.
  120. type ChannelSet map[*Channel]bool
  121. // Add adds the given channel to this set.
  122. func (channels ChannelSet) Add(channel *Channel) {
  123. channels[channel] = true
  124. }
  125. // Remove removes the given channel from this set.
  126. func (channels ChannelSet) Remove(channel *Channel) {
  127. delete(channels, channel)
  128. }