]> git.stg.codes - stg.git/blob - projects/stargazer/users_impl.cpp
Process services on daily basis.
[stg.git] / projects / stargazer / users_impl.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 /*
26  $Revision: 1.61 $
27  $Date: 2010/09/13 05:56:42 $
28  $Author: faust $
29  */
30
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE
33 #endif
34
35 #include <pthread.h>
36
37 #include <csignal>
38 #include <cassert>
39 #include <algorithm>
40 #include <utility>
41 #include <string>
42 #include <vector>
43
44 #include "stg/settings.h"
45 #include "stg/common.h"
46 #include "users_impl.h"
47 #include "stg_timer.h"
48
49 extern volatile time_t stgTime;
50
51 //#define USERS_DEBUG 1
52
53 //-----------------------------------------------------------------------------
54 USERS_IMPL::USERS_IMPL(SETTINGS_IMPL * s, STORE * st, TARIFFS * t, const ADMIN * sa)
55     : USERS(),
56       users(),
57       usersToDelete(),
58       /*userIPNotifiersBefore(),
59       userIPNotifiersAfter(),*/
60       ipIndex(),
61       loginIndex(),
62       settings(s),
63       tariffs(t),
64       store(st),
65       sysAdmin(sa),
66       WriteServLog(GetStgLogger()),
67       nonstop(false),
68       isRunning(false),
69       mutex(),
70       thread(),
71       handle(0),
72       searchDescriptors(),
73       onAddNotifiers(),
74       onDelNotifiers(),
75       onAddNotifiersImpl(),
76       onDelNotifiersImpl()
77 {
78 pthread_mutexattr_t attr;
79 pthread_mutexattr_init(&attr);
80 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
81 pthread_mutex_init(&mutex, &attr);
82 }
83 //-----------------------------------------------------------------------------
84 USERS_IMPL::~USERS_IMPL()
85 {
86 pthread_mutex_destroy(&mutex);
87 }
88 //-----------------------------------------------------------------------------
89 int USERS_IMPL::FindByNameNonLock(const std::string & login, user_iter * user)
90 {
91 const std::map<std::string, user_iter>::const_iterator iter(loginIndex.find(login));
92 if (iter == loginIndex.end())
93     return -1;
94 if (user)
95     *user = iter->second;
96 return 0;
97 }
98 //-----------------------------------------------------------------------------
99 int USERS_IMPL::FindByNameNonLock(const std::string & login, const_user_iter * user) const
100 {
101 const std::map<std::string, user_iter>::const_iterator iter(loginIndex.find(login));
102 if (iter == loginIndex.end())
103     return -1;
104 if (user)
105     *user = iter->second;
106 return 0;
107 }
108 //-----------------------------------------------------------------------------
109 int USERS_IMPL::FindByName(const std::string & login, USER_PTR * user)
110 {
111 STG_LOCKER lock(&mutex);
112 user_iter u;
113 if (FindByNameNonLock(login, &u))
114     return -1;
115 *user = &(*u);
116 return 0;
117 }
118 //-----------------------------------------------------------------------------
119 int USERS_IMPL::FindByName(const std::string & login, CONST_USER_PTR * user) const
120 {
121 STG_LOCKER lock(&mutex);
122 const_user_iter u;
123 if (FindByNameNonLock(login, &u))
124     return -1;
125 *user = &(*u);
126 return 0;
127 }
128 //-----------------------------------------------------------------------------
129 bool USERS_IMPL::Exists(const std::string & login) const
130 {
131 STG_LOCKER lock(&mutex);
132 const std::map<std::string, user_iter>::const_iterator iter(loginIndex.find(login));
133 return iter != loginIndex.end();
134 }
135 //-----------------------------------------------------------------------------
136 bool USERS_IMPL::TariffInUse(const std::string & tariffName) const
137 {
138 STG_LOCKER lock(&mutex);
139 std::list<USER_IMPL>::const_iterator iter;
140 iter = users.begin();
141 while (iter != users.end())
142     {
143     if (iter->GetProperty().tariffName.Get() == tariffName)
144         return true;
145     ++iter;
146     }
147 return false;
148 }
149 //-----------------------------------------------------------------------------
150 int USERS_IMPL::Add(const std::string & login, const ADMIN * admin)
151 {
152 STG_LOCKER lock(&mutex);
153 const PRIV * priv = admin->GetPriv();
154
155 if (!priv->userAddDel)
156     {
157     WriteServLog("%s tried to add user \'%s\'. Access denied.",
158          admin->GetLogStr().c_str(), login.c_str());
159     /*errorStr = "Admin \'" + admin->GetLogin() +
160                "\': tried to add user \'" + ud->login + "\'. Access denied.";*/
161     return -1;
162     }
163
164 //////
165 if (store->AddUser(login))
166     {
167     //TODO
168     //WriteServLog("Admin \'%s\': tried to add user \'%s\'. Access denied.",
169     //     admin->GetLogin().c_str(), ud->login.c_str());
170     return -1;
171     }
172 //////
173
174 USER_IMPL u(settings, store, tariffs, sysAdmin, this);
175
176 /*struct tm * tms;
177 time_t t = stgTime;
178
179 tms = localtime(&t);
180
181 tms->tm_hour = 0;
182 tms->tm_min = 0;
183 tms->tm_sec = 0;
184
185 if (settings->GetDayResetTraff() > tms->tm_mday)
186     tms->tm_mon -= 1;
187
188 tms->tm_mday = settings->GetDayResetTraff();*/
189
190 u.SetLogin(login);
191
192 u.SetPassiveTimeAsNewUser();
193
194 u.WriteConf();
195 u.WriteStat();
196
197 WriteServLog("%s User \'%s\' added.",
198          admin->GetLogStr().c_str(), login.c_str());
199
200 u.OnAdd();
201
202 users.push_front(u);
203
204 AddUserIntoIndexes(users.begin());
205
206     {
207     // Fire all "on add" notifiers
208     std::set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onAddNotifiers.begin();
209     while (ni != onAddNotifiers.end())
210         {
211         (*ni)->Notify(&users.front());
212         ++ni;
213         }
214     }
215
216     {
217     // Fire all "on add" implementation notifiers
218     std::set<NOTIFIER_BASE<USER_IMPL_PTR> *>::iterator ni = onAddNotifiersImpl.begin();
219     while (ni != onAddNotifiersImpl.end())
220         {
221         (*ni)->Notify(&users.front());
222         ++ni;
223         }
224     }
225
226 return 0;
227 }
228 //-----------------------------------------------------------------------------
229 void USERS_IMPL::Del(const std::string & login, const ADMIN * admin)
230 {
231 const PRIV * priv = admin->GetPriv();
232 user_iter u;
233
234 if (!priv->userAddDel)
235     {
236     WriteServLog("%s tried to remove user \'%s\'. Access denied.",
237          admin->GetLogStr().c_str(), login.c_str());
238     return;
239     }
240
241
242     {
243     STG_LOCKER lock(&mutex);
244
245     if (FindByNameNonLock(login, &u))
246         {
247         WriteServLog("%s tried to delete user \'%s\': not found.",
248                      admin->GetLogStr().c_str(),
249                      login.c_str());
250         return;
251         }
252
253     u->SetDeleted();
254     }
255
256     {
257     std::set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onDelNotifiers.begin();
258     while (ni != onDelNotifiers.end())
259         {
260         (*ni)->Notify(&(*u));
261         ++ni;
262         }
263     }
264
265     {
266     std::set<NOTIFIER_BASE<USER_IMPL_PTR> *>::iterator ni = onDelNotifiersImpl.begin();
267     while (ni != onDelNotifiersImpl.end())
268         {
269         (*ni)->Notify(&(*u));
270         ++ni;
271         }
272     }
273
274     {
275     STG_LOCKER lock(&mutex);
276
277     u->OnDelete();
278
279     USER_TO_DEL utd;
280     utd.iter = u;
281     utd.delTime = stgTime;
282     usersToDelete.push_back(utd);
283
284     DelUserFromIndexes(u);
285
286     WriteServLog("%s User \'%s\' deleted.",
287              admin->GetLogStr().c_str(), login.c_str());
288
289     }
290 }
291 //-----------------------------------------------------------------------------
292 bool USERS_IMPL::Authorize(const std::string & login, uint32_t ip,
293                            uint32_t enabledDirs, const AUTH * auth)
294 {
295 user_iter iter;
296 STG_LOCKER lock(&mutex);
297 if (FindByNameNonLock(login, &iter))
298     {
299     WriteServLog("Attempt to authorize non-existant user '%s'", login.c_str());
300     return false;
301     }
302
303 if (FindByIPIdx(ip, iter))
304     {
305     if (iter->GetLogin() != login)
306         {
307         WriteServLog("Attempt to authorize user '%s' from ip %s which already occupied by '%s'",
308                      login.c_str(), inet_ntostring(ip).c_str(),
309                      iter->GetLogin().c_str());
310         return false;
311         }
312     if (iter->Authorize(ip, enabledDirs, auth))
313         return false;
314     return true;
315     }
316
317 if (iter->Authorize(ip, enabledDirs, auth))
318     return false;
319
320 AddToIPIdx(iter);
321 return true;
322 }
323 //-----------------------------------------------------------------------------
324 bool USERS_IMPL::Unauthorize(const std::string & login,
325                              const AUTH * auth,
326                              const std::string & reason)
327 {
328 user_iter iter;
329 STG_LOCKER lock(&mutex);
330 if (FindByNameNonLock(login, &iter))
331     {
332     WriteServLog("Attempt to unauthorize non-existant user '%s'", login.c_str());
333     return false;
334     }
335
336 uint32_t ip = iter->GetCurrIP();
337
338 iter->Unauthorize(auth, reason);
339
340 if (!iter->GetAuthorized())
341     DelFromIPIdx(ip);
342
343 return true;
344 }
345 //-----------------------------------------------------------------------------
346 int USERS_IMPL::ReadUsers()
347 {
348 std::vector<std::string> usersList;
349 usersList.clear();
350 if (store->GetUsersList(&usersList) < 0)
351     {
352     WriteServLog(store->GetStrError().c_str());
353     return -1;
354     }
355
356 user_iter ui;
357
358 for (unsigned int i = 0; i < usersList.size(); i++)
359     {
360     USER_IMPL u(settings, store, tariffs, sysAdmin, this);
361
362     u.SetLogin(usersList[i]);
363     users.push_front(u);
364     ui = users.begin();
365
366     AddUserIntoIndexes(ui);
367
368     if (ui->ReadConf() < 0)
369         return -1;
370
371     if (ui->ReadStat() < 0)
372         return -1;
373     }
374
375 return 0;
376 }
377 //-----------------------------------------------------------------------------
378 void * USERS_IMPL::Run(void * d)
379 {
380 sigset_t signalSet;
381 sigfillset(&signalSet);
382 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
383
384 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
385 USERS_IMPL * us = static_cast<USERS_IMPL *>(d);
386
387 struct tm t;
388 time_t tt = stgTime;
389 localtime_r(&tt, &t);
390
391 int min = t.tm_min;
392 int day = t.tm_mday;
393
394 printfd(__FILE__,"Day = %d Min = %d\n", day, min);
395
396 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
397 std::string monFile = us->settings->GetMonitorDir() + "/users_r";
398 printfd(__FILE__, "Monitor=%d file USERS %s\n", us->settings->GetMonitoring(), monFile.c_str());
399
400 us->isRunning = true;
401 while (us->nonstop)
402     {
403     //printfd(__FILE__,"New Minute. old = %02d current = %02d\n", min, t->tm_min);
404     //printfd(__FILE__,"New Day.    old = %2d current = %2d\n", day, t->tm_mday);
405
406     for_each(us->users.begin(), us->users.end(), std::mem_fun_ref(&USER_IMPL::Run));
407
408     tt = stgTime;
409     localtime_r(&tt, &t);
410
411     if (min != t.tm_min)
412         {
413         printfd(__FILE__,"Sec = %d\n", stgTime);
414         printfd(__FILE__,"New Minute. old = %d current = %d\n", min, t.tm_min);
415         min = t.tm_min;
416
417         us->NewMinute(t);
418         }
419
420     if (day != t.tm_mday)
421         {
422         printfd(__FILE__,"Sec = %d\n", stgTime);
423         printfd(__FILE__,"New Day. old = %d current = %d\n", day, t.tm_mday);
424         day = t.tm_mday;
425         us->NewDay(t);
426         }
427
428     if (us->settings->GetMonitoring() && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
429         {
430         //printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
431         touchTime = stgTime;
432         TouchFile(monFile.c_str());
433         }
434
435     stgUsleep(100000);
436     } //while (us->nonstop)
437
438 user_iter ui = us->users.begin();
439 while (ui != us->users.end())
440     {
441     us->DelUserFromIndexes(ui);
442     ++ui;
443     }
444
445 std::list<USER_TO_DEL>::iterator iter;
446 iter = us->usersToDelete.begin();
447 while (iter != us->usersToDelete.end())
448     {
449     iter->delTime -= 2 * userDeleteDelayTime;
450     ++iter;
451     }
452 us->RealDelUser();
453
454 us->isRunning = false;
455
456 return NULL;
457 }
458 //-----------------------------------------------------------------------------
459 void USERS_IMPL::NewMinute(const struct tm & t)
460 {
461 //Write traff, reset session traff. Fake disconnect-connect
462 if (t.tm_hour == 23 && t.tm_min == 59)
463     {
464     printfd(__FILE__,"MidnightResetSessionStat\n");
465     for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::MidnightResetSessionStat));
466     }
467
468 if (TimeToWriteDetailStat(t))
469     {
470     //printfd(__FILE__, "USER::WriteInetStat\n");
471     int usersCnt = 0;
472
473     // ðÉÛÅÍ ÀÚÅÒÏ× ÞÁÓÔÑÍÉ. ÷ ÐÅÒÅÒÙ×ÁÈ ×ÙÚÙ×ÁÅÍ USER::Run
474     std::list<USER_IMPL>::iterator usr = users.begin();
475     while (usr != users.end())
476         {
477         usersCnt++;
478         usr->WriteDetailStat();
479         ++usr;
480         if (usersCnt % 10 == 0)
481             for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::Run));
482         }
483     }
484
485 RealDelUser();
486 }
487 //-----------------------------------------------------------------------------
488 void USERS_IMPL::NewDay(const struct tm & t)
489 {
490 struct tm t1;
491 time_t tt = stgTime;
492 localtime_r(&tt, &t1);
493 int dayFee = settings->GetDayFee();
494
495 if (dayFee == 0)
496     dayFee = DaysInCurrentMonth();
497
498 printfd(__FILE__, "DayFee = %d\n", dayFee);
499 printfd(__FILE__, "Today = %d DayResetTraff = %d\n", t1.tm_mday, settings->GetDayResetTraff());
500 printfd(__FILE__, "DayFeeIsLastDay = %d\n", settings->GetDayFeeIsLastDay());
501
502 if (!settings->GetDayFeeIsLastDay())
503     {
504     printfd(__FILE__, "DayResetTraff - 1 -\n");
505     DayResetTraff(t1);
506     //printfd(__FILE__, "DayResetTraff - 1 - 1 -\n");
507     }
508
509 if (settings->GetSpreadFee())
510     {
511     printfd(__FILE__, "Spread DayFee\n");
512     for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessDayFeeSpread));
513     }
514 else
515     {
516     if (t.tm_mday == dayFee)
517         {
518         printfd(__FILE__, "DayFee\n");
519         for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessDayFee));
520         }
521     }
522
523 std::for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessDailyFee));
524 std::for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessServices));
525
526 if (settings->GetDayFeeIsLastDay())
527     {
528     printfd(__FILE__, "DayResetTraff - 2 -\n");
529     DayResetTraff(t1);
530     }
531 }
532 //-----------------------------------------------------------------------------
533 void USERS_IMPL::DayResetTraff(const struct tm & t1)
534 {
535 int dayResetTraff = settings->GetDayResetTraff();
536 if (dayResetTraff == 0)
537     dayResetTraff = DaysInCurrentMonth();
538 if (t1.tm_mday == dayResetTraff)
539     {
540     printfd(__FILE__, "ResetTraff\n");
541     for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessNewMonth));
542     //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::SetPrepaidTraff));
543     }
544 }
545 //-----------------------------------------------------------------------------
546 int USERS_IMPL::Start()
547 {
548 if (ReadUsers())
549     {
550     WriteServLog("USERS: Error: Cannot read users!");
551     return -1;
552     }
553
554 nonstop = true;
555 if (pthread_create(&thread, NULL, Run, this))
556     {
557     WriteServLog("USERS: Error: Cannot start thread!");
558     return -1;
559     }
560 return 0;
561 }
562 //-----------------------------------------------------------------------------
563 int USERS_IMPL::Stop()
564 {
565 printfd(__FILE__, "USERS::Stop()\n");
566
567 if (!isRunning)
568     {
569     //printfd(__FILE__, "Alredy stopped\n");
570     return 0;
571     }
572
573 nonstop = false;
574
575 //5 seconds to thread stops itself
576 struct timespec ts = {0, 200000000};
577 for (size_t i = 0; i < 25 * (users.size() / 50 + 1); i++)
578     {
579     if (!isRunning)
580         break;
581
582     nanosleep(&ts, NULL);
583     }
584
585 //after 5 seconds waiting thread still running. now kill it
586 if (isRunning)
587     {
588     printfd(__FILE__, "kill USERS thread.\n");
589     //TODO pthread_cancel()
590     if (pthread_kill(thread, SIGINT))
591         {
592         //errorStr = "Cannot kill USERS thread.";
593         //printfd(__FILE__, "Cannot kill USERS thread.\n");
594         //return 0;
595         }
596     printfd(__FILE__, "USERS killed\n");
597     }
598
599 printfd(__FILE__, "Before USERS::Run()\n");
600 for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::Run));
601
602 // 'cause bind2st accepts only constant first param
603 for (std::list<USER_IMPL>::iterator it = users.begin();
604      it != users.end();
605      ++it)
606     it->WriteDetailStat(true);
607
608 for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::WriteStat));
609 //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteConf));
610
611 printfd(__FILE__, "USERS::Stop()\n");
612 return 0;
613 }
614 //-----------------------------------------------------------------------------
615 void USERS_IMPL::RealDelUser()
616 {
617 STG_LOCKER lock(&mutex);
618
619 printfd(__FILE__, "RealDelUser() users to del: %d\n", usersToDelete.size());
620
621 std::list<USER_TO_DEL>::iterator iter;
622 iter = usersToDelete.begin();
623 while (iter != usersToDelete.end())
624     {
625     printfd(__FILE__, "RealDelUser() user=%s\n", iter->iter->GetLogin().c_str());
626     if (iter->delTime + userDeleteDelayTime < stgTime)
627         {
628         printfd(__FILE__, "RealDelUser() user=%s removed from DB\n", iter->iter->GetLogin().c_str());
629         if (store->DelUser(iter->iter->GetLogin()))
630             {
631             WriteServLog("Error removing user \'%s\' from database.", iter->iter->GetLogin().c_str());
632             }
633         users.erase(iter->iter);
634         usersToDelete.erase(iter++);
635         }
636     else
637         {
638         ++iter;
639         }
640     }
641 return;
642 }
643 //-----------------------------------------------------------------------------
644 void USERS_IMPL::AddToIPIdx(user_iter user)
645 {
646 printfd(__FILE__, "USERS: Add IP Idx\n");
647 uint32_t ip = user->GetCurrIP();
648 //assert(ip && "User has non-null ip");
649 if (!ip)
650     return; // User has disconnected
651
652 STG_LOCKER lock(&mutex);
653
654 const std::map<uint32_t, user_iter>::iterator it(
655         ipIndex.lower_bound(ip)
656 );
657
658 assert((it == ipIndex.end() || it->first != ip) && "User is not in index");
659
660 ipIndex.insert(it, std::make_pair(ip, user));
661 }
662 //-----------------------------------------------------------------------------
663 void USERS_IMPL::DelFromIPIdx(uint32_t ip)
664 {
665 printfd(__FILE__, "USERS: Del IP Idx\n");
666 assert(ip && "User has non-null ip");
667
668 STG_LOCKER lock(&mutex);
669
670 const std::map<uint32_t, user_iter>::iterator it(
671         ipIndex.find(ip)
672 );
673
674 if (it == ipIndex.end())
675     return;
676
677 ipIndex.erase(it);
678 }
679 //-----------------------------------------------------------------------------
680 bool USERS_IMPL::FindByIPIdx(uint32_t ip, user_iter & iter) const
681 {
682 std::map<uint32_t, user_iter>::const_iterator it(ipIndex.find(ip));
683 if (it == ipIndex.end())
684     return false;
685 iter = it->second;
686 return true;
687 }
688 //-----------------------------------------------------------------------------
689 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_PTR * usr) const
690 {
691 STG_LOCKER lock(&mutex);
692
693 user_iter iter;
694 if (FindByIPIdx(ip, iter))
695     {
696     *usr = &(*iter);
697     return 0;
698     }
699
700 return -1;
701 }
702 //-----------------------------------------------------------------------------
703 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_IMPL ** usr) const
704 {
705 STG_LOCKER lock(&mutex);
706
707 user_iter iter;
708 if (FindByIPIdx(ip, iter))
709     {
710     *usr = &(*iter);
711     return 0;
712     }
713
714 return -1;
715 }
716 //-----------------------------------------------------------------------------
717 bool USERS_IMPL::IsIPInIndex(uint32_t ip) const
718 {
719 STG_LOCKER lock(&mutex);
720
721 std::map<uint32_t, user_iter>::const_iterator it(ipIndex.find(ip));
722
723 return it != ipIndex.end();
724 }
725 //-----------------------------------------------------------------------------
726 bool USERS_IMPL::IsIPInUse(uint32_t ip, const std::string & login, CONST_USER_PTR * user) const
727 {
728 STG_LOCKER lock(&mutex);
729 std::list<USER_IMPL>::const_iterator iter;
730 iter = users.begin();
731 while (iter != users.end())
732     {
733     if (iter->GetLogin() != login &&
734         !iter->GetProperty().ips.Get().IsAnyIP() &&
735         iter->GetProperty().ips.Get().IsIPInIPS(ip))
736         {
737         if (user != NULL)
738             *user = &(*iter);
739         return true;
740         }
741     ++iter;
742     }
743 return false;
744 }
745 //-----------------------------------------------------------------------------
746 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
747 {
748 STG_LOCKER lock(&mutex);
749 onAddNotifiers.insert(n);
750 }
751 //-----------------------------------------------------------------------------
752 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
753 {
754 STG_LOCKER lock(&mutex);
755 onAddNotifiers.erase(n);
756 }
757 //-----------------------------------------------------------------------------
758 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
759 {
760 STG_LOCKER lock(&mutex);
761 onDelNotifiers.insert(n);
762 }
763 //-----------------------------------------------------------------------------
764 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
765 {
766 STG_LOCKER lock(&mutex);
767 onDelNotifiers.erase(n);
768 }
769 //-----------------------------------------------------------------------------
770 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
771 {
772 STG_LOCKER lock(&mutex);
773 onAddNotifiersImpl.insert(n);
774 }
775 //-----------------------------------------------------------------------------
776 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
777 {
778 STG_LOCKER lock(&mutex);
779 onAddNotifiersImpl.erase(n);
780 }
781 //-----------------------------------------------------------------------------
782 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
783 {
784 STG_LOCKER lock(&mutex);
785 onDelNotifiersImpl.insert(n);
786 }
787 //-----------------------------------------------------------------------------
788 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
789 {
790 STG_LOCKER lock(&mutex);
791 onDelNotifiersImpl.erase(n);
792 }
793 //-----------------------------------------------------------------------------
794 int USERS_IMPL::OpenSearch()
795 {
796 STG_LOCKER lock(&mutex);
797 handle++;
798 searchDescriptors[handle] = users.begin();
799 return handle;
800 }
801 //-----------------------------------------------------------------------------
802 int USERS_IMPL::SearchNext(int h, USER_PTR * user)
803 {
804     USER_IMPL * ptr = NULL;
805     if (SearchNext(h, &ptr))
806         return -1;
807     *user = ptr;
808     return 0;
809 }
810 //-----------------------------------------------------------------------------
811 int USERS_IMPL::SearchNext(int h, USER_IMPL ** user)
812 {
813 STG_LOCKER lock(&mutex);
814
815 if (searchDescriptors.find(h) == searchDescriptors.end())
816     {
817     WriteServLog("USERS. Incorrect search handle.");
818     return -1;
819     }
820
821 if (searchDescriptors[h] == users.end())
822     return -1;
823
824 while (searchDescriptors[h]->GetDeleted())
825     {
826     ++searchDescriptors[h];
827     if (searchDescriptors[h] == users.end())
828         {
829         return -1;
830         }
831     }
832
833 *user = &(*searchDescriptors[h]);
834
835 ++searchDescriptors[h];
836
837 return 0;
838 }
839 //-----------------------------------------------------------------------------
840 int USERS_IMPL::CloseSearch(int h)
841 {
842 STG_LOCKER lock(&mutex);
843 if (searchDescriptors.find(h) != searchDescriptors.end())
844     {
845     searchDescriptors.erase(searchDescriptors.find(h));
846     return 0;
847     }
848
849 WriteServLog("USERS. Incorrect search handle.");
850 return -1;
851 }
852 //-----------------------------------------------------------------------------
853 void USERS_IMPL::AddUserIntoIndexes(user_iter user)
854 {
855 STG_LOCKER lock(&mutex);
856 loginIndex.insert(make_pair(user->GetLogin(), user));
857 }
858 //-----------------------------------------------------------------------------
859 void USERS_IMPL::DelUserFromIndexes(user_iter user)
860 {
861 STG_LOCKER lock(&mutex);
862 loginIndex.erase(user->GetLogin());
863 }
864 //-----------------------------------------------------------------------------
865 bool USERS_IMPL::TimeToWriteDetailStat(const struct tm & t)
866 {
867 int statTime = settings->GetDetailStatWritePeriod();
868
869 switch (statTime)
870     {
871     case dsPeriod_1:
872         if (t.tm_min == 0)
873             return true;
874         break;
875     case dsPeriod_1_2:
876         if (t.tm_min % 30 == 0)
877             return true;
878         break;
879     case dsPeriod_1_4:
880         if (t.tm_min % 15 == 0)
881             return true;
882         break;
883     case dsPeriod_1_6:
884         if (t.tm_min % 10 == 0)
885             return true;
886         break;
887     }
888 return false;
889 }