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
|
/*
** Copyright 2000-2018 Double Precision, Inc. See COPYING for
** distribution information.
*/
#include "rfc2045_config.h"
#include "rfc2045reply.h"
#include "rfc822/rfc822.h"
#include "rfc822/rfc2047.h"
#include <time.h>
#include <sys/time.h>
static std::string mksalutation_datefmt(std::string_view fmt,
std::string_view date)
{
time_t t;
if (fmt.empty())
{
fmt="%a, %d %b %Y %H:%M:%S %z";
}
std::string date_s{date.begin(), date.end()};
if (rfc822_parsedate_chk(date_s.c_str(), &t) == 0)
{
struct tm tmbuf;
if (localtime_r(&t, &tmbuf))
{
std::string fmtstr{fmt.begin(), fmt.end()};
char fmtbuf[1024];
fmtbuf[strftime(fmtbuf,
sizeof(fmtbuf)-1,
fmtstr.c_str(), &tmbuf)]=0;
date_s=fmtbuf;
}
}
return date_s;
}
std::string rfc2045::reply::mksalutation(std::string_view salutation_template,
std::string_view newsgroup,
std::string_view message_id,
std::string_view newsgroups,
std::string_view sender_addr,
std::string_view sender_name,
std::string_view date,
std::string_view subject)
{
std::string subj_decoded;
rfc822::display_header(
"subject",
subject,
charset,
std::back_inserter(subj_decoded));
std::string salutation;
auto i=salutation_template.size();
auto p=salutation_template.begin();
for (; i; --i, ++p)
{
const char *fmt_start=0, *fmt_end=0;
if (*p != '%' || i == 1)
{
salutation.push_back(*p);
continue;
}
++p;
--i;
if (*p == '{')
{
fmt_start= ++p;
--i;
while (i)
{
if (*p == '}')
{
fmt_end=p;
++p;
--i;
break;
}
}
if (!fmt_end)
{
--p;
++i;
continue;
}
}
if (!i)
break;
switch (*p) {
case 'n':
salutation += "\n";
break;
case 'C':
salutation += newsgroup;
break;
case 'i':
salutation += message_id;
break;
case 'N':
salutation += newsgroups;
break;
case 'f':
salutation += sender_addr;
break;
case 'F':
salutation += sender_name;
break;
case 'd':
if (!date.empty())
salutation += mksalutation_datefmt(
{
fmt_start,
static_cast<size_t>(
fmt_end-fmt_start
)
},
date);
break;
case 's':
salutation += subj_decoded;
break;
default:
salutation.push_back(*p);
break;
}
}
return salutation;
}
std::string rfc2045::reply::mkreferences(std::string_view oldref,
std::string_view oldmsgid)
{
std::string buf;
/* Create new references header */
buf.reserve(oldref.size()+oldmsgid.size()+4);
buf += oldref;
buf += ",";
buf += oldmsgid;
/* Do wrapping the RIGHT way, by
** RFC822 parsing the References: header
*/
rfc822::tokens tp{buf};
rfc822::addresses ap{tp};
/* Keep only the last 20 message IDs */
size_t i=0;
if (ap.size() > 20) i=ap.size()-20;
std::string s;
for (; i < ap.size(); ++i)
{
if (ap[i].address.empty())
continue;
if (!s.empty())
s += " ";
s += "<";
ap[i].address.print(std::back_inserter(s));
s += ">\n";
}
return s;
}
|