QCString dispositionNotificationBodyContent( const QString & r, const QCString & o, const QCString & omid, DispositionType d, ActionMode a, SendingMode s, const QValueList<DispositionModifier> & m, const QString & special ) { // in Perl: chomp(special) QString spec; if ( special.endsWith("\n") ) spec = special.left( special.length() - 1 ); else spec = special; // std headers: QCString result = reportingUAField(); result += orginalRecipient( o ); result += finalRecipient( r ); result += originalMessageID( omid ); result += dispositionField( d, a, s, m ); // headers that are only present for certain disposition {types,modifiers}: if ( d == Failed ) result += "Failure: " + encodeRFC2047String( spec, "utf-8" ) + "\n"; else if ( m.contains( Error ) ) result += "Error: " + encodeRFC2047String( spec, "utf-8" ) + "\n"; else if ( m.contains( Warning ) ) result += "Warning: " + encodeRFC2047String( spec, "utf-8" ) + "\n"; return result; }
static QCString finalRecipient( const QString & recipient ) { if ( recipient.isEmpty() ) return QCString(); else return "Final-Recipient: rfc822; " + encodeRFC2047String( recipient, "utf-8" ) + "\n"; }
/** @short Encode the given string into RFC2047 form, preserving the ASCII leading part if possible */ QByteArray encodeRFC2047StringWithAsciiPrefix(const QString &text) { // The maximal recommended line length, as defined by RFC 5322 const int maxLineLength = 78; // Find first character which needs escaping int pos = 0; while (pos < text.size() && pos < maxLineLength && (text[pos].unicode() == 0x20 || !rfc2047QPNeedsEscpaing(text[pos].unicode()))) ++pos; // Find last character of a word which doesn't need escaping if (pos != text.size()) { while (pos > 0 && text[pos-1].unicode() != 0x20) --pos; if (pos > 0 && text[pos].unicode() == 0x20) --pos; } QByteArray prefix = text.left(pos).toUtf8(); if (pos == text.size()) return prefix; QString rest = text.mid(pos); Rfc2047StringCharacterSetType charset = charsetForInput(rest); return prefix + encodeRFC2047String(rest, charset); }
static QByteArray finalRecipient( const QString &recipient ) { if ( recipient.isEmpty() ) { return QByteArray(); } else { return "Final-Recipient: rfc822; " + encodeRFC2047String( recipient, "utf-8" ) + '\n'; } }
QByteArray encodeRFC2047Phrase( const QString &text ) { /* We want to know if we can encode as ASCII. But bizarrely, Qt (on my system at least) doesn't have an ASCII codec. So we use the ISO-8859-1 superset, and check for any non-ASCII characters in the result. */ QTextCodec *latin1 = QTextCodec::codecForMib(4); if (latin1->canEncode(text)) { /* Attempt to represent it as an RFC2822 'phrase' --- either a sequence of atoms or as a quoted-string. */ if (atomPhraseRx.exactMatch(text)) { /* Simplest case: a sequence of atoms (not dot-atoms) */ return latin1->fromUnicode(text); } else { /* Next-simplest representation: a quoted-string */ QByteArray unquoted = latin1->fromUnicode(text); /* Check for non-ASCII characters. */ for(int i = 0; i < unquoted.size(); i++) { char ch = unquoted[i]; if (ch < 1 || ch >= 127) { /* This string contains non-ASCII characters, so the only way to represent it in a mail header is as an RFC2047 encoded-word. */ return encodeRFC2047String(text, RFC2047_STRING_LATIN); } } return quotedString(unquoted); } } /* If the text has characters outside of the basic ASCII set, then it has to be encoded using the RFC2047 encoded-word syntax. */ return encodeRFC2047String(text, RFC2047_STRING_UTF8); }