aboutsummaryrefslogtreecommitdiff
path: root/hotline
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-27 21:44:20 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-27 21:44:20 -0700
commit6dabc6b44f27f0aa936a7f9518aa6bc1243d9001 (patch)
treef2d1b7dc990fcc444065b6483e9fe3ef0ad6c842 /hotline
parent4d7abe62a6dd692b71090819de0d57a40486bdb2 (diff)
Limit chat message size to 8192 bytes
Diffstat (limited to 'hotline')
-rw-r--r--hotline/transaction_handlers.go11
1 files changed, 9 insertions, 2 deletions
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