diff options
| author | Vincent Richard <[email protected]> | 2014-06-17 19:08:22 +0000 |
|---|---|---|
| committer | Vincent Richard <[email protected]> | 2014-06-17 19:08:22 +0000 |
| commit | 0863f50c261be0a10e98b498d4007cce27184021 (patch) | |
| tree | 4adc34d0fe19861c515b0fce31d06f1256aa05fa /src | |
| parent | Check for NULL pointer in 'ai_canonname'. (diff) | |
| download | vmime-0863f50c261be0a10e98b498d4007cce27184021.tar.gz vmime-0863f50c261be0a10e98b498d4007cce27184021.zip | |
Allow choosing between encoding modes for parameter values.
Diffstat (limited to 'src')
| -rw-r--r-- | src/vmime/generationContext.cpp | 17 | ||||
| -rw-r--r-- | src/vmime/generationContext.hpp | 47 | ||||
| -rw-r--r-- | src/vmime/parameter.cpp | 53 |
3 files changed, 95 insertions, 22 deletions
diff --git a/src/vmime/generationContext.cpp b/src/vmime/generationContext.cpp index e9662883..0a76f4d0 100644 --- a/src/vmime/generationContext.cpp +++ b/src/vmime/generationContext.cpp @@ -32,7 +32,8 @@ generationContext::generationContext() : m_maxLineLength(lineLengthLimits::convenient), m_prologText("This is a multi-part message in MIME format. Your mail reader " \ "does not understand MIME message format."), - m_epilogText("") + m_epilogText(""), + m_paramValueMode(PARAMETER_VALUE_RFC2231_ONLY) { } @@ -89,6 +90,19 @@ void generationContext::setEpilogText(const string& epilogText) } +void generationContext::setEncodedParameterValueMode(const EncodedParameterValueModes mode) +{ + m_paramValueMode = mode; +} + + +generationContext::EncodedParameterValueModes + generationContext::getEncodedParameterValueMode() const +{ + return m_paramValueMode; +} + + generationContext& generationContext::operator=(const generationContext& ctx) { copyFrom(ctx); @@ -103,6 +117,7 @@ void generationContext::copyFrom(const generationContext& ctx) m_maxLineLength = ctx.m_maxLineLength; m_prologText = ctx.m_prologText; m_epilogText = ctx.m_epilogText; + m_paramValueMode = ctx.m_paramValueMode; } diff --git a/src/vmime/generationContext.hpp b/src/vmime/generationContext.hpp index 949f06ac..22700c90 100644 --- a/src/vmime/generationContext.hpp +++ b/src/vmime/generationContext.hpp @@ -82,6 +82,51 @@ public: */ void setEpilogText(const string& epilogText); + /** Modes available for generating values in parameterized header fields. + */ + enum EncodedParameterValueModes + { + PARAMETER_VALUE_NO_ENCODING, /**< Only generate 7-bit (ASCII-only) values, + even if the value contains non-ASCII chars or + if folding is needed. */ + PARAMETER_VALUE_RFC2047_ONLY, /**< Only generate RFC-2047 values (do not use + RFC-2231). This is non-standard but most + mail clients support it. */ + PARAMETER_VALUE_RFC2231_ONLY, /**< Only generate RFC-2231 values (do not use + RFC-2047). Some mail clients may not support + it. This is the default. */ + PARAMETER_VALUE_RFC2231_AND_RFC2047 /**< Generate both RFC-2047- and RFC-2231-encoded + values. */ + }; + + /** Sets the mode used for generating parameter values in a parameterized + * header field (see parameterizedHeaderField class). + * + * PARAMETER_VALUE_NO_ENCODING or PARAMETER_VALUE_RFC2047_ONLY + * can be used for compatibility with implementations that do not + * understand RFC-2231, to generate a normal parameter value. + * PARAMETER_VALUE_RFC2047_ONLY is non-standard (and expressly + * prohibited by the RFC) but most mail clients support it. + * + * Notice: if both the normal value and the extended value are present, + * the latter can be ignored by mail processing systems. This may lead + * to annoying problems, for example, with strange names of attachments + * with all but 7-bit ascii characters removed, etc. Either + * PARAMETER_VALUE_RFC2231_ONLY or PARAMETER_VALUE_RFC2047_ONLY should + * be preferred over PARAMETER_VALUE_RFC2231_AND_RFC2047, not to create + * a normal value if the extended value is to be generated. + * + * @param mode parameter value generation mode + */ + void setEncodedParameterValueMode(const EncodedParameterValueModes mode); + + /** Returns the mode used for generating parameter values in a parameterized + * header field (see parameterizedHeaderField class). + * + * @return parameter value generation mode + */ + EncodedParameterValueModes getEncodedParameterValueMode() const; + /** Returns the default context used for generating messages. * * @return a reference to the default generation context @@ -97,6 +142,8 @@ protected: string m_prologText; string m_epilogText; + + EncodedParameterValueModes m_paramValueMode; }; diff --git a/src/vmime/parameter.cpp b/src/vmime/parameter.cpp index b8d5b36e..45e68ad6 100644 --- a/src/vmime/parameter.cpp +++ b/src/vmime/parameter.cpp @@ -279,7 +279,13 @@ void parameter::generateImpl const string& value = m_value->getBuffer(); // For compatibility with implementations that do not understand RFC-2231, - // also generate a normal "7bit/us-ascii" parameter + // we may also generate a normal "7bit/us-ascii" parameter + generationContext::EncodedParameterValueModes + genMode = ctx.getEncodedParameterValueMode(); + +#if VMIME_ALWAYS_GENERATE_7BIT_PARAMETER + genMode = generationContext::PARAMETER_VALUE_RFC2231_AND_RFC2047; +#endif // [By Eugene A. Shatokhin] // Note that if both the normal "7bit/us-ascii" value and the extended @@ -366,7 +372,8 @@ void parameter::generateImpl const bool alwaysEncode = m_value->getCharset().getRecommendedEncoding(recommendedEnc); bool extended = alwaysEncode; - if (needQuotedPrintable) + if ((needQuotedPrintable || cutValue) && + genMode != generationContext::PARAMETER_VALUE_NO_ENCODING) { // Send the name in quoted-printable, so outlook express et.al. // will understand the real filename @@ -378,7 +385,8 @@ void parameter::generateImpl else { // Do not chop off this value, but just add the complete name as one header line. - for (size_t i = 0 ; i < value.length() ; ++i) + for (size_t i = 0, n = value.length(), curValueLength = 0 ; + i < n && curValueLength < valueLength ; ++i) { const char_t c = value[i]; @@ -391,6 +399,7 @@ void parameter::generateImpl { sevenBitStream << value[i]; ++pos; + ++curValueLength; } else { @@ -406,26 +415,29 @@ void parameter::generateImpl ++pos; } -#if VMIME_ALWAYS_GENERATE_7BIT_PARAMETER - os << sevenBitBuffer; -#endif // !VMIME_ALWAYS_GENERATE_7BIT_PARAMETER + if (genMode == generationContext::PARAMETER_VALUE_RFC2047_ONLY || + genMode == generationContext::PARAMETER_VALUE_RFC2231_AND_RFC2047) + { + os << sevenBitBuffer; + } // Also generate an extended parameter if the value contains 8-bit characters // or is too long for a single line - if (extended || cutValue) + if ((extended || cutValue) && + genMode != generationContext::PARAMETER_VALUE_NO_ENCODING && + genMode != generationContext::PARAMETER_VALUE_RFC2047_ONLY) { -#if VMIME_ALWAYS_GENERATE_7BIT_PARAMETER - - os << ';'; - ++pos; - -#else // !VMIME_ALWAYS_GENERATE_7BIT_PARAMETER - - // The data output to 'sevenBitBuffer' will be discarded in this case - pos = curLinePos; - -#endif // VMIME_ALWAYS_GENERATE_7BIT_PARAMETER + if (genMode == generationContext::PARAMETER_VALUE_RFC2231_AND_RFC2047) + { + os << ';'; + ++pos; + } + else + { + // The data output to 'sevenBitBuffer' will be discarded in this case + pos = curLinePos; + } /* RFC-2231 * ======== @@ -572,8 +584,8 @@ void parameter::generateImpl } } } -#if !VMIME_ALWAYS_GENERATE_7BIT_PARAMETER - else + else if (!(genMode == generationContext::PARAMETER_VALUE_RFC2047_ONLY || + genMode == generationContext::PARAMETER_VALUE_RFC2231_AND_RFC2047)) { // The value does not contain 8-bit characters and // is short enough for a single line. @@ -582,7 +594,6 @@ void parameter::generateImpl // Output what has been stored in temporary buffer so far os << sevenBitBuffer; } -#endif // !VMIME_ALWAYS_GENERATE_7BIT_PARAMETER if (newLinePos) *newLinePos = pos; |
