Debian Codegraph @

Dekko/backend/autoconfig/autoconfig.cpp

raw symbols SCIP
1 /* Copyright (C) 2015 Dan Chapman <[email protected]>
2
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License as
5 published by the Free Software Foundation; either version 2 of
6 the License or (at your option) version 3
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include "autoconfig.h"
17 #include <QDebug>
18 #include <QDomDocument>
19 #include <QFile>
20 #include <Paths.h>
21
22 AutoConfig::AutoConfig(QObject *parent, EmailProvider *config):
23 QObject(parent),
24 #ifdef USE_CONNECTIVITY_API
25 m_netStatus(new Connectivity),
26 #endif
27 m_config(config)
28 {
29 m_nam = new QNetworkAccessManager(this);
30 connect(m_nam, &QNetworkAccessManager::finished, this, &AutoConfig::handleRequestResponse);
31 }
32
33 void AutoConfig::findLocal(const QString &domain)
34 {
35 if (domain.isEmpty()) {
36 emit failed();
37 return;
38 }
39 QString configPath = Paths::configLocationForFile(QStringLiteral("autoconfig/%1/config-v1.2.xml").arg(domain));
40 qDebug() << "ConfigPath is: " << configPath;
41 if (QFile::exists(configPath)) {
42 QFile config(configPath);
43 if (!config.open(QIODevice::ReadOnly | QIODevice::Text)) {
44 qDebug() << "Failed opening file: " << configPath;
45 emit failed();
46 return;
47 }
48 EmailProvider *provider = EmailProvider::fromXml(config.readAll());
49 if (provider->isValid()) {
50 qDebug() << __func__ << "SUCCESS";
51 m_config = provider;
52 emit success(m_config);
53 } else {
54 qDebug() << __func__ << "FAILED";
55 emit failed();
56 }
57 } else {
58 qDebug() << "No local file";
59 }
60 emit failed();
61 }
62
63 void AutoConfig::lookUp(const QUrl &url)
64 {
65 if (!url.isValid()) {
66 qDebug() << "[AutoConfig] Invalid URL: " << url;
67 emit failed();
68 return;
69 }
70 m_nam->get(QNetworkRequest(url));
71 }
72
73 bool AutoConfig::networkAccessible() const
74 {
75 #ifdef USE_CONNECTIVITY_API
76 return m_netStatus->online();
77 #else
78 return m_nam->networkAccessible() == QNetworkAccessManager::Accessible;
79 #endif
80 }
81
82 void AutoConfig::handleRequestResponse(QNetworkReply *reply)
83 {
84 // We should handle redirects so do that first.
85 QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
86 if (redirect.isValid()) {
87 qDebug() << "[AutoConfig] Redirecting to: " << redirect.toUrl();
88 m_nam->get(QNetworkRequest(redirect.toUrl()));
89 reply->deleteLater();
90 return;
91 }
92 bool successful = false;
93 if (reply->error() == QNetworkReply::NoError) {
94 QByteArray data = reply->readAll();
95 if (data.isEmpty() || data.isNull()) {
96 emit failed();
97 return;
98 }
99 EmailProvider *provider = EmailProvider::fromXml(data);
100 successful = provider->isValid();
101 if (successful) {
102 qDebug() << __func__ << "SUCCESS";
103 m_config = provider;
104 emit success(m_config);
105 } else {
106 qDebug() << __func__ << "FAILED";
107 emit failed();
108 }
109 } else {
110 emit failed();
111 }
112
113 }
114
115 void AutoConfig::fakeLookUp(const QUrl &url)
116 {
117 if (url.isEmpty()) {
118 return;
119 }
120 QByteArray fakeXmlResponse = "\
121 <clientConfig version=\"1.1\"> \
122 <emailProvider id=\"example.com\"> \
123 <incomingServer type=\"imap\"> \
124 <hostname>imap.example.com</hostname> \
125 <port>993</port> \
126 <socketType>SSL</socketType> \
127 <authentication>password-cleartext</authentication> \
128 <username>%EMAILADDRESS%</username> \
129 </incomingServer> \
130 <outgoingServer type=\"smtp\"> \
131 <hostname>smtp.example.com</hostname> \
132 <port>587</port> \
133 <socketType>STARTTLS</socketType> \
134 <authentication>password-cleartext</authentication> \
135 <username>%EMAILADDRESS%</username> \
136 </outgoingServer> \
137 </clientConfig>";
138
139 m_config = EmailProvider::fromXml(fakeXmlResponse);
140 emit success(m_config);
141 }
142
143