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