From fbe3b5d7311501f51f346b8591b0cecde7f2e587 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Mon, 11 Nov 2024 17:55:26 -0500
Subject: [PATCH] Fix handling of short send()s

For some reason, I thought socket.send() did this, but apparently
not.  Fixes issue transferring large messages.

Also, add support for the useless NAMESPACE command.
---
 exec/imapservice.js | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/exec/imapservice.js b/exec/imapservice.js
index f0677b4baf..4dc3640c21 100644
--- a/exec/imapservice.js
+++ b/exec/imapservice.js
@@ -184,19 +184,31 @@ function debug_log(line, rx)
 /* Socket I/O */
 /**************/
 
+function full_send(sock, str)
+{
+	var sent = 0;
+	var sret;
+
+	do {
+		sret = sock.send(str.substr(sent));
+		if (sret == undefined)
+			break;
+		sent += sret;
+	} while(sent < str.length);
+}
+
 function tagged(tag, msg, desc)
 {
-	client.socket.send(tag+" "+msg+" "+desc+"\r\n");
+	full_send(client.socket, tag+" "+msg+" "+desc+"\r\n");
 	debug_log("Send: "+tag+" "+msg+" "+desc, false);
 }
 
 function untagged(msg)
 {
-	client.socket.send("* "+msg+"\r\n");
+	full_send(client.socket, "* "+msg+"\r\n");
 	debug_log("Send: * "+msg.length+": "+msg, false);
 }
 
-
 /*************************************************************/
 /* Fetch response generation... this is the tricky bit.  :-) */
 /*************************************************************/
@@ -1788,6 +1800,18 @@ var authenticated_command_handlers = {
 			tagged(tag, "NO", "No appending yet... sorry.");
 		},
 	},
+	NAMESPACE:{
+		arguments: 0,
+		handler:function(args) {
+			var tag=args[0];
+
+			if (user.security.restrictions & UFLAG_G)
+				untagged('NAMESPACE NIL NIL (("" ' + encode_string(sepchar) + '))');
+			else
+				untagged('NAMESPACE (("" ' + encode_string(sepchar) + ')) NIL NIL');
+			tagged(tag, "OK", "Maybe this will get separators to work.");
+		},
+	},
 };
 
 function do_store(seq, uid, item, data)
-- 
GitLab