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.

message_cache.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (c) 2020 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "time"
  6. "github.com/ergochat/irc-go/ircmsg"
  7. "github.com/ergochat/ergo/irc/caps"
  8. "github.com/ergochat/ergo/irc/utils"
  9. )
  10. // MessageCache caches serialized IRC messages.
  11. // First call Initialize or InitializeSplitMessage, which records
  12. // the parameters and builds the cache. Then call Send, which will
  13. // either send a cached version of the message or dispatch to another
  14. // send routine that can synthesize the necessary version on the fly.
  15. type MessageCache struct {
  16. // these cache a single-line message (e.g., JOIN, or PRIVMSG with a 512-byte message)
  17. // one version is "plain" (legacy clients with no tags) and one is "full" (client has
  18. // the message-tags cap)
  19. plain []byte
  20. fullTags []byte
  21. // these cache a multiline message (a PRIVMSG that was sent as a multiline batch)
  22. // one version is "plain" (legacy clients with no tags) and one is "full" (client has
  23. // the multiline cap)
  24. plainMultiline [][]byte
  25. fullTagsMultiline [][]byte
  26. time time.Time
  27. msgid string
  28. accountName string
  29. tags map[string]string
  30. source string
  31. command string
  32. isBot bool
  33. params []string
  34. target string
  35. splitMessage utils.SplitMessage
  36. }
  37. func addAllTags(msg *ircmsg.Message, tags map[string]string, serverTime time.Time, msgid, accountName string, isBot bool) {
  38. msg.UpdateTags(tags)
  39. msg.SetTag("time", serverTime.Format(IRCv3TimestampFormat))
  40. if accountName != "*" {
  41. msg.SetTag("account", accountName)
  42. }
  43. if msgid != "" {
  44. msg.SetTag("msgid", msgid)
  45. }
  46. if isBot {
  47. msg.SetTag(caps.BotTagName, "")
  48. }
  49. }
  50. func (m *MessageCache) handleErr(server *Server, err error) bool {
  51. if !(err == nil || err == ircmsg.ErrorBodyTooLong) {
  52. server.logger.Error("internal", "Error assembling message for sending", err.Error())
  53. // blank these out so Send will be a no-op
  54. m.fullTags = nil
  55. m.fullTagsMultiline = nil
  56. return true
  57. }
  58. return false
  59. }
  60. func (m *MessageCache) Initialize(server *Server, serverTime time.Time, msgid string, nickmask, accountName string, isBot bool, tags map[string]string, command string, params ...string) (err error) {
  61. m.time = serverTime
  62. m.msgid = msgid
  63. m.source = nickmask
  64. m.accountName = accountName
  65. m.isBot = isBot
  66. m.tags = tags
  67. m.command = command
  68. m.params = params
  69. var msg ircmsg.Message
  70. if forceTrailing(server.Config(), command) {
  71. msg.ForceTrailing()
  72. }
  73. msg.Source = nickmask
  74. msg.Command = command
  75. msg.Params = make([]string, len(params))
  76. copy(msg.Params, params)
  77. m.plain, err = msg.LineBytesStrict(false, MaxLineLen)
  78. if m.handleErr(server, err) {
  79. return
  80. }
  81. addAllTags(&msg, tags, serverTime, msgid, accountName, isBot)
  82. m.fullTags, err = msg.LineBytesStrict(false, MaxLineLen)
  83. if m.handleErr(server, err) {
  84. return
  85. }
  86. return
  87. }
  88. func (m *MessageCache) InitializeSplitMessage(server *Server, nickmask, accountName string, isBot bool, tags map[string]string, command, target string, message utils.SplitMessage) (err error) {
  89. m.time = message.Time
  90. m.msgid = message.Msgid
  91. m.source = nickmask
  92. m.accountName = accountName
  93. m.isBot = isBot
  94. m.tags = tags
  95. m.command = command
  96. m.target = target
  97. m.splitMessage = message
  98. forceTrailing := forceTrailing(server.Config(), command)
  99. if message.Is512() {
  100. isTagmsg := command == "TAGMSG"
  101. var msg ircmsg.Message
  102. if forceTrailing {
  103. msg.ForceTrailing()
  104. }
  105. msg.Source = nickmask
  106. msg.Command = command
  107. if isTagmsg {
  108. msg.Params = []string{target}
  109. } else {
  110. msg.Params = []string{target, message.Message}
  111. }
  112. m.params = msg.Params
  113. if !isTagmsg {
  114. m.plain, err = msg.LineBytesStrict(false, MaxLineLen)
  115. if m.handleErr(server, err) {
  116. return
  117. }
  118. }
  119. addAllTags(&msg, tags, message.Time, message.Msgid, accountName, isBot)
  120. m.fullTags, err = msg.LineBytesStrict(false, MaxLineLen)
  121. if m.handleErr(server, err) {
  122. return
  123. }
  124. } else {
  125. var msg ircmsg.Message
  126. if forceTrailing {
  127. msg.ForceTrailing()
  128. }
  129. msg.Source = nickmask
  130. msg.Command = command
  131. msg.Params = make([]string, 2)
  132. msg.Params[0] = target
  133. m.plainMultiline = make([][]byte, len(message.Split))
  134. for i, pair := range message.Split {
  135. msg.Params[1] = pair.Message
  136. m.plainMultiline[i], err = msg.LineBytesStrict(false, MaxLineLen)
  137. if m.handleErr(server, err) {
  138. return
  139. }
  140. }
  141. // we need to send the same batch ID to all recipient sessions;
  142. // ensure it doesn't collide. a half-sized token has 64 bits of entropy,
  143. // so a collision isn't expected until there are on the order of 2**32
  144. // concurrent batches being relayed:
  145. batchID := utils.GenerateSecretToken()[:utils.SecretTokenLength/2]
  146. batch := composeMultilineBatch(batchID, nickmask, accountName, isBot, tags, command, target, message)
  147. m.fullTagsMultiline = make([][]byte, len(batch))
  148. for i, msg := range batch {
  149. if forceTrailing {
  150. msg.ForceTrailing()
  151. }
  152. m.fullTagsMultiline[i], err = msg.LineBytesStrict(false, MaxLineLen)
  153. if m.handleErr(server, err) {
  154. return
  155. }
  156. }
  157. }
  158. return
  159. }
  160. func (m *MessageCache) Send(session *Session) {
  161. if m.fullTags != nil {
  162. // Initialize() path:
  163. if session.capabilities.Has(caps.MessageTags) {
  164. session.sendBytes(m.fullTags, false)
  165. } else if m.plain != nil {
  166. // plain == nil indicates a TAGMSG
  167. if !(session.capabilities.Has(caps.ServerTime) || session.capabilities.Has(caps.AccountTag)) {
  168. session.sendBytes(m.plain, false)
  169. } else {
  170. // slowpath
  171. session.sendFromClientInternal(false, m.time, m.msgid, m.source, m.accountName, m.isBot, nil, m.command, m.params...)
  172. }
  173. }
  174. } else if m.fullTagsMultiline != nil {
  175. // InitializeSplitMessage() path:
  176. if session.capabilities.Has(caps.Multiline) {
  177. for _, line := range m.fullTagsMultiline {
  178. session.sendBytes(line, false)
  179. }
  180. } else if !(session.capabilities.Has(caps.ServerTime) || session.capabilities.Has(caps.AccountTag)) {
  181. for _, line := range m.plainMultiline {
  182. session.sendBytes(line, false)
  183. }
  184. } else {
  185. // slowpath
  186. session.sendSplitMsgFromClientInternal(false, m.source, m.accountName, m.isBot, m.tags, m.command, m.target, m.splitMessage)
  187. }
  188. }
  189. }