From 3fd6782988f01c2f56af19012e470ea16e800f51 Mon Sep 17 00:00:00 2001
From: Michele Comitini <michele.comitini@gmail.com>
Date: Tue, 1 Nov 2016 21:20:16 +0100
Subject: [PATCH 31/33] boundaries fixups: ordering of parts, do not add
 newline on boundary if there's no part after previous header (#34)

* Fix IMAP mailbox maintanence

Update message's mailbox id in a separate explicit transaction to prevent
SQLException: ORA-01453 error on the next db_begin_transaction() call
(see db_mailbox_seq_update() next to 'UPDATE dbmail_messages SET mailbox_idnr'
query).

* add newline only if is_message

* better fix for rfc822 multipart messages with boundary after header

* fixed some race condition when closing boundaries, since key does not change.  Needed ordering parts on depth DESC when key does not vary.

* fixes ordering in insertion
---
 src/dm_message.c            | 12 ++++---
 test/check_dbmail.h         | 78 ++++++++++++++++++++++++++++++++++++++++++++-
 test/check_dbmail_message.c |  8 +++++
 3 files changed, 93 insertions(+), 5 deletions(-)

diff --git src/dm_message.c src/dm_message.c
index 9d30d52..784c091 100644
--- src/dm_message.c
+++ src/dm_message.c
@@ -387,7 +387,7 @@ static DbmailMessage * _mime_retrieve(DbmailMessage *self)
 				"FROM %smimeparts p "
 				"JOIN %spartlists l ON p.id = l.part_id "
 				"JOIN %sphysmessage ph ON ph.id = l.physmessage_id "
-				"WHERE l.physmessage_id = ? ORDER BY l.part_key,l.part_order ASC", 
+				"WHERE l.physmessage_id = ? ORDER BY l.part_key, l.part_order ASC, l.part_depth DESC", 
 				frag, p_string_str(n), DBPFX, DBPFX, DBPFX);
 		db_stmt_set_u64(stmt, 1, self->id);
 		r = db_stmt_query(stmt);
@@ -454,10 +454,14 @@ static DbmailMessage * _mime_retrieve(DbmailMessage *self)
 			if ((depth > 0) && (blist[depth-1][0]))
 				strncpy(boundary, blist[depth-1], MAX_MIME_BLEN-1);
 
-			if (is_header && (!prev_header || prev_boundary || (prev_header && depth>0 && !prev_is_message))) {
+			if (is_header)
+			  if (prev_header && depth>0 && !prev_is_message) {
+				dprint("--%s\n", boundary);
+				p_string_append_printf(m, "--%s\n", boundary);
+			  } else if (!prev_header || prev_boundary) {
 				dprint("\n--%s\n", boundary);
 				p_string_append_printf(m, "\n--%s\n", boundary);
-			}
+			  }
 
 			p_string_append_printf(m, "%s", str);
 			dprint("<part is_header=\"%d\" depth=\"%d\" key=\"%d\" order=\"%d\">\n%s\n</part>\n", 
@@ -558,7 +562,7 @@ static gboolean store_mime_multipart(GMimeObject *object, DbmailMessage *m, cons
 	if (boundary) {
 		n++;
 		m->part_depth--;
-		m->part_order=n;
+		m->part_order++;
 	}
 
 	if (g_mime_content_type_is_type(GMIME_CONTENT_TYPE(content_type), "multipart", "*") &&
diff --git test/check_dbmail.h test/check_dbmail.h
index 09c9958..439c8a1 100644
--- test/check_dbmail.h
+++ test/check_dbmail.h
@@ -1561,7 +1561,7 @@ char *multipart_message8 = "From: nobody@example.org\n"
     "bmdzLnhtbFBLAQItABQABgAIAAAAIQDv0+Pp+gEAAPsDAAAQAAAAAAAAAAAAAAAAALREAABkb2NQ\n"
     "cm9wcy9hcHAueG1sUEsFBgAAAAAPAA8A3AMAAORHAAAAAA==\n"
     "\n"
-    "--_004_AAD42BB12C540843AB5C952ADD4D978901830A1Bswi52mbx1orfloc_--\";\n";
+    "--_004_AAD42BB12C540843AB5C952ADD4D978901830A1Bswi52mbx1orfloc_--\n";
 
 char *multipart_message9 = "From: a\n"
 	"To: b\n"
@@ -28440,3 +28440,79 @@ char *multipart_message_big = "From: \"Gennadiy Poryev\" <vecanoi@gmail.com>\n"
 "/g8HAAAAAwANNP0/pQ4DAA80/T+lDvA8\n"
 "\n"
 "------=_NextPart_000_0000_01CDFBC6.932A1F00--\n";
+
+char *multipart_message_submessage = "Content-Type: multipart/mixed; boundary=\"===============9147350442359610775==\"\n"
+"MIME-Version: 1.0\n"
+"To: test@test.it\n"
+"From: test@test.it\n"
+"\n"
+"--===============9147350442359610775==\n"
+"Content-Type: message/rfc822\n"
+"MIME-Version: 1.0\n"
+"\n"
+"Content-Type: multipart/alternative;\n"
+" boundary=\"===============1150329730008994878==\"\n"
+"MIME-Version: 1.0\n"
+"Subject: Link\n"
+"From: my@email.com\n"
+"To: your@email.com\n"
+"\n"
+"--===============1150329730008994878==\n"
+"Content-Type: text/plain; charset=\"us-ascii\"\n"
+"MIME-Version: 1.0\n"
+"Content-Transfer-Encoding: 7bit\n"
+"\n"
+"Hi!\n"
+"How are you?\n"
+"Here is the link you wanted:\n"
+"https://www.python.org\n"
+"--===============1150329730008994878==\n"
+"Content-Type: text/html; charset=\"us-ascii\"\n"
+"MIME-Version: 1.0\n"
+"Content-Transfer-Encoding: 7bit\n"
+"\n"
+"<html>\n"
+"  <head></head>\n"
+"  <body>\n"
+"    <p>Hi!<br>\n"
+"       How are you?<br>\n"
+"       Here is the <a href=\"https://www.python.org\">link</a> you wanted.\n"
+"    </p>\n"
+"  </body>\n"
+"</html>\n"
+"\n"
+"--===============1150329730008994878==\n"
+"Content-Type: multipart/mixed; boundary=\"===============0782181963306111896==\"\n"
+"MIME-Version: 1.0\n"
+"\n"
+"--===============0782181963306111896==\n"
+"Content-Type: text/plain; charset=\"us-ascii\"\n"
+"MIME-Version: 1.0\n"
+"Content-Transfer-Encoding: 7bit\n"
+"\n"
+"Text part3_1.\n"
+"--===============0782181963306111896==\n"
+"Content-Type: text/plain; charset=\"us-ascii\"\n"
+"MIME-Version: 1.0\n"
+"Content-Transfer-Encoding: 7bit\n"
+"\n"
+"Text part3_2.\n"
+"--===============0782181963306111896==--\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"--===============1150329730008994878==--\n"
+"\n"
+"--===============9147350442359610775==\n"
+"Content-Type: text/plain; charset=\"us-ascii\"\n"
+"MIME-Version: 1.0\n"
+"Content-Transfer-Encoding: 7bit\n"
+"\n"
+"This is a simple text attachment.\n"
+"with some newlines at the end.\n"
+"\n"
+"\n"
+"\n"
+"--===============9147350442359610775==--\n"
+"\n";
diff --git test/check_dbmail_message.c test/check_dbmail_message.c
index e36a2ea..f070257 100644
--- test/check_dbmail_message.c
+++ test/check_dbmail_message.c
@@ -431,6 +431,14 @@ START_TEST(test_dbmail_message_store)
 	COMPARE(e,t);
 	g_free(e);
 	g_free(t);
+	//-----------------------------------------
+	m = message_init(multipart_message_submessage);
+	e = dbmail_message_to_string(m);
+	t = store_and_retrieve(m);
+	COMPARE(e,t);
+	COMPARE(multipart_message_submessage, t);
+	g_free(e);
+	g_free(t);
 }
 END_TEST
 
-- 
2.10.1 (Apple Git-78)

