Messagelib

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

KDE's Doxygen guidelines are available online.