diff options
Diffstat (limited to 'common/mailpreprocessor.cpp')
-rw-r--r-- | common/mailpreprocessor.cpp | 83 |
1 files changed, 13 insertions, 70 deletions
diff --git a/common/mailpreprocessor.cpp b/common/mailpreprocessor.cpp index b1cb1d5..8f5a77d 100644 --- a/common/mailpreprocessor.cpp +++ b/common/mailpreprocessor.cpp | |||
@@ -29,65 +29,6 @@ | |||
29 | 29 | ||
30 | using namespace Sink; | 30 | using namespace Sink; |
31 | 31 | ||
32 | QString MailPropertyExtractor::getFilePathFromMimeMessagePath(const QString &s) const | ||
33 | { | ||
34 | return s; | ||
35 | } | ||
36 | |||
37 | struct MimeMessageReader { | ||
38 | MimeMessageReader(const QString &mimeMessagePath) | ||
39 | : f(mimeMessagePath), | ||
40 | mapped(0) | ||
41 | { | ||
42 | if (mimeMessagePath.isNull()) { | ||
43 | SinkTrace() << "No mime message"; | ||
44 | return; | ||
45 | } | ||
46 | SinkTrace() << "Updating indexed properties " << mimeMessagePath; | ||
47 | if (!f.open(QIODevice::ReadOnly)) { | ||
48 | SinkWarning() << "Failed to open the file: " << mimeMessagePath; | ||
49 | return; | ||
50 | } | ||
51 | if (!f.size()) { | ||
52 | SinkWarning() << "The file is empty."; | ||
53 | return; | ||
54 | } | ||
55 | mapped = f.map(0, f.size()); | ||
56 | if (!mapped) { | ||
57 | SinkWarning() << "Failed to map the file: " << f.errorString(); | ||
58 | return; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | KMime::Message::Ptr mimeMessage() | ||
63 | { | ||
64 | if (!mapped) { | ||
65 | return {}; | ||
66 | } | ||
67 | QByteArray result; | ||
68 | //Seek for end of headers | ||
69 | const auto content = QByteArray::fromRawData(reinterpret_cast<const char*>(mapped), f.size()); | ||
70 | int pos = content.indexOf("\r\n\r\n", 0); | ||
71 | int offset = 2; | ||
72 | if (pos < 0) { | ||
73 | pos = content.indexOf("\n\n", 0); | ||
74 | offset = 1; | ||
75 | } | ||
76 | if (pos > -1) { | ||
77 | const auto header = content.left(pos + offset); //header *must* end with "\n" !! | ||
78 | auto msg = KMime::Message::Ptr(new KMime::Message); | ||
79 | msg->setHead(KMime::CRLFtoLF(header)); | ||
80 | msg->parse(); | ||
81 | return msg; | ||
82 | } | ||
83 | SinkWarning() << "Failed to find end of headers" << content; | ||
84 | return {}; | ||
85 | } | ||
86 | |||
87 | QFile f; | ||
88 | uchar *mapped; | ||
89 | }; | ||
90 | |||
91 | static Sink::ApplicationDomain::Mail::Contact getContact(const KMime::Headers::Generics::MailboxList *header) | 32 | static Sink::ApplicationDomain::Mail::Contact getContact(const KMime::Headers::Generics::MailboxList *header) |
92 | { | 33 | { |
93 | const auto name = header->displayNames().isEmpty() ? QString() : header->displayNames().first(); | 34 | const auto name = header->displayNames().isEmpty() ? QString() : header->displayNames().first(); |
@@ -104,8 +45,18 @@ static QList<Sink::ApplicationDomain::Mail::Contact> getContactList(const KMime: | |||
104 | return list; | 45 | return list; |
105 | } | 46 | } |
106 | 47 | ||
107 | static void updatedIndexedProperties(Sink::ApplicationDomain::Mail &mail, KMime::Message::Ptr msg) | 48 | void MailPropertyExtractor::updatedIndexedProperties(Sink::ApplicationDomain::Mail &mail, const QByteArray &data) |
108 | { | 49 | { |
50 | if (data.isEmpty()) { | ||
51 | return; | ||
52 | } | ||
53 | auto msg = KMime::Message::Ptr(new KMime::Message); | ||
54 | msg->setHead(KMime::CRLFtoLF(data)); | ||
55 | msg->parse(); | ||
56 | if (!msg) { | ||
57 | return; | ||
58 | } | ||
59 | |||
109 | mail.setExtractedSubject(msg->subject(true)->asUnicodeString()); | 60 | mail.setExtractedSubject(msg->subject(true)->asUnicodeString()); |
110 | mail.setExtractedSender(getContact(msg->from(true))); | 61 | mail.setExtractedSender(getContact(msg->from(true))); |
111 | mail.setExtractedTo(getContactList(msg->to(true))); | 62 | mail.setExtractedTo(getContactList(msg->to(true))); |
@@ -156,19 +107,11 @@ static void updatedIndexedProperties(Sink::ApplicationDomain::Mail &mail, KMime: | |||
156 | 107 | ||
157 | void MailPropertyExtractor::newEntity(Sink::ApplicationDomain::Mail &mail) | 108 | void MailPropertyExtractor::newEntity(Sink::ApplicationDomain::Mail &mail) |
158 | { | 109 | { |
159 | MimeMessageReader mimeMessageReader(getFilePathFromMimeMessagePath(mail.getMimeMessagePath())); | 110 | updatedIndexedProperties(mail, mail.getMimeMessage()); |
160 | auto msg = mimeMessageReader.mimeMessage(); | ||
161 | if (msg) { | ||
162 | updatedIndexedProperties(mail, msg); | ||
163 | } | ||
164 | } | 111 | } |
165 | 112 | ||
166 | void MailPropertyExtractor::modifiedEntity(const Sink::ApplicationDomain::Mail &oldMail, Sink::ApplicationDomain::Mail &newMail) | 113 | void MailPropertyExtractor::modifiedEntity(const Sink::ApplicationDomain::Mail &oldMail, Sink::ApplicationDomain::Mail &newMail) |
167 | { | 114 | { |
168 | MimeMessageReader mimeMessageReader(getFilePathFromMimeMessagePath(newMail.getMimeMessagePath())); | 115 | updatedIndexedProperties(newMail, newMail.getMimeMessage()); |
169 | auto msg = mimeMessageReader.mimeMessage(); | ||
170 | if (msg) { | ||
171 | updatedIndexedProperties(newMail, msg); | ||
172 | } | ||
173 | } | 116 | } |
174 | 117 | ||