mdnadvicedialog.cpp 10.6 KB
Newer Older
1
/*
2
  SPDX-FileCopyrightText: 2009 Michael Leupold <lemma@confuego.org>
3

4
  SPDX-License-Identifier: GPL-2.0-or-later
5 6 7 8 9 10
*/

#include "mdnadvicedialog.h"
#include "kernel/mailkernel.h"
#include "mailcommon_debug.h"

Laurent Montel's avatar
Laurent Montel committed
11
#include <MessageComposer/MessageFactoryNG>
12
#include <MessageComposer/Util>
Laurent Montel's avatar
Laurent Montel committed
13
using MessageComposer::MessageFactoryNG;
14

15
#include <MessageViewer/MessageViewerSettings>
16
#include <KCursorSaver>
Sandro Knauß's avatar
Sandro Knauß committed
17
#include <MimeTreeParser/ObjectTreeParser>
18 19 20 21 22 23 24 25 26 27 28 29 30 31

#include <ItemFetchJob>
#include <ItemModifyJob>

#include <KLocalizedString>
#include <KMessageBox>

#include <QPointer>

#include <QDialogButtonBox>
#include <QPushButton>

using namespace MailCommon;

Laurent Montel's avatar
Laurent Montel committed
32
MDNAdviceHelper *MDNAdviceHelper::s_instance = nullptr;
33 34 35

static const struct {
    const char *dontAskAgainID;
Laurent Montel's avatar
Laurent Montel committed
36
    bool canDeny;
37 38 39 40 41
    const char *text;
} mdnMessageBoxes[] = {
    {
        "mdnNormalAsk", true,
        I18N_NOOP("This message contains a request to return a notification "
Laurent Montel's avatar
Laurent Montel committed
42 43 44
                  "about your reception of the message.\n"
                  "You can either ignore the request or let the mail program "
                  "send a \"denied\" or normal response.")
45 46 47 48
    },
    {
        "mdnUnknownOption", false,
        I18N_NOOP("This message contains a request to send a notification "
Laurent Montel's avatar
Laurent Montel committed
49 50 51 52 53
                  "about your reception of the message.\n"
                  "It contains a processing instruction that is marked as "
                  "\"required\", but which is unknown to the mail program.\n"
                  "You can either ignore the request or let the mail program "
                  "send a \"failed\" response.")
54 55 56 57
    },
    {
        "mdnMultipleAddressesInReceiptTo", true,
        I18N_NOOP("This message contains a request to send a notification "
Laurent Montel's avatar
Laurent Montel committed
58 59 60 61 62
                  "about your reception of the message,\n"
                  "but it is requested to send the notification to more "
                  "than one address.\n"
                  "You can either ignore the request or let the mail program "
                  "send a \"denied\" or normal response.")
63 64 65 66
    },
    {
        "mdnReturnPathEmpty", true,
        I18N_NOOP("This message contains a request to send a notification "
Laurent Montel's avatar
Laurent Montel committed
67 68 69 70
                  "about your reception of the message,\n"
                  "but there is no return-path set.\n"
                  "You can either ignore the request or let the mail program "
                  "send a \"denied\" or normal response.")
71 72 73 74
    },
    {
        "mdnReturnPathNotInReceiptTo", true,
        I18N_NOOP("This message contains a request to send a notification "
Laurent Montel's avatar
Laurent Montel committed
75 76 77 78 79
                  "about your reception of the message,\n"
                  "but the return-path address differs from the address "
                  "the notification was requested to be sent to.\n"
                  "You can either ignore the request or let the mail program "
                  "send a \"denied\" or normal response.")
80 81 82
    },
};

Laurent Montel's avatar
Laurent Montel committed
83 84
static const int numMdnMessageBoxes
    = sizeof mdnMessageBoxes / sizeof *mdnMessageBoxes;
85 86

MDNAdviceDialog::MDNAdviceDialog(const QString &text, bool canDeny, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
87
    : QDialog(parent)
88
{
89
    setWindowTitle(i18nc("@title:window", "Message Disposition Notification Request"));
Laurent Montel's avatar
Laurent Montel committed
90
    QPushButton *user1Button = nullptr;
Laurent Montel's avatar
Laurent Montel committed
91 92 93 94
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Yes, this);
    user1Button = new QPushButton;
    buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
    connect(user1Button, &QPushButton::clicked, this, &MDNAdviceDialog::slotUser1Clicked);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    if (canDeny) {
        QPushButton *user2Button = new QPushButton;
        connect(user2Button, &QPushButton::clicked, this, &MDNAdviceDialog::slotUser2Clicked);
        buttonBox->addButton(user2Button, QDialogButtonBox::ActionRole);
        user2Button->setText(i18n("Send \"&denied\""));
    }
    buttonBox->button(QDialogButtonBox::Yes)->setText(i18n("&Ignore"));
    connect(buttonBox->button(QDialogButtonBox::Yes), &QPushButton::clicked, this, &MDNAdviceDialog::slotYesClicked);
    user1Button->setText(i18n("&Send"));
    buttonBox->button(QDialogButtonBox::Yes)->setShortcut(Qt::Key_Escape);
    KMessageBox::createKMessageBox(
        this, buttonBox,
        QMessageBox::Question,
        text,
        QStringList(),
        QString(),
Laurent Montel's avatar
Laurent Montel committed
111
        nullptr,
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        KMessageBox::NoExec);
}

MDNAdviceDialog::~MDNAdviceDialog()
{
}

MessageComposer::MDNAdvice MDNAdviceDialog::result() const
{
    return m_result;
}

MessageComposer::MDNAdvice MDNAdviceHelper::questionIgnoreSend(const QString &text, bool canDeny)
{
    MessageComposer::MDNAdvice rc = MessageComposer::MDNIgnore;
    QPointer<MDNAdviceDialog> dlg(new MDNAdviceDialog(text, canDeny));
    dlg->exec();
    if (dlg) {
        rc = dlg->result();
    }
    delete dlg;
    return rc;
}

QPair< bool, KMime::MDN::SendingMode > MDNAdviceHelper::checkAndSetMDNInfo(
    const Akonadi::Item &item, KMime::MDN::DispositionType d, bool forceSend)
{
Laurent Montel's avatar
Laurent Montel committed
139
    KMime::Message::Ptr msg = MessageComposer::Util::message(item);
140 141 142 143 144 145

    // RFC 2298: At most one MDN may be issued on behalf of each
    // particular recipient by their user agent.  That is, once an MDN
    // has been issued on behalf of a recipient, no further MDNs may be
    // issued on behalf of that recipient, even if another disposition
    // is performed on the message.
Laurent Montel's avatar
Laurent Montel committed
146 147
    if (item.hasAttribute< MailCommon::MDNStateAttribute >()
        && item.attribute< MailCommon::MDNStateAttribute >()->mdnState() != MailCommon::MDNStateAttribute::MDNStateUnknown) {
148 149 150
        // if already dealt with, don't do it again.
        return QPair< bool, KMime::MDN::SendingMode >(false, KMime::MDN::SentAutomatically);
    }
Laurent Montel's avatar
Laurent Montel committed
151 152
    MailCommon::MDNStateAttribute *mdnStateAttr
        = new MailCommon::MDNStateAttribute(MailCommon::MDNStateAttribute::MDNStateUnknown);
153 154 155 156 157 158 159 160 161 162 163 164 165

    KMime::MDN::SendingMode s = KMime::MDN::SentAutomatically; // set to manual if asked user
    bool doSend = false;
    // default:
    int mode = MessageViewer::MessageViewerSettings::self()->defaultPolicy();
    if (forceSend) {   //We must send it
        mode = 3;
    } else {
        if (!mode || mode < 0 || mode > 3) {
            // early out for ignore:
            mdnStateAttr->setMDNState(MailCommon::MDNStateAttribute::MDNIgnore);
            s = KMime::MDN::SentManually;
        } else {
Laurent Montel's avatar
Laurent Montel committed
166
            if (MessageFactoryNG::MDNMDNUnknownOption(msg)) {
167 168 169 170 171 172
                mode = requestAdviceOnMDN("mdnUnknownOption");
                s = KMime::MDN::SentManually;
                // TODO set type to Failed as well
                //      and clear modifiers
            }

Laurent Montel's avatar
Laurent Montel committed
173
            if (MessageFactoryNG::MDNConfirmMultipleRecipients(msg)) {
174 175 176 177
                mode = requestAdviceOnMDN("mdnMultipleAddressesInReceiptTo");
                s = KMime::MDN::SentManually;
            }

Laurent Montel's avatar
Laurent Montel committed
178
            if (MessageFactoryNG::MDNReturnPathEmpty(msg)) {
179 180 181 182
                mode = requestAdviceOnMDN("mdnReturnPathEmpty");
                s = KMime::MDN::SentManually;
            }

Laurent Montel's avatar
Laurent Montel committed
183
            if (MessageFactoryNG::MDNReturnPathNotInRecieptTo(msg)) {
184 185 186 187
                mode = requestAdviceOnMDN("mdnReturnPathNotInReceiptTo");
                s = KMime::MDN::SentManually;
            }

Laurent Montel's avatar
Laurent Montel committed
188
            if (MessageFactoryNG::MDNRequested(msg)) {
189 190 191 192 193 194 195 196 197 198 199 200
                if (s != KMime::MDN::SentManually) {
                    // don't ask again if user has already been asked. use the users' decision
                    mode = requestAdviceOnMDN("mdnNormalAsk");
                    s = KMime::MDN::SentManually; // asked user
                }
            } else { // if message doesn't have a disposition header, never send anything.
                mode = 0;
            }
        }
    }

    // RFC 2298: An MDN MUST NOT be generated in response to an MDN.
201
    if (MessageComposer::Util::findTypeInMessage(msg.data(),
Laurent Montel's avatar
Laurent Montel committed
202
                                                 "message", "disposition-notification")) {
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        mdnStateAttr->setMDNState(MailCommon::MDNStateAttribute::MDNIgnore);
    } else if (mode == 0) {   // ignore
        doSend = false;
        mdnStateAttr->setMDNState(MailCommon::MDNStateAttribute::MDNIgnore);
    } else if (mode == 2) {   // denied
        doSend = true;
        mdnStateAttr->setMDNState(MailCommon::MDNStateAttribute::MDNDenied);
    } else if (mode == 3) {   // the user wants to send. let's make sure we can, according to the RFC.
        doSend = true;
        mdnStateAttr->setMDNState(dispositionToSentState(d));
    }

    // create a minimal version of item with just the attribute we want to change
    Akonadi::Item i(item.id());
    i.setRevision(item.revision());
    i.setMimeType(item.mimeType());
    i.addAttribute(mdnStateAttr);
    Akonadi::ItemModifyJob *modify = new Akonadi::ItemModifyJob(i);
    modify->setIgnorePayload(true);
    modify->disableRevisionCheck();
    return QPair< bool, KMime::MDN::SendingMode >(doSend, s);
}

MailCommon::MDNStateAttribute::MDNSentState MDNAdviceHelper::dispositionToSentState(
    KMime::MDN::DispositionType d)
{
    switch (d) {
    case KMime::MDN::Displayed:
        return MailCommon::MDNStateAttribute::MDNDisplayed;
    case KMime::MDN::Deleted:
        return MailCommon::MDNStateAttribute::MDNDeleted;
    case KMime::MDN::Dispatched:
        return MailCommon::MDNStateAttribute::MDNDispatched;
    case KMime::MDN::Processed:
        return MailCommon::MDNStateAttribute::MDNProcessed;
    case KMime::MDN::Denied:
        return MailCommon::MDNStateAttribute::MDNDenied;
    case KMime::MDN::Failed:
        return MailCommon::MDNStateAttribute::MDNFailed;
    default:
        return MailCommon::MDNStateAttribute::MDNStateUnknown;
Laurent Montel's avatar
Laurent Montel committed
244
    }
245 246 247 248 249 250
}

int MDNAdviceHelper::requestAdviceOnMDN(const char *what)
{
    for (int i = 0; i < numMdnMessageBoxes; ++i) {
        if (!qstrcmp(what, mdnMessageBoxes[i].dontAskAgainID)) {
251
            KCursorSaver saver(Qt::ArrowCursor);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
            MessageComposer::MDNAdvice answer;
            answer = questionIgnoreSend(i18n(mdnMessageBoxes[i].text),
                                        mdnMessageBoxes[i].canDeny);
            switch (answer) {
            case MessageComposer::MDNSend:
                return 3;

            case MessageComposer::MDNSendDenied:
                return 2;

            // don't use 1, as that's used for 'default ask" in checkMDNHeaders
            default:
            case MessageComposer::MDNIgnore:
                return 0;
            }
        }
    }
    qCWarning(MAILCOMMON_LOG) << "didn't find data for message box \""  << what << "\"";
    return MessageComposer::MDNIgnore;
}

void MDNAdviceDialog::slotUser1Clicked()
{
    m_result = MessageComposer::MDNSend;
    accept();
}

void MDNAdviceDialog::slotUser2Clicked()
{
    m_result = MessageComposer::MDNSendDenied;
    accept();
}

void MDNAdviceDialog::slotYesClicked()
{
    m_result = MessageComposer::MDNIgnore;
    accept();
}