moved: templatesnotify -> notify
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@686 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
246
notify/notifythread.cpp
Executable file
246
notify/notifythread.cpp
Executable file
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008-2010, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "notifythread.h"
|
||||
|
||||
|
||||
// first thread (objects are locked)
|
||||
NotifyThread::NotifyThread()
|
||||
{
|
||||
patterns_changed = true;
|
||||
}
|
||||
|
||||
|
||||
// first thread (objects are locked)
|
||||
void NotifyThread::SetConfig(Config * pconfig)
|
||||
{
|
||||
config = pconfig;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// first thread (objects are locked)
|
||||
void NotifyThread::SetUsers(Users * pusers)
|
||||
{
|
||||
users = pusers;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// first thread (objects are locked)
|
||||
void NotifyThread::SetNotifyPool(NotifyPool * pool)
|
||||
{
|
||||
notify_pool = pool;
|
||||
}
|
||||
|
||||
|
||||
// first thread (objects are locked)
|
||||
void NotifyThread::SetPatterns(TemplatesFunctions::Patterns * pat)
|
||||
{
|
||||
pat_global = pat;
|
||||
}
|
||||
|
||||
|
||||
// first thread (objects are locked)
|
||||
void NotifyThread::PatternsChanged()
|
||||
{
|
||||
patterns_changed = true;
|
||||
}
|
||||
|
||||
|
||||
// first thread (objects are locked)
|
||||
bool NotifyThread::Init()
|
||||
{
|
||||
TemplatesNotifyFunctions::CreateFunctions();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// first thread (objects are locked)
|
||||
bool NotifyThread::SignalReceived()
|
||||
{
|
||||
if( patterns_changed )
|
||||
{
|
||||
// !! usunac
|
||||
log << log3 << "NotifyThread: copying global patterns to local ones" << logend;
|
||||
patterns = *pat_global;
|
||||
patterns_changed = false;
|
||||
}
|
||||
|
||||
return !notify_pool->Empty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// second thread (objects are not locked)
|
||||
// return true if there is something to send from the first queue (notify_pool)
|
||||
bool NotifyThread::AddNextNotify()
|
||||
{
|
||||
Users::Iterator i;
|
||||
bool res = false;
|
||||
|
||||
Lock();
|
||||
|
||||
if( !notify_pool->Empty() )
|
||||
{
|
||||
TemplatesNotifyFunctions::notify_msg = notify_pool->GetFirst();
|
||||
notify_pool->DeleteFirst();
|
||||
|
||||
for(i=users->Begin() ; i != users->End() ; ++i)
|
||||
{
|
||||
if( (i->cms_notify & TemplatesNotifyFunctions::notify_msg.code) != 0 ) // !! bedzie jedno pole i->notify
|
||||
{
|
||||
msg.name = i->name;
|
||||
msg.email = i->email;
|
||||
msg.lang = Locale::StrToLang(config->locale_str); // !! bedzie osobno dla kazdego uzytkownika
|
||||
|
||||
if( msg.lang == Locale::lang_unknown )
|
||||
msg.lang = Locale::lang_en;
|
||||
|
||||
notify_user.insert(notify_user.end(), msg);
|
||||
res = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Unlock();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread -- here we must use Lock() and Unlock() explicitly
|
||||
void NotifyThread::Do()
|
||||
{
|
||||
NotifyUser::iterator i;
|
||||
bool queue_end;
|
||||
|
||||
while( AddNextNotify() )
|
||||
{
|
||||
Lock();
|
||||
i = notify_user.begin();
|
||||
queue_end = notify_user.empty();
|
||||
Unlock();
|
||||
|
||||
while( !queue_end )
|
||||
{
|
||||
Lock();
|
||||
TemplatesNotifyFunctions::notify_user_msg = *i;
|
||||
log << log3 << "NotifyThread: sending notification to: " << i->name << ", email: " << i->email << logend;
|
||||
Unlock();
|
||||
|
||||
SendMail();
|
||||
|
||||
Lock();
|
||||
WaitForSignalSleep(2); // automatically unlock and lock again when returns
|
||||
bool stop = synchro->was_stop_signal;
|
||||
Unlock();
|
||||
|
||||
if( stop )
|
||||
return;
|
||||
|
||||
Lock();
|
||||
notify_user.erase(i++);
|
||||
queue_end = (i == notify_user.end());
|
||||
Unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// second thread
|
||||
void NotifyThread::SendMail()
|
||||
{
|
||||
notify_stream.Clear();
|
||||
|
||||
size_t lang_index = static_cast<size_t>(TemplatesNotifyFunctions::notify_user_msg.lang);
|
||||
size_t template_index = TemplatesNotifyFunctions::notify_msg.template_index;
|
||||
|
||||
if( lang_index >= patterns.size() ||
|
||||
template_index >= patterns[lang_index].size() )
|
||||
{
|
||||
// ops, something wrong
|
||||
return;
|
||||
}
|
||||
|
||||
generator.RecognizeSpecialChars(true);
|
||||
generator.TrimWhite(true);
|
||||
generator.SkipNewLine(true);
|
||||
generator.Generate(notify_stream, patterns[lang_index][template_index],
|
||||
TemplatesNotifyFunctions::ezc_functions);
|
||||
|
||||
SendMail(TemplatesNotifyFunctions::notify_user_msg.email, notify_stream.Str());
|
||||
}
|
||||
|
||||
|
||||
// second thread
|
||||
void NotifyThread::SendMail(const std::wstring & email, const std::wstring & message)
|
||||
{
|
||||
nlog.PutDate(log1);
|
||||
|
||||
if( !ValidateEmail(email) )
|
||||
{
|
||||
nlog << log1 << "NotifyThread: email adress: " << email << " is not correct" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
sendmail_command = "sendmail ";
|
||||
Ezc::WideToUTF8(email, sendmail_command, false);
|
||||
FILE * sendmail = popen(sendmail_command.c_str(), "w");
|
||||
|
||||
if( !sendmail )
|
||||
{
|
||||
nlog << log1 << "NotifyThread: can't run sendmail" << logend;
|
||||
return;
|
||||
}
|
||||
|
||||
SendMail(sendmail, message);
|
||||
pclose(sendmail);
|
||||
|
||||
nlog << log1 << "NotifyThread: email to: " << email << " has been sent" << logend;
|
||||
nlog << logsave;
|
||||
}
|
||||
|
||||
|
||||
// second thread
|
||||
void NotifyThread::SendMail(FILE * sendmail, const std::wstring & message)
|
||||
{
|
||||
char buf[10];
|
||||
size_t len;
|
||||
|
||||
for(size_t i=0 ; i<message.length() ; ++i)
|
||||
{
|
||||
if( message[i] == '\n' )
|
||||
{
|
||||
fprintf(sendmail, "\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
len = Ezc::IntToUTF8(int(message[i]), buf, sizeof(buf) / sizeof(char));
|
||||
|
||||
for(size_t a=0 ; a<len ; ++a)
|
||||
fputc(buf[a], sendmail);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user