| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include "content_type.hh" |
|---|
| 21 | #include "multipart.hh" |
|---|
| 22 | #include "../tlate/data_model.hh" |
|---|
| 23 | #include "../tlate/list_value.hh" |
|---|
| 24 | |
|---|
| 25 | namespace msg |
|---|
| 26 | { |
|---|
| 27 | multipart::multipart(entity_header::ptr hdr, std::string str) |
|---|
| 28 | : entity(hdr) |
|---|
| 29 | { |
|---|
| 30 | content_type ct = get_content_type(); |
|---|
| 31 | if (ct.get_type() != content_type::MULTIPART) |
|---|
| 32 | throw std::logic_error("not multipart"); |
|---|
| 33 | std::string boundary = ct.get_param("boundary"); |
|---|
| 34 | size_t bs = str.find("\r\n--"+boundary); |
|---|
| 35 | preamble = str.substr(0,bs); |
|---|
| 36 | std::string default_ct = ct.get_subtype() == "digest" |
|---|
| 37 | ? "message/rfc822" |
|---|
| 38 | : "text/plain; charset=\"US-ASCII\""; |
|---|
| 39 | while (bs != std::string::npos) |
|---|
| 40 | { |
|---|
| 41 | size_t be = str.find("\r\n", bs+2); |
|---|
| 42 | if (be == std::string::npos) be = str.length() - 2; |
|---|
| 43 | if (str.substr(bs + 4 + boundary.length(), 2) == "--") |
|---|
| 44 | { |
|---|
| 45 | trailer = be + 2 < str.length() |
|---|
| 46 | ? str.substr(be+2) |
|---|
| 47 | : ""; |
|---|
| 48 | break; |
|---|
| 49 | } |
|---|
| 50 | size_t ns = str.find("\r\n--"+boundary, be); |
|---|
| 51 | std::string sub = str.substr(be+2, ns - be); |
|---|
| 52 | entity::ptr sen(entity::mk(sub, false, default_ct)); |
|---|
| 53 | parts.push_back(sen); |
|---|
| 54 | bs = ns; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | boost::shared_ptr<tlate::value> |
|---|
| 59 | multipart::get_tlate_value(bool hide_addresses) const |
|---|
| 60 | { |
|---|
| 61 | tlate::list_value::ptr li(new tlate::list_value); |
|---|
| 62 | for (size_t i = 0; i < parts.size(); i++) |
|---|
| 63 | { |
|---|
| 64 | entity::ptr en = parts[i]; |
|---|
| 65 | li->push_back(en->get_tlate_value(hide_addresses)); |
|---|
| 66 | } |
|---|
| 67 | tlate::data_model::ptr am(new tlate::data_model); |
|---|
| 68 | am->insert("multipart", li); |
|---|
| 69 | return am; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | std::string multipart::get_body() const |
|---|
| 73 | { |
|---|
| 74 | content_type ct = get_content_type(); |
|---|
| 75 | std::string boundary = ct.get_param("boundary"); |
|---|
| 76 | std::ostringstream os; |
|---|
| 77 | os << preamble; |
|---|
| 78 | for (size_t i = 0; i < parts.size(); i++) |
|---|
| 79 | { |
|---|
| 80 | os << "\r\n--" << boundary << "\r\n"; |
|---|
| 81 | os << parts[i]->get_entity(); |
|---|
| 82 | } |
|---|
| 83 | os << "\r\n--" << boundary << "--\r\n"; |
|---|
| 84 | os << trailer; |
|---|
| 85 | return os.str(); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|