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