/* * This file is a part of CMSLU -- Content Management System like Unix * and is not publicly distributed * * Copyright (c) 2008-2009, Tomasz Sowa * All rights reserved. * */ #include #include #include "log.h" #include "notify.h" #include "data.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; /* methods for second thread second thread can reference to 'this' by using 'obj' pointer */ void * Notify::ThreadRoutine(void * arg) { obj = (Notify*)arg; obj->templates_notify.Read(obj->templates_dir); while( true ) { CheckQueue(); sleep(1); // !! was 30 } } void Notify::CheckQueue() { std::list::iterator i = obj->notify_pool.begin(); while( i != obj->notify_pool.end() ) { SendEmail(*i); obj->Lock(); i = obj->notify_pool.erase(i); obj->Unlock(); //sleep(3); // !! } } void Notify::SendEmail(NotifyMsg & n) { TemplatesNotifyFunctions::notify_msg = &n; obj->templates_notify.Generate(); SendEmail(n.email, obj->templates_notify.notify_str.str()); } void Notify::SendEmail(const std::string & email, const std::string & message) { nlog.PutDate(log1); if( !ValidateEmail(email) ) { nlog << "Notify: email: " << email << " is not correct" << logend; return; } obj->command = "sendmail " + email; FILE * sendmail = popen(obj->command.c_str(), "w"); if( !sendmail ) { nlog << "Notify: can't run sendmail" << logend; return; } SendMessage(sendmail, message); pclose(sendmail); nlog << "Notify: email to: " << email << " has been sent" << logend; } void Notify::SendMessage(FILE * sendmail, const std::string & message) { for(size_t i=0 ; iurl; dir += '/'; } } void Notify::CreateItemLink(std::string & link) { link = data.base_url; CreateItemDir(link, false); link += request.item.url; } void Notify::ItemChanged(int notify_code) { bool sending; Users::Iterator i; if( notify_code == 0 ) return; n.notify_code = notify_code; n.current_mount_type = data.mounts.CurrentMountType(); n.doc_base_url = data.base_url; 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 for(i=data.users.Begin() ; i != data.users.End() ; ++i) { sending = false; if( data.mounts.CurrentMountType() == Mount::thread ) { if( (i->thread_notify & notify_code) != 0 ) sending = true; } else if( data.mounts.CurrentMountType() == Mount::cms ) { if( (i->cms_notify & notify_code) != 0 ) sending = true; } if( sending ) { n.email = i->email; notify_pool.insert(notify_pool.end(), n); } } } catch(...) { Unlock(); throw; } Unlock(); }