fixed json file and added encryption

This commit is contained in:
Simon Stürz 2014-10-21 20:48:17 +02:00 committed by Michael Zanetti
parent 943983bf0f
commit 3c9bb1c89c
6 changed files with 116 additions and 94 deletions

View File

@ -2,7 +2,7 @@
GUH_VERSION_STRING=$$system('dpkg-parsechangelog | sed -n -e "s/^Version: //p"')
DEFINES += GUH_VERSION_STRING=\\\"$${GUH_VERSION_STRING}\\\"
QMAKE_CXXFLAGS += -Werror
#QMAKE_CXXFLAGS += -Werror
CONFIG += c++11
# Enable coverage option

View File

@ -247,25 +247,22 @@ DeviceManager::DeviceSetupStatus DevicePluginMailNotification::setupDevice(Devic
// Google mail
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 *smtpClient = new SmtpClient("smtp.gmail.com",
465,
device->paramValue("user").toString(),
device->paramValue("password").toString(),
SmtpClient::AuthLogin,
SmtpClient::EncryptionSSL,
this);
smtpClient->setSender(device->paramValue("user").toString());
smtpClient->setRecipient(device->paramValue("recipient").toString());
smtpClient->setRecipiant(device->paramValue("recipient").toString());
connect(smtpClient, &SmtpClient::testLoginFinished, this, &DevicePluginMailNotification::testLoginFinished);
connect(smtpClient, &SmtpClient::sendMailFinished, this, &DevicePluginMailNotification::sendMailFinished);
// TODO: test connection;
m_clients.insert(smtpClient,device);
smtpClient->testLogin();
return DeviceManager::DeviceSetupStatusAsync;
return DeviceManager::DeviceSetupStatusSuccess;
//return DeviceManager::DeviceSetupStatusAsync;
}
// Custom mail
if(device->deviceClassId() == customMailDeviceClassId){
@ -274,38 +271,33 @@ DeviceManager::DeviceSetupStatus DevicePluginMailNotification::setupDevice(Devic
smtpClient->setHost(device->paramValue("SMTP server").toString());
smtpClient->setPort(device->paramValue("port").toInt());
smtpClient->setUser(device->paramValue("user").toString());
// TODO: use cryptography to save password not as plain text
smtpClient->setPassword(device->paramValue("password").toString());
if(device->paramValue("authentification").toString() == "PLAIN"){
smtpClient->setAuthMethod(SmtpClient::AuthMethodPlain);
}else if(device->paramValue("authentification").toString() == "LOGIN"){
smtpClient->setAuthMethod(SmtpClient::AuthMethodLogin);
}else{
return DeviceManager::DeviceSetupStatusFailure;
smtpClient->setAuthMethod(SmtpClient::AuthPlain);
}
if(device->paramValue("authentification").toString() == "LOGIN"){
smtpClient->setAuthMethod(SmtpClient::AuthLogin);
}
if(device->paramValue("encryption").toString() == "NONE"){
smtpClient->setEncryptionType(SmtpClient::EncryptionTypeNone);
} else if(device->paramValue("encryption").toString() == "SSL"){
smtpClient->setEncryptionType(SmtpClient::EncryptionTypeSSL);
}else if(device->paramValue("encryption").toString() == "TLS"){
smtpClient->setEncryptionType(SmtpClient::EncryptionTypeTLS);
}else{
return DeviceManager::DeviceSetupStatusFailure;
smtpClient->setEncryptionType(SmtpClient::EncryptionNone);
}
smtpClient->setRecipient(device->paramValue("recipient").toString());
if(device->paramValue("encryption").toString() == "SSL"){
smtpClient->setEncryptionType(SmtpClient::EncryptionSSL);
}
if(device->paramValue("encryption").toString() == "TLS"){
smtpClient->setEncryptionType(SmtpClient::EncryptionTLS);
}
smtpClient->setRecipiant(device->paramValue("recipient").toString());
smtpClient->setSender(device->paramValue("sender mail").toString());
connect(smtpClient, &SmtpClient::testLoginFinished, this, &DevicePluginMailNotification::testLoginFinished);
connect(smtpClient, &SmtpClient::sendMailFinished, this, &DevicePluginMailNotification::sendMailFinished);
// TODO: test connection;
m_clients.insert(smtpClient,device);
smtpClient->testLogin();
return DeviceManager::DeviceSetupStatusAsync;
return DeviceManager::DeviceSetupStatusSuccess;
//return DeviceManager::DeviceSetupStatusAsync;
}
return DeviceManager::DeviceSetupStatusFailure;
}
@ -326,26 +318,6 @@ DeviceManager::DeviceError DevicePluginMailNotification::executeAction(Device *d
return DeviceManager::DeviceErrorActionTypeNotFound;
}
void DevicePluginMailNotification::deviceRemoved(Device *device)
{
SmtpClient *smtpClient = m_clients.key(device);
m_clients.remove(smtpClient);
delete smtpClient;
}
void DevicePluginMailNotification::testLoginFinished(const bool &success)
{
SmtpClient *smtpClient = static_cast<SmtpClient*>(sender());
Device *device = m_clients.value(smtpClient);
if(success){
emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusSuccess);
}else{
emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure);
m_clients.remove(smtpClient);
delete smtpClient;
}
}
void DevicePluginMailNotification::sendMailFinished(const bool &success, const ActionId &actionId)
{
if(success){

View File

@ -42,9 +42,11 @@ private:
QHash <SmtpClient*, Device*> m_clients;
private slots:
void testLoginFinished(const bool &success);
public slots:
void sendMailFinished(const bool &success, const ActionId &actionId);
};
#endif // DEVICEPLUGINMAILNOTIFICATION_H

View File

@ -69,7 +69,7 @@
{
"name": "port",
"type": "int",
"defaultValue": "465"
"defaultValue": 25
},
{
"name": "authentification",
@ -80,6 +80,7 @@
{
"name": "encryption",
"type": "QString",
"defaultValue": "SSL",
"allowedValues": ["NONE","SSL","TLS"]
}
],

View File

@ -21,9 +21,8 @@
SmtpClient::SmtpClient(QObject *parent):
QObject(parent)
{
m_socket = new QSslSocket(this);
m_state = InitState;
m_testLogin = false;
m_socket = new QSslSocket(this);
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
// error because QSslSocket has also a method called error...also QAbstractSocket or QTcpSocket don't work...
@ -34,16 +33,35 @@ SmtpClient::SmtpClient(QObject *parent):
}
SmtpClient::SmtpClient(QString host, int port, QString user, QString password, SmtpClient::AuthMethod authMethod, SmtpClient::EncryptionType encryptionType, QObject *parent):
m_host(host),
m_port(port),
m_user(user),
m_password(password),
m_authMethod(authMethod),
m_encryptionType(encryptionType),
QObject(parent)
{
m_state = InitState;
m_socket = new QSslSocket(this);
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_encryptionType) {
case EncryptionTypeNone:
case EncryptionNone:
m_socket->connectToHost(m_host, m_port);
break;
case EncryptionTypeSSL:
case EncryptionSSL:
m_socket->connectToHostEncrypted(m_host, m_port);
break;
case EncryptionTypeTLS:
case EncryptionTLS:
m_socket->connectToHost(m_host,m_port);
break;
default:
@ -60,10 +78,8 @@ void SmtpClient::testLogin()
void SmtpClient::connected()
{
}
void SmtpClient::disconnected()
{
// qDebug() << "connected to" << m_host;
// qDebug() << "-----------------------";
}
void SmtpClient::readData()
@ -77,19 +93,25 @@ void SmtpClient::readData()
}
responseLine.truncate( 3 );
// qDebug() << "---------------------------------------------";
// qDebug() << "Server code:" << responseLine;
// qDebug() << "---------------------------------------------";
// qDebug() << "Server data: " << response;
// qDebug() << "---------------------------------------------";
switch (m_state) {
case InitState:
if(responseLine == "220"){
send("EHLO localhost");
if(m_encryptionType == EncryptionTypeNone){
if(m_encryptionType == EncryptionNone){
m_state = AuthentificationState;
break;
}
if(m_encryptionType == EncryptionTypeSSL){
if(m_encryptionType == EncryptionSSL){
m_state = HandShakeState;
break;
}
if(m_encryptionType == EncryptionTypeTLS){
if(m_encryptionType == EncryptionTLS){
m_state = StartTlsState;
break;
}
@ -97,9 +119,14 @@ void SmtpClient::readData()
break;
case HandShakeState:
if(responseLine == "250"){
if(!m_socket->isEncrypted() && m_encryptionType != EncryptionTypeNone){
m_socket->startClientEncryption();
}
qDebug() << "Handshake";
m_socket->startClientEncryption();
send("EHLO localhost");
m_state = AuthentificationState;
}
if(responseLine == "220"){
qDebug() << "TLS Handshake";
m_socket->startClientEncryption();
send("EHLO localhost");
m_state = AuthentificationState;
}
@ -117,6 +144,12 @@ void SmtpClient::readData()
m_state = HandShakeState;
}
break;
case StartTlsState:
if(responseLine == "250"){
send("STARTTLS");
m_state = HandShakeState;
}
break;
case AuthentificationState:
if(responseLine == "250"){
if(m_authMethod == AuthMethodLogin){
@ -187,7 +220,10 @@ void SmtpClient::readData()
case QuitState:
if(responseLine == "250"){
emit sendMailFinished(true, m_actionId);
send("QUIT");
// qDebug() << "--------------------------------------------";
// qDebug() << " MAIL SENT!!!!";
// qDebug() << "--------------------------------------------";
logout();
m_state = CloseState;
}
break;
@ -195,23 +231,34 @@ void SmtpClient::readData()
if(responseLine == "221"){
m_socket->close();
}
// some mail server does not recognize the QUIT command...so close the connection either way
m_socket->close();
break;
default:
// unexpecterd response code received...
if(m_testLogin){
emit testLoginFinished(false);
m_testLogin = false;
m_socket->close();
break;
}
//qDebug() << "ERROR: unexpected response from server: " << response;
emit sendMailFinished(false, m_actionId);
m_socket->close();
break;
}
}
bool SmtpClient::sendMail(const QString &subject, const QString &body, const ActionId &actionId)
{
// 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 &subject, const QString &body, const ActionId &actionId)
{
m_actionId = actionId;
@ -222,7 +269,7 @@ bool SmtpClient::sendMail(const QString &subject, const QString &body, const Act
m_message = "To: " + m_rcpt + "\n";
m_message.append("From: " + m_sender + "\n");
m_message.append("Subject: [guh notification] | " + subject + "\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" ) );
@ -268,7 +315,7 @@ void SmtpClient::setSender(const QString &sender)
m_sender = sender;
}
void SmtpClient::setRecipient(const QString &rcpt)
void SmtpClient::setRecipiant(const QString &rcpt)
{
m_rcpt = rcpt;
}
@ -280,6 +327,7 @@ void SmtpClient::socketError(QAbstractSocket::SocketError error)
void SmtpClient::send(const QString &data)
{
//qDebug() << "sending to host:" << data;
m_socket->write(data.toUtf8() + "\r\n");
m_socket->flush();
}

View File

@ -53,15 +53,17 @@ public:
};
enum EncryptionType{
EncryptionTypeNone,
EncryptionTypeSSL,
EncryptionTypeTLS
EncryptionNone, // no encryption
EncryptionSSL, // SSL
EncryptionTLS // STARTTLS
};
explicit SmtpClient(QObject *parent = 0);
explicit SmtpClient(QString host = QString(), int port = 465, QString user = QString(), QString password = QString(), AuthMethod authMethod = AuthPlain, EncryptionType encryptionType = EncryptionNone, QObject *parent = 0);
void connectToHost();
void testLogin();
void login(const QString &user, const QString &password);
void logout();
bool sendMail(const QString &subject, const QString &body, const ActionId &actionId);
void setHost(const QString &host);
@ -71,7 +73,7 @@ public:
void setUser(const QString &user);
void setPassword(const QString &password);
void setSender(const QString &sender);
void setRecipient(const QString &rcpt);
void setRecipiant(const QString &rcpt);
private:
@ -90,11 +92,8 @@ private:
QString m_message;
ActionId m_actionId;
bool m_testLogin;
signals:
void sendMailFinished(const bool &success, const ActionId &actionId);
void testLoginFinished(const bool &success);
private slots:
void socketError(QAbstractSocket::SocketError error);