2009-10-01 00:31:20 +02:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2009-10-01 00:31:20 +02:00
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2010-02-15 01:31:14 +01:00
|
|
|
* Copyright (c) 2008-2010, Tomasz Sowa
|
2009-10-01 00:31:20 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "notify.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "request.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the second thread uses this pointer to reference to 'this'
|
|
|
|
// (methods for the thread should be static)
|
|
|
|
Notify * Notify::obj;
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
void Notify::SetRequest(Request * prequest)
|
|
|
|
{
|
|
|
|
request = prequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::SetConfig(Config * pconfig)
|
|
|
|
{
|
|
|
|
config = pconfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::SetSystem(System * psystem)
|
|
|
|
{
|
|
|
|
system = psystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notify::SetTemplatesNotify(TemplatesNotify * ptemplates_notify)
|
|
|
|
{
|
|
|
|
templates_notify = ptemplates_notify;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-01 00:31:20 +02:00
|
|
|
|
|
|
|
/*
|
2010-01-28 16:39:01 +01:00
|
|
|
methods for the second thread
|
2009-10-01 00:31:20 +02:00
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
the thread can reference to 'this' by using 'obj' pointer
|
2009-10-01 00:31:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
void * Notify::ThreadRoutine(void * arg)
|
|
|
|
{
|
|
|
|
obj = (Notify*)arg;
|
2010-01-28 16:39:01 +01:00
|
|
|
|
2009-10-01 00:31:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
CheckQueue();
|
2009-12-09 01:52:00 +01:00
|
|
|
sleep(30);
|
2009-10-01 00:31:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::CheckQueue()
|
|
|
|
{
|
|
|
|
std::list<NotifyMsg>::iterator i = obj->notify_pool.begin();
|
|
|
|
|
|
|
|
|
|
|
|
while( i != obj->notify_pool.end() )
|
|
|
|
{
|
|
|
|
SendEmail(*i);
|
|
|
|
|
|
|
|
obj->Lock();
|
|
|
|
i = obj->notify_pool.erase(i);
|
|
|
|
obj->Unlock();
|
|
|
|
|
2009-12-09 01:52:00 +01:00
|
|
|
sleep(3);
|
2009-10-01 00:31:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::SendEmail(NotifyMsg & n)
|
|
|
|
{
|
|
|
|
TemplatesNotifyFunctions::notify_msg = &n;
|
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
obj->Lock(); // templates can be reloaded from the first thread
|
2010-08-10 18:12:50 +02:00
|
|
|
obj->templates_notify->Generate(n.lang);
|
2010-01-28 16:39:01 +01:00
|
|
|
obj->Unlock();
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
SendEmail(n.email, obj->templates_notify->notify_str.str());
|
2009-10-01 00:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::SendEmail(const std::string & email, const std::string & message)
|
|
|
|
{
|
|
|
|
nlog.PutDate(log1);
|
|
|
|
|
|
|
|
if( !ValidateEmail(email) )
|
|
|
|
{
|
2010-09-08 01:54:01 +02:00
|
|
|
nlog << log1 << "Notify: email: " << email << " is not correct" << logend;
|
2009-10-01 00:31:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj->command = "sendmail " + email;
|
|
|
|
FILE * sendmail = popen(obj->command.c_str(), "w");
|
|
|
|
|
|
|
|
if( !sendmail )
|
|
|
|
{
|
2010-09-08 01:54:01 +02:00
|
|
|
nlog << log1 << "Notify: can't run sendmail" << logend;
|
2009-10-01 00:31:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SendMessage(sendmail, message);
|
|
|
|
pclose(sendmail);
|
|
|
|
|
2010-09-08 01:54:01 +02:00
|
|
|
nlog << log1 << "Notify: email to: " << email << " has been sent" << logend;
|
2010-01-28 22:09:48 +01:00
|
|
|
nlog << logsave;
|
2009-10-01 00:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::SendMessage(FILE * sendmail, const std::string & message)
|
|
|
|
{
|
|
|
|
for(size_t i=0 ; i<message.length() ; ++i)
|
|
|
|
{
|
|
|
|
if( message[i] == '\n' )
|
|
|
|
fprintf(sendmail, "\r\n");
|
|
|
|
else
|
|
|
|
fputc(message[i], sendmail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
methods for main thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Notify::Notify() : mutex(PTHREAD_MUTEX_INITIALIZER)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Notify::~Notify()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
void Notify::ReadTemplates()
|
|
|
|
{
|
2010-02-15 01:31:14 +01:00
|
|
|
// we are in the first thread here
|
|
|
|
// the second thread can use our templates and we must Lock()
|
|
|
|
// (the second thread is using Lock() too)
|
|
|
|
Lock();
|
2010-08-10 18:12:50 +02:00
|
|
|
templates_notify->Read();
|
2010-01-28 16:39:01 +01:00
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-01 00:31:20 +02:00
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
bool Notify::Init()
|
2009-10-01 00:31:20 +02:00
|
|
|
{
|
2010-08-10 18:12:50 +02:00
|
|
|
templates_notify->CreateFunctions();
|
2010-01-28 16:39:01 +01:00
|
|
|
|
|
|
|
|
2009-10-01 00:31:20 +02:00
|
|
|
int t = pthread_create(&thread, 0, ThreadRoutine, (void*)this);
|
|
|
|
|
|
|
|
if( t != 0 )
|
|
|
|
{
|
|
|
|
// log (not nlog) here
|
|
|
|
log << log1 << "Notify: can't create a thread" << logend;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Notify::Lock()
|
|
|
|
{
|
|
|
|
if( pthread_mutex_lock(&mutex) != 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::Unlock()
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::CreateItemDir(std::string & dir, bool clear)
|
|
|
|
{
|
|
|
|
if( clear )
|
|
|
|
dir.clear();
|
|
|
|
|
2010-08-10 22:43:38 +02:00
|
|
|
for(size_t a=0 ; a<request->dir_tab.size() ; ++a)
|
2009-10-01 00:31:20 +02:00
|
|
|
{
|
2010-08-10 22:43:38 +02:00
|
|
|
dir += request->dir_tab[a]->url;
|
2009-10-01 00:31:20 +02:00
|
|
|
dir += '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::CreateItemLink(std::string & link)
|
|
|
|
{
|
2010-08-10 18:12:50 +02:00
|
|
|
link = config->base_url;
|
2009-10-01 00:31:20 +02:00
|
|
|
CreateItemDir(link, false);
|
2010-08-10 18:12:50 +02:00
|
|
|
link += request->item.url;
|
2009-10-01 00:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Notify::ItemChanged(int notify_code)
|
|
|
|
{
|
|
|
|
bool sending;
|
|
|
|
Users::Iterator i;
|
|
|
|
|
|
|
|
if( notify_code == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
n.notify_code = notify_code;
|
2010-08-10 18:12:50 +02:00
|
|
|
n.current_mount_type = system->mounts.pmount->type;
|
|
|
|
n.doc_base_url = config->base_url;
|
2009-10-01 00:31:20 +02:00
|
|
|
CreateItemDir(n.item_dir);
|
|
|
|
CreateItemLink(n.item_link);
|
|
|
|
|
|
|
|
|
|
|
|
Lock();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// don't clear notify_pool here -- it is used (and will be cleared) by the second thread
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
for(i=system->users.Begin() ; i != system->users.End() ; ++i)
|
2009-10-01 00:31:20 +02:00
|
|
|
{
|
|
|
|
sending = false;
|
|
|
|
|
2010-09-13 01:33:27 +02:00
|
|
|
if( system->mounts.pmount->type == system->mounts.MountTypeThread() ||
|
|
|
|
system->mounts.pmount->type == system->mounts.MountTypeTicket() )
|
2009-10-01 00:31:20 +02:00
|
|
|
{
|
|
|
|
if( (i->thread_notify & notify_code) != 0 )
|
|
|
|
sending = true;
|
|
|
|
}
|
|
|
|
else
|
2010-09-13 01:33:27 +02:00
|
|
|
if( system->mounts.pmount->type == system->mounts.MountTypeCms() )
|
2009-10-01 00:31:20 +02:00
|
|
|
{
|
|
|
|
if( (i->cms_notify & notify_code) != 0 )
|
|
|
|
sending = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( sending )
|
|
|
|
{
|
|
|
|
n.email = i->email;
|
2010-08-10 18:12:50 +02:00
|
|
|
n.lang = Locale::StrToLang(config->locale_str);// !! bedzie osobno dla kazdego uzytkownika
|
2010-02-15 01:31:14 +01:00
|
|
|
|
|
|
|
if( n.lang == Locale::lang_unknown )
|
|
|
|
n.lang = Locale::lang_en;
|
|
|
|
|
2009-10-01 00:31:20 +02:00
|
|
|
notify_pool.insert(notify_pool.end(), n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
Unlock();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|