Libksieve

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

KDE's Doxygen guidelines are available online.