From 85b678a8fb744454639a65659850407346862240 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 30 Sep 2009 22:31:20 +0000 Subject: [PATCH] added: notifications to users' emails (core/notify.h core/notify.cpp) templatesnotify directory all notifications are managed by a second thread git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@512 e52654a7-88a9-db11-a3e9-0013d4bc506e --- Makefile | 5 +- content/Makefile.dep | 4 + content/content.h | 5 +- content/emacs.cpp | 13 +- content/mkdir.cpp | 3 + core/Makefile.dep | 11 +- core/Makefile.o.dep | 2 +- core/config.cpp | 1 + core/data.h | 4 +- core/db.cpp | 9 +- core/log.cpp | 18 +- core/log.h | 2 + core/main.cpp | 20 ++- core/misc.cpp | 44 +++++ core/misc.h | 2 + core/notify.cpp | 245 ++++++++++++++++++++++++++++ core/notify.h | 76 +++++++++ core/request.cpp | 21 +-- core/request.h | 10 +- core/requestcontroller.cpp | 2 + core/user.h | 11 +- templates/mount.cpp | 10 ++ templates/templates.cpp | 7 +- templates/templates.h | 11 +- templatesnotify/Makefile | 22 +++ templatesnotify/Makefile.dep | 6 + templatesnotify/Makefile.o.dep | 1 + templatesnotify/notify.cpp | 89 ++++++++++ templatesnotify/templatesnotify.cpp | 81 +++++++++ templatesnotify/templatesnotify.h | 71 ++++++++ 30 files changed, 769 insertions(+), 37 deletions(-) create mode 100755 core/notify.cpp create mode 100755 core/notify.h create mode 100755 templatesnotify/Makefile create mode 100755 templatesnotify/Makefile.dep create mode 100755 templatesnotify/Makefile.o.dep create mode 100755 templatesnotify/notify.cpp create mode 100755 templatesnotify/templatesnotify.cpp create mode 100755 templatesnotify/templatesnotify.h diff --git a/Makefile b/Makefile index ab79f5a..80dccf9 100755 --- a/Makefile +++ b/Makefile @@ -18,7 +18,8 @@ cmslu.a: FORCE @cd content ; $(MAKE) -e @cd confparser ; $(MAKE) -e @cd templates ; $(MAKE) -e - ar rcs cmslu.a core/*.o content/*.o templates/*.o confparser/*.o + @cd templatesnotify ; $(MAKE) -e + ar rcs cmslu.a core/*.o content/*.o templates/*.o templatesnotify/*.o confparser/*.o clean: @@ -26,6 +27,7 @@ clean: @cd content ; $(MAKE) -e clean @cd confparser ; $(MAKE) -e clean @cd templates ; $(MAKE) -e clean + @cd templatesnotify ; $(MAKE) -e clean rm -f cmslu.a @@ -37,3 +39,4 @@ depend: @cd content ; $(MAKE) -e depend @cd confparser ; $(MAKE) -e depend @cd templates ; $(MAKE) -e depend + @cd templatesnotify ; $(MAKE) -e depend diff --git a/content/Makefile.dep b/content/Makefile.dep index dcce1b6..219b54a 100755 --- a/content/Makefile.dep +++ b/content/Makefile.dep @@ -48,6 +48,8 @@ emacs.o: ../core/compress.h ../core/error.h ../core/db.h ../core/group.h emacs.o: ../core/dircontainer.h ../core/ugcontainer.h ../core/data.h emacs.o: ../core/dirs.h ../core/users.h ../core/groups.h ../core/functions.h emacs.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h +emacs.o: ../core/notify.h ../templatesnotify/templatesnotify.h +emacs.o: ../core/mount.h last.o: content.h ../core/item.h ../templates/templates.h ../../ezc/src/ezc.h last.o: ../templates/patterncacher.h ../app/templates.h ../core/thread.h login.o: content.h ../core/item.h ../templates/templates.h @@ -84,6 +86,8 @@ mkdir.o: ../core/compress.h ../core/error.h ../core/db.h ../core/group.h mkdir.o: ../core/dircontainer.h ../core/ugcontainer.h ../core/data.h mkdir.o: ../core/dirs.h ../core/users.h ../core/groups.h ../core/functions.h mkdir.o: ../core/lastcontainer.h ../core/mounts.h ../core/mount.h +mkdir.o: ../core/notify.h ../templatesnotify/templatesnotify.h +mkdir.o: ../core/mount.h node.o: content.h ../core/item.h ../templates/templates.h ../../ezc/src/ezc.h node.o: ../templates/patterncacher.h ../app/templates.h ../core/thread.h node.o: ../core/request.h ../core/requesttypes.h ../core/session.h diff --git a/content/content.h b/content/content.h index ecf28a5..9019f43 100755 --- a/content/content.h +++ b/content/content.h @@ -18,12 +18,13 @@ #include "../templates/templates.h" #include "../core/thread.h" + + class Content { Templates templates; - void PrepareUrl(Item & item); @@ -76,12 +77,14 @@ class Content void RedirectTo(const Item & item); void RedirectTo(long item_id); + public: bool Init(); void ReadAdditionalInfo(); void Make(); + }; diff --git a/content/emacs.cpp b/content/emacs.cpp index 7219a71..e722cae 100755 --- a/content/emacs.cpp +++ b/content/emacs.cpp @@ -12,7 +12,7 @@ #include "../core/error.h" #include "../core/db.h" #include "../core/data.h" - +#include "../core/notify.h" @@ -47,6 +47,11 @@ void Content::PostFunEmacsAdd() throw Error(Error::permision_denied); request.session->done_status = db.AddItem(request.item); + + if( request.session->done_status == Error::ok ) + { + request.notify_code |= CMSLU_NOTIFY_ITEM_ADD; + } } @@ -64,7 +69,11 @@ void Content::PostFunEmacsEdit(bool with_url) request.session->done_status = db.EditItemById(request.item, with_url); if( request.session->done_status == Error::ok ) + { TemplatesFunctions::pattern_cacher.UpdatePattern(request.item); + + request.notify_code |= CMSLU_NOTIFY_ITEM_EDIT; + } } @@ -119,6 +128,8 @@ bool adding = true; if( request.session->done_status == Error::ok ) { + request.session->item = request.item; + if( data.mounts.CurrentMountType() == Mount::thread ) { if( adding ) diff --git a/content/mkdir.cpp b/content/mkdir.cpp index cee61ec..930eaf4 100755 --- a/content/mkdir.cpp +++ b/content/mkdir.cpp @@ -12,6 +12,7 @@ #include "../core/error.h" #include "../core/db.h" #include "../core/data.h" +#include "../core/notify.h" @@ -70,6 +71,8 @@ void Content::PostFunMkdir(bool add_to_dir_table, int mask) request.session->item = item; request.session->done_timer = 2; + request.notify_code |= CMSLU_NOTIFY_DIR_ADD; + RedirectTo(item); } else diff --git a/core/Makefile.dep b/core/Makefile.dep index 29c2913..16e21e8 100755 --- a/core/Makefile.dep +++ b/core/Makefile.dep @@ -37,7 +37,8 @@ main.o: sessionmanager.h sessioncontainer.h session.h done.h item.h error.h main.o: log.h user.h functionparser.h requesttypes.h ../app/content.h data.h main.o: dirs.h dircontainer.h users.h ugcontainer.h groups.h group.h main.o: functions.h function.h lastcontainer.h mounts.h mount.h request.h -main.o: thread.h compress.h db.h config.h ../confparser/confparser.h +main.o: thread.h compress.h db.h config.h ../confparser/confparser.h notify.h +main.o: ../templatesnotify/templatesnotify.h ../core/mount.h misc.o: misc.h item.h log.h mount.o: mount.h mountparser.o: mountparser.h mount.h item.h error.h log.h data.h dirs.h @@ -47,6 +48,11 @@ mounts.o: mounts.h mount.h data.h dirs.h item.h dircontainer.h users.h user.h mounts.o: ugcontainer.h log.h groups.h group.h functions.h function.h mounts.o: lastcontainer.h request.h requesttypes.h session.h done.h error.h mounts.o: thread.h compress.h mountparser.h +notify.o: log.h notify.h ../templatesnotify/templatesnotify.h ../core/mount.h +notify.o: ../../ezc/src/ezc.h data.h dirs.h item.h dircontainer.h users.h +notify.o: user.h ugcontainer.h groups.h group.h functions.h function.h +notify.o: lastcontainer.h mounts.h mount.h misc.h request.h requesttypes.h +notify.o: session.h done.h error.h thread.h compress.h request.o: request.h requesttypes.h session.h done.h item.h error.h log.h request.o: user.h function.h thread.h compress.h getparser.h request.o: httpsimpleparser.h postparser.h cookieparser.h data.h dirs.h @@ -61,7 +67,8 @@ requestcontroller.o: functionparser.h requesttypes.h ../app/content.h data.h requestcontroller.o: dirs.h dircontainer.h users.h ugcontainer.h groups.h requestcontroller.o: group.h functions.h function.h lastcontainer.h mounts.h requestcontroller.o: mount.h request.h thread.h compress.h postparser.h -requestcontroller.o: httpsimpleparser.h cookieparser.h +requestcontroller.o: httpsimpleparser.h cookieparser.h notify.h +requestcontroller.o: ../templatesnotify/templatesnotify.h ../core/mount.h session.o: session.h done.h item.h error.h log.h user.h sessioncontainer.o: sessioncontainer.h session.h done.h item.h error.h log.h sessioncontainer.o: user.h data.h dirs.h dircontainer.h users.h ugcontainer.h diff --git a/core/Makefile.o.dep b/core/Makefile.o.dep index 13e698f..31fa5d0 100755 --- a/core/Makefile.o.dep +++ b/core/Makefile.o.dep @@ -1 +1 @@ -o = compress.o config.o data.o db.o db_itemcolumns.o dircontainer.o dirs.o done.o error.o function.o functioncodeparser.o functionparser.o functions.o groups.o httpsimpleparser.o lastcontainer.o log.o main.o misc.o mount.o mountparser.o mounts.o request.o requestcontroller.o session.o sessioncontainer.o sessionmanager.o users.o +o = compress.o config.o data.o db.o db_itemcolumns.o dircontainer.o dirs.o done.o error.o function.o functioncodeparser.o functionparser.o functions.o groups.o httpsimpleparser.o lastcontainer.o log.o main.o misc.o mount.o mountparser.o mounts.o notify.o request.o requestcontroller.o session.o sessioncontainer.o sessionmanager.o users.o diff --git a/core/config.cpp b/core/config.cpp index 35fd4f0..acc79e2 100755 --- a/core/config.cpp +++ b/core/config.cpp @@ -90,6 +90,7 @@ bool Config::ReadConfig(bool errors_to_stdout_) void Config::AssignValues() { data.log_file = Text("log_file"); + data.log_notify_file = Text("log_notify_file"); data.fcgi_socket = Text("fcgi_socket"); data.fcgi_socket_chmod = Int("fcgi_socket_chmod"); data.fcgi_socket_user = Text("fcgi_socket_user"); diff --git a/core/data.h b/core/data.h index 32ebb7a..1f4b334 100755 --- a/core/data.h +++ b/core/data.h @@ -34,8 +34,8 @@ public: // name of the config file (full path can be) std::string config_file; - // log file name - std::string log_file; + // log file name, log file name for notifications (sending emails, etc) + std::string log_file, log_notify_file; // 1 - minimum // 2 - (default) diff --git a/core/db.cpp b/core/db.cpp index 562233a..7df71c4 100755 --- a/core/db.cpp +++ b/core/db.cpp @@ -1227,7 +1227,7 @@ void Db::GetUsers(UGContainer & user_table) AssertConnection(); std::ostringstream query; - query << "select id, login, super_user, group_id from core.user left outer join core.group_mem on core.user.id = core.group_mem.user_id order by id asc;"; + query << "select id, login, super_user, group_id, email, cms_notify, thread_notify from core.user left outer join core.group_mem on core.user.id = core.group_mem.user_id order by id asc;"; r = AssertQuery( query.str() ); AssertResultStatus(r, PGRES_TUPLES_OK); @@ -1238,6 +1238,9 @@ void Db::GetUsers(UGContainer & user_table) int cname = AssertColumn(r, "login"); int csuper_user = AssertColumn(r, "super_user"); int cgroup_id = AssertColumn(r, "group_id"); + int cemail = AssertColumn(r, "email"); + int ccms_notify = AssertColumn(r, "cms_notify"); + int cthread_notify = AssertColumn(r, "thread_notify"); User u; long last_id = -1; @@ -1251,6 +1254,10 @@ void Db::GetUsers(UGContainer & user_table) { u.name = AssertValue(r, i, cname); u.super_user = static_cast( atoi( AssertValue(r, i, csuper_user) ) ); + u.email = AssertValue(r, i, cemail); + u.cms_notify = atoi( AssertValue(r, i, ccms_notify) ); + u.thread_notify = atoi( AssertValue(r, i, cthread_notify) ); + log << log1 << "Db: get user: id:" << u.id << ", name:" << u.name << ", super_user:" << u.super_user << logend; iter = user_table.PushBack( u ); diff --git a/core/log.cpp b/core/log.cpp index 80e4aec..a6ff5a2 100755 --- a/core/log.cpp +++ b/core/log.cpp @@ -8,7 +8,7 @@ */ #include "log.h" - +#include Log::Log() @@ -27,6 +27,22 @@ void Log::Init(int log_l, const std::string & log_f, bool log_std) +void Log::PutDate(Manipulators m) +{ + time_t t = std::time(0); + std::tm * loct = std::localtime(&t); + + char buffer[70]; + sprintf(buffer, "%d.%02d.%02d %02d:%02d:%02d ", int(loct->tm_year + 1900), + int(loct->tm_mon + 1), + int(loct->tm_mday), + int(loct->tm_hour), + int(loct->tm_min), + int(loct->tm_sec)); + + (*this) << m << buffer; +} + Log & Log::operator<<(const char * s) diff --git a/core/log.h b/core/log.h index ac4aeed..a8e6e13 100755 --- a/core/log.h +++ b/core/log.h @@ -36,6 +36,7 @@ public: void Init(int log_l, const std::string & log_f, bool log_std); + void PutDate(Manipulators m); Log & operator<<(const char * s); Log & operator<<(const std::string & s); Log & operator<<(int s); @@ -51,6 +52,7 @@ public: extern Log log; +extern Log nlog; diff --git a/core/main.cpp b/core/main.cpp index 3d84ba4..7883b48 100755 --- a/core/main.cpp +++ b/core/main.cpp @@ -18,16 +18,19 @@ #include "request.h" #include "db.h" #include "config.h" +#include "notify.h" // singletons // first 'data' then 'log' then 'request' Data data; Log log; +Log nlog; // notify log (used by another thread) Request request; Db db; Config config; RequestController req_controller; +Notify notify; @@ -87,6 +90,16 @@ int main(int argv, char ** argc) } + log.Init(data.log_level, data.log_file, data.log_stdout); + nlog.Init(data.log_level, data.log_notify_file, false); + db.Init(data.db_database, data.db_user, data.db_pass); + request.Init(); + + if( !req_controller.Init() ) + return 1; + + if( !notify.Init(data.templates) ) + return 2; signal(SIGTERM, signal_term); signal(SIGINT, signal_term); @@ -95,13 +108,6 @@ int main(int argv, char ** argc) while( true ) { - log.Init(data.log_level, data.log_file, data.log_stdout); - db.Init(data.db_database, data.db_user, data.db_pass); - request.Init(); - - if( !req_controller.Init() ) - return 1; - //log << log2 << "checking for table consistency:" << logend; // !! zrobic wyjatek dla root //db.CheckAllUrlSubject(); diff --git a/core/misc.cpp b/core/misc.cpp index 80e339d..7772af1 100755 --- a/core/misc.cpp +++ b/core/misc.cpp @@ -358,3 +358,47 @@ std::string::size_type i; s[i] = s[i] - 'A' + 'a'; } } + + + +bool ValidateEmail(const std::string & email) +{ + if( email.empty() ) + return false; + + bool correct = true; + size_t i; + char allowed_chars[] = "!#$%&'*+-/=?^_`{|}~.@"; + int at = 0; + + for(i=0 ; i= 'A' && email[i]<='Z') || + (email[i] >= 'a' && email[i]<='z') || + (email[i] >= '0' && email[i]<='9') ) + { + correct = true; + } + else + { + for(size_t a=0 ; a < sizeof(allowed_chars)-1 ; ++a) + { + if( email[i] == allowed_chars[a] ) + { + correct = true; + break; + } + } + } + + if( email[i] == '@' ) + ++at; + } + + if( at != 1 ) + return false; + +return correct; +} diff --git a/core/misc.h b/core/misc.h index fcd0c15..ea822df 100755 --- a/core/misc.h +++ b/core/misc.h @@ -43,4 +43,6 @@ bool IsWhite(int s); void TrimWhite(std::string & s); void ToSmall(std::string & s); +bool ValidateEmail(const std::string & email); + #endif diff --git a/core/notify.cpp b/core/notify.cpp new file mode 100755 index 0000000..e5a96e5 --- /dev/null +++ b/core/notify.cpp @@ -0,0 +1,245 @@ +/* + * 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(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(); +} + + diff --git a/core/notify.h b/core/notify.h new file mode 100755 index 0000000..4cb529c --- /dev/null +++ b/core/notify.h @@ -0,0 +1,76 @@ +/* + * 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. + * + */ + +#ifndef headerfilecmslunotify +#define headerfilecmslunotify + +#include +#include +#include +#include "../templatesnotify/templatesnotify.h" + + +#define CMSLU_NOTIFY_ITEM_ADD 1 +#define CMSLU_NOTIFY_ITEM_EDIT 2 +#define CMSLU_NOTIFY_ITEM_DELETE 4 +#define CMSLU_NOTIFY_DIR_ADD 8 + + + +class Notify +{ +public: + + Notify(); + ~Notify(); + + bool Init(const std::string & tdir); + + /* + this method added an item to our special pool + the pool is used by a second thread + */ + void ItemChanged(int notify_code); + + +private: + + + static void * ThreadRoutine(void * arg); + bool Lock(); + void Unlock(); + static void CheckQueue(); + static void SendEmail(NotifyMsg & n); + static void SendEmail(const std::string & email); + static void SendEmail(const std::string & email, const std::string & message); + static void SendMessage(FILE * sendmail, const std::string & message); + void CreateItemDir(std::string & dir, bool clear = true); + void CreateItemLink(std::string & link); + + pthread_t thread; + pthread_mutex_t mutex; + NotifyMsg n; + + + /* + objects accessed by the second thread + */ + std::list notify_pool; + static Notify * obj; + std::string command; + std::string templates_dir; + TemplatesNotify templates_notify; + +}; + + +extern Notify notify; + + +#endif diff --git a/core/request.cpp b/core/request.cpp index 7282871..b6e332c 100755 --- a/core/request.cpp +++ b/core/request.cpp @@ -44,6 +44,7 @@ void Request::Clear() headers.str(""); page.str(""); debug.str(""); + notify.str(""); env_request_method = &char_empty; env_request_uri = &char_empty; @@ -75,6 +76,9 @@ void Request::Clear() thread.Clear(); thread_tab.clear(); + + notify_code = 0; + } @@ -283,18 +287,8 @@ void Request::ReadParameters() void Request::StandardLog() { - time_t t = std::time(0); - std::tm * loct = std::localtime(&t); - - char buffer[70]; - sprintf(buffer, "%d.%02d.%02d %02d:%02d:%02d ", int(loct->tm_year + 1900), - int(loct->tm_mon + 1), - int(loct->tm_mday), - int(loct->tm_hour), - int(loct->tm_min), - int(loct->tm_sec)); - - log << log1 << buffer << env_remote_addr << ' ' << env_request_method << ' ' << env_request_uri << ' ' << env_http_user_agent << logend; + log.PutDate(log1); + log << env_remote_addr << ' ' << env_request_method << ' ' << env_request_uri << ' ' << env_http_user_agent << logend; } @@ -357,6 +351,9 @@ void Request::SendAll() + + + bool Request::IsParam(const char * s) { std::vector::iterator i; diff --git a/core/request.h b/core/request.h index 9217830..3a74d84 100755 --- a/core/request.h +++ b/core/request.h @@ -13,7 +13,6 @@ #include #include #include -#include #include #include "requesttypes.h" @@ -43,7 +42,8 @@ struct Request // headers, page and debug - std::ostringstream headers, page, debug; + // notify (for mailing) + std::ostringstream headers, page, debug, notify; GetTable get_table; @@ -94,6 +94,10 @@ struct Request Error status; + // last notify + int notify_code; + + // ------------------ // !! stare skladowe @@ -150,7 +154,7 @@ struct Request void ReadParameters(); void Read(); void SendAll(); - + void SendNotify(); bool CanChangeUser(const Item & item, long new_user_id); bool CanChangeGroup(const Item & item, long new_group_id); diff --git a/core/requestcontroller.cpp b/core/requestcontroller.cpp index d471dcd..40088b8 100755 --- a/core/requestcontroller.cpp +++ b/core/requestcontroller.cpp @@ -13,6 +13,7 @@ #include "request.h" #include "postparser.h" #include "cookieparser.h" +#include "notify.h" @@ -165,6 +166,7 @@ void RequestController::Loop() } request.SendAll(); + notify.ItemChanged(request.notify_code); } catch(const std::exception & e) { diff --git a/core/user.h b/core/user.h index c2ecf4f..cda4bf6 100755 --- a/core/user.h +++ b/core/user.h @@ -14,13 +14,18 @@ #include - struct User { long id; std::string name; bool super_user; std::vector groups; + std::string email; + + int cms_notify; + int thread_notify; + + User() { @@ -34,6 +39,9 @@ struct User name.clear(); super_user = false; groups.clear(); + email.clear(); + cms_notify = 0; + thread_notify = 0; } @@ -48,7 +56,6 @@ struct User return false; } - }; diff --git a/templates/mount.cpp b/templates/mount.cpp index 9071bc8..bc0374d 100755 --- a/templates/mount.cpp +++ b/templates/mount.cpp @@ -41,6 +41,16 @@ void mount_param_thread_with_info(Info & i) } +void mount_is_thread(Info & i) +{ + i.result = data.mounts.CurrentMountType() == Mount::thread; +} + +void mount_is_cms(Info & i) +{ + i.result = data.mounts.CurrentMountType() == Mount::cms; +} + } // namespace TemplatesFunctions diff --git a/templates/templates.cpp b/templates/templates.cpp index 05a3e73..a2af748 100755 --- a/templates/templates.cpp +++ b/templates/templates.cpp @@ -37,6 +37,7 @@ Ezc::Pattern pat_item_info; Ezc::Pattern pat_item_tab_info; Ezc::Pattern pat_dir_last_info; + Ezc::Functions functions; @@ -379,7 +380,8 @@ void Templates::CreateFunctions() functions.Insert("mount_param_withinfo", mount_param_withinfo); functions.Insert("mount_param_thread_with_header", mount_param_thread_with_header); functions.Insert("mount_param_thread_with_info", mount_param_thread_with_info); - + functions.Insert("mount_is_thread", mount_is_thread); + functions.Insert("mount_is_cms", mount_is_cms); // !! tymczasowa @@ -459,6 +461,7 @@ void Templates::Read() pat_dir_last_info.Directory(data.templates); pat_dir_last_info.ParseFile("dir_last_info.html"); + CreateFunctions(); #ifdef APPTEMPLATES @@ -478,3 +481,5 @@ void Templates::Generate() generator.Generate(); } + + diff --git a/templates/templates.h b/templates/templates.h index 5c9e39b..73e3eb8 100755 --- a/templates/templates.h +++ b/templates/templates.h @@ -33,6 +33,7 @@ namespace TemplatesFunctions extern Ezc::Pattern pat_item_info; extern Ezc::Pattern pat_item_tab_info; extern Ezc::Pattern pat_dir_last_info; + /* @@ -66,7 +67,7 @@ namespace TemplatesFunctions void item_content_noescape(Info & i); void item_print_content(Info & i); void item_privileges(Info & i); - void item_dir(Info & i); // the same as full_dir() + void item_dir(Info & i); void item_url(Info & i); void item_link(Info & i); void item_can_read(Info & i); @@ -213,6 +214,7 @@ namespace TemplatesFunctions void thread_tab_last_item_user(Info & i); void thread_can_create(Info & i); + /* mount */ @@ -220,6 +222,9 @@ namespace TemplatesFunctions void mount_param_withinfo(Info & i); void mount_param_thread_with_header(Info & i); void mount_param_thread_with_info(Info & i); + void mount_is_thread(Info & i); + void mount_is_cms(Info & i); + } // namespace TemplatesFunctions @@ -236,8 +241,10 @@ public: void Read(); void Generate(); - void CreateFunctions(); + +private: + void CreateFunctions(); }; diff --git a/templatesnotify/Makefile b/templatesnotify/Makefile new file mode 100755 index 0000000..d3c825d --- /dev/null +++ b/templatesnotify/Makefile @@ -0,0 +1,22 @@ +include Makefile.o.dep + + +all: $(o) + + +.SUFFIXES: .cpp .o + +.cpp.o: + $(CXX) -c $(CXXFLAGS) $< + + + +depend: + makedepend $(DEPMACROS) -Y. -f- *.cpp > Makefile.dep + echo -n "o = " > Makefile.o.dep + ls -1 *.cpp | xargs -I foo echo -n foo " " | sed -E "s/([^\.]*)\.cpp[ ]/\1\.o/g" >> Makefile.o.dep + +clean: + rm -f *.o + +include Makefile.dep diff --git a/templatesnotify/Makefile.dep b/templatesnotify/Makefile.dep new file mode 100755 index 0000000..412fa44 --- /dev/null +++ b/templatesnotify/Makefile.dep @@ -0,0 +1,6 @@ +# DO NOT DELETE + +notify.o: templatesnotify.h ../core/mount.h ../../ezc/src/ezc.h +notify.o: ../core/notify.h ../templatesnotify/templatesnotify.h +templatesnotify.o: templatesnotify.h ../core/mount.h ../../ezc/src/ezc.h +templatesnotify.o: ../core/misc.h ../core/item.h diff --git a/templatesnotify/Makefile.o.dep b/templatesnotify/Makefile.o.dep new file mode 100755 index 0000000..5a89fd0 --- /dev/null +++ b/templatesnotify/Makefile.o.dep @@ -0,0 +1 @@ +o = notify.o templatesnotify.o diff --git a/templatesnotify/notify.cpp b/templatesnotify/notify.cpp new file mode 100755 index 0000000..9ae1067 --- /dev/null +++ b/templatesnotify/notify.cpp @@ -0,0 +1,89 @@ +/* + * 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 "templatesnotify.h" +#include "../core/notify.h" + + +namespace TemplatesNotifyFunctions +{ + + + +void notify_item_added(Info & i) +{ + i.result = (notify_msg->notify_code & CMSLU_NOTIFY_ITEM_ADD) != 0; +} + + + +void notify_item_edited(Info & i) +{ + i.result = (notify_msg->notify_code & CMSLU_NOTIFY_ITEM_EDIT) != 0; +} + + + +void notify_item_deleted(Info & i) +{ + i.result = (notify_msg->notify_code & CMSLU_NOTIFY_ITEM_DELETE) != 0; +} + + + +void notify_dir_added(Info & i) +{ + i.result = (notify_msg->notify_code & CMSLU_NOTIFY_DIR_ADD) != 0; +} + + + +void notify_to(Info & i) +{ + i.out << notify_msg->email; +} + + +void notify_mount_is_thread(Info & i) +{ + i.result = notify_msg->current_mount_type == Mount::thread; +} + + +void notify_mount_is_cms(Info & i) +{ + i.result = notify_msg->current_mount_type == Mount::cms; +} + + +void notify_doc_base_url(Info & i) +{ + i.out << notify_msg->doc_base_url; +} + + +void notify_item_dir(Info & i) +{ + i.out << notify_msg->item_dir; +} + + + +void notify_item_link(Info & i) +{ + i.out << notify_msg->item_link; +} + + + + +} // namespace TemplatesNotifyFunctions + + + diff --git a/templatesnotify/templatesnotify.cpp b/templatesnotify/templatesnotify.cpp new file mode 100755 index 0000000..e2004b0 --- /dev/null +++ b/templatesnotify/templatesnotify.cpp @@ -0,0 +1,81 @@ +/* + * 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 "templatesnotify.h" +#include "../core/misc.h" + + + +namespace TemplatesNotifyFunctions +{ +Ezc::Pattern pat_notify_email; +Ezc::Functions functions; + + +// you can use this pointer in template functions (will be always valid) +NotifyMsg * notify_msg; + + + + + +} // namespace TemplatesNotifyFunctions + + + + +void TemplatesNotify::CreateFunctions() +{ +using namespace TemplatesNotifyFunctions; + + functions.Clear(); + + functions.Insert("notify_item_added", notify_item_added); + functions.Insert("notify_item_edited", notify_item_edited); + functions.Insert("notify_item_deleted", notify_item_deleted); + functions.Insert("notify_dir_added", notify_dir_added); + functions.Insert("notify_to", notify_to); + functions.Insert("notify_mount_is_thread", notify_mount_is_thread); + functions.Insert("notify_mount_is_cms", notify_mount_is_cms); + functions.Insert("notify_doc_base_url", notify_doc_base_url); + functions.Insert("notify_item_dir", notify_item_dir); + functions.Insert("notify_item_link", notify_item_link); +} + + + + + +void TemplatesNotify::Read(const std::string & templates_dir) +{ +using namespace TemplatesNotifyFunctions; + + pat_notify_email.Directory(templates_dir); + pat_notify_email.ParseFile("notify_email.txt"); + + CreateFunctions(); + notify_msg = 0; +} + + + +void TemplatesNotify::Generate() +{ +using namespace TemplatesNotifyFunctions; + + notify_str.str(""); + + if( !notify_msg ) + return; + + Ezc::Generator generator(notify_str, pat_notify_email, functions); + generator.Generate(); +} + + diff --git a/templatesnotify/templatesnotify.h b/templatesnotify/templatesnotify.h new file mode 100755 index 0000000..28cc8df --- /dev/null +++ b/templatesnotify/templatesnotify.h @@ -0,0 +1,71 @@ +/* + * 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. + * + */ + +#ifndef headerfilecmslutemplatestemplatesnotify +#define headerfilecmslutemplatestemplatesnotify + +#include +#include +#include + +#include "../core/mount.h" +#include "../../ezc/src/ezc.h" + + +struct NotifyMsg +{ + std::string email; + int notify_code; + Mount::Type current_mount_type; + std::string doc_base_url; + std::string item_dir; + std::string item_link; +}; + + +namespace TemplatesNotifyFunctions +{ + using Ezc::Info; + + extern NotifyMsg * notify_msg; + + void notify_item_added(Info & i); + void notify_item_edited(Info & i); + void notify_item_deleted(Info & i); + void notify_dir_added(Info & i); + void notify_to(Info & i); + void notify_mount_is_thread(Info & i); + void notify_mount_is_cms(Info & i); + void notify_doc_base_url(Info & i); + void notify_item_dir(Info & i); + void notify_item_link(Info & i); + +} // namespace TemplatesNotifyFunctions + + + + +class TemplatesNotify +{ +public: + + void Read(const std::string & templates_dir); + void Generate(); + + std::ostringstream notify_str; + +private: + + void CreateFunctions(); + +}; + + + +#endif