From: Jeff Halter Date: Fri, 28 Jun 2024 04:44:20 +0000 (-0700) Subject: Limit chat message size to 8192 bytes X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/6dabc6b44f27f0aa936a7f9518aa6bc1243d9001?hp=4d7abe62a6dd692b71090819de0d57a40486bdb2 Limit chat message size to 8192 bytes --- diff --git a/hotline/transaction_handlers.go b/hotline/transaction_handlers.go index 718dd59..271f308 100644 --- a/hotline/transaction_handlers.go +++ b/hotline/transaction_handlers.go @@ -67,14 +67,18 @@ var TransactionHandlers = map[TranType]HandlerFunc{ TranDownloadBanner: HandleDownloadBanner, } +// The total size of a chat message data field is 8192 bytes. +const chatMsgLimit = 8192 + func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction) { if !cc.Authorize(accessSendChat) { return cc.NewErrReply(t, "You are not allowed to participate in chat.") } // Truncate long usernames - trunc := fmt.Sprintf("%13s", cc.UserName) - formattedMsg := fmt.Sprintf("\r%.14s: %s", trunc, t.GetField(FieldData).Data) + // %13.13s: This means a string that is right-aligned in a field of 13 characters. + // If the string is longer than 13 characters, it will be truncated to 13 characters. + formattedMsg := fmt.Sprintf("\r%13.13s: %s", cc.UserName, t.GetField(FieldData).Data) // By holding the option key, Hotline chat allows users to send /me formatted messages like: // *** Halcyon does stuff @@ -84,6 +88,9 @@ func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction) { formattedMsg = fmt.Sprintf("\r*** %s %s", cc.UserName, t.GetField(FieldData).Data) } + // Truncate the message to the limit. This does not handle the edge case of a string ending on multibyte character. + formattedMsg = formattedMsg[:min(len(formattedMsg), chatMsgLimit)] + // The ChatID field is used to identify messages as belonging to a private chat. // All clients *except* Frogblast omit this field for public chat, but Frogblast sends a value of 00 00 00 00. chatID := t.GetField(FieldChatID).Data