Messagelib

messagehelper.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
3  SPDX-FileCopyrightText: 2009 Andras Mantia <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "messagehelper.h"
9 #include "settings/messagecomposersettings.h"
10 #include "utils/util.h"
11 
12 #include "MessageCore/MailingList"
13 #include "MessageCore/StringUtil"
14 
15 #include "messagecomposer_debug.h"
16 #include <KIdentityManagement/Identity>
17 #include <KIdentityManagement/IdentityManager>
18 #include <KMime/DateFormatter>
19 #include <KMime/MDN>
20 #include <KMime/Message>
21 
22 using namespace MessageCore;
23 
24 namespace MessageHelper
25 {
26 void initHeader(const KMime::Message::Ptr &message, const KIdentityManagement::IdentityManager *identMan, uint id)
27 {
28  applyIdentity(message, identMan, id);
29  message->removeHeader<KMime::Headers::To>();
30  message->removeHeader<KMime::Headers::Subject>();
31  message->date()->setDateTime(QDateTime::currentDateTime());
32 
33  // This will allow to change Content-Type:
34  message->contentType()->setMimeType("text/plain");
35 }
36 
38  const KMime::Message::Ptr &origMsg,
40  uint id,
41  bool idHeaders)
42 {
43  if (idHeaders) {
44  MessageHelper::initHeader(msg, identMan, id);
45  } else {
46  auto header = new KMime::Headers::Generic("X-KMail-Identity");
47  header->fromUnicodeString(QString::number(id), "utf-8");
48  msg->setHeader(header);
49  }
50 
51  if (auto hdr = origMsg->headerByType("X-KMail-Transport")) {
52  const QString transport = hdr->asUnicodeString();
53  auto header = new KMime::Headers::Generic("X-KMail-Transport");
54  header->fromUnicodeString(transport, "utf-8");
55  msg->setHeader(header);
56  }
57 }
58 
59 void applyIdentity(const KMime::Message::Ptr &message, const KIdentityManagement::IdentityManager *identMan, uint id)
60 {
61  const KIdentityManagement::Identity &ident = identMan->identityForUoidOrDefault(id);
62 
63  if (ident.fullEmailAddr().isEmpty()) {
64  message->removeHeader<KMime::Headers::From>();
65  } else {
66  message->from()->addAddress(ident.primaryEmailAddress().toUtf8(), ident.fullName());
67  }
68 
69  if (ident.replyToAddr().isEmpty()) {
70  message->removeHeader<KMime::Headers::ReplyTo>();
71  } else {
72  message->replyTo()->addAddress(ident.replyToAddr().toUtf8());
73  }
74 
75  if (ident.bcc().isEmpty()) {
76  message->removeHeader<KMime::Headers::Bcc>();
77  } else {
78  const auto mailboxes = KMime::Types::Mailbox::listFromUnicodeString(ident.bcc());
79  for (const KMime::Types::Mailbox &mailbox : mailboxes) {
80  message->bcc()->addAddress(mailbox);
81  }
82  }
83 
84  if (ident.cc().isEmpty()) {
85  message->removeHeader<KMime::Headers::Cc>();
86  } else {
87  const auto mailboxes = KMime::Types::Mailbox::listFromUnicodeString(ident.cc());
88  for (const KMime::Types::Mailbox &mailbox : mailboxes) {
89  message->cc()->addAddress(mailbox);
90  }
91  }
92 
93  if (ident.organization().isEmpty()) {
94  message->removeHeader<KMime::Headers::Organization>();
95  } else {
96  auto const organization = new KMime::Headers::Organization;
97  organization->fromUnicodeString(ident.organization(), "utf-8");
98  message->setHeader(organization);
99  }
100 
101  if (ident.isDefault()) {
102  message->removeHeader("X-KMail-Identity");
103  } else {
104  auto header = new KMime::Headers::Generic("X-KMail-Identity");
105  header->fromUnicodeString(QString::number(ident.uoid()), "utf-8");
106  message->setHeader(header);
107  }
108 
109  if (ident.transport().isEmpty()) {
110  message->removeHeader("X-KMail-Transport");
111  } else {
112  auto header = new KMime::Headers::Generic("X-KMail-Transport");
113  header->fromUnicodeString(ident.transport(), "utf-8");
114  message->setHeader(header);
115  }
116 
117  if (ident.fcc().isEmpty()) {
118  message->removeHeader("X-KMail-Fcc");
119  } else {
120  auto header = new KMime::Headers::Generic("X-KMail-Fcc");
121  header->fromUnicodeString(ident.fcc(), "utf-8");
122  message->setHeader(header);
123  }
124 
125  if (ident.disabledFcc()) {
126  auto header = new KMime::Headers::Generic("X-KMail-FccDisabled");
127  header->fromUnicodeString(QStringLiteral("true"), "utf-8");
128  message->setHeader(header);
129  } else {
130  message->removeHeader("X-KMail-FccDisabled");
131  }
132 }
133 
134 KMime::Types::AddrSpecList extractAddrSpecs(const KMime::Message::Ptr &msg, const QByteArray &header)
135 {
136  KMime::Types::AddrSpecList result;
137  if (auto hrd = msg->headerByType(header.constData())) {
138  // Don't use "asUnicodeString().toUtf8()" it removes \" from \"foo, bla\" <[email protected]> => send failed
139  // Bug 439218
142  for (KMime::Types::AddressList::const_iterator ait = al.constBegin(); ait != alend; ++ait) {
143  KMime::Types::MailboxList::const_iterator mitEnd((*ait).mailboxList.constEnd());
144  for (KMime::Types::MailboxList::const_iterator mit = (*ait).mailboxList.constBegin(); mit != mitEnd; ++mit) {
145  result.push_back((*mit).addrSpec());
146  }
147  }
148  }
149  return result;
150 }
151 
152 void setAutomaticFields(const KMime::Message::Ptr &msg, bool aIsMulti)
153 {
154  auto header = msg->header<KMime::Headers::MIMEVersion>(true);
155  header->from7BitString("1.0");
156 
157  if (aIsMulti || msg->contents().size() > 1) {
158  // Set the type to 'Multipart' and the subtype to 'Mixed'
159  msg->contentType(false)->setMimeType("multipart/mixed");
160  // Create a random printable string and set it as the boundary parameter
161  msg->contentType(false)->setBoundary(KMime::multiPartBoundary());
162  }
163 }
164 }
Contains random helper methods when dealing with messages.
static QVector< Mailbox > listFromUnicodeString(const QString &s)
void fromUnicodeString(const QString &s, const QByteArray &b) override
QString fullEmailAddr() const
QVector::const_iterator constEnd() const const
QString organization() const
QString number(int n, int base)
void initHeader(const KMime::Message::Ptr &message, const KIdentityManagement::IdentityManager *identMan, uint id)
Initialize header fields.
bool isEmpty() const const
const char * constData() const const
void setAutomaticFields(const KMime::Message::Ptr &msg, bool aIsMulti)
Set fields that are either automatically set (Message-id) or that do not change from one message to a...
void applyIdentity(const KMime::Message::Ptr &message, const KIdentityManagement::IdentityManager *identMan, uint id)
Set the from, to, cc, bcc, encryption etc headers as specified in the given identity.
AddressList splitAddressField(const QByteArray &text)
Splits the given address list text into separate addresses.
Definition: stringutil.cpp:281
QVector::const_iterator constBegin() const const
QDateTime currentDateTime()
void initFromMessage(const KMime::Message::Ptr &msg, const KMime::Message::Ptr &origMsg, KIdentityManagement::IdentityManager *identMan, uint id, bool idHeaders)
Initialize headers fields according to the identity and the transport header of the given original me...
typedef const_iterator
QString replyToAddr() const
const Identity & identityForUoidOrDefault(uint uoid) const
QString primaryEmailAddress() const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Oct 24 2021 23:14:39 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.