Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

snomanager.go 2.6KB

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