added yahoo mail notification
This commit is contained in:
parent
be6d676fdf
commit
f35f136a4c
@ -64,6 +64,8 @@ public:
|
||||
DeviceErrorSetupMethodNotSupported,
|
||||
DeviceErrorHardwareNotAvailable,
|
||||
DeviceErrorHardwareFailure,
|
||||
// TODO: Bump API version
|
||||
//DeviceErrorAuthentificationFailure,
|
||||
DeviceErrorAsync,
|
||||
DeviceErrorDeviceInUse,
|
||||
DeviceErrorPairingTransactionIdNotFound,
|
||||
|
||||
@ -230,6 +230,7 @@
|
||||
#include <QDateTime>
|
||||
|
||||
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<SmtpClient*>(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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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();
|
||||
|
||||
Reference in New Issue
Block a user