src/XMLDocumentConstructor.h
branchv_0
changeset 6 47b677177365
parent 5 a3794fe5ea4b
child 7 15d9b0ca161a
--- a/src/XMLDocumentConstructor.h	Sat Feb 06 20:32:26 2021 +0100
+++ b/src/XMLDocumentConstructor.h	Sun Feb 07 00:33:37 2021 +0100
@@ -61,7 +61,22 @@
 		return result;
 	}
 
-	std::string fetchBody(std::shared_ptr<vmime::body> body) {
+	std::string toHex(const std::string& value) {
+		static const char* const hexSymbols = "0123456789abcdef";
+		size_t length = value.length();
+
+		std::string result;
+		result.reserve(3 * length - 1);
+		for (size_t i = 0; i < length; i++) {
+			const unsigned char ch = value[i];
+			result.push_back(hexSymbols[ch >> 4]);
+			result.push_back(hexSymbols[ch & 15]);
+			if (i < length - 1) result.push_back(' ');
+		}
+		return result;
+	}
+
+	std::string fetchBodyText(std::shared_ptr<vmime::body> body) {
 		std::stringstream result;
 		vmime::utility::outputStreamAdapter resultAdapter(result);
 
@@ -77,13 +92,24 @@
 		return result.str();
 	}
 
+	std::string fetchBodyBinary(std::shared_ptr<vmime::body> body) {
+		std::stringstream result;
+		vmime::utility::outputStreamAdapter binaryResultAdapter(result);
+		body->getContents()->extract(binaryResultAdapter);
+		binaryResultAdapter.flush();
+		return toHex(result.str());
+	}
+
 	void appendBody(xmlpp::Element* element, std::shared_ptr<vmime::body> body) {
 		element->set_attribute("content-type", format(body->getContentType()));
 		// element->set_attribute("content-type-charset", body->getCharset().getName());
 		// element->set_attribute("content-transfer-encoding", body->getEncoding().getName());
 
 		if (body->getPartCount() == 0) {
-			element->add_child_cdata(fetchBody(body));
+			if (body->getContentType().getType() == "text") element->add_child_cdata(fetchBodyText(body));
+			else element->add_child_text(fetchBodyBinary(body));
+			// TODO: if content is valid XML, import it in the DOM tree instead of pasting as a nested text/cdata
+			// TODO: optional trim of long data
 		} else {
 			for (auto part : body->getPartList()) {
 				xmlpp::Element* partElement = element->add_child("part");