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

View File

@@ -439,16 +439,17 @@ kaz::encodedWordDecode (string &content) {
} }
// ================================================================================ // ================================================================================
void bool
kaz::charsetValueDecode (string &content) { kaz::charsetValueDecode (string &content) {
// rfc2184 // rfc2184
DEF_LOG ("kazMisc::charsetValueDecode", "content: " << content.substr (0, 100) << "..."); DEF_LOG ("kazMisc::charsetValueDecode", "content: " << content.substr (0, 100) << "...");
string::size_type langPos = content.find ("'"); string::size_type langPos = content.find ("'");
if (langPos == string::npos)
LOG_BUG (langPos == string::npos, return, "kazMisc::charsetValueDecode bug: no '. (content: " << content.substr (0, 100) << "...)"); return false;
string::size_type contentPos = content.find ("'", langPos+1); 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)); string tmp (content.substr (contentPos+1));
quotedDecode<'%'> (tmp); quotedDecode<'%'> (tmp);
LOG ("tmp: " << tmp.substr (0, 100) << "..."); LOG ("tmp: " << tmp.substr (0, 100) << "...");
@@ -458,6 +459,17 @@ kaz::charsetValueDecode (string &content) {
iso2utf (tmp); iso2utf (tmp);
content = tmp; content = tmp;
LOG ("content: " << content.substr (0, 100) << "..."); 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; static vector<string> stringsToUpdate;
/*! mime tokens */ /*! mime tokens */
static const string contentTypeToken, contentDispositionToken, contentTransferEncodingToken, base64Token, quotedPrintableToken, contentIDToken, PLAIN, HTML, MULTIPART, RELATED, ALTERNATIVE, SIGNED, KAZ_ATTACH_NAME; 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 */ /*! pattern to extract mime values */
static const regex nameRegEx, nameCharsetRegEx, boundaryRegEx, cidDefRegEx, textRegEx, multiRegEx; static const regex boundaryRegEx, cidDefRegEx, textRegEx, multiRegEx;
/*! get uniq filename */ /*! get uniq filename */
static string getUnknown (const string &ext = ""); static string getUnknown (const string &ext = "");

View File

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