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

KDE's Doxygen guidelines are available online.