File indexing completed on 2024-11-24 04:49:53

0001 /*
0002    SPDX-FileCopyrightText: 2005 Till Adam <adam@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "util.h"
0008 #include "libksievecore_debug.h"
0009 #include "sieve-vacation.h"
0010 #include "sieveimapinstance/sieveimapinstance.h"
0011 #include "sieveimapinstance/sieveimapinstanceinterfacemanager.h"
0012 #include "util.h"
0013 #include <PimCommon/PimUtil>
0014 #include <pimcommon/imapresourcesettings.h>
0015 
0016 #include <KMime/KMimeMessage>
0017 #include <MailTransport/Transport>
0018 
0019 using namespace KSieveCore;
0020 
0021 QStringList KSieveCore::Util::sieveImapResourceNames()
0022 {
0023     const QList<KSieveCore::SieveImapInstance> lst = KSieveCore::Util::sieveImapInstances();
0024     QStringList resourceNames;
0025     resourceNames.reserve(lst.count());
0026     for (const KSieveCore::SieveImapInstance &type : lst) {
0027         resourceNames << type.identifier();
0028     }
0029     return resourceNames;
0030 }
0031 
0032 QList<KSieveCore::SieveImapInstance> KSieveCore::Util::sieveImapInstances()
0033 {
0034     const QList<KSieveCore::SieveImapInstance> allInstances = KSieveCore::SieveImapInstanceInterfaceManager::self()->sieveImapInstanceList();
0035     QList<KSieveCore::SieveImapInstance> relevantInstances;
0036     for (const KSieveCore::SieveImapInstance &instance : allInstances) {
0037         if (instance.mimeTypes().contains(KMime::Message::mimeType()) && instance.capabilities().contains(QLatin1StringView("Resource"))
0038             && !instance.capabilities().contains(QLatin1StringView("Virtual"))) {
0039             if (PimCommon::Util::isImapResource(instance.identifier())) {
0040                 relevantInstances << instance;
0041             }
0042         }
0043     }
0044     return relevantInstances;
0045 }
0046 
0047 bool KSieveCore::Util::checkOutOfOfficeOnStartup()
0048 {
0049     return VacationSettings::self()->checkOutOfOfficeOnStartup();
0050 }
0051 
0052 bool KSieveCore::Util::allowOutOfOfficeSettings()
0053 {
0054     return VacationSettings::self()->allowOutOfOfficeSettings();
0055 }
0056 
0057 bool Util::hasKep14CapabilitySupport(const QStringList &sieveCapabilities)
0058 {
0059     const bool hasIncludeCapability = sieveCapabilities.contains(QLatin1StringView("include"));
0060     return hasIncludeCapability;
0061 }
0062 
0063 bool Util::hasKep14Support(const QStringList &sieveCapabilities, const QStringList &availableScripts, const QString &activeScript)
0064 {
0065     const bool hasIncludeCapability = Util::hasKep14CapabilitySupport(sieveCapabilities);
0066     if (!hasIncludeCapability) {
0067         return false;
0068     }
0069 
0070     bool masterIsActive = !activeScript.isEmpty();
0071     if (masterIsActive) {
0072         const QString scriptName = activeScript.split(QLatin1Char('.')).first().toLower();
0073         masterIsActive = (scriptName == QLatin1StringView("master") || scriptName == QLatin1StringView("user"));
0074     }
0075     if (!masterIsActive) {
0076         return false;
0077     }
0078 
0079     bool hasUserScript = false;
0080     for (const QString &script : availableScripts) {
0081         if (script.isEmpty()) {
0082             continue;
0083         }
0084         const QString name = script.split(QLatin1Char('.')).first().toLower();
0085         if (name == QLatin1StringView("user")) {
0086             hasUserScript = true;
0087             break;
0088         }
0089     }
0090 
0091     return hasIncludeCapability && masterIsActive && hasUserScript;
0092 }
0093 
0094 bool Util::isKep14ProtectedName(const QString &name)
0095 {
0096     const QString n = name.split(QLatin1Char('.')).first().toLower();
0097     if (n == QLatin1StringView("master") || n == QLatin1StringView("user") || n == QLatin1StringView("management")) {
0098         return true;
0099     }
0100     return false;
0101 }
0102 
0103 QDebug Util::operator<<(QDebug d, const Util::AccountInfo &info)
0104 {
0105     d << "sieveImapAccountSettings " << info.sieveImapAccountSettings;
0106     d << "url " << info.sieveUrl.toString();
0107     return d;
0108 }
0109 
0110 bool Util::AccountInfo::operator==(const Util::AccountInfo &other) const
0111 {
0112     const bool result = (other.sieveImapAccountSettings == sieveImapAccountSettings) && (other.sieveUrl == sieveUrl);
0113     if (!result) {
0114         qCDebug(LIBKSIEVECORE_LOG) << "actual :" << *this << "\n other :" << other;
0115     }
0116     return result;
0117 }