Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/goshuirc/irc-go/ircmsg"
  7. "github.com/oragono/oragono/irc/caps"
  8. "github.com/oragono/oragono/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. params []string
  33. target string
  34. splitMessage utils.SplitMessage
  35. }
  36. func addAllTags(msg *ircmsg.IRCMessage, tags map[string]string, serverTime time.Time, msgid, accountName string) {
  37. msg.UpdateTags(tags)
  38. msg.SetTag("time", serverTime.Format(IRCv3TimestampFormat))
  39. if accountName != "*" {
  40. msg.SetTag("account", accountName)
  41. }
  42. if msgid != "" {
  43. msg.SetTag("msgid", msgid)
  44. }
  45. }
  46. func (m *MessageCache) handleErr(server *Server, err error) bool {
  47. if !(err == nil || err == ircmsg.ErrorBodyTooLong) {
  48. server.logger.Error("internal", "Error assembling message for sending", err.Error())
  49. // blank these out so Send will be a no-op
  50. m.fullTags = nil
  51. m.fullTagsMultiline = nil
  52. return true
  53. }
  54. return false
  55. }
  56. func (m *MessageCache) Initialize(server *Server, serverTime time.Time, msgid string, nickmask, accountName string, tags map[string]string, command string, params ...string) (err error) {
  57. m.time = serverTime
  58. m.msgid = msgid
  59. m.source = nickmask
  60. m.accountName = accountName
  61. m.tags = tags
  62. m.command = command
  63. m.params = params
  64. var msg ircmsg.IRCMessage
  65. config := server.Config()
  66. if config.Server.Compatibility.forceTrailing && commandsThatMustUseTrailing[command] {
  67. msg.ForceTrailing()
  68. }
  69. msg.Prefix = nickmask
  70. msg.Command = command
  71. msg.Params = make([]string, len(params))
  72. copy(msg.Params, params)
  73. m.plain, err = msg.LineBytesStrict(false, MaxLineLen)
  74. if m.handleErr(server, err) {
  75. return
  76. }
  77. addAllTags(&msg, tags, serverTime, msgid, accountName)
  78. m.fullTags, err = msg.LineBytesStrict(false, MaxLineLen)
  79. if m.handleErr(server, err) {
  80. return
  81. }
  82. return
  83. }
  84. func (m *MessageCache) InitializeSplitMessage(server *Server, nickmask, accountName string, tags map[string]string, command, target string, message utils.SplitMessage) (err error) {
  85. m.time = message.Time
  86. m.msgid = message.Msgid
  87. m.source = nickmask
  88. m.accountName = accountName
  89. m.tags = tags
  90. m.command = command
  91. m.target = target
  92. m.splitMessage = message
  93. config := server.Config()
  94. forceTrailing := config.Server.Compatibility.forceTrailing && commandsThatMustUseTrailing[command]
  95. if message.Is512() {
  96. isTagmsg := command == "TAGMSG"
  97. var msg ircmsg.IRCMessage
  98. if forceTrailing {
  99. msg.ForceTrailing()
  100. }
  101. msg.Prefix = nickmask
  102. msg.Command = command
  103. if isTagmsg {
  104. msg.Params = []string{target}
  105. } else {
  106. msg.Params = []string{target, message.Message}
  107. }
  108. m.params = msg.Params
  109. if !isTagmsg {
  110. m.plain, err = msg.LineBytesStrict(false, MaxLineLen)
  111. if m.handleErr(server, err) {
  112. return
  113. }
  114. }
  115. addAllTags(&msg, tags, message.Time, message.Msgid, accountName)
  116. m.fullTags, err = msg.LineBytesStrict(false, MaxLineLen)
  117. if m.handleErr(server, err) {
  118. return
  119. }
  120. } else {
  121. var msg ircmsg.IRCMessage
  122. if forceTrailing {
  123. msg.ForceTrailing()
  124. }
  125. msg.Prefix = nickmask
  126. msg.Command = command
  127. msg.Params = make([]string, 2)
  128. msg.Params[0] = target
  129. m.plainMultiline = make([][]byte, len(message.Split))
  130. for i, pair := range message.Split {
  131. msg.Params[1] = pair.Message
  132. m.plainMultiline[i], err = msg.LineBytesStrict(false, MaxLineLen)
  133. if m.handleErr(server, err) {
  134. return
  135. }
  136. }
  137. // we need to send the same batch ID to all recipient sessions;
  138. // ensure it doesn't collide. a half-sized token has 64 bits of entropy,
  139. // so a collision isn't expected until there are on the order of 2**32
  140. // concurrent batches being relayed:
  141. batchID := utils.GenerateSecretToken()[:utils.SecretTokenLength/2]
  142. batch := composeMultilineBatch(batchID, nickmask, accountName, tags, command, target, message)
  143. m.fullTagsMultiline = make([][]byte, len(batch))
  144. for i, msg := range batch {
  145. if forceTrailing {
  146. msg.ForceTrailing()
  147. }
  148. m.fullTagsMultiline[i], err = msg.LineBytesStrict(false, MaxLineLen)
  149. if m.handleErr(server, err) {
  150. return
  151. }
  152. }
  153. }
  154. return
  155. }
  156. func (m *MessageCache) Send(session *Session) {
  157. if m.fullTags != nil {
  158. // Initialize() path:
  159. if session.capabilities.Has(caps.MessageTags) {
  160. session.sendBytes(m.fullTags, false)
  161. } else if m.plain != nil {
  162. // plain == nil indicates a TAGMSG
  163. if !(session.capabilities.Has(caps.ServerTime) || session.capabilities.Has(caps.AccountTag)) {
  164. session.sendBytes(m.plain, false)
  165. } else {
  166. // slowpath
  167. session.sendFromClientInternal(false, m.time, m.msgid, m.source, m.accountName, nil, m.command, m.params...)
  168. }
  169. }
  170. } else if m.fullTagsMultiline != nil {
  171. // InitializeSplitMessage() path:
  172. if session.capabilities.Has(caps.Multiline) {
  173. for _, line := range m.fullTagsMultiline {
  174. session.sendBytes(line, false)
  175. }
  176. } else if !(session.capabilities.Has(caps.ServerTime) || session.capabilities.Has(caps.AccountTag)) {
  177. for _, line := range m.plainMultiline {
  178. session.sendBytes(line, false)
  179. }
  180. } else {
  181. // slowpath
  182. session.sendSplitMsgFromClientInternal(false, m.source, m.accountName, m.tags, m.command, m.target, m.splitMessage)
  183. }
  184. }
  185. }