From f35f136a4cc9c91cf1fec6ad818d9cd1725a5b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 23 Oct 2014 13:51:55 +0200 Subject: [PATCH] added yahoo mail notification --- libguh/devicemanager.h | 2 + .../devicepluginmailnotification.cpp | 59 +++++++++++----- .../devicepluginmailnotification.json | 35 ++++++++++ .../mailnotification/smtpclient.cpp | 69 ++++++++++--------- 4 files changed, 114 insertions(+), 51 deletions(-) diff --git a/libguh/devicemanager.h b/libguh/devicemanager.h index 9c777759..2385cbf8 100644 --- a/libguh/devicemanager.h +++ b/libguh/devicemanager.h @@ -64,6 +64,8 @@ public: DeviceErrorSetupMethodNotSupported, DeviceErrorHardwareNotAvailable, DeviceErrorHardwareFailure, + // TODO: Bump API version + //DeviceErrorAuthentificationFailure, DeviceErrorAsync, DeviceErrorDeviceInUse, DeviceErrorPairingTransactionIdNotFound, diff --git a/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp index 1e863183..90d99c20 100644 --- a/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp +++ b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp @@ -230,6 +230,7 @@ #include DeviceClassId googleMailDeviceClassId = DeviceClassId("3869884a-1592-4b8f-84a7-994be18ff555"); +DeviceClassId yahooMailDeviceClassId = DeviceClassId("59409e8f-0c83-414f-abd5-bbbf2758acba"); DeviceClassId customMailDeviceClassId = DeviceClassId("f4844c97-7ca6-4349-904e-ff9749a9fe74"); ActionTypeId sendMailActionTypeId = ActionTypeId("054613b0-3666-4dad-9252-e0ebca187edc"); @@ -245,13 +246,34 @@ DevicePluginMailNotification::~DevicePluginMailNotification() DeviceManager::DeviceSetupStatus DevicePluginMailNotification::setupDevice(Device *device) { // Google mail - if(device->deviceClassId() == googleMailDeviceClassId){ + if(device->deviceClassId() == googleMailDeviceClassId) { device->setName("Google Mail (" + device->paramValue("user").toString() + ")"); SmtpClient *smtpClient = new SmtpClient(this); smtpClient->setHost("smtp.gmail.com"); smtpClient->setPort(465); smtpClient->setUser(device->paramValue("user").toString()); + // TODO: use cryptography to save password not as plain text + smtpClient->setPassword(device->paramValue("password").toString()); + smtpClient->setAuthMethod(SmtpClient::AuthMethodLogin); + smtpClient->setEncryptionType(SmtpClient::EncryptionTypeSSL); + smtpClient->setSender(device->paramValue("user").toString()); + smtpClient->setRecipient(device->paramValue("recipient").toString()); + connect(smtpClient, &SmtpClient::testLoginFinished, this, &DevicePluginMailNotification::testLoginFinished); + connect(smtpClient, &SmtpClient::sendMailFinished, this, &DevicePluginMailNotification::sendMailFinished); + m_clients.insert(smtpClient,device); + + smtpClient->testLogin(); + + return DeviceManager::DeviceSetupStatusAsync; + } + // Yahoo mail + if(device->deviceClassId() == yahooMailDeviceClassId) { + device->setName("Yahoo Mail (" + device->paramValue("user").toString() + ")"); + SmtpClient *smtpClient = new SmtpClient(this); + smtpClient->setHost("smtp.mail.yahoo.com"); + smtpClient->setPort(465); + smtpClient->setUser(device->paramValue("user").toString()); // TODO: use cryptography to save password not as plain text smtpClient->setPassword(device->paramValue("password").toString()); smtpClient->setAuthMethod(SmtpClient::AuthMethodLogin); @@ -268,7 +290,7 @@ DeviceManager::DeviceSetupStatus DevicePluginMailNotification::setupDevice(Devic return DeviceManager::DeviceSetupStatusAsync; } // Custom mail - if(device->deviceClassId() == customMailDeviceClassId){ + if(device->deviceClassId() == customMailDeviceClassId) { device->setName("Custom Mail (" + device->paramValue("sender mail").toString() + ")"); SmtpClient *smtpClient = new SmtpClient(this); smtpClient->setHost(device->paramValue("SMTP server").toString()); @@ -278,21 +300,21 @@ DeviceManager::DeviceSetupStatus DevicePluginMailNotification::setupDevice(Devic // TODO: use cryptography to save password not as plain text smtpClient->setPassword(device->paramValue("password").toString()); - if(device->paramValue("authentification").toString() == "PLAIN"){ + if(device->paramValue("authentification").toString() == "PLAIN") { smtpClient->setAuthMethod(SmtpClient::AuthMethodPlain); - }else if(device->paramValue("authentification").toString() == "LOGIN"){ + } else if(device->paramValue("authentification").toString() == "LOGIN") { smtpClient->setAuthMethod(SmtpClient::AuthMethodLogin); - }else{ + } else { return DeviceManager::DeviceSetupStatusFailure; } - if(device->paramValue("encryption").toString() == "NONE"){ + if(device->paramValue("encryption").toString() == "NONE") { smtpClient->setEncryptionType(SmtpClient::EncryptionTypeNone); - } else if(device->paramValue("encryption").toString() == "SSL"){ + } else if(device->paramValue("encryption").toString() == "SSL") { smtpClient->setEncryptionType(SmtpClient::EncryptionTypeSSL); - }else if(device->paramValue("encryption").toString() == "TLS"){ + } else if(device->paramValue("encryption").toString() == "TLS") { smtpClient->setEncryptionType(SmtpClient::EncryptionTypeTLS); - }else{ + } else { return DeviceManager::DeviceSetupStatusFailure; } @@ -317,10 +339,9 @@ DeviceManager::HardwareResources DevicePluginMailNotification::requiredHardware( DeviceManager::DeviceError DevicePluginMailNotification::executeAction(Device *device, const Action &action) { - if(action.actionTypeId() == sendMailActionTypeId){ - SmtpClient *smtpClient= m_clients.key(device); + if(action.actionTypeId() == sendMailActionTypeId) { + SmtpClient *smtpClient = m_clients.key(device); smtpClient->sendMail(action.param("subject").value().toString(), action.param("body").value().toString(), action.id()); - return DeviceManager::DeviceErrorAsync; } return DeviceManager::DeviceErrorActionTypeNotFound; @@ -337,20 +358,22 @@ void DevicePluginMailNotification::testLoginFinished(const bool &success) { SmtpClient *smtpClient = static_cast(sender()); Device *device = m_clients.value(smtpClient); - if(success){ + if(success) { emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusSuccess); - }else{ + } else { emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure); - m_clients.remove(smtpClient); - delete smtpClient; + if(m_clients.contains(smtpClient)) { + m_clients.remove(smtpClient); + } + smtpClient->deleteLater(); } } void DevicePluginMailNotification::sendMailFinished(const bool &success, const ActionId &actionId) { - if(success){ + if(success) { emit actionExecutionFinished(actionId, DeviceManager::DeviceErrorNoError); - }else{ + } else { emit actionExecutionFinished(actionId, DeviceManager::DeviceErrorDeviceNotFound); } } diff --git a/plugins/deviceplugins/mailnotification/devicepluginmailnotification.json b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.json index 7c9c7116..21340329 100644 --- a/plugins/deviceplugins/mailnotification/devicepluginmailnotification.json +++ b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.json @@ -41,6 +41,41 @@ } ] }, + { + "deviceClassId": "59409e8f-0c83-414f-abd5-bbbf2758acba", + "name": "Yahoo Mail", + "createMethods": ["user"], + "paramTypes": [ + { + "name": "user", + "type": "QString" + }, + { + "name": "password", + "type": "QString" + }, + { + "name": "recipient", + "type": "QString" + } + ], + "actionTypes": [ + { + "id": "054613b0-3666-4dad-9252-e0ebca187edc", + "name": "Send mail", + "paramTypes": [ + { + "name": "subject", + "type": "QString" + }, + { + "name": "body", + "type": "QString" + } + ] + } + ] + }, { "deviceClassId": "f4844c97-7ca6-4349-904e-ff9749a9fe74", "name": "Custom Mail", diff --git a/plugins/deviceplugins/mailnotification/smtpclient.cpp b/plugins/deviceplugins/mailnotification/smtpclient.cpp index bc4f3988..8a4a1b84 100644 --- a/plugins/deviceplugins/mailnotification/smtpclient.cpp +++ b/plugins/deviceplugins/mailnotification/smtpclient.cpp @@ -71,7 +71,7 @@ void SmtpClient::readData() QString response; QString responseLine; - while(m_socket->canReadLine() && responseLine[3] != ' '){ + while(m_socket->canReadLine() && responseLine[3] != ' ') { responseLine = m_socket->readLine(); response.append(responseLine); } @@ -79,32 +79,32 @@ void SmtpClient::readData() switch (m_state) { case InitState: - if(responseLine == "220"){ + if(responseLine == "220") { send("EHLO localhost"); - if(m_encryptionType == EncryptionTypeNone){ + if(m_encryptionType == EncryptionTypeNone) { m_state = AuthentificationState; break; } - if(m_encryptionType == EncryptionTypeSSL){ + if(m_encryptionType == EncryptionTypeSSL) { m_state = HandShakeState; break; } - if(m_encryptionType == EncryptionTypeTLS){ + if(m_encryptionType == EncryptionTypeTLS) { m_state = StartTlsState; break; } } break; case HandShakeState: - if(responseLine == "250"){ - if(!m_socket->isEncrypted() && m_encryptionType != EncryptionTypeNone){ + if(responseLine == "250") { + if(!m_socket->isEncrypted() && m_encryptionType != EncryptionTypeNone) { m_socket->startClientEncryption(); } send("EHLO localhost"); m_state = AuthentificationState; } - if(responseLine == "220"){ - if(!m_socket->isEncrypted() && m_encryptionType != EncryptionTypeNone){ + if(responseLine == "220") { + if(!m_socket->isEncrypted() && m_encryptionType != EncryptionTypeNone) { m_socket->startClientEncryption(); } send("EHLO localhost"); @@ -125,24 +125,24 @@ void SmtpClient::readData() } break; case StartTlsState: - if(responseLine == "250"){ + if(responseLine == "250") { send("STARTTLS"); m_state = HandShakeState; } break; case AuthentificationState: - if(responseLine == "250"){ - if(m_authMethod == AuthMethodLogin){ + if(responseLine == "250") { + if(m_authMethod == AuthMethodLogin) { send("AUTH LOGIN"); m_state = UserState; break; } - if(m_authMethod == AuthMethodPlain){ + if(m_authMethod == AuthMethodPlain) { send("AUTH PLAIN " + QByteArray().append((char) 0).append(m_user).append((char) 0).append(m_password).toBase64()); // if we just want to test the Login, we are almost done here - if(!m_testLogin){ + if(!m_testLogin) { m_state = MailState; - }else{ + } else { m_state = TestLoginFinishedState; } break; @@ -150,62 +150,65 @@ void SmtpClient::readData() } break; case UserState: - if(responseLine == "334"){ + if(responseLine == "334") { send(QByteArray().append(m_user).toBase64()); m_state = PasswordState; } break; case PasswordState: - if(responseLine == "334"){ + if(responseLine == "334") { send(QByteArray().append(m_password).toBase64()); // if we just want to test the Login, we are almost done here - if(!m_testLogin){ + if(!m_testLogin) { m_state = MailState; - }else{ + } else { m_state = TestLoginFinishedState; } - break; } - break; - case TestLoginFinishedState: - if(responseLine == "235"){ - emit testLoginFinished(true); - m_socket->close(); - m_testLogin = false; + break; } break; + case TestLoginFinishedState: + if(responseLine == "235") { + emit testLoginFinished(true); + } else { + emit testLoginFinished(false); + } + m_socket->close(); + m_testLogin = false; + break; case MailState: - if(responseLine == "235"){ + if(responseLine == "235") { send("MAIL FROM:<" + m_sender + ">"); m_state = RcptState; } break; case RcptState: - if(responseLine == "250"){ + if(responseLine == "250") { send("RCPT TO:<" + m_rcpt + ">"); m_state = DataState; } break; case DataState: - if(responseLine == "250"){ + if(responseLine == "250") { send("DATA"); m_state = BodyState; } break; case BodyState: - if(responseLine == "354"){ + if(responseLine == "354") { send(m_message + "\r\n.\r\n"); m_state = QuitState; } break; case QuitState: - if(responseLine == "250"){ + if(responseLine == "250") { emit sendMailFinished(true, m_actionId); send("QUIT"); m_state = CloseState; } break; case CloseState: - if(responseLine == "221"){ + if(responseLine == "221") { m_socket->close(); } // some mail server does not recognize the QUIT command...so close the connection either way @@ -213,7 +216,7 @@ void SmtpClient::readData() break; default: // unexpecterd response code received... - if(m_testLogin){ + if(m_testLogin) { emit testLoginFinished(false); m_testLogin = false; m_socket->close();