33 private: |
33 private: |
34 std::istream* input = nullptr; |
34 std::istream* input = nullptr; |
35 xmlpp::DomParser* parser = nullptr; |
35 xmlpp::DomParser* parser = nullptr; |
36 XMLNameCodec nameCodec; |
36 XMLNameCodec nameCodec; |
37 |
37 |
38 enum class Mode { |
38 std::string rootName = "mime-message"; |
39 ROOT, |
|
40 SEQUENCE, |
|
41 MAPPING, |
|
42 MAP_KEY |
|
43 }; |
|
44 |
39 |
45 std::string rootName = "mime"; |
40 std::string format(std::shared_ptr<vmime::datetime> value) { |
46 xmlpp::Element* current; |
41 std::stringstream timestamp; |
47 std::vector<Mode> mode; |
42 int tz = value->getZone(); |
48 |
43 timestamp << value->getYear() << "-"; |
49 /** |
44 timestamp << std::setw(2) << std::setfill('0') << value->getMonth() << "-"; |
50 * Both MIME and XML strings are in UTF-8. |
45 timestamp << std::setw(2) << std::setfill('0') << value->getDay() << "T"; |
51 const char* y2x(mime_char_t* value) { |
46 timestamp << std::setw(2) << std::setfill('0') << value->getHour() << ":"; |
52 return value ? (const char*) value : ""; |
47 timestamp << std::setw(2) << std::setfill('0') << value->getMinute() << ":"; |
53 } |
48 timestamp << std::setw(2) << std::setfill('0') << value->getSecond() << (tz >= 0 ? "+" : "-"); |
54 |
49 timestamp << std::setw(2) << std::setfill('0') << std::abs(tz / 60) << ":"; |
55 const Glib::ustring y2xname(mime_char_t* value) { |
50 timestamp << std::setw(2) << std::setfill('0') << std::abs(tz % 60); |
56 return nameCodec.encode(y2x(value)); |
51 return timestamp.str(); |
57 } |
|
58 */ |
|
59 |
|
60 xmlpp::Element* parentOrSelf(xmlpp::Element* current) { |
|
61 return current->get_parent() == nullptr ? current : current->get_parent(); |
|
62 } |
52 } |
63 |
53 |
64 public: |
54 public: |
65 |
55 |
66 XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) { |
56 XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) { |
73 if (uri == "root-name") rootName = value; |
63 if (uri == "root-name") rootName = value; |
74 else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“"); |
64 else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“"); |
75 } |
65 } |
76 |
66 |
77 void process() { |
67 void process() { |
78 current = parser->get_document()->create_root_node(rootName); |
68 vmime::utility::inputStreamAdapter is(*input); |
|
69 vmime::string data; |
|
70 vmime::utility::outputStreamStringAdapter os(data); |
|
71 vmime::utility::bufferedStreamCopy(is, os); |
|
72 |
|
73 vmime::message m; |
|
74 m.parse(data); |
|
75 |
|
76 // vmime::shared_ptr<vmime::utility::inputStreamAdapter> is = vmime::make_shared<vmime::utility::inputStreamAdapter>(*input); |
|
77 // m.parse(is, 0); |
|
78 |
|
79 vmime::charset ch(vmime::charsets::UTF_8); |
|
80 |
|
81 //std::cerr << "Subject:" << m.getHeader()->Subject()->getValue<vmime::text>()->getConvertedText(ch) << std::endl; |
|
82 |
|
83 xmlpp::Element* root = parser->get_document()->create_root_node(rootName); |
|
84 |
|
85 xmlpp::Element* headers = root->add_child("headers"); |
|
86 |
|
87 for (std::shared_ptr<vmime::headerField> mimeField : m.getHeader()->getFieldList()) { |
|
88 // TODO: Are names always ASCII and subset of UTF-8? |
|
89 // TODO: Convert header names to lower case? (they should be case insensitive) |
|
90 xmlpp::Element* field = headers->add_child(nameCodec.encode(mimeField->getName())); |
|
91 |
|
92 |
|
93 if (auto value = mimeField->getValue<vmime::text>()) { |
|
94 field->add_child_text(value->getConvertedText(ch)); |
|
95 } else if (auto value = mimeField->getValue<vmime::mailbox>()) { |
|
96 std::string name = value->getName().getConvertedText(ch); |
|
97 std::string email = value->getEmail().toString(); |
|
98 if (name.size()) field->set_attribute("name", name); |
|
99 if (email.size()) field->add_child_text(email); |
|
100 } else if (auto value = mimeField->getValue<vmime::addressList>()) { |
|
101 for (auto address : value->getAddressList()) { |
|
102 xmlpp::Element* addressField = field->add_child("address"); |
|
103 if (std::shared_ptr<vmime::mailbox> mailbox = std::dynamic_pointer_cast<vmime::mailbox> (address)) { |
|
104 std::string name = mailbox->getName().getConvertedText(ch); |
|
105 std::string email = mailbox->getEmail().toString(); |
|
106 if (name.size()) addressField->set_attribute("name", name); |
|
107 if (email.size()) addressField->add_child_text(email); |
|
108 } else if (std::shared_ptr<vmime::mailboxGroup> mailbox = std::dynamic_pointer_cast<vmime::mailboxGroup> (address)) { |
|
109 // TODO: mailboxGroup? |
|
110 } |
|
111 } |
|
112 } else if (auto value = mimeField->getValue<vmime::datetime>()) { |
|
113 field->set_attribute("year", std::to_string(value->getYear())); |
|
114 field->set_attribute("month", std::to_string(value->getMonth())); |
|
115 field->set_attribute("day", std::to_string(value->getDay())); |
|
116 field->set_attribute("hour", std::to_string(value->getHour())); |
|
117 field->set_attribute("minute", std::to_string(value->getMinute())); |
|
118 field->set_attribute("second", std::to_string(value->getSecond())); |
|
119 field->set_attribute("zone", std::to_string(value->getZone())); // timezone is in minutes |
|
120 field->add_child_text(format(value)); |
|
121 } else if (auto value = mimeField->getValue<vmime::mediaType>()) { |
|
122 std::string type = value->getType(); |
|
123 std::string subType = value->getSubType(); |
|
124 if (type.size()) field->set_attribute("type", type); |
|
125 if (subType.size()) field->set_attribute("subType", subType); |
|
126 if (type.size() && subType.size()) field->add_child_text(type + "/" + subType); |
|
127 // TODO: encoding from the "Content-Type: text/plain; charset=us-ascii" type header? |
|
128 } else if (auto value = mimeField->getValue<vmime::messageId>()) { |
|
129 field->set_attribute("left", value->getLeft()); |
|
130 field->set_attribute("right", value->getRight()); |
|
131 field->add_child_text(value->getId()); |
|
132 } else if (auto value = mimeField->getValue<vmime::messageIdSequence>()) { |
|
133 for (auto messageId : value->getMessageIdList()) { |
|
134 xmlpp::Element* messageIdField = field->add_child("Mssage-ID"); // TODO: lower case? |
|
135 messageIdField->set_attribute("left", messageId->getLeft()); |
|
136 messageIdField->set_attribute("right", messageId->getRight()); |
|
137 messageIdField->add_child_text(messageId->getId()); |
|
138 } |
|
139 } else if (auto value = mimeField->getValue<vmime::contentDisposition>()) { |
|
140 field->add_child_text(value->getName()); |
|
141 } else if (auto value = mimeField->getValue<vmime::relay>()) { |
|
142 field->set_attribute("from", value->getFrom()); |
|
143 field->set_attribute("via", value->getVia()); |
|
144 field->set_attribute("by", value->getBy()); |
|
145 field->set_attribute("id", value->getId()); |
|
146 field->set_attribute("for", value->getFor()); |
|
147 // TODO: date of Received/relay |
|
148 // TODO: missing values or incomplete parsing of Received/relay in vmime |
|
149 } else if (auto value = mimeField->getValue<vmime::path>()) { |
|
150 std::string local = value->getLocalPart(); |
|
151 std::string domain = value->getDomain(); |
|
152 if (local.size()) field->set_attribute("local", local); |
|
153 if (domain.size()) field->set_attribute("domain", domain); |
|
154 if (local.size() && domain.size()) field->add_child_text(local + "@" + domain); |
|
155 } else if (auto value = mimeField->getValue<vmime::encoding>()) { |
|
156 field->add_child_text(value->getName()); |
|
157 } else { |
|
158 field->add_child_text("TODO: unknown header type"); // TODO: generic conversion as fallback? |
|
159 } |
|
160 } |
|
161 |
|
162 xmlpp::Element* body = root->add_child("body"); |
79 } |
163 } |
80 }; |
164 }; |
81 |
165 |
82 } |
166 } |
83 } |
167 } |