src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 07 Feb 2021 00:51:08 +0100
branchv_0
changeset 7 15d9b0ca161a
parent 6 47b677177365
child 8 04aa5591eee3
permissions -rw-r--r--
references/message-id also in lower case
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <codecvt>
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <libxml++-2.6/libxml++/libxml++.h>
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <vmime/vmime.hpp>
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "XMLNameCodec.h"
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace xmltable {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class XMLDocumentConstructor {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	std::istream* input = nullptr;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	xmlpp::DomParser* parser = nullptr;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	XMLNameCodec nameCodec;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
1
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    38
	std::string rootName = "mime-message";
0
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
1
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    40
	std::string format(std::shared_ptr<vmime::datetime> value) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
		std::stringstream timestamp;
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    42
		int tz = value->getZone();
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
		timestamp << value->getYear() << "-";
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
		timestamp << std::setw(2) << std::setfill('0') << value->getMonth() << "-";
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
		timestamp << std::setw(2) << std::setfill('0') << value->getDay() << "T";
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
		timestamp << std::setw(2) << std::setfill('0') << value->getHour() << ":";
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
		timestamp << std::setw(2) << std::setfill('0') << value->getMinute() << ":";
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
		timestamp << std::setw(2) << std::setfill('0') << value->getSecond() << (tz >= 0 ? "+" : "-");
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
		timestamp << std::setw(2) << std::setfill('0') << std::abs(tz / 60) << ":";
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
		timestamp << std::setw(2) << std::setfill('0') << std::abs(tz % 60);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
		return timestamp.str();
0
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
2
d04d7713344c simplify XML structure + list body parts
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    54
	std::string format(const vmime::mediaType& contentType) {
d04d7713344c simplify XML structure + list body parts
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
		return contentType.getType() + "/" + contentType.getSubType();
d04d7713344c simplify XML structure + list body parts
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    56
	}
d04d7713344c simplify XML structure + list body parts
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    57
5
a3794fe5ea4b normalize header names to lower case
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    58
	std::string toLowerCase(const std::string& value) {
a3794fe5ea4b normalize header names to lower case
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    59
		std::string result = value;
a3794fe5ea4b normalize header names to lower case
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    60
		std::transform(result.begin(), result.end(), result.begin(), ::tolower);
a3794fe5ea4b normalize header names to lower case
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    61
		return result;
a3794fe5ea4b normalize header names to lower case
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    62
	}
a3794fe5ea4b normalize header names to lower case
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    63
6
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    64
	std::string toHex(const std::string& value) {
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    65
		static const char* const hexSymbols = "0123456789abcdef";
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    66
		size_t length = value.length();
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    67
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    68
		std::string result;
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    69
		result.reserve(3 * length - 1);
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    70
		for (size_t i = 0; i < length; i++) {
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    71
			const unsigned char ch = value[i];
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    72
			result.push_back(hexSymbols[ch >> 4]);
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    73
			result.push_back(hexSymbols[ch & 15]);
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    74
			if (i < length - 1) result.push_back(' ');
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    75
		}
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    76
		return result;
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    77
	}
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    78
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    79
	std::string fetchBodyText(std::shared_ptr<vmime::body> body) {
4
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    80
		std::stringstream result;
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    81
		vmime::utility::outputStreamAdapter resultAdapter(result);
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    82
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    83
		const vmime::charset targetEncoding = vmime::charset("utf-8");
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    84
		const vmime::charset sourceEncoding = body->getCharset();
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    85
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    86
		vmime::shared_ptr <vmime::charsetConverter> charsetConverter = vmime::charsetConverter::create(sourceEncoding, targetEncoding);
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    87
		vmime::shared_ptr <vmime::utility::charsetFilteredOutputStream> resultConverter = charsetConverter->getFilteredOutputStream(resultAdapter);
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    88
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    89
		body->getContents()->extract(*resultConverter);
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    90
		resultConverter->flush();
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    91
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    92
		return result.str();
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    93
	}
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    94
6
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    95
	std::string fetchBodyBinary(std::shared_ptr<vmime::body> body) {
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    96
		std::stringstream result;
7
15d9b0ca161a references/message-id also in lower case
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    97
		vmime::utility::outputStreamAdapter resultAdapter(result);
15d9b0ca161a references/message-id also in lower case
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    98
		body->getContents()->extract(resultAdapter);
15d9b0ca161a references/message-id also in lower case
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    99
		resultAdapter.flush();
6
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   100
		return toHex(result.str());
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   101
	}
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   102
3
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   103
	void appendBody(xmlpp::Element* element, std::shared_ptr<vmime::body> body) {
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   104
		element->set_attribute("content-type", format(body->getContentType()));
4
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   105
		// element->set_attribute("content-type-charset", body->getCharset().getName());
dd8ff58fb29b add body/part content as CDATA
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   106
		// element->set_attribute("content-transfer-encoding", body->getEncoding().getName());
7
15d9b0ca161a references/message-id also in lower case
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   107
		// TODO: size of raw data
3
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   108
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   109
		if (body->getPartCount() == 0) {
6
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   110
			if (body->getContentType().getType() == "text") element->add_child_cdata(fetchBodyText(body));
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   111
			else element->add_child_text(fetchBodyBinary(body));
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   112
			// TODO: if content is valid XML, import it in the DOM tree instead of pasting as a nested text/cdata
47b677177365 format binary data as HEX
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   113
			// TODO: optional trim of long data
3
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   114
		} else {
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   115
			for (auto part : body->getPartList()) {
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   116
				xmlpp::Element* partElement = element->add_child("part");
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   117
				appendBody(partElement, part->getBody());
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   118
			}
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   119
		}
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   120
	}
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   121
0
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
public:
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
	virtual ~XMLDocumentConstructor() {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
	void setOption(const std::string& uri, const std::string& value) {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
		if (uri == "root-name") rootName = value;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
		else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“");
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
	void process() {
1
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   136
		vmime::utility::inputStreamAdapter is(*input);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   137
		vmime::string data;
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   138
		vmime::utility::outputStreamStringAdapter os(data);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   139
		vmime::utility::bufferedStreamCopy(is, os);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   140
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   141
		vmime::message m;
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   142
		m.parse(data);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   143
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   144
		// vmime::shared_ptr<vmime::utility::inputStreamAdapter> is = vmime::make_shared<vmime::utility::inputStreamAdapter>(*input);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   145
		// m.parse(is, 0);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   146
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   147
		vmime::charset ch(vmime::charsets::UTF_8);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   148
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   149
		//std::cerr << "Subject:" << m.getHeader()->Subject()->getValue<vmime::text>()->getConvertedText(ch) << std::endl;
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   150
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   151
		xmlpp::Element* root = parser->get_document()->create_root_node(rootName);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   152
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   153
		xmlpp::Element* headers = root->add_child("headers");
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   154
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   155
		for (std::shared_ptr<vmime::headerField> mimeField : m.getHeader()->getFieldList()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   156
			// TODO: Are names always ASCII and subset of UTF-8?
5
a3794fe5ea4b normalize header names to lower case
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   157
			xmlpp::Element* field = headers->add_child(toLowerCase(nameCodec.encode(mimeField->getName())));
1
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   158
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   159
			if (auto value = mimeField->getValue<vmime::text>()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   160
				field->add_child_text(value->getConvertedText(ch));
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   161
			} else if (auto value = mimeField->getValue<vmime::mailbox>()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   162
				std::string name = value->getName().getConvertedText(ch);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   163
				std::string email = value->getEmail().toString();
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   164
				if (name.size()) field->set_attribute("name", name);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   165
				if (email.size()) field->add_child_text(email);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   166
			} else if (auto value = mimeField->getValue<vmime::addressList>()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   167
				for (auto address : value->getAddressList()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   168
					xmlpp::Element* addressField = field->add_child("address");
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   169
					if (std::shared_ptr<vmime::mailbox> mailbox = std::dynamic_pointer_cast<vmime::mailbox> (address)) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   170
						std::string name = mailbox->getName().getConvertedText(ch);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   171
						std::string email = mailbox->getEmail().toString();
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   172
						if (name.size()) addressField->set_attribute("name", name);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   173
						if (email.size()) addressField->add_child_text(email);
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   174
					} else if (std::shared_ptr<vmime::mailboxGroup> mailbox = std::dynamic_pointer_cast<vmime::mailboxGroup> (address)) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   175
						// TODO: mailboxGroup?
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   176
					}
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   177
				}
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   178
			} else if (auto value = mimeField->getValue<vmime::datetime>()) {
2
d04d7713344c simplify XML structure + list body parts
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   179
				// TODO: keep particular timestamp bits attributes or not?
1
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   180
				field->set_attribute("year", std::to_string(value->getYear()));
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   181
				field->set_attribute("month", std::to_string(value->getMonth()));
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   182
				field->set_attribute("day", std::to_string(value->getDay()));
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   183
				field->set_attribute("hour", std::to_string(value->getHour()));
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   184
				field->set_attribute("minute", std::to_string(value->getMinute()));
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   185
				field->set_attribute("second", std::to_string(value->getSecond()));
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   186
				field->set_attribute("zone", std::to_string(value->getZone())); // timezone is in minutes
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   187
				field->add_child_text(format(value));
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   188
			} else if (auto value = mimeField->getValue<vmime::mediaType>()) {
2
d04d7713344c simplify XML structure + list body parts
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   189
				if (value) field->add_child_text(format(*value));
1
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   190
				// TODO: encoding from the "Content-Type: text/plain; charset=us-ascii" type header?
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   191
			} else if (auto value = mimeField->getValue<vmime::messageId>()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   192
				field->add_child_text(value->getId());
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   193
			} else if (auto value = mimeField->getValue<vmime::messageIdSequence>()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   194
				for (auto messageId : value->getMessageIdList()) {
7
15d9b0ca161a references/message-id also in lower case
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   195
					xmlpp::Element* messageIdField = field->add_child("message-id");
1
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   196
					messageIdField->add_child_text(messageId->getId());
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   197
				}
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   198
			} else if (auto value = mimeField->getValue<vmime::contentDisposition>()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   199
				field->add_child_text(value->getName());
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   200
			} else if (auto value = mimeField->getValue<vmime::relay>()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   201
				field->set_attribute("from", value->getFrom());
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   202
				field->set_attribute("via", value->getVia());
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   203
				field->set_attribute("by", value->getBy());
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   204
				field->set_attribute("id", value->getId());
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   205
				field->set_attribute("for", value->getFor());
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   206
				// TODO: date of Received/relay
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   207
				// TODO: missing values or incomplete parsing of Received/relay in vmime
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   208
			} else if (auto value = mimeField->getValue<vmime::path>()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   209
				std::string local = value->getLocalPart();
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   210
				std::string domain = value->getDomain();
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   211
				if (local.size() && domain.size()) field->add_child_text(local + "@" + domain);
2
d04d7713344c simplify XML structure + list body parts
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   212
				else field->add_child_text(local + domain);
1
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   213
			} else if (auto value = mimeField->getValue<vmime::encoding>()) {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   214
				field->add_child_text(value->getName());
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   215
			} else {
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   216
				field->add_child_text("TODO: unknown header type"); // TODO: generic conversion as fallback?
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   217
			}
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   218
		}
3
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   219
2
d04d7713344c simplify XML structure + list body parts
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   220
		// TODO: check null pointers
1
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   221
356d0b024abf basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   222
		xmlpp::Element* body = root->add_child("body");
3
e2d61907e75f process body/parts recursively
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   223
		appendBody(body, m.getBody());
2
d04d7713344c simplify XML structure + list body parts
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   224
0
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   225
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   226
};
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   228
}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   229
}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   230
}