From 40b91f4dbdb2321340b259ec44f9ef7f74f8efc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 1 May 2014 21:02:16 +0200 Subject: [PATCH] created cusom mail notification changed googlemail plugin to mail notification plugin --- plugins/deviceplugins/deviceplugins.pro | 20 +- .../deviceplugins/googlemail/googlemail.pro | 15 - .../deviceplugins/googlemail/smtpclient.cpp | 244 ----------------- .../devicepluginmailnotification.cpp} | 154 +++++++---- .../devicepluginmailnotification.h} | 14 +- .../devicepluginmailnotification.json} | 0 .../mailnotification/mailnotification.pro | 15 + .../mailnotification/smtpclient.cpp | 258 ++++++++++++++++++ .../smtpclient.h | 47 +++- server/main.cpp | 2 +- server/server.pro | 2 +- tests/scripts/addgooglemail.sh | 4 +- tests/scripts/sendmail.sh | 2 +- 13 files changed, 439 insertions(+), 338 deletions(-) delete mode 100644 plugins/deviceplugins/googlemail/googlemail.pro delete mode 100644 plugins/deviceplugins/googlemail/smtpclient.cpp rename plugins/deviceplugins/{googlemail/deviceplugingooglemail.cpp => mailnotification/devicepluginmailnotification.cpp} (54%) rename plugins/deviceplugins/{googlemail/deviceplugingooglemail.h => mailnotification/devicepluginmailnotification.h} (87%) rename plugins/deviceplugins/{googlemail/deviceplugingooglemail.json => mailnotification/devicepluginmailnotification.json} (100%) create mode 100644 plugins/deviceplugins/mailnotification/mailnotification.pro create mode 100644 plugins/deviceplugins/mailnotification/smtpclient.cpp rename plugins/deviceplugins/{googlemail => mailnotification}/smtpclient.h (70%) diff --git a/plugins/deviceplugins/deviceplugins.pro b/plugins/deviceplugins/deviceplugins.pro index fe2650e2..bc49ad4f 100644 --- a/plugins/deviceplugins/deviceplugins.pro +++ b/plugins/deviceplugins/deviceplugins.pro @@ -1,14 +1,14 @@ TEMPLATE = subdirs -SUBDIRS += elro \ - intertechno \ -# meisteranker \ - wifidetector \ - conrad \ - mock \ - openweathermap \ - lircd \ - googlemail \ - wakeonlan \ +SUBDIRS += elro \ + intertechno \ +# meisteranker \ + wifidetector \ + conrad \ + mock \ + openweathermap \ + lircd \ + wakeonlan \ + mailnotification \ boblight { SUBDIRS += boblight diff --git a/plugins/deviceplugins/googlemail/googlemail.pro b/plugins/deviceplugins/googlemail/googlemail.pro deleted file mode 100644 index 9f69bb11..00000000 --- a/plugins/deviceplugins/googlemail/googlemail.pro +++ /dev/null @@ -1,15 +0,0 @@ -include(../../plugins.pri) - -TARGET = $$qtLibraryTarget(guh_deviceplugingooglemail) - -QT+= network - -SOURCES += \ - deviceplugingooglemail.cpp \ - smtpclient.cpp - -HEADERS += \ - deviceplugingooglemail.h \ - smtpclient.h - - diff --git a/plugins/deviceplugins/googlemail/smtpclient.cpp b/plugins/deviceplugins/googlemail/smtpclient.cpp deleted file mode 100644 index 45b0cf1e..00000000 --- a/plugins/deviceplugins/googlemail/smtpclient.cpp +++ /dev/null @@ -1,244 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * This file is part of guh. * - * * - * Guh is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 2 of the License. * - * * - * Guh is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "smtpclient.h" - -SmtpClient::SmtpClient(const QString &host, int port, SmtpClient::ConnectionType connectionType): - m_host(host), m_port(port), m_connectionType(connectionType) -{ - m_socket = new QSslSocket(this); - - m_authMethod = AuthLogin; - m_connectionTimeout = 30000; - m_responseTimeout = 30000; - m_sendMessageTimeout = 60000; - - connect(m_socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socketError(QAbstractSocket::SocketError))); - connect(m_socket,SIGNAL(connected()),this,SLOT(connected())); - connect(m_socket,SIGNAL(disconnected()),this,SLOT(disconnected())); - -} - -bool SmtpClient::connectToHost() -{ - switch (m_connectionType) { - case TlsConnection: - m_socket->connectToHostEncrypted(m_host, m_port); - break; - case TcpConnection: - m_socket->connectToHost(m_host,m_port); - break; - case SslConnection: - m_socket->connectToHostEncrypted(m_host, m_port); - break; - default: - break; - } - - if (!m_socket->waitForConnected(m_connectionTimeout)){ - qWarning() << "Mail TCP connection timeout (5s)."; - return false; - } - - if(waitForResponse() != 220){ - qWarning() << "ERROR: could not connect."; - m_socket->close(); - return false; - } - - send("EHLO localhost"); - - if(waitForResponse() != 250){ - qWarning() << "ERROR: could not connect."; - m_socket->close(); - return false; - } - - if(m_connectionType == TlsConnection){ - //qDebug() << "start client ecryption"; - m_socket->startClientEncryption(); - - if(!m_socket->waitForEncrypted(m_connectionTimeout)){ - qDebug() << "encryption falied..."; - m_socket->close(); - return false; - } - - send("EHLO localhost"); - if(waitForResponse() != 250){ - qWarning() << "ERROR: could not connect."; - m_socket->close(); - return false; - } - } - return true; -} - -void SmtpClient::connected() -{ - qDebug() << "connected to" << m_host; - qDebug() << "-----------------------"; -} - -void SmtpClient::disconnected() -{ - qDebug() << "disconnected from" << m_host; - qDebug() << "-----------------------"; -} - -bool SmtpClient::login(const QString &user, const QString &password, SmtpClient::AuthMethod method) -{ - - if(!connectToHost()){ - m_socket->disconnect(); - return false; - } - - qDebug() << "Try to login with: " << user << password; - - if(method == AuthPlain){ - send("AUTH PLAIN " + QByteArray().append((char) 0).append(user).append((char) 0).append(password).toBase64()); - if(waitForResponse() != 235){ - qWarning() << "ERROR: could not login AUTH PLAIN"; - m_socket->close(); - return false; - } - }else if(method == AuthLogin){ - send("AUTH LOGIN"); - if(waitForResponse() != 334){ - qWarning() << "ERROR: could not login AUTH LOGIN"; - m_socket->close(); - return false; - } - - send(QByteArray().append(user).toBase64()); - if(waitForResponse() != 334){ - qWarning() << "ERROR: could not login AUTH LOGIN...wrong username or password"; - m_socket->close(); - return false; - } - - send(QByteArray().append(password).toBase64()); - if(waitForResponse() != 235){ - qWarning() << "ERROR: could not login AUTH LOGIN...wrong username or password"; - m_socket->close(); - return false; - } - } - qDebug() << "login....OK"; - qDebug() << "-----------------------"; - return true; -} - -bool SmtpClient::logout() -{ - send("QUIT"); - if(waitForResponse() != 221){ - qWarning() << "ERROR: could not QUIT connection."; - } - - m_socket->close(); - return true; -} - -int SmtpClient::waitForResponse() -{ - while(true){ - if(!m_socket->waitForReadyRead(m_responseTimeout)){ - qWarning() << "ERROR: mail TCP response timeout."; - return -1; - } - - qDebug() << "-----------------------"; - QString responseText; - QString responseLine; - while (m_socket->canReadLine() && responseLine[3] != ' ') { - // Save the server's response - responseLine = m_socket->readLine(); - responseText.append(responseLine); - } - int responseCode = responseText.left(3).toInt(); - qDebug() << responseText; - qDebug() << "-----------------------"; - return responseCode; - } -} - -bool SmtpClient::sendMail(const QString &from, const QString &to, const QString &subject, const QString &body) -{ - - send("MAIL FROM:<" + from + ">"); - if(waitForResponse() != 250){ - qWarning() << "ERROR: could not send mail."; - return false; - } - - send("RCPT TO:<" + to + ">"); - if(waitForResponse() != 250){ - qWarning() << "ERROR: could not send mail."; - return false; - } - - send("DATA"); - if(waitForResponse() != 354){ - qWarning() << "ERROR: could not send mail."; - return false; - } - - // mail - QString message; - message = "To: " + to + "\n"; - message.append("From: " + from + "\n"); - message.append("Subject: " + subject + "\n"); - message.append(body); - message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) ); - message.replace( QString::fromLatin1( "\r\n.\r\n" ), - QString::fromLatin1( "\r\n..\r\n" ) ); - - qDebug() << "sending...\n"; - qDebug() << "--------------------------------------------"; - send(message + "\r\n."); - if(waitForResponse() != 250){ - qWarning() << "ERROR: could not send mail."; - return false; - } - - logout(); - - qDebug() << "--------------------------------------------"; - qDebug() << " MAIL SENT!!!!"; - qDebug() << "--------------------------------------------"; - return true; -} - -void SmtpClient::socketError(QAbstractSocket::SocketError error) -{ - qWarning() << "mail socket:" << error << m_socket->errorString(); -} - -void SmtpClient::send(const QString &data) -{ - qDebug() << "send:" << data; - m_socket->write(data.toUtf8() + "\r\n"); - m_socket->flush(); - if (m_socket->waitForBytesWritten(m_sendMessageTimeout)){ - qWarning() << "mail TCP send timeout (60s)"; - } -} - - diff --git a/plugins/deviceplugins/googlemail/deviceplugingooglemail.cpp b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp similarity index 54% rename from plugins/deviceplugins/googlemail/deviceplugingooglemail.cpp rename to plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp index 72a9e5c8..fafa32be 100644 --- a/plugins/deviceplugins/googlemail/deviceplugingooglemail.cpp +++ b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp @@ -42,7 +42,7 @@ "deviceParams":{ "user":"my.address@gmail.com", "password":"my_secret_password" - "sendTo":"recipient@example.com"} + "recipient":"recipient@example.com"} } } } @@ -108,7 +108,7 @@ \li This parameter holds the password for the login \li string \row - \li sendTo + \li recipient \li This parameter holds the mail address of the recipient of the notification \li string \endtable @@ -130,7 +130,7 @@ */ -#include "deviceplugingooglemail.h" +#include "devicepluginmailnotification.h" #include "plugin/device.h" #include "devicemanager.h" @@ -141,20 +141,24 @@ #include extern VendorId guhVendorId; -DeviceClassId mailDeviceClassId = DeviceClassId("38ed6ffc-f43b-48f8-aea2-8d63cdcad87e"); -ActionTypeId sendMailActionTypeId = ActionTypeId("fa54f834-34d0-4aaf-b0ab-a165191d39d3"); -DevicePluginGoogleMail::DevicePluginGoogleMail() +DeviceClassId googleMailDeviceClassId = DeviceClassId("3869884a-1592-4b8f-84a7-994be18ff555"); +DeviceClassId customMailDeviceClassId = DeviceClassId("f4844c97-7ca6-4349-904e-ff9749a9fe74"); + +ActionTypeId sendMailActionTypeId = ActionTypeId("054613b0-3666-4dad-9252-e0ebca187edc"); + + +DevicePluginMailNotification::DevicePluginMailNotification() { m_smtpClient = new SmtpClient(); } -DevicePluginGoogleMail::~DevicePluginGoogleMail() +DevicePluginMailNotification::~DevicePluginMailNotification() { m_smtpClient->deleteLater(); } -QList DevicePluginGoogleMail::supportedVendors() const +QList DevicePluginMailNotification::supportedVendors() const { QList ret; Vendor mail(guhVendorId, "guh"); @@ -162,28 +166,12 @@ QList DevicePluginGoogleMail::supportedVendors() const return ret; } -QList DevicePluginGoogleMail::supportedDevices() const +QList DevicePluginMailNotification::supportedDevices() const { QList ret; - DeviceClass deviceClassGoogleMail(pluginId(), guhVendorId, mailDeviceClassId); - deviceClassGoogleMail.setName("Google Mail Notification"); - deviceClassGoogleMail.setCreateMethod(DeviceClass::CreateMethodUser); - - // Params - QList params; - - ParamType userParam("user", QVariant::String); - params.append(userParam); - - ParamType passwordParam("password", QVariant::String); - params.append(passwordParam); - - ParamType sendToParam("sendTo", QVariant::String); - params.append(sendToParam); - - // Actions - QList googleMailActions; + // General Action + QList mailActions; QList actionParamsMail; ParamType actionParamSubject("subject", QVariant::String); @@ -195,51 +183,125 @@ QList DevicePluginGoogleMail::supportedDevices() const ActionType sendMailAction(sendMailActionTypeId); sendMailAction.setName("send mail"); sendMailAction.setParameters(actionParamsMail); - googleMailActions.append(sendMailAction); + mailActions.append(sendMailAction); - deviceClassGoogleMail.setActions(googleMailActions); - deviceClassGoogleMail.setParams(params); + + + // Google Mail + // --------------------------------------------------------------- + DeviceClass deviceClassGoogleMail(pluginId(), guhVendorId, googleMailDeviceClassId); + deviceClassGoogleMail.setName("Google Mail Notification"); + deviceClassGoogleMail.setCreateMethod(DeviceClass::CreateMethodUser); + + // Params + QList googleMailParams; + + ParamType userGoogleParam("user", QVariant::String); + googleMailParams.append(userGoogleParam); + + ParamType passwordGoogleParam("password", QVariant::String); + googleMailParams.append(passwordGoogleParam); + + ParamType recipientGoogleParam("recipient", QVariant::String); + googleMailParams.append(recipientGoogleParam); + + deviceClassGoogleMail.setActions(mailActions); + deviceClassGoogleMail.setParams(googleMailParams); + + // Custom Mail + // --------------------------------------------------------------- + DeviceClass deviceClassCustomMail(pluginId(), guhVendorId, customMailDeviceClassId); + deviceClassCustomMail.setName("Custom Mail Notification"); + deviceClassCustomMail.setCreateMethod(DeviceClass::CreateMethodUser); + + // Params + QList customMailParams; + + ParamType userCustomParam("user", QVariant::String); + customMailParams.append(userCustomParam); + + ParamType passwordCustomParam("password", QVariant::String); + customMailParams.append(passwordCustomParam); + + ParamType recipientCustomParam("recipient", QVariant::String); + customMailParams.append(recipientCustomParam); + + ParamType hostCustomParam("host", QVariant::String); + customMailParams.append(hostCustomParam); + + ParamType portCustomParam("port", QVariant::Int); + customMailParams.append(portCustomParam); + + ParamType authCustomParam("auth", QVariant::String); + customMailParams.append(authCustomParam); + + deviceClassCustomMail.setActions(mailActions); + deviceClassCustomMail.setParams(customMailParams); ret.append(deviceClassGoogleMail); + ret.append(deviceClassCustomMail); return ret; } -bool DevicePluginGoogleMail::deviceCreated(Device *device) +bool DevicePluginMailNotification::deviceCreated(Device *device) { - if(!m_smtpClient->login(device->paramValue("user").toString(), device->paramValue("password").toString(), SmtpClient::AuthLogin)){ - return false; - } - m_smtpClient->logout(); + // Google mail +// if(device->deviceClassId() == googleMailDeviceClassId){ +// m_smtpClient->setConnectionType(SmtpClient::SslConnection); +// m_smtpClient->setAuthMethod(SmtpClient::AuthLogin); +// m_smtpClient->setPort(465); +// m_smtpClient->setHost("smtp.gmail.com"); +// m_smtpClient->login(device->paramValue("user").toString(), device->paramValue("password").toString()); +// } return true; } -DeviceManager::HardwareResources DevicePluginGoogleMail::requiredHardware() const +DeviceManager::HardwareResources DevicePluginMailNotification::requiredHardware() const { return DeviceManager::HardwareResourceNone; } -QPair DevicePluginGoogleMail::executeAction(Device *device, const Action &action) +QPair DevicePluginMailNotification::executeAction(Device *device, const Action &action) { qDebug() << "execute action " << sendMailActionTypeId.toString(); if(action.actionTypeId() == sendMailActionTypeId){ - if(!m_smtpClient->login(device->paramValue("user").toString(), device->paramValue("password").toString())){ - qDebug() << "ERROR: could nt login for sending mail"; - return report(DeviceManager::DeviceErrorDeviceParameterError, "user, password"); + // Google mail + if(device->deviceClassId() == googleMailDeviceClassId){ + m_smtpClient->setConnectionType(SmtpClient::SslConnection); + m_smtpClient->setAuthMethod(SmtpClient::AuthLogin); + m_smtpClient->setPort(465); + m_smtpClient->setHost("smtp.gmail.com"); + m_smtpClient->setUser(device->paramValue("user").toString()); + m_smtpClient->setPassword(device->paramValue("password").toString()); + m_smtpClient->setRecipiant(device->paramValue("recipiant").toString()); } - m_smtpClient->sendMail(device->paramValue("user").toString(), device->paramValue("sendTo").toString(), action.param("subject").value().toString(), action.param("body").value().toString()); - m_smtpClient->logout(); + + if(device->deviceClassId() == customMailDeviceClassId){ + m_smtpClient->setConnectionType(SmtpClient::SslConnection); + if(device->paramValue("auth").toString() == "PLAIN"){ + m_smtpClient->setAuthMethod(SmtpClient::AuthPlain); + }else if(device->paramValue("auth").toString() == "LOGIN"){ + m_smtpClient->setAuthMethod(SmtpClient::AuthLogin); + } + m_smtpClient->setPort(device->paramValue("port").toInt()); + m_smtpClient->setHost(device->paramValue("host").toString()); + m_smtpClient->setUser(device->paramValue("user").toString()); + m_smtpClient->setPassword(device->paramValue("password").toString()); + } + + m_smtpClient->sendMail(device->paramValue("user").toString(), device->paramValue("recipient").toString(), action.param("subject").value().toString(), action.param("body").value().toString()); } return report(); } -QString DevicePluginGoogleMail::pluginName() const +QString DevicePluginMailNotification::pluginName() const { - return "Google Mail"; + return "Mail notification"; } -PluginId DevicePluginGoogleMail::pluginId() const +PluginId DevicePluginMailNotification::pluginId() const { - return PluginId("edfbbf03-e243-4b33-bc46-b31e973febdd"); + return PluginId("72aef158-07a3-4714-93b5-fec2f9d912d1"); } diff --git a/plugins/deviceplugins/googlemail/deviceplugingooglemail.h b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.h similarity index 87% rename from plugins/deviceplugins/googlemail/deviceplugingooglemail.h rename to plugins/deviceplugins/mailnotification/devicepluginmailnotification.h index 3730ec19..dd810e79 100644 --- a/plugins/deviceplugins/googlemail/deviceplugingooglemail.h +++ b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.h @@ -16,22 +16,22 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#ifndef DEVICEPLUGINGOOGLEMAIL_H -#define DEVICEPLUGINGOOGLEMAIL_H +#ifndef DEVICEPLUGINMAILNOTIFICATION_H +#define DEVICEPLUGINMAILNOTIFICATION_H #include "plugin/deviceplugin.h" #include "smtpclient.h" -class DevicePluginGoogleMail : public DevicePlugin +class DevicePluginMailNotification : public DevicePlugin { Q_OBJECT - Q_PLUGIN_METADATA(IID "guru.guh.DevicePlugin" FILE "deviceplugingooglemail.json") + Q_PLUGIN_METADATA(IID "guru.guh.DevicePlugin" FILE "devicepluginmailnotification.json") Q_INTERFACES(DevicePlugin) public: - explicit DevicePluginGoogleMail(); - ~DevicePluginGoogleMail(); + explicit DevicePluginMailNotification(); + ~DevicePluginMailNotification(); QList supportedVendors() const override; QList supportedDevices() const override; @@ -53,4 +53,4 @@ public slots: }; -#endif // DEVICEPLUGINMAIL_H +#endif // DEVICEPLUGINMAILNOTIFICATION_H diff --git a/plugins/deviceplugins/googlemail/deviceplugingooglemail.json b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.json similarity index 100% rename from plugins/deviceplugins/googlemail/deviceplugingooglemail.json rename to plugins/deviceplugins/mailnotification/devicepluginmailnotification.json diff --git a/plugins/deviceplugins/mailnotification/mailnotification.pro b/plugins/deviceplugins/mailnotification/mailnotification.pro new file mode 100644 index 00000000..7a018277 --- /dev/null +++ b/plugins/deviceplugins/mailnotification/mailnotification.pro @@ -0,0 +1,15 @@ +include(../../plugins.pri) + +TARGET = $$qtLibraryTarget(guh_devicepluginmailnotification) + +QT+= network + +SOURCES += \ + devicepluginmailnotification.cpp \ + smtpclient.cpp + +HEADERS += \ + devicepluginmailnotification.h \ + smtpclient.h + + diff --git a/plugins/deviceplugins/mailnotification/smtpclient.cpp b/plugins/deviceplugins/mailnotification/smtpclient.cpp new file mode 100644 index 00000000..765ef465 --- /dev/null +++ b/plugins/deviceplugins/mailnotification/smtpclient.cpp @@ -0,0 +1,258 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "smtpclient.h" + +SmtpClient::SmtpClient() +{ + + m_socket = new QSslSocket(this); + m_host = "smtp.gmail.com"; + m_port = 465; + m_connectionType = SslConnection; + m_authMethod = AuthLogin; + m_state = InitState; + + connect(m_socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socketError(QAbstractSocket::SocketError))); + connect(m_socket,SIGNAL(connected()),this,SLOT(connected())); + connect(m_socket,SIGNAL(readyRead()),this,SLOT(readData())); + connect(m_socket,SIGNAL(disconnected()),this,SLOT(disconnected())); + +} + +void SmtpClient::connectToHost() +{ + switch (m_connectionType) { + case TlsConnection: + m_socket->connectToHostEncrypted(m_host, m_port); + break; + case SslConnection: + m_socket->connectToHostEncrypted(m_host, m_port); + break; + case TcpConnection: + m_socket->connectToHost(m_host,m_port); + break; + default: + break; + } +} + +void SmtpClient::connected() +{ + qDebug() << "connected to" << m_host; + qDebug() << "-----------------------"; + +} + +void SmtpClient::readData() +{ + QString response; + QString responseLine; + + while(m_socket->canReadLine() && responseLine[3] != ' '){ + responseLine = m_socket->readLine(); + response.append(responseLine); + } + responseLine.truncate( 3 ); + + qDebug() << "---------------------------------------------"; + qDebug() << "Server code:" << responseLine; + qDebug() << "---------------------------------------------"; + qDebug() << "Server data: " << response; + qDebug() << "---------------------------------------------"; + + switch (m_state) { + case InitState: + if(responseLine == "220"){ + qDebug() << "Init"; + send("EHLO localhost"); + if(m_connectionType == TlsConnection || m_connectionType == SslConnection){ + m_state = HandShakeState; + }else{ + m_state = AuthentificationState; + } + } + break; + case HandShakeState: + if(responseLine == "250"){ + qDebug() << "Handshake"; + m_socket->startClientEncryption(); + if(!m_socket->waitForEncrypted(1000)){ + qDebug() << m_socket->errorString(); + } + send("EHLO localhost"); + m_state = AuthentificationState; + } + break; + case AuthentificationState: + if(responseLine == "250"){ + qDebug() << "Autentificate"; + if(m_authMethod == AuthLogin){ + send("AUTH LOGIN"); + m_state = UserState; + break; + } + if(m_authMethod == AuthPlain){ + send("AUTH PLAIN " + QByteArray().append((char) 0).append(m_user).append((char) 0).append(m_password).toBase64()); + m_state = MailState; + break; + } + } + break; + case UserState: + if(responseLine == "334"){ + send(QByteArray().append(m_user).toBase64()); + m_state = PasswordState; + } + break; + case PasswordState: + if(responseLine == "334"){ + send(QByteArray().append(m_password).toBase64()); + m_state = MailState; + } + break; + case MailState: + if(responseLine == "235"){ + send("MAIL FROM:<" + m_from + ">"); + m_state = RcptState; + } + break; + case RcptState: + if(responseLine == "250"){ + send("RCPT TO:<" + m_rcpt + ">"); + m_state = DataState; + } + break; + case DataState: + if(responseLine == "250"){ + send("DATA"); + m_state = BodyState; + } + break; + case BodyState: + if(responseLine == "354"){ + send(m_message + "\r\n.\r\n"); + m_state = QuitState; + } + break; + case QuitState: + if(responseLine == "250"){ + qDebug() << "--------------------------------------------"; + qDebug() << " MAIL SENT!!!!"; + qDebug() << "--------------------------------------------"; + send("QUIT"); + m_state = CloseState; + } + break; + case CloseState: + m_socket->close(); + break; + default: + qDebug() << "ERROR: unexpected response from server: " << response; + break; + } +} + +void SmtpClient::disconnected() +{ + qDebug() << "disconnected from" << m_host; + qDebug() << "-----------------------"; +} + +void SmtpClient::login(const QString &user, const QString &password) +{ + if(!m_socket->isOpen()){ + connectToHost(); + } + qDebug() << "Try to login with: " << user << password; +} + +void SmtpClient::logout() +{ + send("QUIT"); +} + +bool SmtpClient::sendMail(const QString &from, const QString &to, const QString &subject, const QString &body) +{ + // mail + m_rcpt = to; + m_state = InitState; + m_from = from; + m_message.clear(); + + m_message = "To: " + to + "\n"; + m_message.append("From: " + from + "\n"); + m_message.append("Subject: " + subject + "\n"); + m_message.append(body); + m_message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) ); + m_message.replace( QString::fromLatin1( "\r\n.\r\n" ), QString::fromLatin1( "\r\n..\r\n" ) ); + + m_socket->close(); + connectToHost(); + + return true; +} + +void SmtpClient::setHost(const QString &host) +{ + m_host = host; +} + +void SmtpClient::setPort(const int &port) +{ + m_port = port; +} + +void SmtpClient::setConnectionType(const SmtpClient::ConnectionType &connectionType) +{ + m_connectionType = connectionType; +} + +void SmtpClient::setAuthMethod(const SmtpClient::AuthMethod &authMethod) +{ + m_authMethod = authMethod; +} + +void SmtpClient::setUser(const QString &user) +{ + m_user = user; +} + +void SmtpClient::setPassword(const QString &password) +{ + m_password = password; +} + + void SmtpClient::setRecipiant(const QString &rcpt) +{ + m_rcpt = rcpt; +} + +void SmtpClient::socketError(QAbstractSocket::SocketError error) +{ + qWarning() << "mail socket:" << error << m_socket->errorString(); +} + +void SmtpClient::send(const QString &data) +{ + qDebug() << "sending to host:" << data; + m_socket->write(data.toUtf8() + "\r\n"); + m_socket->flush(); +} + + diff --git a/plugins/deviceplugins/googlemail/smtpclient.h b/plugins/deviceplugins/mailnotification/smtpclient.h similarity index 70% rename from plugins/deviceplugins/googlemail/smtpclient.h rename to plugins/deviceplugins/mailnotification/smtpclient.h index af93b6a5..9b1d7909 100644 --- a/plugins/deviceplugins/googlemail/smtpclient.h +++ b/plugins/deviceplugins/mailnotification/smtpclient.h @@ -34,37 +34,62 @@ public: AuthLogin }; + enum SendState{ + InitState, + HandShakeState, + AuthentificationState, + UserState, + PasswordState, + MailState, + RcptState, + DataState, + BodyState, + QuitState, + CloseState + }; + enum ConnectionType{ TcpConnection, // no encryption SslConnection, // SSL TlsConnection // STARTTLS }; - explicit SmtpClient(const QString &host = "smtp.gmail.com", int port = 465, ConnectionType connectionType = SslConnection); - bool connectToHost(); - bool login(const QString &user, const QString &password, AuthMethod method = AuthLogin); - bool logout(); + explicit SmtpClient(); + void connectToHost(); + void login(const QString &user, const QString &password); + void logout(); bool sendMail(const QString &from, const QString &to, const QString &subject, const QString &body); + void setHost(const QString &host); + void setPort(const int &port); + void setConnectionType(const ConnectionType &connectionType); + void setAuthMethod(const AuthMethod &authMethod); + void setUser(const QString &user); + void setPassword(const QString &password); + void setRecipiant(const QString &rcpt); + + private: + SendState m_state; QSslSocket *m_socket; QString m_host; int m_port; - ConnectionType m_connectionType; AuthMethod m_authMethod; - - int m_responseCode; - int m_connectionTimeout; - int m_responseTimeout; - int m_sendMessageTimeout; - int waitForResponse(); + QString m_user; + QString m_password; + QString m_from; + QString m_rcpt; + QString m_subject; + QString m_boy; + QString m_message; signals: private slots: void socketError(QAbstractSocket::SocketError error); void connected(); + void readData(); void disconnected(); void send(const QString &data); diff --git a/server/main.cpp b/server/main.cpp index 81df9a25..bca4affd 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -29,8 +29,8 @@ Q_IMPORT_PLUGIN(DevicePluginConrad) Q_IMPORT_PLUGIN(DevicePluginMock) Q_IMPORT_PLUGIN(DevicePluginOpenweathermap) Q_IMPORT_PLUGIN(DevicePluginLircd) -Q_IMPORT_PLUGIN(DevicePluginGoogleMail) Q_IMPORT_PLUGIN(DevicePluginWakeOnLan) +Q_IMPORT_PLUGIN(DevicePluginMailNotification) #if USE_BOBLIGHT Q_IMPORT_PLUGIN(DevicePluginBoblight) diff --git a/server/server.pro b/server/server.pro index bcdf0e7b..07ca67c7 100644 --- a/server/server.pro +++ b/server/server.pro @@ -23,7 +23,7 @@ LIBS += -L../plugins/deviceplugins/conrad -lguh_devicepluginconrad LIBS += -L../plugins/deviceplugins/mock -lguh_devicepluginmock LIBS += -L../plugins/deviceplugins/openweathermap -lguh_devicepluginopenweathermap LIBS += -L../plugins/deviceplugins/lircd -lguh_devicepluginlircd -LIBS += -L../plugins/deviceplugins/googlemail -lguh_deviceplugingooglemail +LIBS += -L../plugins/deviceplugins/mailnotification -lguh_devicepluginmailnotification LIBS += -L../plugins/deviceplugins/wakeonlan -lguh_devicepluginwakeonlan boblight { diff --git a/tests/scripts/addgooglemail.sh b/tests/scripts/addgooglemail.sh index ad9f8bd6..e08c6870 100755 --- a/tests/scripts/addgooglemail.sh +++ b/tests/scripts/addgooglemail.sh @@ -1,7 +1,7 @@ #!/bin/bash -if [ -z $1 ]; then +if [ -z $2 ]; then echo "usage: $0 host user password sendTo" else - (echo '{"id":1, "method":"Devices.AddConfiguredDevice", "params":{"deviceClassId": "{38ed6ffc-f43b-48f8-aea2-8d63cdcad87e}","deviceParams":{"user":"'$2'", "password":"'$3'", "sendTo":"'$4'"}}}'; sleep 1) | nc $1 1234 + (echo '{"id":1, "method":"Devices.AddConfiguredDevice", "params":{"deviceClassId": "{3869884a-1592-4b8f-84a7-994be18ff555}","deviceParams":{"user":"'$2'", "password":"'$3'", "recipient":"'$4'"}}}'; sleep 1) | nc $1 1234 fi diff --git a/tests/scripts/sendmail.sh b/tests/scripts/sendmail.sh index 81c76e67..7ba7406d 100755 --- a/tests/scripts/sendmail.sh +++ b/tests/scripts/sendmail.sh @@ -3,5 +3,5 @@ if [ -z $1 ]; then echo "usage: $0 host deviceId subject body" else - (echo '{"id":1, "method":"Actions.ExecuteAction","params":{"actionTypeId": "{fa54f834-34d0-4aaf-b0ab-a165191d39d3}", "deviceId":"{'$2'}","params":{"subject":"'$3'", "body":"'$4'"}}}'; sleep 1) | nc $1 1234 + (echo '{"id":1, "method":"Actions.ExecuteAction","params":{"actionTypeId": "{054613b0-3666-4dad-9252-e0ebca187edc}", "deviceId":"{'$2'}","params":[{"subject":"'$3'"}, {"body":"'$4'"}]}}'; sleep 1) | nc $1 1234 fi