fix rfc2047 / filter log / filterTest options

This commit is contained in:
François 2022-12-25 06:57:44 +01:00
parent 596ae82fe4
commit 61bdc3a4ba
9 changed files with 193 additions and 101 deletions

View File

@ -35,6 +35,7 @@
########################################################################## ##########################################################################
# - installer l'utilitaire apg pour génération de mot de passes # - installer l'utilitaire apg pour génération de mot de passes
# - installer l'utilitaire dos2unix
# - le contenu de INSPECT_DIR doit être accessible en écriture pour le # - le contenu de INSPECT_DIR doit être accessible en écriture pour le
# proriétaire du script # proriétaire du script
# - shrinkEMail et jirafeau.sh doivent être accessible en execution pour # - shrinkEMail et jirafeau.sh doivent être accessible en execution pour
@ -54,6 +55,7 @@ EX_TOO_LARGE=552
INSPECT_DIR=/var/spool/filter INSPECT_DIR=/var/spool/filter
DIR_LOG=/var/log/mail DIR_LOG=/var/log/mail
FIC_LOG="${DIR_LOG}/filter.log" FIC_LOG="${DIR_LOG}/filter.log"
TMP_LOG="$(mktemp)"
SENDMAIL="/usr/sbin/sendmail -G -i" SENDMAIL="/usr/sbin/sendmail -G -i"
MAILS=/tmp/FILTER MAILS=/tmp/FILTER
MAX_KEEP_IN_MAIL=5ki MAX_KEEP_IN_MAIL=5ki
@ -85,11 +87,13 @@ NL='
#--------------------- Fichier de LOG ------------------- #--------------------- Fichier de LOG -------------------
LOG_FIC () { LOG_FIC () {
echo "${BLUE}$(date +%d-%m-%Y-%H-%M-%S)${NC} : $*" >> "${FIC_LOG}" echo "${BLUE}$(date +%d-%m-%Y-%H-%M-%S)${NC} : $*" >> "${TMP_LOG}"
} }
quitFilter () { quitFilter () {
LOG_FIC "${GREEN}######################################## filter stop${NC}" LOG_FIC "${GREEN}######################################## filter stop${NC}"
cat "${TMP_LOG}" >> "${FIC_LOG}"
rm -f "${TMP_LOG}"
exit $1 exit $1
} }
@ -100,8 +104,8 @@ keepFailed () {
} }
#################### MAIN ################################################# #################### MAIN #################################################
echo "${NL}" >> "${FIC_LOG}" echo "${NL}${BLUE}$(date +%d-%m-%Y-%H-%M-%S)${NC} : ${GREEN}######################################## filter start (log in ${TMP_LOG})${NC}" >> "${FIC_LOG}"
LOG_FIC "${GREEN}######################################## filter start${NC}" LOG_FIC "${GREEN}######################################## ${TMP_LOG} ${NC}"
if ! mkdir -p "${MAILS}"; then if ! mkdir -p "${MAILS}"; then
LOG_FIC "${RED}Can't mkdir ${MAILS} ${NC}" LOG_FIC "${RED}Can't mkdir ${MAILS} ${NC}"

81
src/bash/filterTest.sh Normal file → Executable file
View File

@ -1,4 +1,37 @@
#!/bin/bash #!/bin/bash
##########################################################################
# Copyright KAZ 2021 #
# #
# contact (at) kaz.bzh #
# #
# This software is a filter to shrink email by attachment extraction. #
# #
# This software is governed by the CeCILL-B license under French law and #
# abiding by the rules of distribution of free software. You can use, #
# modify and/or redistribute the software under the terms of the #
# CeCILL-B license as circulated by CEA, CNRS and INRIA at the following #
# URL "http://www.cecill.info". #
# #
# As a counterpart to the access to the source code and rights to copy, #
# modify and redistribute granted by the license, users are provided #
# only with a limited warranty and the software's author, the holder of #
# the economic rights, and the successive licensors have only limited #
# liability. #
# #
# In this respect, the user's attention is drawn to the risks associated #
# with loading, using, modifying and/or developing or reproducing the #
# software by the user in light of its specific status of free software, #
# that may mean that it is complicated to manipulate, and that also #
# therefore means that it is reserved for developers and experienced #
# professionals having in-depth computer knowledge. Users are therefore #
# encouraged to load and test the software's suitability as regards #
# their requirements in conditions enabling the security of their #
# systems and/or data to be ensured and, more generally, to use and #
# operate it in the same conditions as regards security. #
# #
# The fact that you are presently reading this means that you have had #
# knowledge of the CeCILL-B license and that you accept its terms. #
##########################################################################
PRG=$(basename $0) PRG=$(basename $0)
@ -16,21 +49,16 @@ NL='
' '
TTY=$(tty) TTY=$(tty)
######################################## ########################################
LOG () { LOG () {
echo "$1" >> "${TTY}" echo "$1" >> "${TTY}"
} }
usage () { usage () {
echo "Usage: ${PRG} mbox" echo "Usage: ${PRG} [-h|-v|-g] [-m {NONE|FOOTER|ATTACHMENT|BOTH}] mbox"
exit 1 exit 1
} }
[ "$#" -eq 1 ] || usage
######################################## ########################################
mbox=$(realpath $1) mbox=$(realpath $1)
dos2unix "${mbox}" dos2unix "${mbox}"
@ -41,23 +69,48 @@ JIRAFEAU_LOCAL="${JIRAFEAU_URL}"
TMP_DIR="$(mktemp)" TMP_DIR="$(mktemp)"
########################################
# recherche des binaires
eMailShrinker="$(realpath "./eMailShrinker")" eMailShrinker="$(realpath "./eMailShrinker")"
[ -x "${eMailShrinker}" ] || eMailShrinker="$(realpath "../../build/out/eMailShrinker")" [ -x "${eMailShrinker}" ] || eMailShrinker="$(realpath "../../build/out/eMailShrinker")"
[ -x "${eMailShrinker}" ] || ( echo "${RED}eMailShrinker not found${NC}" ; exit)
jirafeauAPI="$(realpath "./jirafeauAPI")" jirafeauAPI="$(realpath "./jirafeauAPI")"
[ -x "${jirafeauAPI}" ] || jirafeauAPI="$(realpath "../../build/out/jirafeauAPI")" [ -x "${jirafeauAPI}" ] || jirafeauAPI="$(realpath "../../build/out/jirafeauAPI")"
[ -x "${jirafeauAPI}" ] || ( echo "${RED}jirafeauAPI not found${NC}" ; exit)
for ARG in $*; do
case "$1" in
-h*) usage;;
-v*) "${eMailShrinker}" -v; exit;;
-g) DEBUG="-g"; shift;;
-m) shift; ATTACH_MODE="-m $1"; shift;; # XXX test option
*) break;;
esac
done
case "${ATTACH_MODE}" in
NONE|FOOTER|ATTACHMENT|BOTH);;
*) usage;;
esac
[ "$#" -eq 1 ] || usage
########################################
# nettoyage
rm -f "${TMP_DIR}" ; mkdir -p "${TMP_DIR}" rm -f "${TMP_DIR}" ; mkdir -p "${TMP_DIR}"
rm -fr "${TMP_DIR}/PJ-name.txt" "${TMP_DIR}/PJ-Keys.txt" "${TMP_DIR}/PJ" "${TMP_DIR}/archive-content.txt" "${TMP_DIR}/url-to-refresh.txt" "${TMP_DIR}/new-mbox" rm -fr "${TMP_DIR}/PJ-name.txt" "${TMP_DIR}/PJ-Keys.txt" "${TMP_DIR}/PJ" "${TMP_DIR}/archive-content.txt" "${TMP_DIR}/url-to-refresh.txt" "${TMP_DIR}/new-mbox"
echo -e "time: $(date "+%Y-%m-%d-%H:%M:%S")\nid: $(date +%s)" > "${TMP_DIR}/archive-content.txt" echo -e "time: $(date "+%Y-%m-%d-%H:%M:%S")\nid: $(date +%s)" > "${TMP_DIR}/archive-content.txt"
########################################
# affichage de la structure de départ
LOG " - ${BLUE}mbox: ${mbox}${NC}" LOG " - ${BLUE}mbox: ${mbox}${NC}"
"${eMailShrinker}" -l "${mbox}" "${eMailShrinker}" -l "${mbox}"
LOG LOG
########################################
# recherche des prolongations des délais de grace
"${eMailShrinker}" -u "${mbox}" > "${TMP_DIR}/url-to-refresh.txt" 2>> "${TTY}" "${eMailShrinker}" -u "${mbox}" > "${TMP_DIR}/url-to-refresh.txt" 2>> "${TTY}"
cat "${TMP_DIR}/url-to-refresh.txt" | grep "${JIRAFEAU_URL}" | while read REMOTE_LINK; do cat "${TMP_DIR}/url-to-refresh.txt" | grep "${JIRAFEAU_URL}" | while read REMOTE_LINK; do
REMOTE_REF=$(echo "${REMOTE_LINK}" | sed -e 's/.*h=\([^&]*\).*/\1/' -e 's/.*http.*//') REMOTE_REF=$(echo "${REMOTE_LINK}" | sed -e 's/.*h=\([^&]*\).*/\1/' -e 's/.*http.*//')
[ -z "${REMOTE_REF}" ] && continue [ -z "${REMOTE_REF}" ] && continue
@ -67,12 +120,16 @@ cat "${TMP_DIR}/url-to-refresh.txt" | grep "${JIRAFEAU_URL}" | while read REMOTE
echo "old: ${REMOTE_REF} ${REMOTE_KEY}" >> "${TMP_DIR}/archive-content.txt" echo "old: ${REMOTE_REF} ${REMOTE_KEY}" >> "${TMP_DIR}/archive-content.txt"
done done
########################################
# extraction des pièces jointes
"${eMailShrinker}" -s "5ki" -d "${TMP_DIR}/PJ" "${mbox}" > "${TMP_DIR}/PJ-name.txt" "${eMailShrinker}" -s "5ki" -d "${TMP_DIR}/PJ" "${mbox}" > "${TMP_DIR}/PJ-name.txt"
LOG " - ${BLUE}PJ-name: ${NC}" LOG " - ${BLUE}PJ-name: ${NC}"
cat "${TMP_DIR}/PJ-name.txt" cat "${TMP_DIR}/PJ-name.txt"
LOG LOG
########################################
# dépot des extractions dans jirafeau et récupération des codes
cat "${TMP_DIR}/PJ-name.txt" | { cat "${TMP_DIR}/PJ-name.txt" | {
while read ATTACH_TMP_NAME; do while read ATTACH_TMP_NAME; do
LOG " - ${BLUE}find ATTACH_TMP_NAME: (${ATTACH_TMP_NAME}) ${NC}" LOG " - ${BLUE}find ATTACH_TMP_NAME: (${ATTACH_TMP_NAME}) ${NC}"
@ -138,13 +195,13 @@ LOG
LOG " - ${GREEN}ATTACH_MODE: ${ATTACH_MODE}${NC}" LOG " - ${GREEN}ATTACH_MODE: ${ATTACH_MODE}${NC}"
########################################
# substitution des pièces jointes par les codes fournis par jirafeau
cat "${TMP_DIR}/PJ-Keys.txt" | "${eMailShrinker}" ${ATTACH_MODE} -s "5ki" "${mbox}" "${TMP_DIR}/new-mbox" 2>> "${TTY}" cat "${TMP_DIR}/PJ-Keys.txt" | "${eMailShrinker}" ${ATTACH_MODE} -s "5ki" "${mbox}" "${TMP_DIR}/new-mbox" 2>> "${TTY}"
########################################
# affichage de la structure à la fin
LOG " - ${BLUE}new-mbox:${NC}" LOG " - ${BLUE}new-mbox:${NC}"
"${eMailShrinker}" -l "${TMP_DIR}/new-mbox" 2>> "${TTY}" "${eMailShrinker}" -l "${TMP_DIR}/new-mbox" 2>> "${TTY}"
#echo "${TMP_DIR}"
#find "${TMP_DIR}" -type f -print
echo -e "\nresul in ${TMP_DIR}/new-mbox" echo -e "\nresul in ${TMP_DIR}/new-mbox"

View File

@ -65,17 +65,9 @@ const string Attachment::ALTERNATIVE ("alternative");
const string Attachment::KAZ_ATTACH_NAME (".---KazAttachment---.html"); const string Attachment::KAZ_ATTACH_NAME (".---KazAttachment---.html");
const string Attachment::MULTIPART ("multipart/"); const string Attachment::MULTIPART ("multipart/");
const regex Attachment::nameCharsetRegEx (".*name\\*=\\s*([; \t]*)");
const regex Attachment::nameCharsetRegEx (".*name\\*=[ \t]*(.*)"); const regex Attachment::nameRegEx ( ".*name=\\s*((\"(\\\\.|[^\\\\])*\")|[^; \t]*).*");
const regex Attachment::nameRegEx (".*name=[ \t]*((\"(\\\\.|[^\\\\\r])*\")|[^\r; ]*);?.*"); const regex Attachment::boundaryRegEx (".*boundary=\\s*((\"(\\\\.|[^\\\\])*\")|[^; \t]*).*");
// boundary="----=_Part_796779_1154936629.1668080348646"
// boundary="------------040709000505010508040808"
// boundary="----------=_1668606031-941125-91"
// boundary="_004_PAVPR10MB6792713B313048E3A259B215B2079PAVPR10MB6792EURP_";
// boundary="_000_PAVPR10MB6792713B313048E3A259B215B2079PAVPR10MB6792EURP_"
// boundary=--boundary_1351_64006126-2b0e-4a3b-98ac-4797d1634188
// boundary=--boundary_1352_7e294c9a-cfab-44a0-bfb3-7310380ac7cb;
const regex Attachment::boundaryRegEx (".*boundary=[ \t]*((\"(\\\\.|[^\\\\])*\")|[^; ]*);?.*");
const regex Attachment::cidDefRegEx (".*<([^>]*)>.*"); const regex Attachment::cidDefRegEx (".*<([^>]*)>.*");
const regex Attachment::textRegEx (".*text/("+PLAIN+"|"+HTML+").*"); const regex Attachment::textRegEx (".*text/("+PLAIN+"|"+HTML+").*");
const regex Attachment::multiRegEx ("\\s*"+MULTIPART+"(mixed|"+RELATED+"|"+ALTERNATIVE+"|"+SIGNED+").*"); const regex Attachment::multiRegEx ("\\s*"+MULTIPART+"(mixed|"+RELATED+"|"+ALTERNATIVE+"|"+SIGNED+").*");
@ -188,23 +180,49 @@ 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=
string result = getProp (token, nameRegEx); string result = getProp (token, nameRegEx);
removeQuote (result); removeQuote (result);
if (result.length ()) { if (result.length ()) {
LOG ("name=: " << result); LOG ("name=: " << result);
encodedWord (result); encodedWordDecode (result);
return 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); result = getProp (token, nameCharsetRegEx);
removeQuote (result); removeQuote (result);
if (result.length ()) { if (result.length ()) {
LOG ("name*=: " << result); LOG ("name*=: " << result);
charsetValue (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 result;
} }
} }
// XXX il faut composer s'il y a plusieurs ligne filename*x=
// XXX il faut composer s'il y a plusieurs ligne filename*x*=
return getUnknown (getContentType ()); return getUnknown (getContentType ());
} }
@ -257,7 +275,7 @@ Attachment::isDefProp (const string &token, const string &val) const {
if (it == env.end ()) if (it == env.end ())
return false; return false;
// XXX case insensitive ?? // XXX case insensitive ??
return it->second.find (val) != string::npos; return caseInsensitiveFind (it->second, val) != string::npos;
} }
// ================================================================================ // ================================================================================
@ -312,7 +330,8 @@ Attachment::readMime (ifstream &mbox, streamoff &curPos) {
lastVar = line.substr (0, colonPos); lastVar = line.substr (0, colonPos);
toLower (lastVar); toLower (lastVar);
LOG ("find var: " << lastVar); LOG ("find var: " << lastVar);
string val (cleanString (line.length () >= colonPos+2 ? line.substr (colonPos+2) : "")); // XXX check RFC " " after ": " // XXX check in RFC if " " after ": " (=> +2 or +1)
string val (cleanString (line.length () >= colonPos+2 ? line.substr (colonPos+2) : ""));
LOG ("new var: <" << lastVar << " <=> " << val << ">"); LOG ("new var: <" << lastVar << " <=> " << val << ">");
env [lastVar] = val; env [lastVar] = val;
} }

View File

@ -61,7 +61,7 @@ static const string TMPL_FILENAME ("{{FILENAME}}");
static const string CID ("cid:"); static const string CID ("cid:");
// "l=/" => v1 compatibility // "l=/" => v1 compatibility
static const regex archiveURLSignature (".*(([&?]g=)|([&?]l=/)).*"); static const regex archiveURLRegex (".*(([&?]g=)|([&?]l=/)).*");
static const string KAZ_PLAIN_HR ("______________________________________________________________________________"); static const string KAZ_PLAIN_HR ("______________________________________________________________________________");
static const string KAZ_PLAIN_START ("~~ PJ-KAZ !"); // don't end whith space static const string KAZ_PLAIN_START ("~~ PJ-KAZ !"); // don't end whith space
@ -423,7 +423,7 @@ MainAttachment::removePreviousArchive () {
vector<string> toRemove; vector<string> toRemove;
for (map <string, string>::const_iterator it = previousLinks.begin (); it != previousLinks.end (); ++it) { for (map <string, string>::const_iterator it = previousLinks.begin (); it != previousLinks.end (); ++it) {
const string key (it->first); const string key (it->first);
if (regex_match (key, archiveURLSignature)) if (regex_match (key, archiveURLRegex))
toRemove.push_back (key); toRemove.push_back (key);
} }
for (string old : toRemove) for (string old : toRemove)
@ -567,7 +567,7 @@ MainAttachment::extract (ifstream &mbox, const SizeArg &minSize) const {
// ================================================================================ // ================================================================================
void void
MainAttachment::substitute (ifstream &mbox, ofstream &outbox, const SizeArg &minSize, const AttachMode &attachMode) { MainAttachment::substitute (ifstream &mbox, ofstream &outbox, const SizeArg &minSize, AttachMode attachMode) {
DEF_LOG ("MainAttachment::substitute", "minSize: " << minSize << " AttachMode: " << attachMode); DEF_LOG ("MainAttachment::substitute", "minSize: " << minSize << " AttachMode: " << attachMode);
// preparation // preparation
@ -601,7 +601,10 @@ MainAttachment::substitute (ifstream &mbox, ofstream &outbox, const SizeArg &min
getDisclaim (plainDisclaim, htmlDisclaim); getDisclaim (plainDisclaim, htmlDisclaim);
// copy email // copy email
if (plainDisclaim.size () && emptyEMail && boundary.empty ()) { if (!boundary.size () && plainDisclaim.size ()) {
if (attachMode & ATTACHMENT)
attachMode = FOOTER;
if (emptyEMail) {
// only one attachment must be replace // only one attachment must be replace
cerr << "eMailShrinker: force one attachment" << endl; cerr << "eMailShrinker: force one attachment" << endl;
string mime (getMime (mbox)); string mime (getMime (mbox));
@ -626,6 +629,7 @@ MainAttachment::substitute (ifstream &mbox, ofstream &outbox, const SizeArg &min
outbox.close (); outbox.close ();
return; return;
} }
}
streamoff curPos = 0; streamoff curPos = 0;
copy (mbox, outbox, curPos, contentPos); copy (mbox, outbox, curPos, contentPos);
curPos = contentPos; curPos = contentPos;
@ -694,8 +698,7 @@ MainAttachment::substitute (ifstream &mbox, ofstream &outbox, const SizeArg &min
} }
removeSection (content, KAZ_HTML_START, KAZ_HTML_STOP); removeSection (content, KAZ_HTML_START, KAZ_HTML_STOP);
removeSection (content, KAZ_PLAIN_START, KAZ_PLAIN_STOP); removeSection (content, KAZ_PLAIN_START, KAZ_PLAIN_STOP);
// XXX case insensitive ?? if (caseInsensitiveFind (content, CID) != string::npos)
if (content.find (CID) != string::npos)
replaceAll (content, translateHtml); replaceAll (content, translateHtml);
attachP->replaceEmbedded (content); attachP->replaceEmbedded (content);
} }
@ -733,12 +736,9 @@ MainAttachment::substitute (ifstream &mbox, ofstream &outbox, const SizeArg &min
string content (KAZ_HTML_CONTENT+htmlDisclaim+BODY_END+HTML_END); string content (KAZ_HTML_CONTENT+htmlDisclaim+BODY_END+HTML_END);
base64Encode (content); base64Encode (content);
if (boundary.size ())
outbox << boundary.substr (0, boundary.length () -2) << endl outbox << boundary.substr (0, boundary.length () -2) << endl
<< KAZ_ATTACHMENT_TEXT_HTML << endl << KAZ_ATTACHMENT_TEXT_HTML << endl
<< content << endl; << content << endl;
else
outbox << "coucou No multipart" << endl;
outbox.flush (); outbox.flush ();
} }
copy (mbox, outbox, curPos, endPos); copy (mbox, outbox, curPos, endPos);

View File

@ -33,8 +33,8 @@
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
#include "version.hpp" #include "version.hpp"
const std::string kaz::LAST_VERSION_NUM ("2.8"); const std::string kaz::LAST_VERSION_NUM ("2.9");
const std::string kaz::LAST_VERSION_DATE ("2022-12-23"); const std::string kaz::LAST_VERSION_DATE ("2022-12-24");
const std::string kaz::LAST_VERSION (LAST_VERSION_NUM+" "+LAST_VERSION_DATE+" eMailShrinker"); const std::string kaz::LAST_VERSION (LAST_VERSION_NUM+" "+LAST_VERSION_DATE+" eMailShrinker");
#include <iostream> #include <iostream>
@ -106,7 +106,7 @@ static const char *const inputFileC = inputFile.c_str ();
int int
main (int argc, char** argv) { main (int argc, char** argv) {
// XXX debug before parse options // uncomment next line in case of debug parse options
// Log::debug = true; // Log::debug = true;
DEF_LOG ("main:", ""); DEF_LOG ("main:", "");
prog = argv [0]; prog = argv [0];

View File

@ -106,7 +106,7 @@ static const char *const inputFileC = inputFile.c_str ();
int int
main (int argc, char** argv) { main (int argc, char** argv) {
// XXX debug before parse options // uncomment next line in case of debug parse options
// Log::debug = true; // Log::debug = true;
DEF_LOG ("main:", ""); DEF_LOG ("main:", "");
prog = argv [0]; prog = argv [0];

View File

@ -65,6 +65,14 @@ const string kaz::availableURLChars =
"abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
"~"; "~";
const regex kaz::encodedWordRegex ("\\s*=\\?" // flag begin
"([0-9A-Za-z!#$%&'+^_`{}~-]+)" // charset
"\\?" // flag sep
"([QqBb])" // quoted our base64
"\\?" // flag sep
"([^ ?]+)" // encoded string
"\\?=\\s*"); // flag end
// ================================================================================ // ================================================================================
uint16_t uint16_t
@ -369,65 +377,66 @@ kaz::iso2utf (string &content) {
// ================================================================================ // ================================================================================
void void
kaz::encodedWord (string &content) { kaz::encodedWordDecode (string &content) {
// rfc2047 // rfc2047
DEF_LOG ("kaz::extendedWord", "content: " << content); DEF_LOG ("kaz::encodedWordDecode", "content: " << content);
string::size_type charsetPos = content.find ("=?"); string::size_type charsetPos = content.find ("=?");
if (charsetPos == string::npos) if (charsetPos == string::npos)
return; return;
LOG ("charsetPos: " << charsetPos); LOG ("charsetPos: " << charsetPos);
LOG_BUG (charsetPos != 0, return, "kazMisc::extendedWord bug: =? not at begin pos. (content: " << content << ")");
string result; string result;
for ( ; auto pos (0);
(charsetPos = content.find ("=?", charsetPos)) != string::npos; sregex_iterator ewItEnd;
) { for (sregex_iterator ewIt (content.begin (), content.end (), encodedWordRegex);
string::size_type modePos = content.find ("?", charsetPos+2); ewIt != ewItEnd;
++ewIt) {
smatch m = *ewIt;
if (pos != m.position ()) {
result += content.substr (pos, m.position () - pos);
LOG ("stantad " << content.substr (pos, m.position () - pos));
}
string encoded (m[3]);
replace (encoded.begin (), encoded.end (), '_', ' ');
LOG_BUG (modePos == string::npos, return, "kazMisc::extendedWord bug: no end chartset. (content: " << content << ")"); LOG ("charset: " << m[1] << " mode: " << m[2] << " string: " << encoded);
string::size_type contentPos = content.find ("?", modePos+1);
LOG_BUG (contentPos != modePos+2, return, "kazMisc::extendedWord bug: no end chartset. (content: " << content << ")"); switch (m[2].str ()[0]) {
string::size_type endPos = content.find ("?=", contentPos+1);
LOG_BUG (endPos == string::npos, return, "kazMisc::extendedWord bug: no end chartset. (content: " << content << ")");
string tmp (content.substr (contentPos+1, endPos-contentPos-1));
switch (content [modePos+1]) {
case 'B': case 'B':
case 'b': case 'b':
base64Decode (tmp); base64Decode (encoded);
break; break;
case 'Q': case 'Q':
case 'q': case 'q':
quotedDecode (tmp); quotedDecode (encoded);
break; break;
default: default:
LOG_BUG (true, return, "kazMisc::extendedWord bug: unknown mode. (mode: " << content [modePos+1] << ")"); LOG_BUG (true, return, "kazMisc::encodedWordDecode bug: unknown mode. (mode: " << m[2] << ")");
} }
LOG ("tmp: " << tmp); LOG ("decoded: " << encoded);
string charset (content.substr (charsetPos, modePos-charsetPos-2)); string charset (m[1]);
toLower (charset); toLower (charset);
if (! caseInsensitiveFind (charset, "ISO")) if (! caseInsensitiveFind (charset, "ISO"))
iso2utf (tmp); iso2utf (encoded);
result += tmp; result += encoded;
charsetPos = endPos+2; pos = m.position () + m.str ().length ();
} }
content = result; content = result + content.substr (pos);
LOG ("content: " << content); LOG ("content: " << content);
} }
// ================================================================================ // ================================================================================
void void
kaz::charsetValue (string &content) { kaz::charsetValueDecode (string &content) {
// rfc2184 // rfc2184
DEF_LOG ("kaz::charsetValue", "content: " << content); DEF_LOG ("kaz::charsetValueDecode", "content: " << content);
string::size_type langPos = content.find ("'"); string::size_type langPos = content.find ("'");
LOG_BUG (langPos == string::npos, return, "kazMisc::charsetValue bug: no '. (content: " << content << ")"); LOG_BUG (langPos == string::npos, return, "kazMisc::charsetValueDecode bug: no '. (content: " << content << ")");
string::size_type contentPos = content.find ("'", langPos+1); string::size_type contentPos = content.find ("'", langPos+1);
LOG_BUG (contentPos == string::npos, return, "kazMisc::charsetValue bug: no double '. (content: " << content << ")"); LOG_BUG (contentPos == string::npos, return, "kazMisc::charsetValueDecode bug: no double '. (content: " << content << ")");
string tmp (content.substr (contentPos+1)); string tmp (content.substr (contentPos+1));
quotedDecode<'%'> (tmp); quotedDecode<'%'> (tmp);
LOG ("tmp: " << tmp); LOG ("tmp: " << tmp);

View File

@ -126,7 +126,7 @@ namespace kaz {
/*! extract big attachments in mbox to extractDir and write to stdout le dirname of each extraction */ /*! extract big attachments in mbox to extractDir and write to stdout le dirname of each extraction */
void extract (ifstream &mbox, const SizeArg &minSize) const; void extract (ifstream &mbox, const SizeArg &minSize) const;
/*! substitute big attachments by the url give in stdin */ /*! substitute big attachments by the url give in stdin */
void substitute (ifstream &mbox, ofstream &outbox, const SizeArg &minSize, const AttachMode &attachMode); void substitute (ifstream &mbox, ofstream &outbox, const SizeArg &minSize, AttachMode attachMode);
}; };
// ================================================================================ // ================================================================================

View File

@ -38,6 +38,7 @@
#include <string> #include <string>
#include <ctype.h> #include <ctype.h>
#include <map> #include <map>
#include <regex>
namespace kaz { namespace kaz {
using namespace std; using namespace std;
@ -47,6 +48,8 @@ namespace kaz {
extern const char * const base64Chars; extern const char * const base64Chars;
/*! set of chars available in URL */ /*! set of chars available in URL */
extern const string availableURLChars; extern const string availableURLChars;
/*! pattern for encoded words */
extern const regex encodedWordRegex;
// ======================================================================= // =======================================================================
/*! get the width of the terminal */ /*! get the width of the terminal */
@ -82,10 +85,10 @@ namespace kaz {
void base64Encode (string &content); void base64Encode (string &content);
/*! side effect to change charset of content */ /*! side effect to change charset of content */
void iso2utf (string &content); void iso2utf (string &content);
/*! side effect to get the encodedWord according rfc2047 */ /*! side effect to get the encoded word according rfc2047 rfc5987 rfc2978 */
void encodedWord (string &content); void encodedWordDecode (string &content);
/*! side effect to get the charsetValue according rfc2184 */ /*! side effect to get the charsetValue according rfc2184 */
void charsetValue (string &content); void charsetValueDecode (string &content);
/*! side effect to remove quote */ /*! side effect to remove quote */
void removeQuote (string &content); void removeQuote (string &content);