File indexing completed on 2024-05-12 05:10:48

0001 /*
0002   SPDX-FileCopyrightText: 1998 Barry D Benowitz <b.benowitz@telesciences.com>
0003   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
0004   SPDX-FileCopyrightText: 2009 Allen Winter <winter@kde.org>
0005 
0006   SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include "akonadi-calendar_tests_export.h"
0012 #include "itiphandler.h"
0013 #include <KCalendarCore/IncidenceBase>
0014 #include <KMime/KMimeMessage>
0015 #include <QObject>
0016 
0017 #include <optional>
0018 
0019 struct UnitTestResult {
0020     using List = QList<UnitTestResult>;
0021     QString from;
0022     QStringList to;
0023     QStringList cc;
0024     QStringList bcc;
0025     int transportId;
0026     KMime::Message::Ptr message;
0027     UnitTestResult()
0028         : transportId(-1)
0029     {
0030     }
0031 };
0032 
0033 namespace KIdentityManagementCore
0034 {
0035 class Identity;
0036 }
0037 
0038 namespace Kleo
0039 {
0040 class KeyResolver;
0041 }
0042 
0043 namespace MessageComposer
0044 {
0045 class Composer;
0046 class ContactPreference;
0047 }
0048 
0049 namespace MailTransport
0050 {
0051 class Transport;
0052 }
0053 
0054 namespace GpgME
0055 {
0056 class Key;
0057 }
0058 
0059 class KJob;
0060 
0061 namespace Akonadi
0062 {
0063 class ITIPHandlerComponentFactory;
0064 
0065 class AKONADI_CALENDAR_TESTS_EXPORT MailClient : public QObject
0066 {
0067     Q_OBJECT
0068 
0069     struct MessageData {
0070         QString from;
0071         QStringList to;
0072         QStringList cc;
0073         QString subject;
0074         QString body;
0075         QString attachment;
0076         QString method;
0077         bool hidden = false;
0078         bool bccMe = false;
0079         bool sign = false;
0080         bool encrypt = false;
0081     };
0082 
0083     bool mAkonadiLookupEnabled = true;
0084 
0085 public:
0086     enum Result { ResultSuccess, ResultNoAttendees, ResultReallyNoAttendees, ResultErrorCreatingTransport, ResultErrorFetchingTransport, ResultQueueJobError };
0087 
0088     enum MailPrivacy { MailPrivacyPlain = 0, MailPrivacySign = 1, MailPrivacyEncrypt = 2 };
0089     Q_DECLARE_FLAGS(MailPrivacyFlags, MailPrivacy)
0090 
0091     explicit MailClient(ITIPHandlerComponentFactory *factory, QObject *parent = nullptr);
0092     ~MailClient() override;
0093 
0094     void mailAttendees(const KCalendarCore::IncidenceBase::Ptr &incidence,
0095                        const KIdentityManagementCore::Identity &identity,
0096                        KCalendarCore::iTIPMethod method,
0097                        bool bccMe,
0098                        const QString &attachment = QString(),
0099                        const QString &mailTransport = QString(),
0100                        MailPrivacyFlags mailPrivacy = MailPrivacyPlain);
0101 
0102     void mailOrganizer(const KCalendarCore::IncidenceBase::Ptr &incidence,
0103                        const KIdentityManagementCore::Identity &identity,
0104                        const QString &from,
0105                        KCalendarCore::iTIPMethod method,
0106                        bool bccMe,
0107                        const QString &attachment = QString(),
0108                        const QString &sub = QString(),
0109                        const QString &mailTransport = QString(),
0110                        MailPrivacyFlags mailPrivacy = MailPrivacyPlain);
0111 
0112     void mailTo(const KCalendarCore::IncidenceBase::Ptr &incidence,
0113                 const KIdentityManagementCore::Identity &identity,
0114                 const QString &from,
0115                 KCalendarCore::iTIPMethod method,
0116                 bool bccMe,
0117                 const QString &recipients,
0118                 const QString &attachment = QString(),
0119                 const QString &mailTransport = QString(),
0120                 MailPrivacyFlags mailPrivacy = MailPrivacyPlain);
0121 
0122     /**
0123       Sends mail with specified from, to and subject field and body as text.
0124       If bcc is set, send a blind carbon copy to the sender
0125 
0126       @param incidence is the incidence, that is sended
0127       @param identity is the Identity of the sender
0128       @param from is the address of the sender of the message
0129       @param to a list of addresses to receive the message
0130       @param cc a list of addresses to receive message carbon copies
0131       @param subject is the subject of the message
0132       @param body is the boody of the message
0133       @param hidden if true and using KMail as the mailer, send the message
0134       without opening a composer window.
0135       @param bcc if true, send a blind carbon copy to the message sender
0136       @param attachment optional attachment (raw data)
0137       @param mailTransport defines the mail transport method. See here the
0138       kdepimlibs/mailtransport library.
0139     */
0140     void send(const KCalendarCore::IncidenceBase::Ptr &incidence,
0141               const KIdentityManagementCore::Identity &identity,
0142               const MessageData &msg,
0143               const QString &mailTransport = QString());
0144 
0145 private:
0146     void handleQueueJobFinished(KJob *job);
0147 
0148 Q_SIGNALS:
0149     void finished(Akonadi::MailClient::Result result, const QString &errorString);
0150 
0151 public:
0152     // For unit-test usage, since we can't depend on kdepim-runtime on jenkins
0153     ITIPHandlerComponentFactory *mFactory = nullptr;
0154 
0155 protected:
0156     /** Allows to disable Akonadi lookup in KeyResolver (for tests) */
0157     void setAkonadiLookupEnabled(bool enabled);
0158     /** Allows to override KeyResolver contact preferences (useful for tests) */
0159     virtual std::optional<MessageComposer::ContactPreference> contactPreference(const QString &email);
0160 
0161     /** Whether to show key approval dialog or not. **/
0162     virtual bool showKeyApprovalDialog() const;
0163 
0164 private:
0165     std::vector<std::unique_ptr<MessageComposer::Composer>>
0166     buildComposers(const KCalendarCore::IncidenceBase::Ptr &incidence, const KIdentityManagementCore::Identity &identity, const MessageData &msg);
0167 
0168     void populateComposer(MessageComposer::Composer *composer, const MessageData &msg);
0169 
0170     bool determineWhetherToSign(bool doSignCompletely,
0171                                 Kleo::KeyResolver *keyResolver,
0172                                 ITIPHandlerDialogDelegate *dialogDelegate,
0173                                 const KIdentityManagementCore::Identity &identity,
0174                                 bool signSomething,
0175                                 bool &signAttachments,
0176                                 bool &result,
0177                                 bool &canceled);
0178 
0179     bool determineWhetherToEncrypt(bool doEncryptCompletely,
0180                                    Kleo::KeyResolver *keyResolver,
0181                                    ITIPHandlerDialogDelegate *dialogDelegate,
0182                                    const KIdentityManagementCore::Identity &identity,
0183                                    bool encryptSomething,
0184                                    bool signSomething,
0185                                    bool &encryptAttachments,
0186                                    bool &result,
0187                                    bool &canceled);
0188 
0189     bool
0190     addKeysToContext(const QString &gnupgHome, const QList<QPair<QStringList, std::vector<GpgME::Key>>> &data, const std::map<QByteArray, QString> &autocrypt);
0191 
0192     void queueMessage(const MailTransport::Transport *transport,
0193                       const MessageComposer::Composer *composer,
0194                       const KCalendarCore::IncidenceBase::Ptr &incidence,
0195                       const KIdentityManagementCore::Identity &identity,
0196                       const MessageData &msg,
0197                       const KMime::Message::Ptr &message);
0198 
0199     void populateKeyResolverContactsPreferences(Kleo::KeyResolver &keyResolver, const QStringList &addresses);
0200 };
0201 }
0202 
0203 Q_DECLARE_METATYPE(Akonadi::MailClient::Result)