2017-10-08 03:05:05 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2018-12-28 19:45:55 +01:00
|
|
|
"runtime/debug"
|
2017-10-08 03:05:05 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/goshuirc/irc-go/ircmsg"
|
|
|
|
"github.com/oragono/oragono/irc/caps"
|
2018-11-26 11:23:27 +01:00
|
|
|
"github.com/oragono/oragono/irc/utils"
|
2017-10-08 03:05:05 +02:00
|
|
|
)
|
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
const (
|
|
|
|
// https://ircv3.net/specs/extensions/labeled-response.html
|
|
|
|
batchType = "draft/labeled-response"
|
|
|
|
)
|
|
|
|
|
2017-10-08 03:05:05 +02:00
|
|
|
// ResponseBuffer - put simply - buffers messages and then outputs them to a given client.
|
|
|
|
//
|
|
|
|
// Using a ResponseBuffer lets you really easily implement labeled-response, since the
|
|
|
|
// buffer will silently create a batch if required and label the outgoing messages as
|
|
|
|
// necessary (or leave it off and simply tag the outgoing message).
|
|
|
|
type ResponseBuffer struct {
|
2018-12-28 19:45:55 +01:00
|
|
|
Label string
|
|
|
|
batchID string
|
|
|
|
target *Client
|
|
|
|
messages []ircmsg.IrcMessage
|
|
|
|
finalized bool
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabel returns the label from the given message.
|
|
|
|
func GetLabel(msg ircmsg.IrcMessage) string {
|
2018-02-10 23:57:15 +01:00
|
|
|
return msg.Tags[caps.LabelTagName].Value
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResponseBuffer returns a new ResponseBuffer.
|
|
|
|
func NewResponseBuffer(target *Client) *ResponseBuffer {
|
|
|
|
return &ResponseBuffer{
|
|
|
|
target: target,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a standard new message to our queue.
|
|
|
|
func (rb *ResponseBuffer) Add(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) {
|
2018-12-28 19:45:55 +01:00
|
|
|
if rb.finalized {
|
|
|
|
rb.target.server.logger.Error("message added to finalized ResponseBuffer, undefined behavior")
|
|
|
|
debug.PrintStack()
|
|
|
|
return
|
|
|
|
}
|
2017-10-08 03:05:05 +02:00
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
message := ircmsg.MakeMessage(tags, prefix, command, params...)
|
2017-10-08 03:05:05 +02:00
|
|
|
rb.messages = append(rb.messages, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddFromClient adds a new message from a specific client to our queue.
|
2018-12-28 19:45:55 +01:00
|
|
|
func (rb *ResponseBuffer) AddFromClient(msgid string, fromNickMask string, fromAccount string, tags *map[string]ircmsg.TagValue, command string, params ...string) {
|
2017-10-08 03:05:05 +02:00
|
|
|
// attach account-tag
|
2018-12-28 19:45:55 +01:00
|
|
|
if rb.target.capabilities.Has(caps.AccountTag) {
|
|
|
|
if fromAccount != "*" {
|
|
|
|
tags = ensureTag(tags, "account", fromAccount)
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// attach message-id
|
|
|
|
if len(msgid) > 0 && rb.target.capabilities.Has(caps.MessageTags) {
|
2018-12-28 19:45:55 +01:00
|
|
|
tags = ensureTag(tags, "draft/msgid", msgid)
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.Add(tags, fromNickMask, command, params...)
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddSplitMessageFromClient adds a new split message from a specific client to our queue.
|
2018-12-28 19:45:55 +01:00
|
|
|
func (rb *ResponseBuffer) AddSplitMessageFromClient(msgid string, fromNickMask string, fromAccount string, tags *map[string]ircmsg.TagValue, command string, target string, message utils.SplitMessage) {
|
2018-11-26 11:23:27 +01:00
|
|
|
if rb.target.capabilities.Has(caps.MaxLine) || message.Wrapped == nil {
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.AddFromClient(msgid, fromNickMask, fromAccount, tags, command, target, message.Original)
|
2017-10-08 03:05:05 +02:00
|
|
|
} else {
|
2018-11-26 11:23:27 +01:00
|
|
|
for _, str := range message.Wrapped {
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.AddFromClient(msgid, fromNickMask, fromAccount, tags, command, target, str)
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
func (rb *ResponseBuffer) sendBatchStart(blocking bool) {
|
|
|
|
if rb.batchID != "" {
|
|
|
|
// batch already initialized
|
|
|
|
return
|
2018-02-04 12:34:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
// formerly this combined time.Now.UnixNano() in base 36 with an incrementing counter,
|
|
|
|
// also in base 36. but let's just use a uuidv4-alike (26 base32 characters):
|
|
|
|
rb.batchID = utils.GenerateSecretToken()
|
2017-10-08 03:05:05 +02:00
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
message := ircmsg.MakeMessage(nil, rb.target.server.name, "BATCH", "+"+rb.batchID, batchType)
|
|
|
|
message.Tags[caps.LabelTagName] = ircmsg.MakeTagValue(rb.Label)
|
|
|
|
rb.target.SendRawMessage(message, blocking)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rb *ResponseBuffer) sendBatchEnd(blocking bool) {
|
|
|
|
if rb.batchID == "" {
|
|
|
|
// we are not sending a batch, skip this
|
|
|
|
return
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
message := ircmsg.MakeMessage(nil, rb.target.server.name, "BATCH", "-"+rb.batchID)
|
|
|
|
rb.target.SendRawMessage(message, blocking)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send sends all messages in the buffer to the client.
|
|
|
|
// Afterwards, the buffer is in an undefined state and MUST NOT be used further.
|
|
|
|
// If `blocking` is true you MUST be sending to the client from its own goroutine.
|
|
|
|
func (rb *ResponseBuffer) Send(blocking bool) error {
|
|
|
|
return rb.flushInternal(true, blocking)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush sends all messages in the buffer to the client.
|
|
|
|
// Afterwards, the buffer can still be used. Client code MUST subsequently call Send()
|
|
|
|
// to ensure that the final `BATCH -` message is sent.
|
|
|
|
// If `blocking` is true you MUST be sending to the client from its own goroutine.
|
|
|
|
func (rb *ResponseBuffer) Flush(blocking bool) error {
|
|
|
|
return rb.flushInternal(false, blocking)
|
|
|
|
}
|
|
|
|
|
|
|
|
// flushInternal sends the contents of the buffer, either blocking or nonblocking
|
|
|
|
// It sends the `BATCH +` message if the client supports it and it hasn't been sent already.
|
|
|
|
// If `final` is true, it also sends `BATCH -` (if necessary).
|
|
|
|
func (rb *ResponseBuffer) flushInternal(final bool, blocking bool) error {
|
|
|
|
useLabel := rb.target.capabilities.Has(caps.LabeledResponse) && rb.Label != ""
|
|
|
|
// use a batch if we have a label, and we either currently have multiple messages,
|
|
|
|
// or we are doing a Flush() and we have to assume that there will be more messages
|
|
|
|
// in the future.
|
|
|
|
useBatch := useLabel && (len(rb.messages) > 1 || !final)
|
|
|
|
|
|
|
|
// if label but no batch, add label to first message
|
|
|
|
if useLabel && !useBatch && len(rb.messages) == 1 {
|
|
|
|
rb.messages[0].Tags[caps.LabelTagName] = ircmsg.MakeTagValue(rb.Label)
|
|
|
|
} else if useBatch {
|
|
|
|
rb.sendBatchStart(blocking)
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// send each message out
|
|
|
|
for _, message := range rb.messages {
|
|
|
|
// attach server-time if needed
|
|
|
|
if rb.target.capabilities.Has(caps.ServerTime) {
|
2018-12-28 19:45:55 +01:00
|
|
|
if !message.Tags["time"].HasValue {
|
|
|
|
t := time.Now().UTC().Format(IRCv3TimestampFormat)
|
|
|
|
message.Tags["time"] = ircmsg.MakeTagValue(t)
|
|
|
|
}
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// attach batch ID
|
2018-12-28 19:45:55 +01:00
|
|
|
if rb.batchID != "" {
|
|
|
|
message.Tags["batch"] = ircmsg.MakeTagValue(rb.batchID)
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// send message out
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.target.SendRawMessage(message, blocking)
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// end batch if required
|
2018-12-28 19:45:55 +01:00
|
|
|
if final {
|
|
|
|
rb.sendBatchEnd(blocking)
|
|
|
|
rb.finalized = true
|
2017-10-08 03:05:05 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:34:44 +01:00
|
|
|
// clear out any existing messages
|
2018-11-26 11:23:27 +01:00
|
|
|
rb.messages = rb.messages[:0]
|
2018-02-04 12:34:44 +01:00
|
|
|
|
2017-10-08 03:05:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-05 15:21:08 +01:00
|
|
|
|
|
|
|
// Notice sends the client the given notice from the server.
|
|
|
|
func (rb *ResponseBuffer) Notice(text string) {
|
|
|
|
rb.Add(nil, rb.target.server.name, "NOTICE", rb.target.nick, text)
|
|
|
|
}
|