Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

whowas.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
  3. // released under the MIT license
  4. package irc
  5. import (
  6. "sync"
  7. )
  8. // WhoWasList holds our list of prior clients (for use with the WHOWAS command).
  9. type WhoWasList struct {
  10. buffer []WhoWas
  11. // three possible states:
  12. // empty: start == end == -1
  13. // partially full: start != end
  14. // full: start == end > 0
  15. // if entries exist, they go from `start` to `(end - 1) % length`
  16. start int
  17. end int
  18. accessMutex sync.RWMutex // tier 1
  19. }
  20. // WhoWas is an entry in the WhoWasList.
  21. type WhoWas struct {
  22. nicknameCasefolded string
  23. nickname string
  24. username string
  25. hostname string
  26. realname string
  27. }
  28. // NewWhoWasList returns a new WhoWasList
  29. func NewWhoWasList(size uint) *WhoWasList {
  30. return &WhoWasList{
  31. buffer: make([]WhoWas, size),
  32. start: -1,
  33. end: -1,
  34. }
  35. }
  36. // Append adds an entry to the WhoWasList.
  37. func (list *WhoWasList) Append(whowas WhoWas) {
  38. list.accessMutex.Lock()
  39. defer list.accessMutex.Unlock()
  40. if len(list.buffer) == 0 {
  41. return
  42. }
  43. var pos int
  44. if list.start == -1 { // empty
  45. pos = 0
  46. list.start = 0
  47. list.end = 1
  48. } else if list.start != list.end { // partially full
  49. pos = list.end
  50. list.end = (list.end + 1) % len(list.buffer)
  51. } else if list.start == list.end { // full
  52. pos = list.end
  53. list.end = (list.end + 1) % len(list.buffer)
  54. list.start = list.end // advance start as well, overwriting first entry
  55. }
  56. list.buffer[pos] = whowas
  57. }
  58. // Find tries to find an entry in our WhoWasList with the given details.
  59. func (list *WhoWasList) Find(nickname string, limit int) (results []WhoWas) {
  60. casefoldedNickname, err := CasefoldName(nickname)
  61. if err != nil {
  62. return
  63. }
  64. list.accessMutex.RLock()
  65. defer list.accessMutex.RUnlock()
  66. if list.start == -1 {
  67. return
  68. }
  69. // iterate backwards through the ring buffer
  70. pos := list.prev(list.end)
  71. for limit == 0 || len(results) < limit {
  72. if casefoldedNickname == list.buffer[pos].nicknameCasefolded {
  73. results = append(results, list.buffer[pos])
  74. }
  75. if pos == list.start {
  76. break
  77. }
  78. pos = list.prev(pos)
  79. }
  80. return
  81. }
  82. func (list *WhoWasList) prev(index int) int {
  83. switch index {
  84. case 0:
  85. return len(list.buffer) - 1
  86. default:
  87. return index - 1
  88. }
  89. }