Libksieve

findaccountinfojob.cpp
1/*
2 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6#include "findaccountinfojob.h"
7#include "abstractakonadiimapsettinginterface.h"
8#include "akonadiimapsettinginterface.h"
9#include "libksievecore_debug.h"
10#include "sieveimappasswordprovider.h"
11#include <MailTransport/Transport>
12#include <PimCommon/PimUtil>
13#include <QUrlQuery>
14#include <memory>
15#include <pimcommon/imapresourcesettings.h>
16
17using namespace KSieveCore;
18FindAccountInfoJob::FindAccountInfoJob(QObject *parent)
19 : QObject(parent)
20{
21}
22
23FindAccountInfoJob::~FindAccountInfoJob() = default;
24
25bool FindAccountInfoJob::canStart() const
26{
27 return !mIdentifier.isEmpty();
28}
29
30void FindAccountInfoJob::setCustomImapSettingsInterface(AbstractAkonadiImapSettingInterface *newCustomImapSettingsInterface)
31{
32 mCustomImapSettingsInterface = newCustomImapSettingsInterface;
33}
34
35void FindAccountInfoJob::sendAccountInfo()
36{
38 Q_EMIT findAccountInfoFinished(mAccountInfo);
39}
40
41void FindAccountInfoJob::start()
42{
43 if (!canStart()) {
44 qCWarning(LIBKSIEVECORE_LOG) << "Impossible to start findAccountInfoJob";
45 sendAccountInfo();
46 return;
47 }
48 if (!mPasswordProvider) {
49 sendAccountInfo();
50 return;
51 }
52
53 mInterfaceImap.reset(PimCommon::Util::createImapSettingsInterface(mIdentifier));
54 mInterface = std::make_unique<KSieveCore::AkonadiImapSettingInterface>(mInterfaceImap);
55 if (!mCustomImapSettingsInterface) {
56 mCustomImapSettingsInterface = mInterface.get();
57 }
58
59 if (!mCustomImapSettingsInterface->sieveSupport()) {
60 sendAccountInfo();
61 return;
62 }
63
64 QString server;
65 const QString reply = mCustomImapSettingsInterface->imapServer();
66 if (!reply.isEmpty()) {
67 server = reply;
68 server = server.section(QLatin1Char(':'), 0, 0);
69 } else {
70 sendAccountInfo();
71 return;
72 }
73 connect(mPasswordProvider, &SieveImapPasswordProvider::passwordsRequested, this, &FindAccountInfoJob::slotPasswordsRequested);
74 mPasswordProvider->setProperty("server", server);
75 mPasswordProvider->passwords(mIdentifier);
76}
77
78void FindAccountInfoJob::slotPasswordsRequested(const QString &sievePassword, const QString &sieveCustomPassword)
79{
80 const QString server = sender()->property("server").toString();
81 QUrl sieveUrl;
82 sieveUrl.setScheme(QStringLiteral("sieve"));
83
84 if (mCustomImapSettingsInterface->sieveReuseConfig()) {
85 // assemble Sieve url from the settings of the account:
86 const QString userName = mCustomImapSettingsInterface->userName();
87 mAccountInfo.sieveImapAccountSettings.setServerName(server);
88 mAccountInfo.sieveImapAccountSettings.setUserName(userName);
89
90 sieveUrl.setHost(server);
91 sieveUrl.setUserName(userName);
92
93 sieveUrl.setPassword(sievePassword);
94 mAccountInfo.sieveImapAccountSettings.setPassword(sievePassword);
95 mAccountInfo.sieveImapAccountSettings.setPort(mCustomImapSettingsInterface->imapPort());
96 sieveUrl.setPort(mCustomImapSettingsInterface->sievePort());
97 QString authStr;
98 mAccountInfo.sieveImapAccountSettings.setAuthenticationType(
99 static_cast<SieveImapAccountSettings::AuthenticationMode>((int)mCustomImapSettingsInterface->authentication()));
100 switch (mCustomImapSettingsInterface->authentication()) {
101 case MailTransport::Transport::EnumAuthenticationType::CLEAR:
102 case MailTransport::Transport::EnumAuthenticationType::PLAIN:
103 authStr = QStringLiteral("PLAIN");
104 break;
105 case MailTransport::Transport::EnumAuthenticationType::LOGIN:
106 authStr = QStringLiteral("LOGIN");
107 break;
108 case MailTransport::Transport::EnumAuthenticationType::CRAM_MD5:
109 authStr = QStringLiteral("CRAM-MD5");
110 break;
111 case MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5:
112 authStr = QStringLiteral("DIGEST-MD5");
113 break;
114 case MailTransport::Transport::EnumAuthenticationType::GSSAPI:
115 authStr = QStringLiteral("GSSAPI");
116 break;
117 case MailTransport::Transport::EnumAuthenticationType::ANONYMOUS:
118 authStr = QStringLiteral("ANONYMOUS");
119 break;
120 default:
121 authStr = QStringLiteral("PLAIN");
122 break;
123 }
125 query.addQueryItem(QStringLiteral("x-mech"), authStr);
126 const QString resultSafety = mCustomImapSettingsInterface->safety();
127 if (resultSafety == QLatin1StringView("None")) {
128 mAccountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::Unencrypted);
129 query.addQueryItem(QStringLiteral("x-allow-unencrypted"), QStringLiteral("true"));
130 } else if (resultSafety == QLatin1StringView("SSL")) {
131 mAccountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::SSLorTLS);
132 } else if (resultSafety == QLatin1StringView("STARTTLS")) {
133 mAccountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::STARTTLS);
134 } else {
135 mAccountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::Unencrypted);
136 }
137 sieveUrl.setQuery(query);
138 } else {
139 const QString userName = mCustomImapSettingsInterface->userName();
140 mAccountInfo.sieveImapAccountSettings.setServerName(server);
141 mAccountInfo.sieveImapAccountSettings.setUserName(userName);
142 mAccountInfo.sieveImapAccountSettings.setAuthenticationType(
143 static_cast<SieveImapAccountSettings::AuthenticationMode>((int)mCustomImapSettingsInterface->authentication()));
144 mAccountInfo.sieveImapAccountSettings.setPassword(sievePassword);
145 mAccountInfo.sieveImapAccountSettings.setPort(mCustomImapSettingsInterface->imapPort());
146
147 sieveUrl.setHost(mCustomImapSettingsInterface->sieveAlternateUrl());
148 sieveUrl.setPort(mCustomImapSettingsInterface->sievePort());
149 QString authStr;
150 const QString resultSafety = mCustomImapSettingsInterface->safety();
151 switch (mCustomImapSettingsInterface->alternateAuthentication()) {
152 case MailTransport::Transport::EnumAuthenticationType::CLEAR:
153 case MailTransport::Transport::EnumAuthenticationType::PLAIN:
154 authStr = QStringLiteral("PLAIN");
155 break;
156 case MailTransport::Transport::EnumAuthenticationType::LOGIN:
157 authStr = QStringLiteral("LOGIN");
158 break;
159 case MailTransport::Transport::EnumAuthenticationType::CRAM_MD5:
160 authStr = QStringLiteral("CRAM-MD5");
161 break;
162 case MailTransport::Transport::EnumAuthenticationType::DIGEST_MD5:
163 authStr = QStringLiteral("DIGEST-MD5");
164 break;
165 case MailTransport::Transport::EnumAuthenticationType::GSSAPI:
166 authStr = QStringLiteral("GSSAPI");
167 break;
168 case MailTransport::Transport::EnumAuthenticationType::ANONYMOUS:
169 authStr = QStringLiteral("ANONYMOUS");
170 break;
171 default:
172 authStr = QStringLiteral("PLAIN");
173 break;
174 }
176 query.addQueryItem(QStringLiteral("x-mech"), authStr);
177
178 if (resultSafety == QLatin1StringView("None")) {
179 mAccountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::Unencrypted);
180 query.addQueryItem(QStringLiteral("x-allow-unencrypted"), QStringLiteral("true"));
181 } else if (resultSafety == QLatin1StringView("SSL")) {
182 mAccountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::SSLorTLS);
183 } else if (resultSafety == QLatin1StringView("STARTTLS")) {
184 mAccountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::STARTTLS);
185 } else {
186 mAccountInfo.sieveImapAccountSettings.setEncryptionMode(SieveImapAccountSettings::Unencrypted);
187 }
188
189 sieveUrl.setQuery(query);
190
191 const QString resultCustomAuthentication = mCustomImapSettingsInterface->sieveCustomAuthentification();
192 if (resultCustomAuthentication == QLatin1StringView("ImapUserPassword")) {
193 sieveUrl.setUserName(mCustomImapSettingsInterface->userName());
194 const QString imapPwd = sievePassword;
195 sieveUrl.setPassword(imapPwd);
196 } else if (resultCustomAuthentication == QLatin1StringView("CustomUserPassword")) {
197 const QString customPwd = sieveCustomPassword;
198 sieveUrl.setPassword(customPwd);
199 sieveUrl.setUserName(mCustomImapSettingsInterface->sieveCustomUsername());
200 } else {
201 qCWarning(LIBKSIEVECORE_LOG) << "resultCustomAuthentication undefined " << resultCustomAuthentication;
202 }
203 }
204 sieveUrl = sieveUrl.adjusted(QUrl::RemoveFilename);
205 if (mWithVacationFileName) {
206 sieveUrl.setPath(sieveUrl.path() + QLatin1Char('/') + mCustomImapSettingsInterface->sieveVacationFilename());
207 }
208 mAccountInfo.sieveUrl = sieveUrl;
209 sendAccountInfo();
210}
211
212QString FindAccountInfoJob::identifier() const
213{
214 return mIdentifier;
215}
216
217void FindAccountInfoJob::setIdentifier(const QString &newIdentifier)
218{
219 mIdentifier = newIdentifier;
220}
221
222bool FindAccountInfoJob::withVacationFileName() const
223{
224 return mWithVacationFileName;
225}
226
227void FindAccountInfoJob::setWithVacationFileName(bool newWithVacationFileName)
228{
229 mWithVacationFileName = newWithVacationFileName;
230}
231
232SieveImapPasswordProvider *FindAccountInfoJob::provider() const
233{
234 return mPasswordProvider;
235}
236
237void FindAccountInfoJob::setProvider(SieveImapPasswordProvider *newProvider)
238{
239 mPasswordProvider = newProvider;
240}
241
242#include "moc_findaccountinfojob.cpp"
The SieveImapPasswordProvider class.
std::optional< QSqlQuery > query(const QString &queryStatement)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QVariant property(const char *name) const const
QObject * sender() const const
bool setProperty(const char *name, QVariant &&value)
bool isEmpty() const const
QString section(QChar sep, qsizetype start, qsizetype end, SectionFlags flags) const const
RemoveFilename
QUrl adjusted(FormattingOptions options) const const
QString path(ComponentFormattingOptions options) const const
void setHost(const QString &host, ParsingMode mode)
void setPassword(const QString &password, ParsingMode mode)
void setPath(const QString &path, ParsingMode mode)
void setPort(int port)
void setQuery(const QString &query, ParsingMode mode)
void setScheme(const QString &scheme)
void setUserName(const QString &userName, ParsingMode mode)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.