nymea/plugins/deviceplugins/mailnotification/devicepluginmailnotificatio...

308 lines
10 KiB
C++

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* 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/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*!
\page googlemail.html
\title Google Mail Notification
\ingroup plugins
\ingroup services
The Google Mail plugin allows you to send a mail notification from your Google mail
account by performing an \l{Action}.
ATTENTION: The password currently will be saved as plain text in the guh configuration file.
\section1 Examples
\section2 Adding a Google Mail Notification service
In order to add a Google Mail Notification service you need to configure
the "Google Mail login" (user), the password for your Gmail account and the address
of the recipient.
\code
{
"id":1,
"method":"Devices.AddConfiguredDevice",
"params":{
"deviceClassId": "{38ed6ffc-f43b-48f8-aea2-8d63cdcad87e}",
"deviceParams":{
"user":"my.address@gmail.com",
"password":"my_secret_password"
"recipient":"recipient@example.com"}
}
}
}
\endcode
Before the device will be added, the plugin trys to login. If the username or the password
are wrong, an error message will be send.
\code
{
"id": 1,
"params": {
"deviceId": "{0b99ea27-896a-4a23-a044-3f1441f6a9a7}",
"errorMessage": "",
"success": true
},
"status": "success"
}
\endcode
\section2 Sending a mail notification
In order to send a mail notification from a configured Gmail service use following message
format.
\code
{
"id":1,
"method":"Actions.ExecuteAction",
"params":{
"actionTypeId": "{fa54f834-34d0-4aaf-b0ab-a165191d39d3}",
"deviceId":"{0b99ea27-896a-4a23-a044-3f1441f6a9a7}",
"params":{
"subject":"GUH notification",
"body":"Hello world!"
}
}
}
\endcode
response...
\code
{
"id": 1,
"params": {
"errorMessage": "",
"success": true
},
"status": "success"
}
\endcode
\section1 Plugin propertys:
\section2 Plugin parameters
Each configured plugin has following paramters:
\table
\header
\li Name
\li Description
\li Data Type
\row
\li user
\li This parameter holds the username (mail address) for the login
\li string
\row
\li password
\li This parameter holds the password for the login
\li string
\row
\li recipient
\li This parameter holds the mail address of the recipient of the notification
\li string
\endtable
\section2 Plugin actions:
Following list contains all plugin \l{Action}s:
\table
\header
\li Name
\li Description
\li UUID
\row
\li sendMail
\li This action sends a mail to the recipient address of the configured device
with a given subject and text body.
\li fa54f834-34d0-4aaf-b0ab-a165191d39d3
\endtable
*/
#include "devicepluginmailnotification.h"
#include "plugin/device.h"
#include "devicemanager.h"
#include <QDebug>
#include <QJsonDocument>
#include <QVariantMap>
#include <QDateTime>
extern VendorId guhVendorId;
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();
}
DevicePluginMailNotification::~DevicePluginMailNotification()
{
m_smtpClient->deleteLater();
}
QList<Vendor> DevicePluginMailNotification::supportedVendors() const
{
QList<Vendor> ret;
Vendor mail(guhVendorId, "guh");
ret.append(mail);
return ret;
}
QList<DeviceClass> DevicePluginMailNotification::supportedDevices() const
{
QList<DeviceClass> ret;
// General Action
QList<ActionType> mailActions;
QList<ParamType> actionParamsMail;
ParamType actionParamSubject("subject", QVariant::String);
actionParamsMail.append(actionParamSubject);
ParamType actionParamBody("body", QVariant::String);
actionParamsMail.append(actionParamBody);
ActionType sendMailAction(sendMailActionTypeId);
sendMailAction.setName("send mail");
sendMailAction.setParameters(actionParamsMail);
mailActions.append(sendMailAction);
// 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 DevicePluginMailNotification::deviceCreated(Device *device)
{
// 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 DevicePluginMailNotification::requiredHardware() const
{
return DeviceManager::HardwareResourceNone;
}
QPair<DeviceManager::DeviceError, QString> DevicePluginMailNotification::executeAction(Device *device, const Action &action)
{
qDebug() << "execute action " << sendMailActionTypeId.toString();
if(action.actionTypeId() == sendMailActionTypeId){
// 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());
}
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 DevicePluginMailNotification::pluginName() const
{
return "Mail notification";
}
PluginId DevicePluginMailNotification::pluginId() const
{
return PluginId("72aef158-07a3-4714-93b5-fec2f9d912d1");
}