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ů.

responsebuffer.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "runtime/debug"
  6. "time"
  7. "github.com/goshuirc/irc-go/ircmsg"
  8. "github.com/oragono/oragono/irc/caps"
  9. "github.com/oragono/oragono/irc/utils"
  10. )
  11. const (
  12. // https://ircv3.net/specs/extensions/labeled-response.html
  13. defaultBatchType = "draft/labeled-response"
  14. )
  15. // ResponseBuffer - put simply - buffers messages and then outputs them to a given client.
  16. //
  17. // Using a ResponseBuffer lets you really easily implement labeled-response, since the
  18. // buffer will silently create a batch if required and label the outgoing messages as
  19. // necessary (or leave it off and simply tag the outgoing message).
  20. type ResponseBuffer struct {
  21. Label string
  22. batchID string
  23. target *Client
  24. messages []ircmsg.IrcMessage
  25. finalized bool
  26. }
  27. // GetLabel returns the label from the given message.
  28. func GetLabel(msg ircmsg.IrcMessage) string {
  29. return msg.Tags[caps.LabelTagName].Value
  30. }
  31. // NewResponseBuffer returns a new ResponseBuffer.
  32. func NewResponseBuffer(target *Client) *ResponseBuffer {
  33. return &ResponseBuffer{
  34. target: target,
  35. }
  36. }
  37. // Add adds a standard new message to our queue.
  38. func (rb *ResponseBuffer) Add(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) {
  39. if rb.finalized {
  40. rb.target.server.logger.Error("internal", "message added to finalized ResponseBuffer, undefined behavior")
  41. debug.PrintStack()
  42. // TODO(dan): send a NOTICE to the end user with a string representation of the message,
  43. // for debugging purposes
  44. return
  45. }
  46. message := ircmsg.MakeMessage(tags, prefix, command, params...)
  47. rb.messages = append(rb.messages, message)
  48. }
  49. // AddFromClient adds a new message from a specific client to our queue.
  50. func (rb *ResponseBuffer) AddFromClient(msgid string, fromNickMask string, fromAccount string, tags *map[string]ircmsg.TagValue, command string, params ...string) {
  51. // attach account-tag
  52. if rb.target.capabilities.Has(caps.AccountTag) {
  53. if fromAccount != "*" {
  54. tags = ensureTag(tags, "account", fromAccount)
  55. }
  56. }
  57. // attach message-id
  58. if len(msgid) > 0 && rb.target.capabilities.Has(caps.MessageTags) {
  59. tags = ensureTag(tags, "draft/msgid", msgid)
  60. }
  61. rb.Add(tags, fromNickMask, command, params...)
  62. }
  63. // AddSplitMessageFromClient adds a new split message from a specific client to our queue.
  64. func (rb *ResponseBuffer) AddSplitMessageFromClient(msgid string, fromNickMask string, fromAccount string, tags *map[string]ircmsg.TagValue, command string, target string, message utils.SplitMessage) {
  65. if rb.target.capabilities.Has(caps.MaxLine) || message.Wrapped == nil {
  66. rb.AddFromClient(msgid, fromNickMask, fromAccount, tags, command, target, message.Original)
  67. } else {
  68. for _, str := range message.Wrapped {
  69. rb.AddFromClient(msgid, fromNickMask, fromAccount, tags, command, target, str)
  70. }
  71. }
  72. }
  73. // InitializeBatch forcibly starts a batch of batch `batchType`.
  74. // Normally, Send/Flush will decide automatically whether to start a batch
  75. // of type draft/labeled-response. This allows changing the batch type
  76. // and forcing the creation of a possibly empty batch.
  77. func (rb *ResponseBuffer) InitializeBatch(batchType string, blocking bool) {
  78. rb.sendBatchStart(batchType, blocking)
  79. }
  80. func (rb *ResponseBuffer) sendBatchStart(batchType string, blocking bool) {
  81. if rb.batchID != "" {
  82. // batch already initialized
  83. return
  84. }
  85. // formerly this combined time.Now.UnixNano() in base 36 with an incrementing counter,
  86. // also in base 36. but let's just use a uuidv4-alike (26 base32 characters):
  87. rb.batchID = utils.GenerateSecretToken()
  88. message := ircmsg.MakeMessage(nil, rb.target.server.name, "BATCH", "+"+rb.batchID, batchType)
  89. if rb.Label != "" {
  90. message.Tags[caps.LabelTagName] = ircmsg.MakeTagValue(rb.Label)
  91. }
  92. rb.target.SendRawMessage(message, blocking)
  93. }
  94. func (rb *ResponseBuffer) sendBatchEnd(blocking bool) {
  95. if rb.batchID == "" {
  96. // we are not sending a batch, skip this
  97. return
  98. }
  99. message := ircmsg.MakeMessage(nil, rb.target.server.name, "BATCH", "-"+rb.batchID)
  100. rb.target.SendRawMessage(message, blocking)
  101. }
  102. // Send sends all messages in the buffer to the client.
  103. // Afterwards, the buffer is in an undefined state and MUST NOT be used further.
  104. // If `blocking` is true you MUST be sending to the client from its own goroutine.
  105. func (rb *ResponseBuffer) Send(blocking bool) error {
  106. return rb.flushInternal(true, blocking)
  107. }
  108. // Flush sends all messages in the buffer to the client.
  109. // Afterwards, the buffer can still be used. Client code MUST subsequently call Send()
  110. // to ensure that the final `BATCH -` message is sent.
  111. // If `blocking` is true you MUST be sending to the client from its own goroutine.
  112. func (rb *ResponseBuffer) Flush(blocking bool) error {
  113. return rb.flushInternal(false, blocking)
  114. }
  115. // flushInternal sends the contents of the buffer, either blocking or nonblocking
  116. // It sends the `BATCH +` message if the client supports it and it hasn't been sent already.
  117. // If `final` is true, it also sends `BATCH -` (if necessary).
  118. func (rb *ResponseBuffer) flushInternal(final bool, blocking bool) error {
  119. if rb.finalized {
  120. return nil
  121. }
  122. useLabel := rb.target.capabilities.Has(caps.LabeledResponse) && rb.Label != ""
  123. // use a batch if we have a label, and we either currently have 0 or 2+ messages,
  124. // or we are doing a Flush() and we have to assume that there will be more messages
  125. // in the future.
  126. useBatch := useLabel && (len(rb.messages) != 1 || !final)
  127. // if label but no batch, add label to first message
  128. if useLabel && !useBatch && len(rb.messages) == 1 && rb.batchID == "" {
  129. rb.messages[0].Tags[caps.LabelTagName] = ircmsg.MakeTagValue(rb.Label)
  130. } else if useBatch {
  131. rb.sendBatchStart(defaultBatchType, blocking)
  132. }
  133. // send each message out
  134. for _, message := range rb.messages {
  135. // attach server-time if needed
  136. if rb.target.capabilities.Has(caps.ServerTime) {
  137. if !message.Tags["time"].HasValue {
  138. t := time.Now().UTC().Format(IRCv3TimestampFormat)
  139. message.Tags["time"] = ircmsg.MakeTagValue(t)
  140. }
  141. }
  142. // attach batch ID
  143. if rb.batchID != "" {
  144. message.Tags["batch"] = ircmsg.MakeTagValue(rb.batchID)
  145. }
  146. // send message out
  147. rb.target.SendRawMessage(message, blocking)
  148. }
  149. // end batch if required
  150. if final {
  151. rb.sendBatchEnd(blocking)
  152. rb.finalized = true
  153. }
  154. // clear out any existing messages
  155. rb.messages = rb.messages[:0]
  156. return nil
  157. }
  158. // Notice sends the client the given notice from the server.
  159. func (rb *ResponseBuffer) Notice(text string) {
  160. rb.Add(nil, rb.target.server.name, "NOTICE", rb.target.nick, text)
  161. }