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 "../ServerConfig.h"
|
||||
|
||||
#include "../Crypto/Obfus_array.h"
|
||||
#include "../Crypto/DRRandom.h"
|
||||
|
||||
#include "../SingletonManager/LanguageManager.h"
|
||||
|
||||
#include "Poco/Net/SecureSMTPClientSession.h"
|
||||
#include "Poco/Net/SSLException.h"
|
||||
|
||||
|
||||
EmailManager::EmailManager()
|
||||
: Thread("emails", false), mEmailLog(Poco::Logger::get("emailLog")), mInitalized(false), mDisableEmail(false)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
EmailManager::~EmailManager()
|
||||
{
|
||||
exit();
|
||||
|
||||
}
|
||||
|
||||
EmailManager* EmailManager::getInstance()
|
||||
{
|
||||
static EmailManager theOne;
|
||||
return &theOne;
|
||||
}
|
||||
|
||||
bool EmailManager::init(const Poco::Util::LayeredConfiguration& cfg)
|
||||
{
|
||||
try {
|
||||
mDisableEmail = cfg.getBool("email.disable", false);
|
||||
if (!mDisableEmail) {
|
||||
mEmailAccount.sender = cfg.getString("email.sender");
|
||||
mEmailAccount.admin_receiver = cfg.getString("email.admin_receiver");
|
||||
mEmailAccount.username = cfg.getString("email.username");
|
||||
mEmailAccount.password = cfg.getString("email.password");
|
||||
mEmailAccount.url = cfg.getString("email.smtp.url");
|
||||
mEmailAccount.port = cfg.getInt("email.smtp.port");
|
||||
}
|
||||
}
|
||||
catch (Poco::Exception& ex) {
|
||||
printf("email account not set in config: %s\n", ex.displayText().data());
|
||||
return false;
|
||||
}
|
||||
Thread::init("emails");
|
||||
mInitalized = true;
|
||||
|
||||
DISASM_FALSERET;
|
||||
ServerConfig::g_ServerKeySeed->put(3, DRRandom::r64());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void EmailManager::addEmail(model::Email* email) {
|
||||
if (mDisableEmail) {
|
||||
std::string log_message = "Email should be sended to: ";
|
||||
auto email_user = email->getUser();
|
||||
if (email_user && email_user->getModel()) {
|
||||
log_message += email_user->getModel()->getNameWithEmailHtml();
|
||||
}
|
||||
else {
|
||||
log_message += "<missing>";
|
||||
}
|
||||
log_message += ", type: ";
|
||||
log_message += model::Email::emailTypeString(email->getType());
|
||||
mEmailLog.log(log_message);
|
||||
delete email;
|
||||
return;
|
||||
}
|
||||
mPendingEmails.push(email);
|
||||
condSignal();
|
||||
}
|
||||
|
||||
void EmailManager::exit()
|
||||
{
|
||||
model::Email* email = nullptr;
|
||||
while (mPendingEmails.pop(email)) {
|
||||
delete email;
|
||||
}
|
||||
mInitalized = false;
|
||||
}
|
||||
|
||||
int EmailManager::ThreadFunction()
|
||||
{
|
||||
// prepare connection to email server
|
||||
if (ServerConfig::g_disableEmail) return 0;
|
||||
|
||||
if (mPendingEmails.empty()) return 0;
|
||||
|
||||
auto lm = LanguageManager::getInstance();
|
||||
|
||||
Poco::Net::SecureSMTPClientSession mailClientSession(mEmailAccount.url, mEmailAccount.port);
|
||||
mailClientSession.login();
|
||||
try {
|
||||
mailClientSession.startTLS(ServerConfig::g_SSL_CLient_Context);
|
||||
mailClientSession.login(Poco::Net::SMTPClientSession::AUTH_LOGIN, mEmailAccount.username, mEmailAccount.password);
|
||||
}
|
||||
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());
|
||||
return -1;
|
||||
}
|
||||
|
||||
model::Email* email = nullptr;
|
||||
Poco::AutoPtr<LanguageCatalog> catalogs[2];
|
||||
|
||||
// if email list empty, wait 500ms x time before exit thread and closing connection
|
||||
int timeoutWaits = 20;
|
||||
|
||||
while (mPendingEmails.pop(email) || timeoutWaits > 0) {
|
||||
if (email) {
|
||||
Poco::Net::MailMessage mailMessage;
|
||||
mailMessage.setSender(mEmailAccount.sender);
|
||||
Languages lang_code = ServerConfig::g_default_locale;
|
||||
if (email->getUser()) {
|
||||
Poco::AutoPtr<model::table::User> userModel = email->getUser()->getModel();
|
||||
|
||||
if (!userModel.isNull()) {
|
||||
userModel->lock("EmailManager::ThreadFunction");
|
||||
lang_code = LanguageManager::languageFromString(userModel->getLanguageKey());
|
||||
userModel->unlock();
|
||||
if (lang_code > LANG_COUNT) lang_code = ServerConfig::g_default_locale;
|
||||
}
|
||||
}
|
||||
if (catalogs[lang_code].isNull()) {
|
||||
catalogs[lang_code] = lm->getFreeCatalog(lang_code);
|
||||
}
|
||||
if (email->draft(&mailMessage, catalogs[lang_code])) {
|
||||
|
||||
mailClientSession.sendMessage(mailMessage);
|
||||
// add for debugging
|
||||
if (email->getUser()) {
|
||||
//printf("send email to %s\n", user_model->getEmail().data());
|
||||
auto user_model = email->getUser()->getModel();
|
||||
std::string log_message = "Email sended to: ";
|
||||
auto email_user = email->getUser();
|
||||
if (user_model) {
|
||||
log_message += email_user->getModel()->getNameWithEmailHtml();
|
||||
}
|
||||
else {
|
||||
log_message += "<missing>";
|
||||
}
|
||||
log_message += ", type: ";
|
||||
log_message += model::Email::emailTypeString(email->getType());
|
||||
mEmailLog.log(log_message);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// error drafting email, shouldn't happend
|
||||
printf("[EmailManager::ThreadFunction] Error drafting email\n");
|
||||
}
|
||||
delete email;
|
||||
email = nullptr;
|
||||
}
|
||||
if (mPendingEmails.empty()) {
|
||||
Poco::Thread::sleep(500);
|
||||
timeoutWaits--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
mailClientSession.close();
|
||||
|
||||
return 0;
|
||||
#include "EmailManager.h"
|
||||
#include "../ServerConfig.h"
|
||||
|
||||
#include "../Crypto/Obfus_array.h"
|
||||
#include "../Crypto/DRRandom.h"
|
||||
|
||||
#include "../SingletonManager/LanguageManager.h"
|
||||
|
||||
#include "Poco/Net/SecureSMTPClientSession.h"
|
||||
#include "Poco/Net/SSLException.h"
|
||||
|
||||
|
||||
EmailManager::EmailManager()
|
||||
: Thread("emails", false), mEmailLog(Poco::Logger::get("emailLog")), mInitalized(false), mDisableEmail(false)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
EmailManager::~EmailManager()
|
||||
{
|
||||
exit();
|
||||
|
||||
}
|
||||
|
||||
EmailManager* EmailManager::getInstance()
|
||||
{
|
||||
static EmailManager theOne;
|
||||
return &theOne;
|
||||
}
|
||||
|
||||
bool EmailManager::init(const Poco::Util::LayeredConfiguration& cfg)
|
||||
{
|
||||
try {
|
||||
mDisableEmail = cfg.getBool("email.disable", false);
|
||||
if (!mDisableEmail) {
|
||||
mEmailAccount.sender = cfg.getString("email.sender");
|
||||
mEmailAccount.admin_receiver = cfg.getString("email.admin_receiver");
|
||||
mEmailAccount.username = cfg.getString("email.username");
|
||||
mEmailAccount.password = cfg.getString("email.password");
|
||||
mEmailAccount.url = cfg.getString("email.smtp.url");
|
||||
mEmailAccount.port = cfg.getInt("email.smtp.port");
|
||||
}
|
||||
}
|
||||
catch (Poco::Exception& ex) {
|
||||
printf("email account not set in config: %s\n", ex.displayText().data());
|
||||
return false;
|
||||
}
|
||||
Thread::init("emails");
|
||||
mInitalized = true;
|
||||
|
||||
DISASM_FALSERET;
|
||||
ServerConfig::g_ServerKeySeed->put(3, DRRandom::r64());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void EmailManager::addEmail(model::Email* email) {
|
||||
if (mDisableEmail) {
|
||||
std::string log_message = "Email should be sended to: ";
|
||||
auto email_user = email->getUser();
|
||||
Poco::AutoPtr<model::table::User> email_model;
|
||||
if (email_user) {
|
||||
email_model = email_user->getModel();
|
||||
log_message += email_model->getNameWithEmailHtml();
|
||||
}
|
||||
if (email_model.isNull()) {
|
||||
log_message += "<missing>";
|
||||
}
|
||||
log_message += ", type: ";
|
||||
log_message += model::Email::emailTypeString(email->getType());
|
||||
mEmailLog.log(log_message);
|
||||
|
||||
delete email;
|
||||
return;
|
||||
}
|
||||
mPendingEmails.push(email);
|
||||
condSignal();
|
||||
}
|
||||
|
||||
void EmailManager::exit()
|
||||
{
|
||||
model::Email* email = nullptr;
|
||||
while (mPendingEmails.pop(email)) {
|
||||
delete email;
|
||||
}
|
||||
mInitalized = false;
|
||||
}
|
||||
|
||||
int EmailManager::ThreadFunction()
|
||||
{
|
||||
// prepare connection to email server
|
||||
if (ServerConfig::g_disableEmail) return 0;
|
||||
|
||||
if (mPendingEmails.empty()) return 0;
|
||||
|
||||
auto lm = LanguageManager::getInstance();
|
||||
|
||||
Poco::Net::SecureSMTPClientSession mailClientSession(mEmailAccount.url, mEmailAccount.port);
|
||||
mailClientSession.login();
|
||||
try {
|
||||
mailClientSession.startTLS(ServerConfig::g_SSL_CLient_Context);
|
||||
mailClientSession.login(Poco::Net::SMTPClientSession::AUTH_LOGIN, mEmailAccount.username, mEmailAccount.password);
|
||||
}
|
||||
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());
|
||||
return -1;
|
||||
}
|
||||
|
||||
model::Email* email = nullptr;
|
||||
Poco::AutoPtr<LanguageCatalog> catalogs[2];
|
||||
|
||||
// if email list empty, wait 500ms x time before exit thread and closing connection
|
||||
int timeoutWaits = 20;
|
||||
|
||||
while (mPendingEmails.pop(email) || timeoutWaits > 0) {
|
||||
if (email) {
|
||||
Poco::Net::MailMessage mailMessage;
|
||||
mailMessage.setSender(mEmailAccount.sender);
|
||||
Languages lang_code = ServerConfig::g_default_locale;
|
||||
auto email_user = email->getUser();
|
||||
if (email_user) {
|
||||
Poco::AutoPtr<model::table::User> userModel = email_user->getModel();
|
||||
|
||||
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 (email->draft(&mailMessage, catalogs[lang_code])) {
|
||||
|
||||
mailClientSession.sendMessage(mailMessage);
|
||||
// add for debugging
|
||||
if (email_user) {
|
||||
//printf("send email to %s\n", user_model->getEmail().data());
|
||||
auto user_model = email_user->getModel();
|
||||
std::string log_message = "Email sended to: ";
|
||||
if (user_model) {
|
||||
log_message += email_user->getModel()->getNameWithEmailHtml();
|
||||
}
|
||||
else {
|
||||
log_message += "<missing>";
|
||||
}
|
||||
log_message += ", type: ";
|
||||
log_message += model::Email::emailTypeString(email->getType());
|
||||
mEmailLog.log(log_message);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// error drafting email, shouldn't happend
|
||||
printf("[EmailManager::ThreadFunction] Error drafting email\n");
|
||||
}
|
||||
delete email;
|
||||
email = nullptr;
|
||||
}
|
||||
if (mPendingEmails.empty()) {
|
||||
Poco::Thread::sleep(500);
|
||||
timeoutWaits--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
mailClientSession.close();
|
||||
|
||||
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)
|
||||
{
|
||||
Profiler usedTime;
|
||||
|
||||
if (mNewUser->getModel()->getRole() != model::table::ROLE_ADMIN) {
|
||||
auto user_model = mNewUser->getModel();
|
||||
if (user_model->getRole() != model::table::ROLE_ADMIN) {
|
||||
addError(new Error(gettext("Benutzer"), gettext("Eingeloggter Benutzer ist kein Admin")), 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
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user