From 34f11ddb97cf43ed1b7dc6c48d08dd7a444b1903 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Thu, 13 Jun 2019 02:17:21 -0400 Subject: [PATCH 1/2] implement draft/labeled-response-0.2 --- gencapdefs.py | 2 +- irc/caps/defs.go | 4 ++-- irc/responsebuffer.go | 20 ++++++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/gencapdefs.py b/gencapdefs.py index 2daf59bd..0dab5b3e 100644 --- a/gencapdefs.py +++ b/gencapdefs.py @@ -77,7 +77,7 @@ CAPDEFS = [ ), CapDef( identifier="LabeledResponse", - name="draft/labeled-response", + name="draft/labeled-response-0.2", url="https://ircv3.net/specs/extensions/labeled-response.html", standard="draft IRCv3", ), diff --git a/irc/caps/defs.go b/irc/caps/defs.go index 6a691bbc..4092bb2f 100644 --- a/irc/caps/defs.go +++ b/irc/caps/defs.go @@ -53,7 +53,7 @@ const ( // https://ircv3.net/specs/extensions/invite-notify-3.2.html InviteNotify Capability = iota - // LabeledResponse is the draft IRCv3 capability named "draft/labeled-response": + // LabeledResponse is the draft IRCv3 capability named "draft/labeled-response-0.2": // https://ircv3.net/specs/extensions/labeled-response.html LabeledResponse Capability = iota @@ -135,7 +135,7 @@ var ( "echo-message", "extended-join", "invite-notify", - "draft/labeled-response", + "draft/labeled-response-0.2", "draft/languages", "oragono.io/maxline-2", "message-tags", diff --git a/irc/responsebuffer.go b/irc/responsebuffer.go index 794254c0..e651cd4a 100644 --- a/irc/responsebuffer.go +++ b/irc/responsebuffer.go @@ -212,16 +212,24 @@ func (rb *ResponseBuffer) flushInternal(final bool, blocking bool) error { } useLabel := rb.session.capabilities.Has(caps.LabeledResponse) && rb.Label != "" - // use a batch if we have a label, and we either currently have 0 or 2+ messages, + // use a batch if we have a label, and we either currently have 2+ 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) + startBatch := useLabel && (1 < len(rb.messages) || !final) - // if label but no batch, add label to first message - if useLabel && !useBatch && len(rb.messages) == 1 && rb.batchID == "" { - rb.messages[0].SetTag(caps.LabelTagName, rb.Label) - } else if useBatch { + if startBatch { rb.sendBatchStart(blocking) + } else if useLabel && len(rb.messages) == 0 && rb.batchID == "" && final { + // ACK message + message := ircmsg.MakeMessage(nil, rb.session.client.server.name, "ACK") + message.SetTag(caps.LabelTagName, rb.Label) + if rb.session.capabilities.Has(caps.ServerTime) { + message.SetTag("time", time.Now().UTC().Format(IRCv3TimestampFormat)) + } + rb.session.SendRawMessage(message, blocking) + } else if useLabel && len(rb.messages) == 1 && rb.batchID == "" && final { + // single labeled message + rb.messages[0].SetTag(caps.LabelTagName, rb.Label) } // send each message out From 8dca545264c297b79ad9e8409aee64e1aa198aaf Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Thu, 13 Jun 2019 02:24:14 -0400 Subject: [PATCH 2/2] consolidate some time-setting code --- irc/client.go | 20 +++++++++++--------- irc/responsebuffer.go | 12 +++--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/irc/client.go b/irc/client.go index 355876d8..b1c7fe6c 100644 --- a/irc/client.go +++ b/irc/client.go @@ -1160,12 +1160,7 @@ func (session *Session) sendFromClientInternal(blocking bool, serverTime time.Ti msg.SetTag("msgid", msgid) } // attach server-time - if session.capabilities.Has(caps.ServerTime) { - if serverTime.IsZero() { - serverTime = time.Now().UTC() - } - msg.SetTag("time", serverTime.Format(IRCv3TimestampFormat)) - } + session.setTimeTag(&msg, serverTime) return session.SendRawMessage(msg, blocking) } @@ -1246,12 +1241,19 @@ func (client *Client) Send(tags map[string]string, prefix string, command string func (session *Session) Send(tags map[string]string, prefix string, command string, params ...string) (err error) { msg := ircmsg.MakeMessage(tags, prefix, command, params...) - if session.capabilities.Has(caps.ServerTime) && !msg.HasTag("time") { - msg.SetTag("time", time.Now().UTC().Format(IRCv3TimestampFormat)) - } + session.setTimeTag(&msg, time.Time{}) return session.SendRawMessage(msg, false) } +func (session *Session) setTimeTag(msg *ircmsg.IrcMessage, serverTime time.Time) { + if session.capabilities.Has(caps.ServerTime) && !msg.HasTag("time") { + if serverTime.IsZero() { + serverTime = time.Now() + } + msg.SetTag("time", serverTime.UTC().Format(IRCv3TimestampFormat)) + } +} + // Notice sends the client a notice from the server. func (client *Client) Notice(text string) { client.Send(nil, client.server.name, "NOTICE", client.Nick(), text) diff --git a/irc/responsebuffer.go b/irc/responsebuffer.go index e651cd4a..9df090a0 100644 --- a/irc/responsebuffer.go +++ b/irc/responsebuffer.go @@ -93,9 +93,7 @@ func (rb *ResponseBuffer) AddFromClient(time time.Time, msgid string, fromNickMa msg.SetTag("msgid", msgid) } // attach server-time - if rb.session.capabilities.Has(caps.ServerTime) && !msg.HasTag("time") { - msg.SetTag("time", time.UTC().Format(IRCv3TimestampFormat)) - } + rb.session.setTimeTag(&msg, time) rb.AddMessage(msg) } @@ -223,9 +221,7 @@ func (rb *ResponseBuffer) flushInternal(final bool, blocking bool) error { // ACK message message := ircmsg.MakeMessage(nil, rb.session.client.server.name, "ACK") message.SetTag(caps.LabelTagName, rb.Label) - if rb.session.capabilities.Has(caps.ServerTime) { - message.SetTag("time", time.Now().UTC().Format(IRCv3TimestampFormat)) - } + rb.session.setTimeTag(&message, time.Time{}) rb.session.SendRawMessage(message, blocking) } else if useLabel && len(rb.messages) == 1 && rb.batchID == "" && final { // single labeled message @@ -235,9 +231,7 @@ func (rb *ResponseBuffer) flushInternal(final bool, blocking bool) error { // send each message out for _, message := range rb.messages { // attach server-time if needed - if rb.session.capabilities.Has(caps.ServerTime) && !message.HasTag("time") { - message.SetTag("time", time.Now().UTC().Format(IRCv3TimestampFormat)) - } + rb.session.setTimeTag(&message, time.Time{}) // attach batch ID, unless this message was part of a nested batch and is // already tagged