| 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 "autodiscover.h" |
| 17 | #include <QDebug> |
| 18 | |
| 19 | AutoDiscover::AutoDiscover(QObject *parent) : |
| 20 | QObject(parent), m_inProgress(false), m_testMode(false) |
| 21 | { |
| 22 | m_serverConfig = new EmailProvider(this); |
| 23 | m_autoConfig = new AutoConfig(this, m_serverConfig); |
| 24 | connect(m_autoConfig, &AutoConfig::failed, this, &AutoDiscover::handleRequestFailed); |
| 25 | connect(m_autoConfig, &AutoConfig::success, this, &AutoDiscover::handleRequestSucceeded); |
| 26 | // m_srvLookup = new SrvLookup(this, m_serverConfig); |
| 27 | // connect(m_srvLookup, &SrvLookup::failed, this, &AutoDiscover::handleRequestFailed); |
| 28 | // connect(m_srvLookup, &SrvLookup::success, this, &AutoDiscover::handleRequestSucceeded); |
| 29 | m_status = INVALID; |
| 30 | } |
| 31 | |
| 32 | bool AutoDiscover::isValidMailAddress(const QString &address) |
| 33 | { |
| 34 | return EmailValidator::instance()->validate(address); |
| 35 | } |
| 36 | |
| 37 | bool AutoDiscover::inProgress() const |
| 38 | { |
| 39 | return m_inProgress; |
| 40 | } |
| 41 | |
| 42 | AutoDiscover::Status AutoDiscover::status() const |
| 43 | { |
| 44 | return m_status; |
| 45 | } |
| 46 | |
| 47 | void AutoDiscover::lookUp(const QString &mailAddress) |
| 48 | { |
| 49 | if (!isValidMailAddress(mailAddress)) { |
| 50 | emit invalidMailAddress(mailAddress); |
| 51 | emit failed(); |
| 52 | return; |
| 53 | } |
| 54 | if (!mailAddress.contains("@", Qt::CaseInsensitive)) { |
| 55 | emit failed(); |
| 56 | return; |
| 57 | } |
| 58 | m_domain = mailAddress.split("@")[1]; |
| 59 | // Although this is a valid email address, it may not |
| 60 | // actually contain a domain. AutoDiscover works on the |
| 61 | // domain being valid so needs to have at least 1 period |
| 62 | // i.e .com .co.uk etc |
| 63 | // So fail autoconfig now and request manual entry |
| 64 | if (!m_domain.contains(QLatin1Char('.'))) { |
| 65 | emit failed(); |
| 66 | return; |
| 67 | } |
| 68 | setStatus(NEW_REQUEST); |
| 69 | buildNextRequest(); |
| 70 | } |
| 71 | |
| 72 | void AutoDiscover::buildNextRequest() |
| 73 | { |
| 74 | |
| 75 | if (!m_testMode && !m_autoConfig->networkAccessible()) { |
| 76 | emit noNetworkAvailable(); |
| 77 | emit failed(); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | switch(m_status) { |
| 82 | case INVALID: |
| 83 | setStatus(REQUEST_FAILED); |
| 84 | emit failed(); // Nothing we can do from here now. |
| 85 | break; |
| 86 | case NEW_REQUEST: |
| 87 | qDebug() << "[AutoDiscover::NEW_REQUEST]" << "Requesting local first"; |
| 88 | setStatus(REQUEST_LOCAL); |
| 89 | buildNextRequest(); |
| 90 | break; |
| 91 | case REQUEST_LOCAL: |
| 92 | qDebug() << "[AutoDiscover::REQUEST_LOCAL]" << "Looking for local config"; |
| 93 | m_autoConfig->findLocal(m_domain); |
| 94 | break; |
| 95 | case REQUEST_AUTOCONFIG_V12: |
| 96 | qDebug() << "[AutoDiscover::REQUEST_AUTOCONFIG_V12]" << "Looking for autoconfig v1.2"; |
| 97 | m_autoConfig->lookUp(QString("http://autoconfig.%1/mail/config-v1.2.xml").arg(m_domain)); |
| 98 | break; |
| 99 | case REQUEST_AUTOCONFIG_WELLKNOWN_V12: |
| 100 | qDebug() << "[AutoDiscover::REQUEST_AUTOCONFIG_WELLKNOWN_V12]" << "Looking for wellknow autocnfig v1.2"; |
| 101 | m_autoConfig->lookUp(QString("http://%1/.well-known/autoconfig/mail/config-v1.2.xml").arg(m_domain)); |
| 102 | break; |
| 103 | case REQUEST_AUTOCONFIG_V11: |
| 104 | qDebug() << "[AutoDiscover::NEW_REQUEST]" << "Looking for autoconfig v1.1"; |
| 105 | m_autoConfig->lookUp(QString("http://autoconfig.%1/mail/config-v1.1.xml").arg(m_domain)); |
| 106 | break; |
| 107 | case REQUEST_AUTOCONFIG_WELLKNOWN_V11: |
| 108 | qDebug() << "[AutoDiscover::REQUEST_AUTOCONFIG_WELLKNOWN_V11]" << "Looking for wellknow autocnfig v1.1"; |
| 109 | m_autoConfig->lookUp(QString("http://%1/.well-known/autoconfig/mail/config-v1.1.xml").arg(m_domain)); |
| 110 | break; |
| 111 | case REQUEST_AUTOCONFIG_ISPDB: |
| 112 | qDebug() << "[AutoDiscover::REQUEST_AUTOCONFIG_ISPDB]" << "Last chance! trying thunderbirds ispdb..."; |
| 113 | m_autoConfig->lookUp(QString("https://autoconfig.thunderbird.net/v1.1/%1").arg(m_domain)); |
| 114 | break; |
| 115 | case REQUEST_FAILED: |
| 116 | case REQUEST_SUCCEEDED: |
| 117 | case BUILDING_RESULT: |
| 118 | setStatus(REQUEST_FAILED); |
| 119 | emit failed(); // Nothing we can do from here now. |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void AutoDiscover::handleRequestFailed() |
| 125 | { |
| 126 | |
| 127 | switch(m_status) { |
| 128 | case INVALID: |
| 129 | case NEW_REQUEST: |
| 130 | setStatus(REQUEST_FAILED); |
| 131 | emit failed(); // Nothing we can do from here now. |
| 132 | break; |
| 133 | case REQUEST_LOCAL: |
| 134 | qDebug() << "[AutoDiscover::REQUEST_LOCAL]" << "No local configs"; |
| 135 | setStatus(REQUEST_AUTOCONFIG_V12); |
| 136 | buildNextRequest(); |
| 137 | break; |
| 138 | case REQUEST_AUTOCONFIG_V12: |
| 139 | qDebug() << "[AutoDiscover::REQUEST_AUTOCONFIG_V12]" << "Failed lookup"; |
| 140 | setStatus(REQUEST_AUTOCONFIG_WELLKNOWN_V12); |
| 141 | buildNextRequest(); |
| 142 | break; |
| 143 | case REQUEST_AUTOCONFIG_WELLKNOWN_V12: |
| 144 | qDebug() << "[AutoDiscover::REQUEST_AUTOCONFIG_WELLKNOWN_V12]" << "Failed lookup"; |
| 145 | setStatus(REQUEST_AUTOCONFIG_V11); |
| 146 | buildNextRequest(); |
| 147 | break; |
| 148 | case REQUEST_AUTOCONFIG_V11: |
| 149 | qDebug() << "[AutoDiscover::REQUEST_AUTOCONFIG_V11]" << "Failed lookup"; |
| 150 | setStatus(REQUEST_AUTOCONFIG_WELLKNOWN_V11); |
| 151 | buildNextRequest(); |
| 152 | break; |
| 153 | case REQUEST_AUTOCONFIG_WELLKNOWN_V11: |
| 154 | qDebug() << "[AutoDiscover::REQUEST_AUTOCONFIG_WELLKNOWN_V11]" << "Failed lookup"; |
| 155 | setStatus(REQUEST_AUTOCONFIG_ISPDB); |
| 156 | buildNextRequest(); |
| 157 | break; |
| 158 | case REQUEST_AUTOCONFIG_ISPDB: |
| 159 | qDebug() << "[AutoDiscover::REQUEST_AUTOCONFIG_ISPDB]" << "Failed lookup"; |
| 160 | setStatus(REQUEST_FAILED); |
| 161 | buildNextRequest(); |
| 162 | break; |
| 163 | case REQUEST_FAILED: |
| 164 | case REQUEST_SUCCEEDED: |
| 165 | case BUILDING_RESULT: |
| 166 | setStatus(REQUEST_FAILED); |
| 167 | buildNextRequest(); |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void AutoDiscover::setStatus(AutoDiscover::Status status) |
| 173 | { |
| 174 | // Anything other than INVALID and REQUEST_FAILED means we are in progress. |
| 175 | if (status == INVALID || status == REQUEST_FAILED || status == REQUEST_SUCCEEDED) { |
| 176 | m_inProgress = false; |
| 177 | } else if (!m_inProgress) { |
| 178 | m_inProgress = true; |
| 179 | } |
| 180 | m_status = status; |
| 181 | emit progressChanged(); |
| 182 | emit statusChanged(); |
| 183 | } |
| 184 | |
| 185 | void AutoDiscover::handleRequestSucceeded(EmailProvider *config) |
| 186 | { |
| 187 | qDebug() << "++++++++++++++++++++++++"; |
| 188 | qDebug() << "SUCCESS!"; |
| 189 | qDebug() << "VERSION: " << config->version(); |
| 190 | qDebug() << "DisplayName: " << config->displayName(); |
| 191 | qDebug() << "Short display name: " << config->shortName(); |
| 192 | qDebug() << "Total incoming: " << config->incoming()->size(); |
| 193 | qDebug() << "Incoming host" << config->incoming()->at(0)->hostname(); |
| 194 | qDebug() << "++++++++++++++++++++++++"; |
| 195 | if (config == NULL || !config->isValid()) { |
| 196 | setStatus(INVALID); |
| 197 | emit failed(); |
| 198 | return; |
| 199 | } |
| 200 | setStatus(REQUEST_SUCCEEDED); |
| 201 | m_serverConfig = config; |
| 202 | if (config != NULL) { |
| 203 | emit success(); |
| 204 | } else { |
| 205 | // errr how did this happen?? |
| 206 | qDebug() << "[AutoDiscover] Somehow the configs don't match, they are: "; |
| 207 | // qDebug() << "this: " << m_serverConfig->toMap(); |
| 208 | // qDebug() << "other: " << config->toMap(); |
| 209 | setStatus(INVALID); |
| 210 | emit failed(); |
| 211 | return; |
| 212 | } |
| 213 | } |