Browse Source

Merge pull request #2069 from slingamn/nestedbatch.1

some cleanups
tags/v2.12.0-rc1
Shivaram Lingamneni 1 year ago
parent
commit
2aded271c5
No account linked to committer's email address
8 changed files with 35 additions and 39 deletions
  1. 2
    0
      irc/caps/constants.go
  2. 1
    1
      irc/channel.go
  3. 16
    16
      irc/client.go
  4. 2
    4
      irc/handlers.go
  5. 2
    4
      irc/message_cache.go
  6. 3
    13
      irc/responsebuffer.go
  7. 8
    0
      irc/utils/types.go
  8. 1
    1
      irctest

+ 2
- 0
irc/caps/constants.go View File

@@ -62,6 +62,8 @@ const (
62 62
 	RelaymsgTagName = "draft/relaymsg"
63 63
 	// BOT mode: https://ircv3.net/specs/extensions/bot-mode
64 64
 	BotTagName = "bot"
65
+	// https://ircv3.net/specs/extensions/chathistory
66
+	ChathistoryTargetsBatchType = "draft/chathistory-targets"
65 67
 )
66 68
 
67 69
 func init() {

+ 1
- 1
irc/channel.go View File

@@ -1059,7 +1059,7 @@ func (channel *Channel) replayHistoryItems(rb *ResponseBuffer, items []history.I
1059 1059
 		}
1060 1060
 	}
1061 1061
 
1062
-	batchID := rb.StartNestedHistoryBatch(chname)
1062
+	batchID := rb.StartNestedBatch(chname, "chathistory")
1063 1063
 	defer rb.EndNestedBatch(batchID)
1064 1064
 
1065 1065
 	for _, item := range items {

+ 16
- 16
irc/client.go View File

@@ -850,7 +850,7 @@ func (client *Client) replayPrivmsgHistory(rb *ResponseBuffer, items []history.I
850 850
 	if target == "" {
851 851
 		target = nick
852 852
 	}
853
-	batchID = rb.StartNestedHistoryBatch(target)
853
+	batchID = rb.StartNestedBatch(target, "chathistory")
854 854
 
855 855
 	isSelfMessage := func(item *history.Item) bool {
856 856
 		// XXX: Params[0] is the message target. if the source of this message is an in-memory
@@ -1425,27 +1425,27 @@ func composeMultilineBatch(batchID, fromNickMask, fromAccount string, isBot bool
1425 1425
 }
1426 1426
 
1427 1427
 var (
1428
-	// these are all the output commands that MUST have their last param be a trailing.
1429
-	// this is needed because dumb clients like to treat trailing params separately from the
1430
-	// other params in messages.
1431
-	commandsThatMustUseTrailing = map[string]bool{
1432
-		"PRIVMSG": true,
1433
-		"NOTICE":  true,
1434
-
1435
-		RPL_WHOISCHANNELS: true,
1436
-		RPL_USERHOST:      true,
1437
-
1428
+	// in practice, many clients require that the final parameter be a trailing
1429
+	// (prefixed with `:`) even when this is not syntactically necessary.
1430
+	// by default, force the following commands to use a trailing:
1431
+	commandsThatMustUseTrailing = utils.SetLiteral(
1432
+		"PRIVMSG",
1433
+		"NOTICE",
1434
+		RPL_WHOISCHANNELS,
1435
+		RPL_USERHOST,
1438 1436
 		// mirc's handling of RPL_NAMREPLY is broken:
1439 1437
 		// https://forums.mirc.com/ubbthreads.php/topics/266939/re-nick-list
1440
-		RPL_NAMREPLY: true,
1441
-	}
1438
+		RPL_NAMREPLY,
1439
+	)
1442 1440
 )
1443 1441
 
1442
+func forceTrailing(config *Config, command string) bool {
1443
+	return config.Server.Compatibility.forceTrailing && commandsThatMustUseTrailing.Has(command)
1444
+}
1445
+
1444 1446
 // SendRawMessage sends a raw message to the client.
1445 1447
 func (session *Session) SendRawMessage(message ircmsg.Message, blocking bool) error {
1446
-	// use dumb hack to force the last param to be a trailing param if required
1447
-	config := session.client.server.Config()
1448
-	if config.Server.Compatibility.forceTrailing && commandsThatMustUseTrailing[message.Command] {
1448
+	if forceTrailing(session.client.server.Config(), message.Command) {
1449 1449
 		message.ForceTrailing()
1450 1450
 	}
1451 1451
 

+ 2
- 4
irc/handlers.go View File

@@ -655,10 +655,8 @@ func chathistoryHandler(server *Server, client *Client, msg ircmsg.Message, rb *
655 655
 		} else {
656 656
 			// successful responses are sent as a chathistory or history batch
657 657
 			if listTargets {
658
-				if rb.session.capabilities.Has(caps.Batch) { // #2066
659
-					batchID := rb.StartNestedBatch("draft/chathistory-targets")
660
-					defer rb.EndNestedBatch(batchID)
661
-				}
658
+				batchID := rb.StartNestedBatch(caps.ChathistoryTargetsBatchType)
659
+				defer rb.EndNestedBatch(batchID)
662 660
 				for _, target := range targets {
663 661
 					name := server.UnfoldName(target.CfName)
664 662
 					rb.Add(nil, server.name, "CHATHISTORY", "TARGETS", name,

+ 2
- 4
irc/message_cache.go View File

@@ -79,8 +79,7 @@ func (m *MessageCache) Initialize(server *Server, serverTime time.Time, msgid st
79 79
 	m.params = params
80 80
 
81 81
 	var msg ircmsg.Message
82
-	config := server.Config()
83
-	if config.Server.Compatibility.forceTrailing && commandsThatMustUseTrailing[command] {
82
+	if forceTrailing(server.Config(), command) {
84 83
 		msg.ForceTrailing()
85 84
 	}
86 85
 	msg.Source = nickmask
@@ -111,8 +110,7 @@ func (m *MessageCache) InitializeSplitMessage(server *Server, nickmask, accountN
111 110
 	m.target = target
112 111
 	m.splitMessage = message
113 112
 
114
-	config := server.Config()
115
-	forceTrailing := config.Server.Compatibility.forceTrailing && commandsThatMustUseTrailing[command]
113
+	forceTrailing := forceTrailing(server.Config(), command)
116 114
 
117 115
 	if message.Is512() {
118 116
 		isTagmsg := command == "TAGMSG"

+ 3
- 13
irc/responsebuffer.go View File

@@ -193,6 +193,9 @@ func (rb *ResponseBuffer) sendBatchEnd(blocking bool) {
193 193
 // Starts a nested batch (see the ResponseBuffer struct definition for a description of
194 194
 // how this works)
195 195
 func (rb *ResponseBuffer) StartNestedBatch(batchType string, params ...string) (batchID string) {
196
+	if !rb.session.capabilities.Has(caps.Batch) {
197
+		return
198
+	}
196 199
 	batchID = rb.session.generateBatchID()
197 200
 	msgParams := make([]string, len(params)+2)
198 201
 	msgParams[0] = "+" + batchID
@@ -219,19 +222,6 @@ func (rb *ResponseBuffer) EndNestedBatch(batchID string) {
219 222
 	rb.AddMessage(ircmsg.MakeMessage(nil, rb.target.server.name, "BATCH", "-"+batchID))
220 223
 }
221 224
 
222
-// Convenience to start a nested batch for history lines, at the highest level
223
-// supported by the client (`history`, `chathistory`, or no batch, in descending order).
224
-func (rb *ResponseBuffer) StartNestedHistoryBatch(params ...string) (batchID string) {
225
-	var batchType string
226
-	if rb.session.capabilities.Has(caps.Batch) {
227
-		batchType = "chathistory"
228
-	}
229
-	if batchType != "" {
230
-		batchID = rb.StartNestedBatch(batchType, params...)
231
-	}
232
-	return
233
-}
234
-
235 225
 // Send sends all messages in the buffer to the client.
236 226
 // Afterwards, the buffer is in an undefined state and MUST NOT be used further.
237 227
 // If `blocking` is true you MUST be sending to the client from its own goroutine.

+ 8
- 0
irc/utils/types.go View File

@@ -20,6 +20,14 @@ func (s HashSet[T]) Remove(elem T) {
20 20
 	delete(s, elem)
21 21
 }
22 22
 
23
+func SetLiteral[T comparable](elems ...T) HashSet[T] {
24
+	result := make(HashSet[T], len(elems))
25
+	for _, elem := range elems {
26
+		result.Add(elem)
27
+	}
28
+	return result
29
+}
30
+
23 31
 func CopyMap[K comparable, V any](input map[K]V) (result map[K]V) {
24 32
 	result = make(map[K]V, len(input))
25 33
 	for key, value := range input {

+ 1
- 1
irctest

@@ -1 +1 @@
1
-Subproject commit 22c6743b24f2a85bf79a92fb0c7fab325c047a6c
1
+Subproject commit 5a5dbdb50dda3dc9e5ed3d542ab75b1b87fccac9

Loading…
Cancel
Save