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

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