managed rfc2047 and rfc2184 for attache name

manage name= / name*= / name*[0-9]= / name*[0-9]*=
This commit is contained in:
2026-01-11 18:03:17 +01:00
parent e600985404
commit 430317c1b9
4 changed files with 49 additions and 50 deletions

View File

@@ -65,8 +65,8 @@ const string Attachment::ALTERNATIVE ("alternative");
const string Attachment::KAZ_ATTACH_NAME ("vos-pieces-jointes-kaz-ici.html");
const string Attachment::MULTIPART ("multipart/");
const regex Attachment::nameCharsetRegEx ( ".*name\\*=\\s*([; \t]*)");
const regex Attachment::nameRegEx ( ".*name=\\s*((\"(\\\\.|[^\\\\])*\")|[^; \t]*).*");
const string Attachment::nameLeftToken (".*name");
const string Attachment::nameRightToken ("=\\s*((\"(\\\\.|[^\\\\])*\")|[^; \t]*).*");
const regex Attachment::boundaryRegEx (".*boundary=\\s*((\"(\\\\.|[^\\\\])*\")|[^; \t]*).*");
const regex Attachment::cidDefRegEx (".*<([^>]*)>.*");
const regex Attachment::textRegEx (".*text/("+PLAIN+"|"+HTML+").*");
@@ -179,47 +179,31 @@ Attachment::getAttachName () const {
static string tokens [] = {contentTypeToken, contentDispositionToken};
DEF_LOG ("Attachment::getAttachName", "");
for (string token : tokens) {
// name=
string result = getProp (token, nameRegEx);
removeQuote (result);
if (result.length ()) {
LOG ("name=: " << result);
encodedWordDecode (result);
return result;
}
// name*x=
for (int id = 0; ; ++id) {
string item = getProp (token, regex (".*name\\*"+to_string (id)+"=\\s*((\"(\\\\.|[^\\\\])*\")|[; \t]*).*"));
if (item.empty ())
break;
result += item;
}
removeQuote (result);
if (result.length ()) {
LOG ("name*x=: " << result);
encodedWordDecode (result);
return result;
}
// name*=
result = getProp (token, nameCharsetRegEx);
removeQuote (result);
if (result.length ()) {
LOG ("name*=: " << result);
charsetValueDecode (result);
return result;
}
// name*x*=
for (int id = 0; ; ++id) {
string item = getProp (token, regex (".*name\\*"+to_string (id)+"\\*=\\s*([^; ]*)"));
if (item.empty ())
break;
result += item;
}
removeQuote (result);
if (result.length ()) {
LOG ("name*x*=: " << result);
encodedWordDecode (result);
return result;
for (string star : {"", "\\*"}) {
// name= | name*=
regex nameAloneRegEx (nameLeftToken+star+nameRightToken);
string result = getProp (token, nameAloneRegEx);
removeQuote (result);
if (result.length ()) {
LOG (("name"+star+=": ") << result);
charsetDecode (result);
return result;
}
// name*[0-9]= | name*[0-9]*=
for (int id = 0; ; ++id) {
string item = getProp (token, regex (nameLeftToken+"\\*"+to_string (id)+star+nameRightToken));
if (item.empty ())
break;
result += item;
}
removeQuote (result);
if (result.length ()) {
LOG (("name*x"+star+"=: ") << result);
charsetDecode (result);
return result;
}
}
}
return getUnknown (getContentType ());

View File

@@ -439,16 +439,17 @@ kaz::encodedWordDecode (string &content) {
}
// ================================================================================
void
bool
kaz::charsetValueDecode (string &content) {
// rfc2184
DEF_LOG ("kazMisc::charsetValueDecode", "content: " << content.substr (0, 100) << "...");
string::size_type langPos = content.find ("'");
LOG_BUG (langPos == string::npos, return, "kazMisc::charsetValueDecode bug: no '. (content: " << content.substr (0, 100) << "...)");
if (langPos == string::npos)
return false;
string::size_type contentPos = content.find ("'", langPos+1);
if (contentPos == string::npos)
return false;
LOG_BUG (contentPos == string::npos, return, "kazMisc::charsetValueDecode bug: no double '. (content: " << content.substr (0, 100) << "...)");
string tmp (content.substr (contentPos+1));
quotedDecode<'%'> (tmp);
LOG ("tmp: " << tmp.substr (0, 100) << "...");
@@ -458,6 +459,17 @@ kaz::charsetValueDecode (string &content) {
iso2utf (tmp);
content = tmp;
LOG ("content: " << content.substr (0, 100) << "...");
return true;
}
// ================================================================================
void
kaz::charsetDecode (string &content) {
// rfc2047 | rfc2184
DEF_LOG ("kazMisc::charsetDecode", "content: " << content.substr (0, 100) << "...");
if (charsetValueDecode (content))
return;
encodedWordDecode (content);
}
// ================================================================================

View File

@@ -55,8 +55,9 @@ namespace kaz {
static vector<string> stringsToUpdate;
/*! mime tokens */
static const string contentTypeToken, contentDispositionToken, contentTransferEncodingToken, base64Token, quotedPrintableToken, contentIDToken, PLAIN, HTML, MULTIPART, RELATED, ALTERNATIVE, SIGNED, KAZ_ATTACH_NAME;
static const string nameLeftToken, nameRightToken;
/*! pattern to extract mime values */
static const regex nameRegEx, nameCharsetRegEx, boundaryRegEx, cidDefRegEx, textRegEx, multiRegEx;
static const regex boundaryRegEx, cidDefRegEx, textRegEx, multiRegEx;
/*! get uniq filename */
static string getUnknown (const string &ext = "");

View File

@@ -36,7 +36,7 @@
#define _kaz_misc_hpp
#include <string>
#include <ctype.h>
#include <inttypes.h>
#include <map>
#include <regex>
@@ -91,7 +91,9 @@ namespace kaz {
/*! side effect to get the encoded word according rfc2047 rfc5987 rfc2978 */
void encodedWordDecode (string &content);
/*! side effect to get the charsetValue according rfc2184 */
void charsetValueDecode (string &content);
bool charsetValueDecode (string &content);
/*! side effect to remove quote */
void charsetDecode (string &content);
/*! side effect to remove quote */
void removeQuote (string &content);