| 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 "entity_header.hh" |
|---|
| 22 | #include "text_plain.hh" |
|---|
| 23 | |
|---|
| 24 | #include "../html/util.hh" |
|---|
| 25 | #include "../tlate/list_value.hh" |
|---|
| 26 | #include "../tlate/data_model.hh" |
|---|
| 27 | #include "../util.hh" |
|---|
| 28 | |
|---|
| 29 | namespace |
|---|
| 30 | { |
|---|
| 31 | msg::entity_header::ptr ct_hdr(msg::content_type ct) |
|---|
| 32 | { |
|---|
| 33 | std::string str = |
|---|
| 34 | std::string("Content-Type: ") + |
|---|
| 35 | ct.to_string() + "\r\n" + |
|---|
| 36 | "Content-Transfer-Encoding: 8bit\r\n" + |
|---|
| 37 | "\r\n"; |
|---|
| 38 | msg::entity_header::ptr rv(new msg::entity_header(str, |
|---|
| 39 | false, |
|---|
| 40 | ct.to_string() |
|---|
| 41 | ) |
|---|
| 42 | ); |
|---|
| 43 | return rv; |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | namespace msg |
|---|
| 48 | { |
|---|
| 49 | text_plain::text_plain(entity_header::ptr hdr, std::string body) |
|---|
| 50 | : entity(hdr) |
|---|
| 51 | { |
|---|
| 52 | init(body); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | text_plain::text_plain(std::string body, content_type ct) |
|---|
| 56 | : entity(ct_hdr(ct)) |
|---|
| 57 | { |
|---|
| 58 | init(body); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | text_plain::text_plain(entity::const_ptr e) |
|---|
| 62 | : entity(*e) |
|---|
| 63 | { |
|---|
| 64 | init(e->get_body()); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void text_plain::init(std::string text) |
|---|
| 68 | { |
|---|
| 69 | raw_body = text; |
|---|
| 70 | content_type ct = get_content_type(); |
|---|
| 71 | if (ct.get_type() != msg::content_type::TEXT || |
|---|
| 72 | ct.get_subtype() != "plain") |
|---|
| 73 | { |
|---|
| 74 | para p(0); |
|---|
| 75 | p.push_back("[This entity is not plain text.]"); |
|---|
| 76 | paras.push_back(p); |
|---|
| 77 | |
|---|
| 78 | return; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | if (util::to_lower(ct.get_param("format")) != "flowed") |
|---|
| 82 | { |
|---|
| 83 | while (!text.empty()) |
|---|
| 84 | { |
|---|
| 85 | size_t ps = text.find("\r\n\r\n"); |
|---|
| 86 | std::string pars = text.substr(0, ps); |
|---|
| 87 | if (ps == std::string::npos) |
|---|
| 88 | text.clear(); |
|---|
| 89 | else |
|---|
| 90 | text.erase(0, ps+4); |
|---|
| 91 | para p(0); |
|---|
| 92 | while (!pars.empty()) |
|---|
| 93 | { |
|---|
| 94 | size_t nl = pars.find("\r\n"); |
|---|
| 95 | std::string line = pars.substr(0, nl); |
|---|
| 96 | if (nl == std::string::npos) |
|---|
| 97 | pars.clear(); |
|---|
| 98 | else |
|---|
| 99 | pars.erase(0, nl+2); |
|---|
| 100 | p.push_back(line); |
|---|
| 101 | } |
|---|
| 102 | paras.push_back(p); |
|---|
| 103 | } |
|---|
| 104 | return; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | const bool delsp = util::to_lower(ct.get_param("delsp")) |
|---|
| 108 | == "true"; |
|---|
| 109 | |
|---|
| 110 | std::string cp; |
|---|
| 111 | size_t cqd = 0; |
|---|
| 112 | bool eop = false; |
|---|
| 113 | while (!text.empty()) |
|---|
| 114 | { |
|---|
| 115 | size_t nl = text.find("\r\n"); |
|---|
| 116 | std::string line = text.substr(0, nl); |
|---|
| 117 | if (nl == std::string::npos) |
|---|
| 118 | text.clear(); |
|---|
| 119 | else |
|---|
| 120 | text.erase(0, nl+2); |
|---|
| 121 | size_t qd = line.find_first_not_of(">"); |
|---|
| 122 | if (qd == std::string::npos) qd = line.length(); |
|---|
| 123 | if (cqd == size_t(-1)) cqd = qd; |
|---|
| 124 | if (!line.empty() && line[0] == ' ') |
|---|
| 125 | line.erase(0,1); |
|---|
| 126 | if (qd > 0) line.erase(0, qd); |
|---|
| 127 | if (line == "-- " || cp == "-- " || qd != cqd || eop) |
|---|
| 128 | { |
|---|
| 129 | para p(cqd); |
|---|
| 130 | p.push_back(cp); |
|---|
| 131 | paras.push_back(p); |
|---|
| 132 | cp = ""; |
|---|
| 133 | cqd = qd; |
|---|
| 134 | } |
|---|
| 135 | eop = line.empty() || line[line.length()-1] != ' '; |
|---|
| 136 | if (delsp && |
|---|
| 137 | !line.empty() && line[line.length()-1] == ' ') |
|---|
| 138 | line.erase(line.length()-1); |
|---|
| 139 | cp += line; |
|---|
| 140 | } |
|---|
| 141 | if (!cp.empty()) |
|---|
| 142 | { |
|---|
| 143 | para p(cqd); |
|---|
| 144 | p.push_back(cp); |
|---|
| 145 | paras.push_back(p); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | boost::shared_ptr<tlate::value> |
|---|
| 150 | text_plain::get_tlate_value(bool authn) const |
|---|
| 151 | { |
|---|
| 152 | tlate::list_value::ptr bodyv(new tlate::list_value); |
|---|
| 153 | size_t qd = 0; |
|---|
| 154 | for (size_t i = 0; i < num_paras(); i++) |
|---|
| 155 | { |
|---|
| 156 | const msg::text_plain::para &par = get_para(i); |
|---|
| 157 | |
|---|
| 158 | while (qd < par.quote_depth()) |
|---|
| 159 | { |
|---|
| 160 | tlate::data_model::ptr tag |
|---|
| 161 | (new tlate::data_model); |
|---|
| 162 | tag->insert("start_quote"); |
|---|
| 163 | bodyv->push_back(tag); |
|---|
| 164 | qd++; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | while (qd > par.quote_depth()) |
|---|
| 168 | { |
|---|
| 169 | tlate::data_model::ptr tag |
|---|
| 170 | (new tlate::data_model); |
|---|
| 171 | tag->insert("end_quote"); |
|---|
| 172 | bodyv->push_back(tag); |
|---|
| 173 | qd--; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | tlate::list_value::ptr parav(new tlate::list_value); |
|---|
| 177 | for (size_t j = 0; j < par.num_lines(); j++) |
|---|
| 178 | { |
|---|
| 179 | if (j > 0) |
|---|
| 180 | { |
|---|
| 181 | tlate::data_model::ptr tag |
|---|
| 182 | (new tlate::data_model); |
|---|
| 183 | tag->insert("line_separator"); |
|---|
| 184 | parav->push_back(tag); |
|---|
| 185 | } |
|---|
| 186 | std::string line = par.get_line(j); |
|---|
| 187 | tlate::string_value::ptr linev |
|---|
| 188 | (new tlate::string_value |
|---|
| 189 | (html::quote(line, !authn))); |
|---|
| 190 | parav->push_back(linev); |
|---|
| 191 | } |
|---|
| 192 | bodyv->push_back(parav); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | while (qd > 0) |
|---|
| 196 | { |
|---|
| 197 | tlate::data_model::ptr tag(new tlate::data_model); |
|---|
| 198 | tag->insert("end_quote"); |
|---|
| 199 | bodyv->push_back(tag); |
|---|
| 200 | qd--; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | tlate::data_model::ptr rv(new tlate::data_model); |
|---|
| 204 | rv->insert("text_plain", bodyv); |
|---|
| 205 | return rv; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|