author | František Kučera <franta-hg@frantovo.cz> |
Mon, 25 Jan 2021 22:43:37 +0100 | |
branch | v_0 |
changeset 1 | 356d0b024abf |
parent 0 | ea26b3359fed |
child 2 | d04d7713344c |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <codecvt> |
|
20 |
#include <vector> |
|
21 |
||
22 |
#include <libxml++-2.6/libxml++/libxml++.h> |
|
23 |
||
24 |
#include <vmime/vmime.hpp> |
|
25 |
||
26 |
#include "XMLNameCodec.h" |
|
27 |
||
28 |
namespace relpipe { |
|
29 |
namespace in { |
|
30 |
namespace xmltable { |
|
31 |
||
32 |
class XMLDocumentConstructor { |
|
33 |
private: |
|
34 |
std::istream* input = nullptr; |
|
35 |
xmlpp::DomParser* parser = nullptr; |
|
36 |
XMLNameCodec nameCodec; |
|
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 | 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 | 52 |
} |
53 |
||
54 |
public: |
|
55 |
||
56 |
XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) { |
|
57 |
} |
|
58 |
||
59 |
virtual ~XMLDocumentConstructor() { |
|
60 |
} |
|
61 |
||
62 |
void setOption(const std::string& uri, const std::string& value) { |
|
63 |
if (uri == "root-name") rootName = value; |
|
64 |
else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“"); |
|
65 |
} |
|
66 |
||
67 |
void process() { |
|
1
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
vmime::utility::inputStreamAdapter is(*input); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
vmime::string data; |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
vmime::utility::outputStreamStringAdapter os(data); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
vmime::utility::bufferedStreamCopy(is, os); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
vmime::message m; |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
m.parse(data); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
// 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
|
77 |
// m.parse(is, 0); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
vmime::charset ch(vmime::charsets::UTF_8); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
//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
|
82 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
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
|
84 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
xmlpp::Element* headers = root->add_child("headers"); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
87 |
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
|
88 |
// TODO: Are names always ASCII and subset of UTF-8? |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
// TODO: Convert header names to lower case? (they should be case insensitive) |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
xmlpp::Element* field = headers->add_child(nameCodec.encode(mimeField->getName())); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
92 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
93 |
if (auto value = mimeField->getValue<vmime::text>()) { |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
94 |
field->add_child_text(value->getConvertedText(ch)); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
95 |
} 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
|
96 |
std::string name = value->getName().getConvertedText(ch); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
97 |
std::string email = value->getEmail().toString(); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
98 |
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
|
99 |
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
|
100 |
} 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
|
101 |
for (auto address : value->getAddressList()) { |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
102 |
xmlpp::Element* addressField = field->add_child("address"); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
103 |
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
|
104 |
std::string name = mailbox->getName().getConvertedText(ch); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
105 |
std::string email = mailbox->getEmail().toString(); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
106 |
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
|
107 |
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
|
108 |
} 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
|
109 |
// TODO: mailboxGroup? |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
110 |
} |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
111 |
} |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
112 |
} else if (auto value = mimeField->getValue<vmime::datetime>()) { |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
113 |
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
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
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
|
120 |
field->add_child_text(format(value)); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
121 |
} else if (auto value = mimeField->getValue<vmime::mediaType>()) { |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
122 |
std::string type = value->getType(); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
123 |
std::string subType = value->getSubType(); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
124 |
if (type.size()) field->set_attribute("type", type); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
125 |
if (subType.size()) field->set_attribute("subType", subType); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
126 |
if (type.size() && subType.size()) field->add_child_text(type + "/" + subType); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
127 |
// 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
|
128 |
} 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
|
129 |
field->set_attribute("left", value->getLeft()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
130 |
field->set_attribute("right", value->getRight()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
131 |
field->add_child_text(value->getId()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
132 |
} 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
|
133 |
for (auto messageId : value->getMessageIdList()) { |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
134 |
xmlpp::Element* messageIdField = field->add_child("Mssage-ID"); // TODO: lower case? |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
135 |
messageIdField->set_attribute("left", messageId->getLeft()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
136 |
messageIdField->set_attribute("right", messageId->getRight()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
137 |
messageIdField->add_child_text(messageId->getId()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
138 |
} |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
139 |
} 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
|
140 |
field->add_child_text(value->getName()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
141 |
} 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
|
142 |
field->set_attribute("from", value->getFrom()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
143 |
field->set_attribute("via", value->getVia()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
144 |
field->set_attribute("by", value->getBy()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
145 |
field->set_attribute("id", value->getId()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
146 |
field->set_attribute("for", value->getFor()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
147 |
// TODO: date of Received/relay |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
148 |
// 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
|
149 |
} 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
|
150 |
std::string local = value->getLocalPart(); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
151 |
std::string domain = value->getDomain(); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
152 |
if (local.size()) field->set_attribute("local", local); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
153 |
if (domain.size()) field->set_attribute("domain", domain); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
154 |
if (local.size() && domain.size()) field->add_child_text(local + "@" + domain); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
155 |
} 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
|
156 |
field->add_child_text(value->getName()); |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
157 |
} else { |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
158 |
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
|
159 |
} |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
160 |
} |
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
161 |
|
356d0b024abf
basic MIME header support
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
162 |
xmlpp::Element* body = root->add_child("body"); |
0 | 163 |
} |
164 |
}; |
|
165 |
||
166 |
} |
|
167 |
} |
|
168 |
} |