some improvements

* add plugin tree
* add emailnotify plugin template
This commit is contained in:
Evgenii Alekseev 2017-11-17 00:28:15 +03:00
parent 3717d1426e
commit 881abd4c51
18 changed files with 516 additions and 55 deletions

View File

@ -27,6 +27,7 @@ message(STATUS "Version: ${PROJECT_VERSION}")
message(STATUS "Build date: ${CURRENT_DATE}")
# components
option(BUILD_PLUGINS "Build plugins" ON)
option(BUILD_DEB_PACKAGE "Build deb package" OFF)
option(BUILD_RPM_PACKAGE "Build rpm package" OFF)
# build details
@ -53,6 +54,9 @@ add_subdirectory("queued")
add_subdirectory("queued-daemon")
add_subdirectory("queued-server")
add_subdirectory("queuedctl")
if (BUILD_PLUGINS)
add_subdirectory("plugins")
endif()
if (BUILD_TESTING)
enable_testing()
add_subdirectory("test")

View File

@ -0,0 +1,9 @@
set (SUBPROJECT "plugins")
message (STATUS "Subproject ${SUBPROJECT}")
include_directories("${CMAKE_BINARY_DIR}"
"${PROJECT_LIBRARY_DIR}/include" # required by moc
"${PROJECT_TRDPARTY_DIR}")
find_package(Queued PATHS "${CMAKE_CURRENT_SOURCE_DIR}" NO_DEFAULT_PATH)
queued_install_plugin("emailnotify" "emailnotify" curl)

View File

@ -0,0 +1,13 @@
set(QUEUED_DEFINITIONS "")
find_path(QUEUEDCORE_INCLUDE_DIR "queued/Queued.h" PATH_SUFFIXES "queued"
PATHS "${PROJECT_LIBRARY_DIR}/include" NO_DEFAULT_PATH)
set(QUEUED_INCLUDE_DIRS "${QUEUEDCORE_INCLUDE_DIR}")
set(QUEUED_LIBRARIES "${PROJECT_LIBRARY}")
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Queued DEFAULT_MSG
QUEUED_LIBRARIES QUEUED_INCLUDE_DIRS)
set(QUEUED_PLUGIN_ROOT "${DATA_INSTALL_DIR}/queued/plugins")
include("${PROJECT_LIBRARY_DIR}/QueuedLibraries.cmake")
include("${PROJECT_LIBRARY_DIR}/QueuedMacros.cmake")

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2017 Queued team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#include "QueuedEmailNotify.h"
#include <queued/Queued.h>
#include "QueuedEmailNotifyHelper.h"
void QueuedEmailNotify::init(const QVariantHash &_settings)
{
m_helper = new QueuedEmailNotifyHelper(this);
m_helper->setFrom(_settings.value("From", "mail@example.com").toString());
m_helper->setInsecureCurl(_settings.value("InsecureCurl", false).toBool());
m_helper->setPassword(_settings.value("Password", "").toString());
m_helper->setPort(_settings.value("Port", 465).toInt());
m_helper->setServer(
_settings.value("Server", "smtp://smtp.example.com").toString());
m_helper->setSslEnabled(_settings.value("UseSSL", false).toBool());
m_helper->setUsername(_settings.value("Username", "").toString());
}
void QueuedEmailNotify::setToken(const QString &_token)
{
m_token = _token;
}
void QueuedEmailNotify::setup(const QueuedPluginManagerInterface *_manager)
{
connect(_manager, SIGNAL(onStopTask(const long long)), m_helper,
SLOT(sendEmail(const long long)));
}
void QueuedEmailNotify::updateSettings(const QString &_key,
const QVariant &_value)
{
qCDebug(LOG_PL) << "Update settings for" << _key;
if (!m_helper) {
qCWarning(LOG_PL)
<< "Helper is not initialized. Did you forget to call ::init()?";
return;
}
if (_key == "From") {
m_helper->setFrom(_value.toString());
} else if (_key == "Password") {
m_helper->setPassword(_value.toString());
} else if (_key == "Port") {
m_helper->setPort(_value.toInt());
} else if (_key == "Server") {
m_helper->setServer(_value.toString());
} else if (_key == "Username") {
m_helper->setUsername(_value.toString());
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2017 Queued team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#ifndef QUEUEDEMAILNOTIFY_H
#define QUEUEDEMAILNOTIFY_H
#include <queued/QueuedPluginInterface.h>
class QueuedEmailNotifyHelper;
class QueuedEmailNotify : public QObject, public QueuedPluginInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.queued.emailnotify/1.0")
Q_INTERFACES(QueuedPluginInterface)
public:
virtual ~QueuedEmailNotify() = default;
void init(const QVariantHash &_settings) override;
void setToken(const QString &_token) override;
void setup(const QueuedPluginManagerInterface *_manager) override;
void updateSettings(const QString &_key, const QVariant &_value) override;
private:
QueuedEmailNotifyHelper *m_helper = nullptr;
QString m_token;
};
#endif /* QUEUEDEMAILNOTIFY_H */

View File

@ -0,0 +1,249 @@
/*
* Copyright (c) 2017 Queued team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#include "QueuedEmailNotifyHelper.h"
#include <queued/Queued.h>
extern "C" {
#include <curl/curl.h>
}
QueuedEmailNotifyHelper::QueuedEmailNotifyHelper(QObject *_parent)
: QObject(_parent)
{
qCDebug(LOG_PL) << __PRETTY_FUNCTION__;
}
QString QueuedEmailNotifyHelper::curlEmail(const QString &_from)
{
qCDebug(LOG_PL) << "Convert email to curl format from" << _from;
return QString("<%1>").arg(_from);
}
char *QueuedEmailNotifyHelper::curlString(const QString &_source)
{
return strdup(_source.toStdString().c_str());
}
size_t QueuedEmailNotifyHelper::curlReadCallback(char *buffer, size_t size,
size_t nitems, void *instream)
{
// FIXME not really best choice to use here
auto text = reinterpret_cast<MailBody *>(instream);
// check sizes and whatever
if ((size == 0) || (nitems == 0) || ((size * nitems) < 1))
return 0;
if (text->text.count() <= text->currentLine)
return 0;
// get char* string from related qstring
auto data = curlString(text->text.at(text->currentLine));
size_t len = strlen(data);
memcpy(buffer, data, len);
text->currentLine++;
return len;
}
QString QueuedEmailNotifyHelper::from() const
{
return m_from;
}
bool QueuedEmailNotifyHelper::isInsecureCurl() const
{
return m_insecure;
}
bool QueuedEmailNotifyHelper::isSslEnabled() const
{
return m_ssl;
}
QString QueuedEmailNotifyHelper::password() const
{
return m_password;
}
int QueuedEmailNotifyHelper::port() const
{
return m_port;
}
QString QueuedEmailNotifyHelper::server() const
{
return m_server;
}
QString QueuedEmailNotifyHelper::username() const
{
return m_username;
}
void QueuedEmailNotifyHelper::setFrom(const QString &_from)
{
qCDebug(LOG_PL) << "Set from" << _from;
m_from = _from;
}
void QueuedEmailNotifyHelper::setInsecureCurl(const bool _insecureCurl)
{
qCDebug(LOG_PL) << "Set insecure curl" << _insecureCurl;
m_insecure = _insecureCurl;
}
void QueuedEmailNotifyHelper::setPassword(const QString &_password)
{
qCDebug(LOG_PL) << "Set password";
m_password = _password;
}
void QueuedEmailNotifyHelper::setPort(const int &_port)
{
qCDebug(LOG_PL) << "Set port" << _port;
m_port = _port;
}
void QueuedEmailNotifyHelper::setServer(const QString &_server)
{
qCDebug(LOG_PL) << "Set server" << _server;
m_server = _server;
}
void QueuedEmailNotifyHelper::setSslEnabled(const bool _sslEnabled)
{
qCDebug(LOG_PL) << "Set ssl enabled" << _sslEnabled;
m_ssl = _sslEnabled;
}
void QueuedEmailNotifyHelper::setUsername(const QString &_username)
{
qCDebug(LOG_PL) << "Set username" << _username;
m_username = _username;
}
void QueuedEmailNotifyHelper::sendEmail(const long long _id)
{
auto rcpt = getEmail(_id);
if (rcpt.isEmpty())
return;
MailBody text = {getEmailText(_id, rcpt), 0};
CURL *curl = curl_easy_init();
QString url = QString("%1:%2").arg(server()).arg(port());
curl_easy_setopt(curl, CURLOPT_URL, curlString(url));
// ssl options
if (isSslEnabled())
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
if (isInsecureCurl())
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
// auth if applicable
if ((!username().isEmpty()) && (!password().isEmpty())) {
curl_easy_setopt(curl, CURLOPT_USERNAME, curlString(username()));
curl_easy_setopt(curl, CURLOPT_PASSWORD, curlString(password()));
}
// from & to
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, curlString(curlEmail(from())));
curl_slist *recipients = nullptr;
recipients = curl_slist_append(recipients, curlString(curlEmail(rcpt)));
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
// mail body
curl_easy_setopt(curl, CURLOPT_READFUNCTION,
&QueuedEmailNotifyHelper::curlReadCallback);
curl_easy_setopt(curl, CURLOPT_READDATA, &text);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
// send
auto res = curl_easy_perform(curl);
if (res != CURLE_OK)
qCWarning(LOG_PL) << "Could not perform curl action"
<< curl_easy_strerror(res);
// cleanup
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
QString QueuedEmailNotifyHelper::getEmail(const long long _id) const
{
qCDebug(LOG_PL) << "Get email for task ID" << _id;
auto task = QueuedCoreAdaptor::getTask(_id, "user");
if (task.type() != Result::Content::Value) {
qCWarning(LOG_LIB) << "Could not get task information" << _id;
return "";
}
auto userId = task.get().toLongLong();
auto user = QueuedCoreAdaptor::getUser(userId, "email");
if (user.type() != Result::Content::Value) {
qCWarning(LOG_LIB) << "Could not get user information" << userId;
return "";
}
return user.get().toString();
}
QStringList QueuedEmailNotifyHelper::getEmailText(const long long _id,
const QString &_to) const
{
qCDebug(LOG_PL) << "Get email text for user" << _to << "for task" << _id;
auto now
= QDateTime::currentDateTimeUtc().toString(Qt::DateFormat::RFC2822Date);
return {QString("Date: %1\r\n").arg(now),
QString("To: %1\r\n").arg(curlEmail(_to)),
QString("From: %1\r\n").arg(curlEmail(from())),
// message-id?
QString("Subject: %1\r\n").arg("Job %1 done").arg(_id), "\r\n",
QString("Job %1 done\r\n").arg(_id)};
}

View File

@ -0,0 +1,78 @@
/*
* Copyright (c) 2017 Queued team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#ifndef QUEUEDEMAILNOTIFYHELPER_H
#define QUEUEDEMAILNOTIFYHELPER_H
#include <QObject>
class QueuedEmailNotifyHelper : public QObject
{
Q_OBJECT
Q_PROPERTY(QString from READ from WRITE setFrom)
Q_PROPERTY(bool insecureCurl READ isInsecureCurl WRITE setInsecureCurl)
Q_PROPERTY(QString password READ password WRITE setPassword)
Q_PROPERTY(int port READ port WRITE setPort)
Q_PROPERTY(QString server READ server WRITE setServer)
Q_PROPERTY(bool ssl READ isSslEnabled WRITE setSslEnabled)
Q_PROPERTY(QString username READ username WRITE setUsername)
public:
typedef struct {
QStringList text;
int currentLine = 0;
} MailBody;
explicit QueuedEmailNotifyHelper(QObject *_parent);
virtual ~QueuedEmailNotifyHelper() = default;
static QString curlEmail(const QString &_from);
static char *curlString(const QString &_source);
static size_t curlReadCallback(char *buffer, size_t size, size_t nitems,
void *instream);
// properties
QString from() const;
bool isInsecureCurl() const;
bool isSslEnabled() const;
QString password() const;
int port() const;
QString server() const;
QString username() const;
void setFrom(const QString &_from);
void setInsecureCurl(const bool _insecureCurl);
void setPassword(const QString &_password);
void setPort(const int &_port);
void setServer(const QString &_server);
void setSslEnabled(const bool _sslEnabled);
void setUsername(const QString &_username);
public slots:
void sendEmail(const long long _id);
private:
QString getEmail(const long long _id) const;
QStringList getEmailText(const long long _id, const QString &_to) const;
QString m_from;
bool m_insecure = false;
QString m_password;
int m_port = 0;
QString m_server;
bool m_ssl = false;
QString m_username;
};
#endif /* QUEUEDEMAILNOTIFYHELPER_H */

View File

@ -4,10 +4,10 @@ file (GLOB_RECURSE SUBPROJECT_HEADERS "*.h")
# include_path
include_directories ("${PROJECT_LIBRARY_DIR}/include"
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_BINARY_DIR}"
"${PROJECT_TRDPARTY_DIR}"
"${Qt_INCLUDE}")
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_BINARY_DIR}"
"${PROJECT_TRDPARTY_DIR}"
"${Qt_INCLUDE}")
qt5_wrap_cpp (SUBPROJECT_MOC_SOURCES "${SUBPROJECT_HEADERS}")

View File

@ -4,10 +4,10 @@ file (GLOB_RECURSE SUBPROJECT_HEADERS "*.h")
# include_path
include_directories ("${PROJECT_LIBRARY_DIR}/include"
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_BINARY_DIR}"
"${PROJECT_TRDPARTY_DIR}"
"${Qt_INCLUDE}")
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_BINARY_DIR}"
"${PROJECT_TRDPARTY_DIR}"
"${Qt_INCLUDE}")
qt5_wrap_cpp (SUBPROJECT_MOC_SOURCES "${SUBPROJECT_HEADERS}")

View File

@ -115,7 +115,7 @@ static const char CG_FS_PATH[] = "/sys/fs/cgroup";
* @remark required by Qt macro
*/
#define PLUGIN_INTERFACE_NAME \
"queued.plugin/@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@"
"org.queued.plugin/@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@"
/**
* @brief plugin interface name
*/

View File

@ -37,8 +37,8 @@ add_definitions(
# Qt include paths
##
set(Qt_INCLUDE
${Qt5Core_INCLUDE_DIRS} ${Qt5DBus_INCLUDE_DIRS} ${Qt5Network_INCLUDE_DIRS}
${Qt5Sql_INCLUDE_DIRS}
${Qt5Core_INCLUDE_DIRS} ${Qt5DBus_INCLUDE_DIRS}
${Qt5Network_INCLUDE_DIRS} ${Qt5Sql_INCLUDE_DIRS}
)
##
# @def Qt_LIBRARIES

View File

@ -27,11 +27,12 @@
##
# @fn queued_install_plugin root [name] [libraries]
# @brief build and install plugin
# @param PLUGIN_ROOT plugin root directory
# @param PLUGIN plugin name (optional). Will be assigned from path
# if empty
# @param ADDS_LIBRARIES additional libraries (optional)
#
# @param PLUGIN_ROOT
# plugin root directory
# @param PLUGIN
# plugin name (optional). Will be assigned from path if empty
# @param ADDS_LIBRARIES
# additional libraries (optional)
##
macro(queued_install_plugin PLUGIN_ROOT)
set(PLUGIN "${ARGV1}")
@ -47,13 +48,13 @@ macro(queued_install_plugin PLUGIN_ROOT)
# include directories
include_directories ("${CMAKE_CURRENT_BINARY_DIR}"
"${Qt_INCLUDE}"
"${QUEUED_INCLUDE_DIRS}")
"${Qt_INCLUDE}" "${QUEUED_INCLUDE_DIRS}")
# build
add_library ("${PLUGIN}" MODULE "${${PLUGIN}_SOURCES}" "${${PLUGIN}_HEADERS}"
"${${PLUGIN}_MOC_SOURCES}")
target_link_libraries ("${PLUGIN}" "${QUEUED_LIBRARIES}" "${Qt_LIBRARIES}" "${ADDS_LIBRARIES}")
add_library ("${PLUGIN}" MODULE "${${PLUGIN}_SOURCES}"
"${${PLUGIN}_HEADERS}" "${${PLUGIN}_MOC_SOURCES}")
target_link_libraries ("${PLUGIN}" "${QUEUED_LIBRARIES}" "${Qt_LIBRARIES}"
"${ADDS_LIBRARIES}")
# install
install (TARGETS "${PLUGIN}" DESTINATION "${QUEUED_PLUGIN_ROOT}")

View File

@ -305,19 +305,6 @@ template <class T> QDBusVariant toDBusVariant(const QueuedResult<T> &_data)
{
return QDBusVariant(QVariant::fromValue<QueuedResult<T>>(_data));
};
/**
* @brief additional method to avoid conversion from QDBusVariant to
* QueuedResult
* @tparam T
* QueuedResult payload class
* @param _data
* input data
* @return converted data to QueuedResult
*/
template <class T> QueuedResult<T> toResult(const QDBusVariant &_data)
{
return qdbus_cast<QueuedResult<T>>(_data.variant().value<QDBusArgument>());
};
/**
* @brief additional method to avoid conversion from QVariant to
* QueuedResult
@ -361,7 +348,7 @@ QueuedResult<T> sendRequest(const QString &_service, const QString &_path,
if (dbusResponse.isValid()) {
auto response = dbusResponse.value();
return toResult<T>(response);
return toResult<T>(response.variant());
} else {
return QueuedError(dbusResponse.error().message().toStdString());
}

View File

@ -40,13 +40,7 @@ public:
/**
* @brief QueuedPluginInterface class destructor
*/
virtual ~QueuedPluginInterface() = default;
/**
* @brief will be emitted to map interface signals to plugin slots
* @param _manager
* pointer to manager interface object
*/
virtual void connect(const QueuedPluginManagerInterface *_manager) = 0;
virtual ~QueuedPluginInterface(){};
/**
* @brief plugin initialization method
* @param _settings
@ -62,6 +56,12 @@ public:
* new token ID
*/
virtual void setToken(const QString &_token) = 0;
/**
* @brief will be emitted to map interface signals to plugin slots
* @param _manager
* pointer to manager interface object
*/
virtual void setup(const QueuedPluginManagerInterface *_manager) = 0;
/**
* @brief method which will be called on option update
* @param _key

View File

@ -4,15 +4,15 @@ file (GLOB_RECURSE SUBPROJECT_HEADERS "*.h" "${PROJECT_LIBRARY_DIR}/*h")
# include_path
include_directories ("${PROJECT_LIBRARY_DIR}/include"
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_BINARY_DIR}"
"${PROJECT_TRDPARTY_DIR}"
"${Qt_INCLUDE}")
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_BINARY_DIR}"
"${PROJECT_TRDPARTY_DIR}"
"${Qt_INCLUDE}")
qt5_wrap_cpp (SUBPROJECT_MOC_SOURCES "${SUBPROJECT_HEADERS}")
add_library ("${SUBPROJECT}" SHARED "${SUBPROJECT_SOURCES}" "${SUBPROJECT_HEADERS}"
"${SUBPROJECT_MOC_SOURCES}")
add_library ("${SUBPROJECT}" SHARED "${SUBPROJECT_SOURCES}"
"${SUBPROJECT_HEADERS}" "${SUBPROJECT_MOC_SOURCES}")
set_target_properties ("${SUBPROJECT}" PROPERTIES
SOVERSION "${PROJECT_VERSION_MAJOR}"
VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")

View File

@ -386,8 +386,10 @@ QVariantHash QueuedCorePrivate::pluginSettings(const QString &_plugin)
QString("WHERE key LIKE 'Plugin.%1.%'").arg(_plugin));
QVariantHash settings;
std::for_each(dbSettings.cbegin(), dbSettings.cend(),
[&settings](const QVariantHash &value) {
settings[value["key"].toString()] = value["value"];
[&settings, &_plugin](const QVariantHash &value) {
auto key = value["key"].toString();
key.remove(QString("Plugin.%1.").arg(_plugin));
settings[key] = value["value"];
});
return settings;

View File

@ -131,7 +131,7 @@ bool QueuedPluginManager::loadPlugin(const QString &_name,
m_plugins[_name] = item;
item->init(pluginSettings);
item->setToken(m_token);
item->connect(interface());
item->setup(interface());
} else {
qCCritical(LOG_PL) << "Could not cast plugin" << _name;
}

View File

@ -4,10 +4,10 @@ file (GLOB_RECURSE SUBPROJECT_HEADERS "*.h")
# include_path
include_directories ("${PROJECT_LIBRARY_DIR}/include"
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_BINARY_DIR}"
"${PROJECT_TRDPARTY_DIR}"
"${Qt_INCLUDE}")
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_BINARY_DIR}"
"${PROJECT_TRDPARTY_DIR}"
"${Qt_INCLUDE}")
qt5_wrap_cpp (SUBPROJECT_MOC_SOURCES "${SUBPROJECT_HEADERS}")