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#include <KMime/Message>
17
18using namespace MessageCore;
19
21{
22void initHeader(const KMime::Message::Ptr &message, const KIdentityManagementCore::IdentityManager *identMan, uint id)
23{
24 applyIdentity(message, identMan, id);
25 message->removeHeader<KMime::Headers::To>();
26 message->removeHeader<KMime::Headers::Subject>();
27 message->date()->setDateTime(QDateTime::currentDateTime());
28
29 // This will allow to change Content-Type:
30 message->contentType()->setMimeType("text/plain");
31}
32
34 const KMime::Message::Ptr &origMsg,
36 uint id,
37 bool idHeaders)
38{
39 if (idHeaders) {
40 MessageHelper::initHeader(msg, identMan, id);
41 } else {
42 auto header = new KMime::Headers::Generic("X-KMail-Identity");
43 header->fromUnicodeString(QString::number(id));
44 msg->setHeader(header);
45 }
46
47 if (auto hdr = origMsg->headerByType("X-KMail-Transport")) {
48 const QString transport = hdr->asUnicodeString();
49 auto header = new KMime::Headers::Generic("X-KMail-Transport");
50 header->fromUnicodeString(transport);
51 msg->setHeader(header);
52 }
53}
54
56{
58
59 if (ident.fullEmailAddr().isEmpty()) {
60 message->removeHeader<KMime::Headers::From>();
61 } else {
62 message->from()->addAddress(ident.primaryEmailAddress().toUtf8(), ident.fullName());
63 }
64
65 if (ident.replyToAddr().isEmpty()) {
66 message->removeHeader<KMime::Headers::ReplyTo>();
67 } else {
68 message->replyTo()->addAddress(ident.replyToAddr().toUtf8());
69 }
70
71 if (ident.bcc().isEmpty()) {
72 message->removeHeader<KMime::Headers::Bcc>();
73 } else {
74 const auto mailboxes = KMime::Types::Mailbox::listFromUnicodeString(ident.bcc());
75 for (const KMime::Types::Mailbox &mailbox : mailboxes) {
76 message->bcc()->addAddress(mailbox);
77 }
78 }
79
80 if (ident.cc().isEmpty()) {
81 message->removeHeader<KMime::Headers::Cc>();
82 } else {
83 const auto mailboxes = KMime::Types::Mailbox::listFromUnicodeString(ident.cc());
84 for (const KMime::Types::Mailbox &mailbox : mailboxes) {
85 message->cc()->addAddress(mailbox);
86 }
87 }
88
89 if (ident.organization().isEmpty()) {
90 message->removeHeader<KMime::Headers::Organization>();
91 } else {
92 auto const organization = new KMime::Headers::Organization;
93 organization->fromUnicodeString(ident.organization());
94 message->setHeader(organization);
95 }
96
97 if (ident.isDefault()) {
98 message->removeHeader("X-KMail-Identity");
99 } else {
100 auto header = new KMime::Headers::Generic("X-KMail-Identity");
101 header->fromUnicodeString(QString::number(ident.uoid()));
102 message->setHeader(header);
103 }
104
105 if (ident.transport().isEmpty()) {
106 message->removeHeader("X-KMail-Transport");
107 } else {
108 auto header = new KMime::Headers::Generic("X-KMail-Transport");
109 header->fromUnicodeString(ident.transport());
110 message->setHeader(header);
111 }
112
113 if (ident.fcc().isEmpty()) {
114 message->removeHeader("X-KMail-Fcc");
115 } else {
116 auto header = new KMime::Headers::Generic("X-KMail-Fcc");
117 header->fromUnicodeString(ident.fcc());
118 message->setHeader(header);
119 }
120
121 if (ident.disabledFcc()) {
122 auto header = new KMime::Headers::Generic("X-KMail-FccDisabled");
123 header->fromUnicodeString(QStringLiteral("true"));
124 message->setHeader(header);
125 } else {
126 message->removeHeader("X-KMail-FccDisabled");
127 }
128}
129
130KMime::Types::AddrSpecList extractAddrSpecs(const KMime::Message::Ptr &msg, const QByteArray &header)
131{
132 KMime::Types::AddrSpecList result;
133 if (auto hrd = msg->headerByType(header.constData())) {
134 // Don't use "asUnicodeString().toUtf8()" it removes \" from \"foo, bla\" <foo@kde.org> => send failed
135 // Bug 439218
138 for (KMime::Types::AddressList::const_iterator ait = al.constBegin(); ait != alend; ++ait) {
139 KMime::Types::MailboxList::const_iterator mitEnd((*ait).mailboxList.constEnd());
140 for (KMime::Types::MailboxList::const_iterator mit = (*ait).mailboxList.constBegin(); mit != mitEnd; ++mit) {
141 result.push_back((*mit).addrSpec());
142 }
143 }
144 }
145 return result;
146}
147
148void setAutomaticFields(const KMime::Message::Ptr &msg, bool aIsMulti)
149{
150 auto header = msg->header<KMime::Headers::MIMEVersion>(true);
151 header->from7BitString("1.0");
152
153 if (aIsMulti || msg->contents().size() > 1) {
154 // Set the type to 'Multipart' and the subtype to 'Mixed'
155 msg->contentType(false)->setMimeType("multipart/mixed");
156 // Create a random printable string and set it as the boundary parameter
157 msg->contentType(false)->setBoundary(KMime::multiPartBoundary());
158 }
159}
160}
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 Fri Jul 19 2024 11:56:56 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.