created cusom mail notification
changed googlemail plugin to mail notification plugin
This commit is contained in:
parent
737f6f564e
commit
40b91f4dbd
@ -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
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
include(../../plugins.pri)
|
||||
|
||||
TARGET = $$qtLibraryTarget(guh_deviceplugingooglemail)
|
||||
|
||||
QT+= network
|
||||
|
||||
SOURCES += \
|
||||
deviceplugingooglemail.cpp \
|
||||
smtpclient.cpp
|
||||
|
||||
HEADERS += \
|
||||
deviceplugingooglemail.h \
|
||||
smtpclient.h
|
||||
|
||||
|
||||
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#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)";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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 <QDateTime>
|
||||
|
||||
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<Vendor> DevicePluginGoogleMail::supportedVendors() const
|
||||
QList<Vendor> DevicePluginMailNotification::supportedVendors() const
|
||||
{
|
||||
QList<Vendor> ret;
|
||||
Vendor mail(guhVendorId, "guh");
|
||||
@ -162,28 +166,12 @@ QList<Vendor> DevicePluginGoogleMail::supportedVendors() const
|
||||
return ret;
|
||||
}
|
||||
|
||||
QList<DeviceClass> DevicePluginGoogleMail::supportedDevices() const
|
||||
QList<DeviceClass> DevicePluginMailNotification::supportedDevices() const
|
||||
{
|
||||
QList<DeviceClass> ret;
|
||||
|
||||
DeviceClass deviceClassGoogleMail(pluginId(), guhVendorId, mailDeviceClassId);
|
||||
deviceClassGoogleMail.setName("Google Mail Notification");
|
||||
deviceClassGoogleMail.setCreateMethod(DeviceClass::CreateMethodUser);
|
||||
|
||||
// Params
|
||||
QList<ParamType> 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<ActionType> googleMailActions;
|
||||
// General Action
|
||||
QList<ActionType> mailActions;
|
||||
|
||||
QList<ParamType> actionParamsMail;
|
||||
ParamType actionParamSubject("subject", QVariant::String);
|
||||
@ -195,51 +183,125 @@ QList<DeviceClass> 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<ParamType> 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<ParamType> 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<DeviceManager::DeviceError, QString> DevicePluginGoogleMail::executeAction(Device *device, const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> 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");
|
||||
}
|
||||
|
||||
@ -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<Vendor> supportedVendors() const override;
|
||||
QList<DeviceClass> supportedDevices() const override;
|
||||
@ -53,4 +53,4 @@ public slots:
|
||||
|
||||
};
|
||||
|
||||
#endif // DEVICEPLUGINMAIL_H
|
||||
#endif // DEVICEPLUGINMAILNOTIFICATION_H
|
||||
15
plugins/deviceplugins/mailnotification/mailnotification.pro
Normal file
15
plugins/deviceplugins/mailnotification/mailnotification.pro
Normal file
@ -0,0 +1,15 @@
|
||||
include(../../plugins.pri)
|
||||
|
||||
TARGET = $$qtLibraryTarget(guh_devicepluginmailnotification)
|
||||
|
||||
QT+= network
|
||||
|
||||
SOURCES += \
|
||||
devicepluginmailnotification.cpp \
|
||||
smtpclient.cpp
|
||||
|
||||
HEADERS += \
|
||||
devicepluginmailnotification.h \
|
||||
smtpclient.h
|
||||
|
||||
|
||||
258
plugins/deviceplugins/mailnotification/smtpclient.cpp
Normal file
258
plugins/deviceplugins/mailnotification/smtpclient.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user