#include "bodyparser.hh"
|
|
#include "pEpMIME_internal.hh"
|
|
#include "rules.hh"
|
|
#include "base64.hxx"
|
|
#include "quoted_printable.hxx"
|
|
#include "string_case.hh"
|
|
#include "nfc.hh"
|
|
#include "to_utf8.hh"
|
|
#include <pEp/pEp_string.h>
|
|
|
|
#include <boost/spirit/include/qi.hpp>
|
|
#include <boost/spirit/include/phoenix.hpp>
|
|
#include <boost/fusion/include/adapt_struct.hpp>
|
|
|
|
|
|
namespace qi = boost::spirit::qi;
|
|
namespace px = boost::phoenix;
|
|
|
|
using qi::_val;
|
|
using qi::_1;
|
|
|
|
struct ContentType
|
|
{
|
|
std::string type = "text";
|
|
std::string subtype = "plain";
|
|
std::vector<pEpMIME::NameValue> params;
|
|
void tolower(); // only for ASCII chars, but that's sufficient here.
|
|
void unwrap(); // reverses the wrapping of overlong (andtherefore split) parameter values.
|
|
void sanitize()
|
|
{
|
|
tolower();
|
|
unwrap();
|
|
}
|
|
};
|
|
|
|
struct Rfc2231ParamName
|
|
{
|
|
std::string name;
|
|
int count = -1;
|
|
bool ext_value = false; // extended value: charset'language'encoded_value
|
|
};
|
|
|
|
struct Rfc2231ParamValue
|
|
{
|
|
std::string charset;
|
|
// language is ignored
|
|
std::string value;
|
|
};
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
ContentType,
|
|
(std::string, type)
|
|
(std::string, subtype)
|
|
(std::vector<pEpMIME::NameValue>, params)
|
|
)
|
|
|
|
/*
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
Rfc2231ParamName,
|
|
(std::string, name)
|
|
(unsigned, count)
|
|
(bool, with_charset)
|
|
)
|
|
*/
|
|
|
|
// that boost::fusion magic seems to work only in the actual TU
|
|
// so it has to be defined here, instead of at the end of pEpMIME_internal.cc *sigh*
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
pEpMIME::NameValue,
|
|
(std::string, name)
|
|
(std::string, value)
|
|
)
|
|
|
|
|
|
void ContentType::tolower()
|
|
{
|
|
pEpMIME::ascii_tolower(type);
|
|
pEpMIME::ascii_tolower(subtype);
|
|
}
|
|
|
|
qi::uint_parser<unsigned char, 16,2,2> hex_octet;
|
|
|
|
pEpMIME::TRule<char> ext_octet = qi::lit('%') >> hex_octet;
|
|
pEpMIME::TRule<char> attrib_char = qi::ascii::print - qi::char_("]*'%()<>@,:\\\"/?=[");
|
|
|
|
pEpMIME::TRule<Rfc2231ParamName> param_name =
|
|
(+(qi::char_ - '*')) [ &(_val)->*&Rfc2231ParamName::name <<= _1 ]
|
|
>> -(qi::lit('*') >> qi::uint_[ &(_val)->*&Rfc2231ParamName::count = _1] )
|
|
>> -(qi::lit('*')[ &(_val)->*&Rfc2231ParamName::ext_value = true] );
|
|
|
|
|
|
pEpMIME::TRule<Rfc2231ParamValue> param_value =
|
|
-qi::hold[
|
|
(+qi::char_("A-Za-z0-9_./-"))[ &(_val)->*&Rfc2231ParamValue::charset <<= _1 ]
|
|
>> '\''
|
|
>> qi::omit[ *(qi::char_ - '\'') ] // language is ignored
|
|
>> '\''
|
|
] // charset & language is optional and normally only present in the 1st part
|
|
>> ( +(ext_octet | attrib_char))[ &(_val)->*&Rfc2231ParamValue::value <<= _1 ];
|
|
|
|
std::string convert(std::string& charset, const std::string& input)
|
|
{
|
|
Rfc2231ParamValue pv;
|
|
std::string::const_iterator begin = input.begin();
|
|
if(qi::parse(begin, input.end(), param_value, pv))
|
|
{
|
|
if(pv.charset.size())
|
|
{
|
|
charset = pv.charset;
|
|
}
|
|
return to_utf8(charset, pv.value);
|
|
}
|
|
return to_utf8(charset, input);
|
|
}
|
|
|
|
|
|
void ContentType::unwrap()
|
|
{
|
|
std::vector<pEpMIME::NameValue> new_params;
|
|
std::string ml_name, ml_value; // multiline parameters
|
|
std::string charset = "UTF-8";
|
|
int old_count = -1;
|
|
for(auto& p:params)
|
|
{
|
|
Rfc2231ParamName pn;
|
|
std::string::const_iterator begin = p.name.cbegin();
|
|
if(qi::parse(begin, p.name.cend(), param_name, pn))
|
|
{
|
|
const std::string& value = pn.ext_value ? convert(charset, p.value ) : p.value;
|
|
switch(pn.count)
|
|
{
|
|
case -1 : // has charset but no multi-line value
|
|
new_params.emplace_back( pn.name, value );
|
|
break;
|
|
case 0 : // start of a multi-line value
|
|
if(!ml_name.empty())
|
|
{
|
|
new_params.emplace_back( ml_name, ml_value);
|
|
}
|
|
ml_name = pn.name;
|
|
ml_value = value;
|
|
old_count = 0;
|
|
break;
|
|
default:
|
|
if(pn.name == ml_name && pn.count == old_count+1)
|
|
{
|
|
ml_value += value;
|
|
old_count = pn.count;
|
|
}else{
|
|
// non-contiguous counter -> discard it.
|
|
}
|
|
break;
|
|
}
|
|
}else{
|
|
if(!ml_name.empty())
|
|
{
|
|
new_params.emplace_back( ml_name, ml_value);
|
|
ml_name.clear(); ml_value.clear();
|
|
}
|
|
// "legacy" parameter:
|
|
new_params.emplace_back( std::move(p) );
|
|
}
|
|
}
|
|
|
|
params.swap(new_params);
|
|
}
|
|
|
|
std::ostream& operator<<(std::ostream& o, const ContentType& ct)
|
|
{
|
|
return o << "CT:{" << ct.type << "/" << ct.subtype << ". params=" << ct.params << " } ";
|
|
}
|
|
|
|
|
|
namespace pEpMIME
|
|
{
|
|
|
|
typedef char* (*Decoder)(const BodyLines&, size_t&);
|
|
|
|
// for "7bit", "8bit" or "binary"
|
|
char* identity_decode(const BodyLines& bl, size_t& output_size)
|
|
{
|
|
const sv body = combineLines(bl);
|
|
output_size = body.size();
|
|
return new_string(body.data(), body.size());
|
|
}
|
|
|
|
|
|
char* base64_decode(const BodyLines& bl, size_t& output_size)
|
|
{
|
|
size_t out_size = 0;
|
|
for(const auto& line : bl)
|
|
{
|
|
out_size += (line.size()+3)/4 * 3;
|
|
}
|
|
|
|
const sv body = combineLines(bl);
|
|
|
|
char* out_string = new_string(nullptr, out_size);
|
|
char* out_begin = out_string;
|
|
char* out_end = out_string + out_size;
|
|
|
|
base64::decode_iter( body.begin(), body.end(), out_begin, out_end);
|
|
output_size = out_begin - out_string;
|
|
return out_string;
|
|
}
|
|
|
|
|
|
char* qp_decode(const BodyLines& bl, size_t& output_size)
|
|
{
|
|
const sv body = combineLines(bl);
|
|
|
|
char* out_string = new_string(nullptr, body.size());
|
|
char* out_begin = out_string;
|
|
char* out_end = out_string + body.size();
|
|
|
|
qp::decode_iter( body.begin(), body.end(), out_begin, out_end);
|
|
output_size = out_begin - out_string;
|
|
return out_string;
|
|
}
|
|
|
|
Decoder getDecoder(const sv transfer_encoding)
|
|
{
|
|
if(transfer_encoding == "base64")
|
|
{
|
|
return &base64_decode;
|
|
}else if(transfer_encoding == "quoted-printable")
|
|
{
|
|
return &qp_decode;
|
|
}
|
|
|
|
return &identity_decode;
|
|
}
|
|
|
|
// Tokens from RFC 2045
|
|
Rule token = +( vchar.alias() - qi::char_("]()<>@,;:\\\"/?=["));
|
|
|
|
TRule<NameValue> parameter = token >> '=' >> (token | quoted_string.alias());
|
|
TRule<ContentType> content_type = token >> '/' >> token >> *( qi::omit[*cfws] >> ';' >> qi::omit[*cfws] >> parameter);
|
|
|
|
|
|
|
|
char* create_string(const BodyLines& body, const sv& charset, Decoder decoder)
|
|
{
|
|
size_t decoded_size = 0;
|
|
char* decoded = decoder(body, decoded_size);
|
|
if(charset=="UTF-8" || charset=="UTF8")
|
|
{
|
|
return decoded; // fine. :-)
|
|
}else{
|
|
// Sigh, the hard way. At the moment with a lot of unecessary copying. :-/
|
|
// Rule1: Make it work. Profile. Make it fast. In this order.
|
|
const std::string converted = to_utf8((charset.empty() ? "us-ascii" : charset), std::string(decoded, decoded+decoded_size) ); // 1st copy...
|
|
return new_string( converted.data(), converted.size() ); // copy again. :'-(
|
|
}
|
|
}
|
|
|
|
|
|
void add_attachment(message* msg, const BodyLines& body, const ContentType& ct, Decoder decoder)
|
|
{
|
|
throw "Unimplemented!";
|
|
}
|
|
|
|
|
|
// parses the header and fill the parts in msg
|
|
void parse_body(message* msg, const HeaderSection& headers, const BodyLines& body)
|
|
{
|
|
const std::string mime_version = header_value(headers, "mime-version").to_string();
|
|
const std::string cts = header_value(headers, "content-type").to_string();
|
|
ContentType ct;
|
|
auto begin = cts.cbegin();
|
|
const bool okay = qi::parse(begin, cts.cend(), content_type, ct);
|
|
if(!okay)
|
|
{
|
|
LOG << "Cannot parse \"" + std::string{cts} + "\" as ContentType.\n";
|
|
}
|
|
LOG << "<<< CT raw: " << ct << ">>>\n";
|
|
ct.sanitize();
|
|
LOG << "<<< CT san: " << ct << ">>>\n";
|
|
|
|
if( mime_version == "1.0" ) // TODO: According to RFC 2048 there can be comments in the header field value. -.-
|
|
{
|
|
// TODO: for whatever reason "string_view cts" does not work with qi::parse(). WTF!
|
|
|
|
if(ct.type == "text")
|
|
{
|
|
const sv charset = header_value( ct.params, "charset" );
|
|
Decoder decoder = getDecoder( header_value( headers, "content-transfer-encoding" ) );
|
|
if(ct.subtype == "plain")
|
|
{
|
|
// put it in msg->longmsg
|
|
msg->longmsg = create_string(body, charset, decoder);
|
|
}else if(ct.subtype=="html")
|
|
{
|
|
// put it in msg->longmsg_formatted
|
|
msg->longmsg_formatted = create_string(body, charset, decoder);
|
|
}else{
|
|
// add it as attachment
|
|
add_attachment(msg, body, ct, decoder);
|
|
}
|
|
}
|
|
|
|
}else{ // Non-MIME mail
|
|
LOG << "<<< NO_MIME_MAIL >>> " << body.size() << " body lines.\n";
|
|
sv combined_body = combineLines(body);
|
|
if(isUtf8(combined_body.data(), combined_body.data()+combined_body.size()) )
|
|
{
|
|
const std::string& nfc_string = toNFC( std::string(combined_body) ); // FIXME: double copy! :-((
|
|
msg->longmsg = new_string(nfc_string.c_str(), nfc_string.size()); // FIXME: 3rd copy! :-(((
|
|
}else{
|
|
char* pbody = msg->longmsg = new_string(combined_body.data(), combined_body.size());
|
|
// no valid UTF-8? Hum, whatever it is, make it 7-bit ASCII for safety.
|
|
std::for_each(pbody, pbody+combined_body.size(), [](char& c) { c &= 0x7f; } );
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
} // end of namespace pEpMIME
|