mirror of
https://github.com/arcan1s/queued.git
synced 2025-07-14 06:15:47 +00:00
some improvements
* add plugin tree * add emailnotify plugin template
This commit is contained in:
74
sources/plugins/emailnotify/QueuedEmailNotify.cpp
Normal file
74
sources/plugins/emailnotify/QueuedEmailNotify.cpp
Normal 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());
|
||||
}
|
||||
}
|
44
sources/plugins/emailnotify/QueuedEmailNotify.h
Normal file
44
sources/plugins/emailnotify/QueuedEmailNotify.h
Normal 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 */
|
249
sources/plugins/emailnotify/QueuedEmailNotifyHelper.cpp
Normal file
249
sources/plugins/emailnotify/QueuedEmailNotifyHelper.cpp
Normal 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)};
|
||||
}
|
78
sources/plugins/emailnotify/QueuedEmailNotifyHelper.h
Normal file
78
sources/plugins/emailnotify/QueuedEmailNotifyHelper.h
Normal 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 */
|
Reference in New Issue
Block a user