mapiTextPart.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. // based on htmlTextPart, but with additions
  18. // we cannot use a class derived from htmlTextPart, since that class has alot of privates
  19. //
  20. // VMime library (http://www.vmime.org)
  21. // Copyright (C) 2002-2009 Vincent Richard <vincent@vincent-richard.net>
  22. //
  23. // This program is free software; you can redistribute it and/or
  24. // modify it under the terms of the GNU General Public License as
  25. // published by the Free Software Foundation; either version 3 of
  26. // the License, or (at your option) any later version.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. // General Public License for more details.
  32. //
  33. // You should have received a copy of the GNU General Public License along
  34. // with this program; if not, write to the Free Software Foundation, Inc.,
  35. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  36. //
  37. // Linking this library statically or dynamically with other modules is making
  38. // a combined work based on this library. Thus, the terms and conditions of
  39. // the GNU General Public License cover the whole combination.
  40. //
  41. #include <memory>
  42. #include "mapiTextPart.h"
  43. #include <vmime/exception.hpp>
  44. #include <vmime/contentTypeField.hpp>
  45. #include <vmime/contentDisposition.hpp>
  46. #include <vmime/contentDispositionField.hpp>
  47. #include <vmime/text.hpp>
  48. #include <vmime/emptyContentHandler.hpp>
  49. #include <vmime/stringContentHandler.hpp>
  50. #include <vmime/utility/outputStreamAdapter.hpp>
  51. namespace vmime {
  52. mapiTextPart::mapiTextPart()
  53. : m_plainText(vmime::make_shared<emptyContentHandler>()),
  54. m_text(vmime::make_shared<emptyContentHandler>()),
  55. m_otherText(vmime::make_shared<emptyContentHandler>())
  56. {
  57. m_bHaveOtherCharset = false;
  58. }
  59. const mediaType mapiTextPart::getType() const
  60. {
  61. // TODO: fixme?
  62. if (m_text->isEmpty())
  63. return (mediaType(mediaTypes::TEXT, mediaTypes::TEXT_PLAIN));
  64. else
  65. return (mediaType(mediaTypes::TEXT, mediaTypes::TEXT_HTML));
  66. }
  67. size_t mapiTextPart::getPartCount() const
  68. {
  69. int count = 0;
  70. if (!m_plainText->isEmpty())
  71. ++count;
  72. if (!m_text->isEmpty())
  73. ++count;
  74. if (!m_otherText->isEmpty())
  75. ++count;
  76. return count;
  77. }
  78. void mapiTextPart::generateIn(vmime::shared_ptr<bodyPart> /* message */,
  79. vmime::shared_ptr<bodyPart> parent) const
  80. {
  81. // Plain text
  82. if (!m_plainText->isEmpty())
  83. {
  84. // -- Create a new part
  85. auto part = vmime::make_shared<bodyPart>();
  86. parent->getBody()->appendPart(part);
  87. // -- Set contents
  88. part->getBody()->setContents(m_plainText,
  89. mediaType(mediaTypes::TEXT, mediaTypes::TEXT_PLAIN), m_charset,
  90. encoding::decide(m_plainText, m_charset, encoding::USAGE_TEXT));
  91. }
  92. // HTML text
  93. // -- Create a new part
  94. if (!m_text->isEmpty())
  95. {
  96. auto htmlPart = vmime::make_shared<bodyPart>();
  97. // -- Set contents
  98. htmlPart->getBody()->setContents(m_text,
  99. mediaType(mediaTypes::TEXT, mediaTypes::TEXT_HTML), m_charset,
  100. encoding::decide(m_text, m_charset, encoding::USAGE_TEXT));
  101. // Handle the case we have embedded objects
  102. if (!m_objects.empty())
  103. {
  104. // Create a "multipart/related" body part
  105. auto relPart = vmime::make_shared<bodyPart>();
  106. parent->getBody()->appendPart(relPart);
  107. relPart->getHeader()->ContentType()->
  108. setValue(mediaType(mediaTypes::MULTIPART, mediaTypes::MULTIPART_RELATED));
  109. // Add the HTML part into this part
  110. relPart->getBody()->appendPart(htmlPart);
  111. // Also add objects into this part
  112. for (auto obj : m_objects) {
  113. auto objPart = vmime::make_shared<bodyPart>();
  114. relPart->getBody()->appendPart(objPart);
  115. std::string id = obj->getId();
  116. std::string name = obj->getName();
  117. if (id.substr(0, 4) == "CID:")
  118. id = id.substr(4);
  119. // throw an error when id and location are empty?
  120. objPart->getHeader()->ContentType()->setValue(obj->getType());
  121. if (!id.empty())
  122. objPart->getHeader()->ContentId()->setValue(messageId("<" + id + ">"));
  123. objPart->getHeader()->ContentDisposition()->setValue(contentDisposition(contentDispositionTypes::INLINE));
  124. objPart->getHeader()->ContentTransferEncoding()->setValue(obj->getEncoding());
  125. if (!obj->getLocation().empty())
  126. objPart->getHeader()->ContentLocation()->setValue(obj->getLocation());
  127. //encoding(encodingTypes::BASE64);
  128. if (!name.empty())
  129. vmime::dynamicCast<vmime::contentDispositionField>(objPart->getHeader()->ContentDisposition())->setFilename(vmime::word(name));
  130. objPart->getBody()->setContents(obj->getData()->clone());
  131. }
  132. }
  133. else
  134. {
  135. // Add the HTML part into the parent part
  136. parent->getBody()->appendPart(htmlPart);
  137. }
  138. } // if (html)
  139. // Other text
  140. if (!m_otherText->isEmpty())
  141. {
  142. // -- Create a new part
  143. auto otherPart = vmime::make_shared<bodyPart>();
  144. parent->getBody()->appendPart(otherPart);
  145. // used by ical
  146. if (!m_otherMethod.empty())
  147. {
  148. auto p = vmime::make_shared<vmime::parameter>("method");
  149. p->component::parse(m_otherMethod);
  150. vmime::dynamicCast<contentTypeField>(otherPart->getHeader()->ContentType())->appendParameter(p);
  151. }
  152. // -- Set contents
  153. otherPart->getBody()->setContents(m_otherText, m_otherMediaType, m_bHaveOtherCharset ? m_otherCharset : m_charset, m_otherEncoding);
  154. }
  155. }
  156. void mapiTextPart::findEmbeddedParts(const bodyPart& part,
  157. std::vector<vmime::shared_ptr<const bodyPart> > &cidParts,
  158. std::vector<vmime::shared_ptr<const bodyPart> > &locParts)
  159. {
  160. for (size_t i = 0 ; i < part.getBody()->getPartCount() ; ++i)
  161. {
  162. auto p = part.getBody()->getPartAt(i);
  163. // For a part to be an embedded object, it must have a
  164. // Content-Id field or a Content-Location field.
  165. if (p->getHeader()->hasField(fields::CONTENT_ID))
  166. cidParts.push_back(p);
  167. if (p->getHeader()->hasField(fields::CONTENT_LOCATION))
  168. locParts.push_back(p);
  169. findEmbeddedParts(*p, cidParts, locParts);
  170. }
  171. }
  172. void mapiTextPart::addEmbeddedObject(const bodyPart& part, const string& id)
  173. {
  174. // The object may already exists. This can happen if an object is
  175. // identified by both a Content-Id and a Content-Location. In this
  176. // case, there will be two embedded objects with two different IDs
  177. // but referencing the same content.
  178. mediaType type;
  179. if (part.getHeader()->hasField(fields::CONTENT_TYPE)) {
  180. auto ctf = part.getHeader()->ContentType();
  181. type = *vmime::dynamicCast<const vmime::mediaType>(ctf->getValue());
  182. }
  183. // Else assume "application/octet-stream".
  184. m_objects.push_back(vmime::make_shared<embeddedObject>(
  185. vmime::dynamicCast<vmime::contentHandler>(part.getBody()->getContents()->clone()),
  186. part.getBody()->getEncoding(), id, type, string(), string()));
  187. }
  188. void mapiTextPart::parse(vmime::shared_ptr<const vmime::bodyPart> message,
  189. vmime::shared_ptr<const vmime::bodyPart> parent,
  190. vmime::shared_ptr<const vmime::bodyPart> textPart)
  191. {
  192. // Search for possible embedded objects in the _whole_ message.
  193. std::vector<vmime::shared_ptr<const vmime::bodyPart> > cidParts;
  194. std::vector<vmime::shared_ptr<const vmime::bodyPart> > locParts;
  195. findEmbeddedParts(*message, cidParts, locParts);
  196. // Extract HTML text
  197. std::ostringstream oss;
  198. utility::outputStreamAdapter adapter(oss);
  199. textPart->getBody()->getContents()->extract(adapter);
  200. const string data = oss.str();
  201. m_text = textPart->getBody()->getContents()->clone();
  202. if (textPart->getHeader()->hasField(fields::CONTENT_TYPE)) {
  203. auto ctf = vmime::dynamicCast<vmime::contentTypeField>(textPart->getHeader()->findField(fields::CONTENT_TYPE));
  204. m_charset = ctf->getCharset();
  205. }
  206. // Extract embedded objects. The algorithm is quite simple: for each previously
  207. // found inline part, we check if its CID/Location is contained in the HTML text.
  208. for (const auto &part : cidParts) {
  209. auto midField = part->getHeader()->findField(fields::CONTENT_ID);
  210. auto mid = *vmime::dynamicCast<const vmime::messageId>(midField->getValue());
  211. if (data.find("CID:" + mid.getId()) != string::npos ||
  212. data.find("cid:" + mid.getId()) != string::npos)
  213. // This part is referenced in the HTML text.
  214. // Add it to the embedded object list.
  215. addEmbeddedObject(*part, mid.getId());
  216. }
  217. for (const auto &part : locParts) {
  218. auto locField = part->getHeader()->findField(fields::CONTENT_LOCATION);
  219. auto loc = *vmime::dynamicCast<const vmime::text>(locField->getValue());
  220. const string locStr = loc.getWholeBuffer();
  221. if (data.find(locStr) != string::npos)
  222. // This part is referenced in the HTML text.
  223. // Add it to the embedded object list.
  224. addEmbeddedObject(*part, locStr);
  225. }
  226. // Extract plain text, if any.
  227. if (!findPlainTextPart(*message, *parent, *textPart))
  228. {
  229. m_plainText = vmime::make_shared<vmime::emptyContentHandler>();
  230. }
  231. }
  232. bool mapiTextPart::findPlainTextPart(const bodyPart& part, const bodyPart& parent, const bodyPart& textPart)
  233. {
  234. // We search for the nearest "multipart/alternative" part.
  235. if (part.getHeader()->hasField(fields::CONTENT_TYPE)) {
  236. auto ctf = part.getHeader()->findField(fields::CONTENT_TYPE);
  237. auto type = *vmime::dynamicCast<const vmime::mediaType>(ctf->getValue());
  238. if (type.getType() == mediaTypes::MULTIPART &&
  239. type.getSubType() == mediaTypes::MULTIPART_ALTERNATIVE)
  240. {
  241. vmime::shared_ptr<const vmime::bodyPart> foundPart;
  242. for (size_t i = 0; i < part.getBody()->getPartCount(); ++i) {
  243. auto p = part.getBody()->getPartAt(i);
  244. if (p.get() == &parent || // if "text/html" is in "multipart/related"
  245. p.get() == &textPart) // if not...
  246. {
  247. foundPart = p;
  248. }
  249. }
  250. if (foundPart)
  251. {
  252. bool found = false;
  253. // Now, search for the alternative plain text part
  254. for (size_t i = 0; !found && i < part.getBody()->getPartCount(); ++i) {
  255. auto p = part.getBody()->getPartAt(i);
  256. if (p->getHeader()->hasField(fields::CONTENT_TYPE)) {
  257. auto ctf = p->getHeader()->findField(fields::CONTENT_TYPE);
  258. auto type = *vmime::dynamicCast<const vmime::mediaType>(ctf->getValue());
  259. if (type.getType() == mediaTypes::TEXT &&
  260. type.getSubType() == mediaTypes::TEXT_PLAIN)
  261. {
  262. m_plainText = p->getBody()->getContents()->clone();
  263. found = true;
  264. }
  265. }
  266. }
  267. // If we don't have found the plain text part here, it means that
  268. // it does not exist (the MUA which built this message probably
  269. // did not include it...).
  270. return found;
  271. }
  272. }
  273. }
  274. bool found = false;
  275. for (size_t i = 0; !found && i < part.getBody()->getPartCount(); ++i)
  276. found = findPlainTextPart(*part.getBody()->getPartAt(i), parent, textPart);
  277. return found;
  278. }
  279. void mapiTextPart::setCharset(const charset& ch)
  280. {
  281. m_charset = ch;
  282. }
  283. void mapiTextPart::setPlainText(vmime::shared_ptr<vmime::contentHandler> plainText)
  284. {
  285. m_plainText = plainText->clone();
  286. }
  287. void mapiTextPart::setOtherText(vmime::shared_ptr<vmime::contentHandler> otherText)
  288. {
  289. m_otherText = otherText->clone();
  290. }
  291. void mapiTextPart::setOtherContentType(const mediaType& type)
  292. {
  293. m_otherMediaType = type;
  294. }
  295. void mapiTextPart::setOtherContentEncoding(const encoding& enc)
  296. {
  297. m_otherEncoding = enc;
  298. }
  299. void mapiTextPart::setOtherMethod(const string& method)
  300. {
  301. m_otherMethod = method;
  302. }
  303. void mapiTextPart::setOtherCharset(const charset& ch)
  304. {
  305. m_otherCharset = ch;
  306. m_bHaveOtherCharset = true;
  307. }
  308. void mapiTextPart::setText(vmime::shared_ptr<vmime::contentHandler> text)
  309. {
  310. m_text = text->clone();
  311. }
  312. vmime::shared_ptr<const mapiTextPart::embeddedObject>
  313. mapiTextPart::findObject(const std::string &id_) const
  314. {
  315. const string id = cleanId(id_);
  316. for (const auto &obj : m_objects)
  317. if (obj->getId() == id)
  318. return obj;
  319. return vmime::null;
  320. }
  321. bool mapiTextPart::hasObject(const string& id_) const
  322. {
  323. const string id = cleanId(id_);
  324. for (const auto &obj : m_objects)
  325. if (obj->getId() == id)
  326. return true;
  327. return false;
  328. }
  329. const std::string
  330. mapiTextPart::addObject(vmime::shared_ptr<vmime::contentHandler> data,
  331. const vmime::encoding &enc, const vmime::mediaType &type)
  332. {
  333. const messageId mid(messageId::generateId());
  334. const string id = mid.getId();
  335. return "CID:" + addObject(data, enc, type, id);
  336. }
  337. const string mapiTextPart::addObject(vmime::shared_ptr<vmime::contentHandler> data,
  338. const vmime::mediaType &type)
  339. {
  340. return addObject(data, encoding::decide(data), type);
  341. }
  342. const string mapiTextPart::addObject(const string& data, const mediaType& type)
  343. {
  344. auto cts = vmime::make_shared<vmime::stringContentHandler>(data);
  345. return addObject(cts, encoding::decide(cts), type);
  346. }
  347. const string mapiTextPart::addObject(vmime::shared_ptr<vmime::contentHandler> data,
  348. const vmime::encoding &enc, const vmime::mediaType &type,
  349. const std::string &id, const std::string &name, const std::string &loc)
  350. {
  351. m_objects.push_back(vmime::make_shared<embeddedObject>(data, enc, id, type, name, loc));
  352. return (id);
  353. }
  354. // static
  355. const string mapiTextPart::cleanId(const string& id)
  356. {
  357. if (id.length() >= 4 &&
  358. (id[0] == 'c' || id[0] == 'C') &&
  359. (id[1] == 'i' || id[1] == 'I') &&
  360. (id[2] == 'd' || id[2] == 'D') &&
  361. id[3] == ':')
  362. {
  363. return id.substr(4);
  364. }
  365. else
  366. {
  367. return id;
  368. }
  369. }
  370. //
  371. // mapiTextPart::embeddedObject
  372. //
  373. mapiTextPart::embeddedObject::embeddedObject(vmime::shared_ptr<vmime::contentHandler> data,
  374. const vmime::encoding &enc, const std::string &id,
  375. const vmime::mediaType &type, const std::string &name,
  376. const std::string &loc) :
  377. m_data(vmime::dynamicCast<vmime::contentHandler>(data->clone())),
  378. m_encoding(enc), m_id(id), m_type(type), m_name(name), m_loc(loc)
  379. {
  380. }
  381. } // vmime