File indexing completed on 2025-02-02 05:08:35

0001 // SPDX-FileCopyrightText: 2010 Omat Holding B.V. <info@omat.nl>
0002 // SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0004 // SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 #include "ispdb/serverconfiguration.h"
0007 #include <KLocalizedString>
0008 
0009 QString replacePlaceholders(const QString &in, const KMime::Types::AddrSpec &addrSpec)
0010 {
0011     QString out(in);
0012     out.replace(QLatin1StringView("%EMAILLOCALPART%"), addrSpec.localPart);
0013     out.replace(QLatin1StringView("%EMAILADDRESS%"), addrSpec.asString());
0014     out.replace(QLatin1StringView("%EMAILDOMAIN%"), addrSpec.domain);
0015     return out;
0016 }
0017 
0018 QStringList Server::tags() const
0019 {
0020     QStringList tags;
0021     switch (type) {
0022     case IMAP:
0023         tags << i18n("IMAP");
0024         break;
0025     case POP3:
0026         tags << i18n("POP3");
0027         break;
0028     case SMTP:
0029         tags << i18n("SMTP");
0030         break;
0031     }
0032 
0033     switch (socketType) {
0034     case SSL:
0035         tags << i18n("SSL/TLS");
0036         break;
0037     case StartTLS:
0038         tags << i18n("StartTLS");
0039         break;
0040     case None:
0041         tags << i18nc("No security mechanism", "None");
0042         break;
0043     }
0044 
0045     return tags;
0046 }
0047 
0048 std::optional<Server> Server::fromDomElement(const QDomElement &element, const KMime::Types::AddrSpec &addrSpec)
0049 {
0050     QDomNode o = element.firstChild();
0051     Server server;
0052     while (!o.isNull()) {
0053         QDomElement f = o.toElement();
0054         if (f.isNull()) {
0055             o = o.nextSibling();
0056             continue;
0057         }
0058 
0059         const QString tagName(f.tagName());
0060         if (tagName == QLatin1StringView("hostname")) {
0061             server.hostname = replacePlaceholders(f.text(), addrSpec);
0062         } else if (tagName == QLatin1StringView("port")) {
0063             server.port = f.text().toInt();
0064         } else if (tagName == QLatin1StringView("socketType")) {
0065             const QString type(f.text());
0066             if (type == QLatin1StringView("plain")) {
0067                 server.socketType = None;
0068             } else if (type == QLatin1StringView("SSL")) {
0069                 server.socketType = SSL;
0070             }
0071             if (type == QLatin1StringView("STARTTLS")) {
0072                 server.socketType = StartTLS;
0073             }
0074         } else if (tagName == QLatin1StringView("username")) {
0075             server.username = replacePlaceholders(f.text(), addrSpec);
0076         } else if (tagName == QLatin1StringView("authentication") && server.authType == 0) {
0077             const QString type(f.text());
0078             if (type == QLatin1StringView("password-cleartext") || type == QLatin1StringView("plain")) {
0079                 server.authType = Plain;
0080             } else if (type == QLatin1StringView("password-encrypted") || type == QLatin1StringView("secure")) {
0081                 server.authType = CramMD5;
0082             } else if (type == QLatin1StringView("NTLM")) {
0083                 server.authType = NTLM;
0084             } else if (type == QLatin1StringView("GSSAPI")) {
0085                 server.authType = GSSAPI;
0086             } else if (type == QLatin1StringView("client-ip-based")) {
0087                 server.authType = ClientIP;
0088             } else if (type == QLatin1StringView("none")) {
0089                 server.authType = NoAuth;
0090             } else if (type == QLatin1StringView("OAuth2")) {
0091                 server.authType = OAuth2;
0092             }
0093         }
0094         o = o.nextSibling();
0095     }
0096     if (server.port == -1) {
0097         return std::nullopt;
0098     }
0099     return server;
0100 }
0101 
0102 QDebug operator<<(QDebug d, const EmailProvider &t)
0103 {
0104     d.space() << "domains" << t.domains;
0105     d.space() << "displayName" << t.displayName;
0106     d.space() << "shortDisplayName" << t.shortDisplayName;
0107     d.space() << "imapServers" << t.imapServers;
0108     d.space() << "popServers" << t.popServers;
0109     d.space() << "smtpServers" << t.smtpServers;
0110 
0111     return d;
0112 }
0113 
0114 QDebug operator<<(QDebug d, const Server &t)
0115 {
0116     d.space() << "type" << t.type;
0117     d.space() << "hostname" << t.hostname;
0118     d.space() << "port" << t.port;
0119     d.space() << "username" << t.username;
0120     d.space() << "socketType" << t.socketType;
0121     d.space() << "authType" << t.authType;
0122     return d;
0123 }