mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
change code style
This commit is contained in:
parent
f4a67b476d
commit
c1e82dda94
@ -1,172 +1,173 @@
|
|||||||
#include "EmailManager.h"
|
#include "EmailManager.h"
|
||||||
#include "../ServerConfig.h"
|
#include "../ServerConfig.h"
|
||||||
|
|
||||||
#include "../Crypto/Obfus_array.h"
|
#include "../Crypto/Obfus_array.h"
|
||||||
#include "../Crypto/DRRandom.h"
|
#include "../Crypto/DRRandom.h"
|
||||||
|
|
||||||
#include "../SingletonManager/LanguageManager.h"
|
#include "../SingletonManager/LanguageManager.h"
|
||||||
|
|
||||||
#include "Poco/Net/SecureSMTPClientSession.h"
|
#include "Poco/Net/SecureSMTPClientSession.h"
|
||||||
#include "Poco/Net/SSLException.h"
|
#include "Poco/Net/SSLException.h"
|
||||||
|
|
||||||
|
|
||||||
EmailManager::EmailManager()
|
EmailManager::EmailManager()
|
||||||
: Thread("emails", false), mEmailLog(Poco::Logger::get("emailLog")), mInitalized(false), mDisableEmail(false)
|
: Thread("emails", false), mEmailLog(Poco::Logger::get("emailLog")), mInitalized(false), mDisableEmail(false)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EmailManager::~EmailManager()
|
EmailManager::~EmailManager()
|
||||||
{
|
{
|
||||||
exit();
|
exit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EmailManager* EmailManager::getInstance()
|
EmailManager* EmailManager::getInstance()
|
||||||
{
|
{
|
||||||
static EmailManager theOne;
|
static EmailManager theOne;
|
||||||
return &theOne;
|
return &theOne;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EmailManager::init(const Poco::Util::LayeredConfiguration& cfg)
|
bool EmailManager::init(const Poco::Util::LayeredConfiguration& cfg)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
mDisableEmail = cfg.getBool("email.disable", false);
|
mDisableEmail = cfg.getBool("email.disable", false);
|
||||||
if (!mDisableEmail) {
|
if (!mDisableEmail) {
|
||||||
mEmailAccount.sender = cfg.getString("email.sender");
|
mEmailAccount.sender = cfg.getString("email.sender");
|
||||||
mEmailAccount.admin_receiver = cfg.getString("email.admin_receiver");
|
mEmailAccount.admin_receiver = cfg.getString("email.admin_receiver");
|
||||||
mEmailAccount.username = cfg.getString("email.username");
|
mEmailAccount.username = cfg.getString("email.username");
|
||||||
mEmailAccount.password = cfg.getString("email.password");
|
mEmailAccount.password = cfg.getString("email.password");
|
||||||
mEmailAccount.url = cfg.getString("email.smtp.url");
|
mEmailAccount.url = cfg.getString("email.smtp.url");
|
||||||
mEmailAccount.port = cfg.getInt("email.smtp.port");
|
mEmailAccount.port = cfg.getInt("email.smtp.port");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Poco::Exception& ex) {
|
catch (Poco::Exception& ex) {
|
||||||
printf("email account not set in config: %s\n", ex.displayText().data());
|
printf("email account not set in config: %s\n", ex.displayText().data());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Thread::init("emails");
|
Thread::init("emails");
|
||||||
mInitalized = true;
|
mInitalized = true;
|
||||||
|
|
||||||
DISASM_FALSERET;
|
DISASM_FALSERET;
|
||||||
ServerConfig::g_ServerKeySeed->put(3, DRRandom::r64());
|
ServerConfig::g_ServerKeySeed->put(3, DRRandom::r64());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EmailManager::addEmail(model::Email* email) {
|
void EmailManager::addEmail(model::Email* email) {
|
||||||
if (mDisableEmail) {
|
if (mDisableEmail) {
|
||||||
std::string log_message = "Email should be sended to: ";
|
std::string log_message = "Email should be sended to: ";
|
||||||
auto email_user = email->getUser();
|
auto email_user = email->getUser();
|
||||||
if (email_user && email_user->getModel()) {
|
Poco::AutoPtr<model::table::User> email_model;
|
||||||
log_message += email_user->getModel()->getNameWithEmailHtml();
|
if (email_user) {
|
||||||
}
|
email_model = email_user->getModel();
|
||||||
else {
|
log_message += email_model->getNameWithEmailHtml();
|
||||||
log_message += "<missing>";
|
}
|
||||||
}
|
if (email_model.isNull()) {
|
||||||
log_message += ", type: ";
|
log_message += "<missing>";
|
||||||
log_message += model::Email::emailTypeString(email->getType());
|
}
|
||||||
mEmailLog.log(log_message);
|
log_message += ", type: ";
|
||||||
delete email;
|
log_message += model::Email::emailTypeString(email->getType());
|
||||||
return;
|
mEmailLog.log(log_message);
|
||||||
}
|
|
||||||
mPendingEmails.push(email);
|
delete email;
|
||||||
condSignal();
|
return;
|
||||||
}
|
}
|
||||||
|
mPendingEmails.push(email);
|
||||||
void EmailManager::exit()
|
condSignal();
|
||||||
{
|
}
|
||||||
model::Email* email = nullptr;
|
|
||||||
while (mPendingEmails.pop(email)) {
|
void EmailManager::exit()
|
||||||
delete email;
|
{
|
||||||
}
|
model::Email* email = nullptr;
|
||||||
mInitalized = false;
|
while (mPendingEmails.pop(email)) {
|
||||||
}
|
delete email;
|
||||||
|
}
|
||||||
int EmailManager::ThreadFunction()
|
mInitalized = false;
|
||||||
{
|
}
|
||||||
// prepare connection to email server
|
|
||||||
if (ServerConfig::g_disableEmail) return 0;
|
int EmailManager::ThreadFunction()
|
||||||
|
{
|
||||||
if (mPendingEmails.empty()) return 0;
|
// prepare connection to email server
|
||||||
|
if (ServerConfig::g_disableEmail) return 0;
|
||||||
auto lm = LanguageManager::getInstance();
|
|
||||||
|
if (mPendingEmails.empty()) return 0;
|
||||||
Poco::Net::SecureSMTPClientSession mailClientSession(mEmailAccount.url, mEmailAccount.port);
|
|
||||||
mailClientSession.login();
|
auto lm = LanguageManager::getInstance();
|
||||||
try {
|
|
||||||
mailClientSession.startTLS(ServerConfig::g_SSL_CLient_Context);
|
Poco::Net::SecureSMTPClientSession mailClientSession(mEmailAccount.url, mEmailAccount.port);
|
||||||
mailClientSession.login(Poco::Net::SMTPClientSession::AUTH_LOGIN, mEmailAccount.username, mEmailAccount.password);
|
mailClientSession.login();
|
||||||
}
|
try {
|
||||||
catch (Poco::Net::SSLException& ex) {
|
mailClientSession.startTLS(ServerConfig::g_SSL_CLient_Context);
|
||||||
printf("[PrepareEmailTask] ssl certificate error: %s\nPlease make sure you have cacert.pem (CA/root certificates) next to binary from https://curl.haxx.se/docs/caextract.html\n", ex.displayText().data());
|
mailClientSession.login(Poco::Net::SMTPClientSession::AUTH_LOGIN, mEmailAccount.username, mEmailAccount.password);
|
||||||
return -1;
|
}
|
||||||
}
|
catch (Poco::Net::SSLException& ex) {
|
||||||
|
printf("[PrepareEmailTask] ssl certificate error: %s\nPlease make sure you have cacert.pem (CA/root certificates) next to binary from https://curl.haxx.se/docs/caextract.html\n", ex.displayText().data());
|
||||||
model::Email* email = nullptr;
|
return -1;
|
||||||
Poco::AutoPtr<LanguageCatalog> catalogs[2];
|
}
|
||||||
|
|
||||||
// if email list empty, wait 500ms x time before exit thread and closing connection
|
model::Email* email = nullptr;
|
||||||
int timeoutWaits = 20;
|
Poco::AutoPtr<LanguageCatalog> catalogs[2];
|
||||||
|
|
||||||
while (mPendingEmails.pop(email) || timeoutWaits > 0) {
|
// if email list empty, wait 500ms x time before exit thread and closing connection
|
||||||
if (email) {
|
int timeoutWaits = 20;
|
||||||
Poco::Net::MailMessage mailMessage;
|
|
||||||
mailMessage.setSender(mEmailAccount.sender);
|
while (mPendingEmails.pop(email) || timeoutWaits > 0) {
|
||||||
Languages lang_code = ServerConfig::g_default_locale;
|
if (email) {
|
||||||
if (email->getUser()) {
|
Poco::Net::MailMessage mailMessage;
|
||||||
Poco::AutoPtr<model::table::User> userModel = email->getUser()->getModel();
|
mailMessage.setSender(mEmailAccount.sender);
|
||||||
|
Languages lang_code = ServerConfig::g_default_locale;
|
||||||
if (!userModel.isNull()) {
|
auto email_user = email->getUser();
|
||||||
userModel->lock("EmailManager::ThreadFunction");
|
if (email_user) {
|
||||||
lang_code = LanguageManager::languageFromString(userModel->getLanguageKey());
|
Poco::AutoPtr<model::table::User> userModel = email_user->getModel();
|
||||||
userModel->unlock();
|
|
||||||
if (lang_code > LANG_COUNT) lang_code = ServerConfig::g_default_locale;
|
if (!userModel.isNull()) {
|
||||||
}
|
lang_code = LanguageManager::languageFromString(userModel->getLanguageKey());
|
||||||
}
|
if (lang_code > LANG_COUNT) lang_code = ServerConfig::g_default_locale;
|
||||||
if (catalogs[lang_code].isNull()) {
|
}
|
||||||
catalogs[lang_code] = lm->getFreeCatalog(lang_code);
|
}
|
||||||
}
|
if (catalogs[lang_code].isNull()) {
|
||||||
if (email->draft(&mailMessage, catalogs[lang_code])) {
|
catalogs[lang_code] = lm->getFreeCatalog(lang_code);
|
||||||
|
}
|
||||||
mailClientSession.sendMessage(mailMessage);
|
if (email->draft(&mailMessage, catalogs[lang_code])) {
|
||||||
// add for debugging
|
|
||||||
if (email->getUser()) {
|
mailClientSession.sendMessage(mailMessage);
|
||||||
//printf("send email to %s\n", user_model->getEmail().data());
|
// add for debugging
|
||||||
auto user_model = email->getUser()->getModel();
|
if (email_user) {
|
||||||
std::string log_message = "Email sended to: ";
|
//printf("send email to %s\n", user_model->getEmail().data());
|
||||||
auto email_user = email->getUser();
|
auto user_model = email_user->getModel();
|
||||||
if (user_model) {
|
std::string log_message = "Email sended to: ";
|
||||||
log_message += email_user->getModel()->getNameWithEmailHtml();
|
if (user_model) {
|
||||||
}
|
log_message += email_user->getModel()->getNameWithEmailHtml();
|
||||||
else {
|
}
|
||||||
log_message += "<missing>";
|
else {
|
||||||
}
|
log_message += "<missing>";
|
||||||
log_message += ", type: ";
|
}
|
||||||
log_message += model::Email::emailTypeString(email->getType());
|
log_message += ", type: ";
|
||||||
mEmailLog.log(log_message);
|
log_message += model::Email::emailTypeString(email->getType());
|
||||||
}
|
mEmailLog.log(log_message);
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
// error drafting email, shouldn't happend
|
else {
|
||||||
printf("[EmailManager::ThreadFunction] Error drafting email\n");
|
// error drafting email, shouldn't happend
|
||||||
}
|
printf("[EmailManager::ThreadFunction] Error drafting email\n");
|
||||||
delete email;
|
}
|
||||||
email = nullptr;
|
delete email;
|
||||||
}
|
email = nullptr;
|
||||||
if (mPendingEmails.empty()) {
|
}
|
||||||
Poco::Thread::sleep(500);
|
if (mPendingEmails.empty()) {
|
||||||
timeoutWaits--;
|
Poco::Thread::sleep(500);
|
||||||
}
|
timeoutWaits--;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mailClientSession.close();
|
|
||||||
|
mailClientSession.close();
|
||||||
return 0;
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
@ -145,8 +145,8 @@ Poco::AutoPtr<controller::EmailVerificationCode> Session::getEmailVerificationCo
|
|||||||
bool Session::adminCreateUser(const std::string& first_name, const std::string& last_name, const std::string& email, int group_id, const std::string &baseUrl)
|
bool Session::adminCreateUser(const std::string& first_name, const std::string& last_name, const std::string& email, int group_id, const std::string &baseUrl)
|
||||||
{
|
{
|
||||||
Profiler usedTime;
|
Profiler usedTime;
|
||||||
|
auto user_model = mNewUser->getModel();
|
||||||
if (mNewUser->getModel()->getRole() != model::table::ROLE_ADMIN) {
|
if (user_model->getRole() != model::table::ROLE_ADMIN) {
|
||||||
addError(new Error(gettext("Benutzer"), gettext("Eingeloggter Benutzer ist kein Admin")), false);
|
addError(new Error(gettext("Benutzer"), gettext("Eingeloggter Benutzer ist kein Admin")), false);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -167,7 +167,7 @@ bool Session::adminCreateUser(const std::string& first_name, const std::string&
|
|||||||
|
|
||||||
|
|
||||||
// check if user with that email already exist
|
// check if user with that email already exist
|
||||||
if (mNewUser->getModel()->isExistInDB("email", email)) {
|
if (user_model->isExistInDB("email", email)) {
|
||||||
addError(new Error(gettext("E-Mail"), gettext("Für diese E-Mail Adresse gibt es bereits einen Account")), false);
|
addError(new Error(gettext("E-Mail"), gettext("Für diese E-Mail Adresse gibt es bereits einen Account")), false);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user