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.

snomanager.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package irc
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/ergochat/ergo/irc/sno"
  6. "github.com/ergochat/irc-go/ircfmt"
  7. )
  8. // SnoManager keeps track of which clients to send snomasks to.
  9. type SnoManager struct {
  10. sendListMutex sync.RWMutex // tier 2
  11. sendLists map[sno.Mask]map[*Client]bool
  12. }
  13. func (m *SnoManager) Initialize() {
  14. m.sendLists = make(map[sno.Mask]map[*Client]bool)
  15. }
  16. // AddMasks adds the given snomasks to the client.
  17. func (m *SnoManager) AddMasks(client *Client, masks ...sno.Mask) {
  18. m.sendListMutex.Lock()
  19. defer m.sendListMutex.Unlock()
  20. for _, mask := range masks {
  21. currentClientList := m.sendLists[mask]
  22. if currentClientList == nil {
  23. currentClientList = map[*Client]bool{}
  24. }
  25. currentClientList[client] = true
  26. m.sendLists[mask] = currentClientList
  27. }
  28. }
  29. // RemoveMasks removes the given snomasks from the client.
  30. func (m *SnoManager) RemoveMasks(client *Client, masks ...sno.Mask) {
  31. m.sendListMutex.Lock()
  32. defer m.sendListMutex.Unlock()
  33. for _, mask := range masks {
  34. currentClientList := m.sendLists[mask]
  35. if len(currentClientList) == 0 {
  36. continue
  37. }
  38. delete(currentClientList, client)
  39. m.sendLists[mask] = currentClientList
  40. }
  41. }
  42. // RemoveClient removes the given client from all of our lists.
  43. func (m *SnoManager) RemoveClient(client *Client) {
  44. m.sendListMutex.Lock()
  45. defer m.sendListMutex.Unlock()
  46. for mask := range m.sendLists {
  47. currentClientList := m.sendLists[mask]
  48. if len(currentClientList) == 0 {
  49. continue
  50. }
  51. delete(currentClientList, client)
  52. m.sendLists[mask] = currentClientList
  53. }
  54. }
  55. // Send sends the given snomask to all users signed up for it.
  56. func (m *SnoManager) Send(mask sno.Mask, content string) {
  57. m.sendListMutex.RLock()
  58. defer m.sendListMutex.RUnlock()
  59. currentClientList := m.sendLists[mask]
  60. if len(currentClientList) == 0 {
  61. return
  62. }
  63. // make the message
  64. name := sno.NoticeMaskNames[mask]
  65. if name == "" {
  66. name = string(mask)
  67. }
  68. message := fmt.Sprintf(ircfmt.Unescape("$c[grey]-$r%s$c[grey]-$c %s"), name, content)
  69. // send it out
  70. for client := range currentClientList {
  71. client.Notice(message)
  72. }
  73. }
  74. // MasksEnabled returns the snomasks currently enabled.
  75. func (m *SnoManager) MasksEnabled(client *Client) (result sno.Masks) {
  76. m.sendListMutex.RLock()
  77. defer m.sendListMutex.RUnlock()
  78. for mask, clients := range m.sendLists {
  79. for c := range clients {
  80. if c == client {
  81. result = append(result, mask)
  82. break
  83. }
  84. }
  85. }
  86. result.Sort()
  87. return
  88. }
  89. func (m *SnoManager) String(client *Client) string {
  90. masks := m.MasksEnabled(client)
  91. return masks.String()
  92. }