1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/*
** Copyright 2025 Double Precision, Inc.
** See COPYING for distribution information.
**
*/
#include "rfc2045/rfc2045.h"
#include <idn2.h>
static std::string_view dsnaddr(std::string &addr)
{
auto e=addr.end();
auto b=std::find_if(addr.begin(), e,
[](unsigned char c) { return !isspace(c); });
auto sep=std::find(b, e, ';');
if (sep != e) ++sep;
rfc2045::entity::tolowercase(b, sep);
std::string_view format{&*b, static_cast<size_t>(sep-b)};
while (sep != e && isspace((unsigned char)*sep))
++sep;
while (sep != e && isspace((unsigned char)e[-1]))
--e;
if (format == "utf-8;")
{
size_t ntrim=sep-addr.begin();
addr.resize(e-addr.begin());
addr.erase(0, ntrim);
bool conv_error{false};
addr=unicode::iconvert::convert(
addr,
unicode::utf_8,
rfc2045_getdefaultcharset(),
conv_error
);
return addr;
}
if (format != "rfc822;")
{
return {};
}
size_t ntrim=sep-addr.begin();
addr.resize(e-addr.begin());
addr.erase(0, ntrim);
size_t p=addr.rfind('@');
if (p < addr.size())
{
char *utf8;
++p;
if (idn2_to_unicode_8z8z(addr.c_str()+p, &utf8, 0) !=
IDNA_SUCCESS)
{
utf8=0;
}
if (utf8)
{
std::string s{utf8};
free(utf8);
bool conv_error{false};
s=unicode::iconvert::convert(
s,
unicode::utf_8,
rfc2045_getdefaultcharset(),
conv_error
);
if (!conv_error)
addr.replace(p, addr.size()-p, s);
}
}
return {addr.data(), addr.size()};
}
static void print_dsn_recip(std::string origrecip,
std::string finalrecip,
std::string action,
const rfc2045::entity::dsn_callback_t &cb)
{
rfc2045::entity::tolowercase(action);
auto ab=action.begin(), ae=action.end();
while (ab != ae && isspace((unsigned char)*ab))
++ab;
while (ab != ae && isspace((unsigned char)ae[-1]))
++ae;
if (ab == ae)
return;
cb({
{&*ab, static_cast<size_t>(ae-ab)},
dsnaddr(origrecip),
dsnaddr(finalrecip)
});
}
rfc2045::entity::dsn_handler::dsn_handler(const dsn_callback_t &callback)
: callback{callback}
{
// Truncate each line to 1024 characters
linebuf.reserve(1024);
}
const rfc2045::entity *rfc2045::entity::dsn_handler::report(
const rfc2045::entity &entity
)
{
std::string report_type_value;
if (auto report_type=entity.content_type.parameters.find(
"report-type"
); report_type != entity.content_type.parameters.end())
{
report_type_value=report_type->second.value;
tolowercase(report_type_value);
}
auto delivery_status=std::find_if(
entity.subentities.begin(),
entity.subentities.end(),
[]
(auto &se)
{
return rfc2045_delivery_status_content_type(
se.content_type.value.c_str()
);
});
if (entity.content_type.value != "multipart/report" ||
report_type_value != "delivery-status" ||
delivery_status == entity.subentities.end())
return nullptr;;
return &*delivery_status;
}
void rfc2045::entity::dsn_handler::operator()(const char *p, size_t n)
{
while (n)
{
auto q=p;
p=std::find(p, p+n, '\n');
n -= p-q;
size_t c=p-q;
if (c+linebuf.size() > 1024)
c=1024-linebuf.size();
linebuf.insert(linebuf.end(), q, q+c);
if (n && *p == '\n')
{
++p;
--n;
if (linebuf.empty() ||
isspace(linebuf[0]))
{
finish();
origreceip.clear();
finalreceip.clear();
action.clear();
}
size_t colon=linebuf.find(':');
if (colon > linebuf.size())
colon=linebuf.size();
auto vb=linebuf.begin()+colon;
tolowercase(linebuf.begin(), vb);
std::string_view word{
linebuf.data(),
static_cast<size_t>(vb-linebuf.begin())
};
auto ve=linebuf.end();
if (vb < ve) ++vb;
while (vb < ve && isspace(*vb))
++vb;
while (vb < ve && isspace(ve[-1]))
--ve;
if (word == "action")
action=std::string{vb, ve};
if (word == "final-recipient")
finalreceip=std::string{vb, ve};
if (word == "original-recipient")
origreceip=std::string{vb, ve};
linebuf.clear();
}
}
}
void rfc2045::entity::dsn_handler::finish()
{
print_dsn_recip(
std::move(origreceip),
std::move(finalreceip),
std::move(action),
callback
);
}
|