summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/mime/mailtemplates.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-06-23 12:46:58 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-06-23 12:46:58 +0200
commit6ffde9d47bef4d37aa6f778ac893956fe879a86a (patch)
treea2d91fec5ae1538ff90050bfbfd81433494a75e3 /framework/src/domain/mime/mailtemplates.cpp
parentc605c01fb480e634c9edc21e8430e753e4595be8 (diff)
downloadkube-6ffde9d47bef4d37aa6f778ac893956fe879a86a.tar.gz
kube-6ffde9d47bef4d37aa6f778ac893956fe879a86a.zip
Fixed the reply regexp
Diffstat (limited to 'framework/src/domain/mime/mailtemplates.cpp')
-rw-r--r--framework/src/domain/mime/mailtemplates.cpp91
1 files changed, 59 insertions, 32 deletions
diff --git a/framework/src/domain/mime/mailtemplates.cpp b/framework/src/domain/mime/mailtemplates.cpp
index 6158f787..d41039db 100644
--- a/framework/src/domain/mime/mailtemplates.cpp
+++ b/framework/src/domain/mime/mailtemplates.cpp
@@ -77,56 +77,83 @@ void initHeader(const KMime::Message::Ptr &message)
77 message->contentType()->setMimeType("text/plain"); 77 message->contentType()->setMimeType("text/plain");
78} 78}
79 79
80QString replacePrefixes(const QString &str, const QStringList &prefixRegExps, 80QString replacePrefixes(const QString &str, const QStringList &prefixRegExps, const QString &newPrefix)
81 bool replace, const QString &newPrefix)
82{ 81{
83 bool recognized = false;
84 // construct a big regexp that 82 // construct a big regexp that
85 // 1. is anchored to the beginning of str (sans whitespace) 83 // 1. is anchored to the beginning of str (sans whitespace)
86 // 2. matches at least one of the part regexps in prefixRegExps 84 // 2. matches at least one of the part regexps in prefixRegExps
87 QString bigRegExp = QStringLiteral("^(?:\\s+|(?:%1))+\\s*").arg(prefixRegExps.join(QStringLiteral(")|(?:"))); 85 const QString bigRegExp = QStringLiteral("^(?:\\s+|(?:%1))+\\s*").arg(prefixRegExps.join(QStringLiteral(")|(?:")));
88 QRegExp rx(bigRegExp, Qt::CaseInsensitive); 86 QRegExp rx(bigRegExp, Qt::CaseInsensitive);
89 if (rx.isValid()) { 87 if (!rx.isValid()) {
90 QString tmp = str;
91 if (rx.indexIn(tmp) == 0) {
92 recognized = true;
93 if (replace) {
94 return tmp.replace(0, rx.matchedLength(), newPrefix + QLatin1String(" "));
95 }
96 }
97 } else {
98 qWarning() << "bigRegExp = \"" 88 qWarning() << "bigRegExp = \""
99 << bigRegExp << "\"\n" 89 << bigRegExp << "\"\n"
100 << "prefix regexp is invalid!"; 90 << "prefix regexp is invalid!";
101 // try good ole Re/Fwd: 91 qWarning() << "Error: " << rx.errorString() << rx;
102 recognized = str.startsWith(newPrefix); 92 Q_ASSERT(false);
93 return str;
103 } 94 }
104 95
105 if (!recognized) { 96 QString tmp = str;
106 return newPrefix + QLatin1String(" ") + str; 97 //We expect a match at the beginning of the string
107 } else { 98 if (rx.indexIn(tmp) == 0) {
108 return str; 99 return tmp.replace(0, rx.matchedLength(), newPrefix + QLatin1String(" "));
109 } 100 }
101 return str;
102}
103
104const QStringList getForwardPrefixes()
105{
106 //See https://en.wikipedia.org/wiki/List_of_email_subject_abbreviations
107 QStringList list;
108 //We want to be able to potentially reply to a variety of languages, so only translating is not enough
109 list << QObject::tr("fwd");
110 list << "fwd";
111 list << "fw";
112 list << "wg";
113 list << "vs";
114 list << "tr";
115 list << "rv";
116 list << "enc";
117 return list;
110} 118}
111 119
112QString forwardSubject(const KMime::Message::Ptr &msg) 120
121static QString forwardSubject(const QString &s)
113{ 122{
114 bool replaceForwardPrefix = true; 123 //The standandard prefix
124 const auto localPrefix = "FW:";
115 QStringList forwardPrefixes; 125 QStringList forwardPrefixes;
116 forwardPrefixes << "Fwd:"; 126 for (const auto &prefix : getForwardPrefixes()) {
117 forwardPrefixes << "FW:"; 127 forwardPrefixes << prefix + "\\s*:";
118 return replacePrefixes(msg->subject()->asUnicodeString(), forwardPrefixes, replaceForwardPrefix, QStringLiteral("Fwd:")); 128 }
129 return replacePrefixes(s, forwardPrefixes, localPrefix);
119} 130}
120 131
121QString replySubject(const KMime::Message::Ptr &msg) 132static QStringList getReplyPrefixes()
122{ 133{
123 bool replaceReplyPrefix = true; 134 //See https://en.wikipedia.org/wiki/List_of_email_subject_abbreviations
135 QStringList list;
136 //We want to be able to potentially reply to a variety of languages, so only translating is not enough
137 list << QObject::tr("re");
138 list << "re";
139 list << "aw";
140 list << "sv";
141 list << "antw";
142 list << "ref";
143 return list;
144}
145
146static QString replySubject(const QString &s)
147{
148 //The standandard prefix (latin for "in re", in matter of)
149 const auto localPrefix = "RE:";
124 QStringList replyPrefixes; 150 QStringList replyPrefixes;
125 //We're escaping the regex escape sequences. awesome 151 for (const auto &prefix : getReplyPrefixes()) {
126 replyPrefixes << "Re\\s*:"; 152 replyPrefixes << prefix + "\\s*:";
127 replyPrefixes << "Re[\\d+\\]:"; 153 replyPrefixes << prefix + "\\[.+\\]:";
128 replyPrefixes << "Re\\d+:"; 154 replyPrefixes << prefix + "\\d+:";
129 return replacePrefixes(msg->subject()->asUnicodeString(), replyPrefixes, replaceReplyPrefix, QStringLiteral("Re:")); 155 }
156 return replacePrefixes(s, replyPrefixes, localPrefix);
130} 157}
131 158
132QByteArray getRefStr(const KMime::Message::Ptr &msg) 159QByteArray getRefStr(const KMime::Message::Ptr &msg)
@@ -793,7 +820,7 @@ void MailTemplates::reply(const KMime::Message::Ptr &origMsg, const std::functio
793 //In-Reply-To = original msg-id 820 //In-Reply-To = original msg-id
794 msg->inReplyTo()->from7BitString(origMsg->messageID()->as7BitString(false)); 821 msg->inReplyTo()->from7BitString(origMsg->messageID()->as7BitString(false));
795 822
796 msg->subject()->fromUnicodeString(replySubject(origMsg), "utf-8"); 823 msg->subject()->fromUnicodeString(replySubject(origMsg->subject()->asUnicodeString()), "utf-8");
797 824
798 auto definedLocale = QLocale::system(); 825 auto definedLocale = QLocale::system();
799 826