]> git.stg.codes - stg.git/blob - projects/stargazer/users_impl.cpp
3dd7638b9b2ecf392685ccd2f68d19b9fd17edde
[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
525 if (settings->GetDayFeeIsLastDay())
526     {
527     printfd(__FILE__, "DayResetTraff - 2 -\n");
528     DayResetTraff(t1);
529     }
530 }
531 //-----------------------------------------------------------------------------
532 void USERS_IMPL::DayResetTraff(const struct tm & t1)
533 {
534 int dayResetTraff = settings->GetDayResetTraff();
535 if (dayResetTraff == 0)
536     dayResetTraff = DaysInCurrentMonth();
537 if (t1.tm_mday == dayResetTraff)
538     {
539     printfd(__FILE__, "ResetTraff\n");
540     for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessNewMonth));
541     //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::SetPrepaidTraff));
542     }
543 }
544 //-----------------------------------------------------------------------------
545 int USERS_IMPL::Start()
546 {
547 if (ReadUsers())
548     {
549     WriteServLog("USERS: Error: Cannot read users!");
550     return -1;
551     }
552
553 nonstop = true;
554 if (pthread_create(&thread, NULL, Run, this))
555     {
556     WriteServLog("USERS: Error: Cannot start thread!");
557     return -1;
558     }
559 return 0;
560 }
561 //-----------------------------------------------------------------------------
562 int USERS_IMPL::Stop()
563 {
564 printfd(__FILE__, "USERS::Stop()\n");
565
566 if (!isRunning)
567     {
568     //printfd(__FILE__, "Alredy stopped\n");
569     return 0;
570     }
571
572 nonstop = false;
573
574 //5 seconds to thread stops itself
575 struct timespec ts = {0, 200000000};
576 for (size_t i = 0; i < 25 * (users.size() / 50 + 1); i++)
577     {
578     if (!isRunning)
579         break;
580
581     nanosleep(&ts, NULL);
582     }
583
584 //after 5 seconds waiting thread still running. now kill it
585 if (isRunning)
586     {
587     printfd(__FILE__, "kill USERS thread.\n");
588     //TODO pthread_cancel()
589     if (pthread_kill(thread, SIGINT))
590         {
591         //errorStr = "Cannot kill USERS thread.";
592         //printfd(__FILE__, "Cannot kill USERS thread.\n");
593         //return 0;
594         }
595     printfd(__FILE__, "USERS killed\n");
596     }
597
598 printfd(__FILE__, "Before USERS::Run()\n");
599 for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::Run));
600
601 // 'cause bind2st accepts only constant first param
602 for (std::list<USER_IMPL>::iterator it = users.begin();
603      it != users.end();
604      ++it)
605     it->WriteDetailStat(true);
606
607 for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::WriteStat));
608 //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteConf));
609
610 printfd(__FILE__, "USERS::Stop()\n");
611 return 0;
612 }
613 //-----------------------------------------------------------------------------
614 void USERS_IMPL::RealDelUser()
615 {
616 STG_LOCKER lock(&mutex);
617
618 printfd(__FILE__, "RealDelUser() users to del: %d\n", usersToDelete.size());
619
620 std::list<USER_TO_DEL>::iterator iter;
621 iter = usersToDelete.begin();
622 while (iter != usersToDelete.end())
623     {
624     printfd(__FILE__, "RealDelUser() user=%s\n", iter->iter->GetLogin().c_str());
625     if (iter->delTime + userDeleteDelayTime < stgTime)
626         {
627         printfd(__FILE__, "RealDelUser() user=%s removed from DB\n", iter->iter->GetLogin().c_str());
628         if (store->DelUser(iter->iter->GetLogin()))
629             {
630             WriteServLog("Error removing user \'%s\' from database.", iter->iter->GetLogin().c_str());
631             }
632         users.erase(iter->iter);
633         usersToDelete.erase(iter++);
634         }
635     else
636         {
637         ++iter;
638         }
639     }
640 return;
641 }
642 //-----------------------------------------------------------------------------
643 void USERS_IMPL::AddToIPIdx(user_iter user)
644 {
645 printfd(__FILE__, "USERS: Add IP Idx\n");
646 uint32_t ip = user->GetCurrIP();
647 //assert(ip && "User has non-null ip");
648 if (!ip)
649     return; // User has disconnected
650
651 STG_LOCKER lock(&mutex);
652
653 const std::map<uint32_t, user_iter>::iterator it(
654         ipIndex.lower_bound(ip)
655 );
656
657 assert((it == ipIndex.end() || it->first != ip) && "User is not in index");
658
659 ipIndex.insert(it, std::make_pair(ip, user));
660 }
661 //-----------------------------------------------------------------------------
662 void USERS_IMPL::DelFromIPIdx(uint32_t ip)
663 {
664 printfd(__FILE__, "USERS: Del IP Idx\n");
665 assert(ip && "User has non-null ip");
666
667 STG_LOCKER lock(&mutex);
668
669 const std::map<uint32_t, user_iter>::iterator it(
670         ipIndex.find(ip)
671 );
672
673 if (it == ipIndex.end())
674     return;
675
676 ipIndex.erase(it);
677 }
678 //-----------------------------------------------------------------------------
679 bool USERS_IMPL::FindByIPIdx(uint32_t ip, user_iter & iter) const
680 {
681 std::map<uint32_t, user_iter>::const_iterator it(ipIndex.find(ip));
682 if (it == ipIndex.end())
683     return false;
684 iter = it->second;
685 return true;
686 }
687 //-----------------------------------------------------------------------------
688 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_PTR * usr) const
689 {
690 STG_LOCKER lock(&mutex);
691
692 user_iter iter;
693 if (FindByIPIdx(ip, iter))
694     {
695     *usr = &(*iter);
696     return 0;
697     }
698
699 return -1;
700 }
701 //-----------------------------------------------------------------------------
702 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_IMPL ** usr) const
703 {
704 STG_LOCKER lock(&mutex);
705
706 user_iter iter;
707 if (FindByIPIdx(ip, iter))
708     {
709     *usr = &(*iter);
710     return 0;
711     }
712
713 return -1;
714 }
715 //-----------------------------------------------------------------------------
716 bool USERS_IMPL::IsIPInIndex(uint32_t ip) const
717 {
718 STG_LOCKER lock(&mutex);
719
720 std::map<uint32_t, user_iter>::const_iterator it(ipIndex.find(ip));
721
722 return it != ipIndex.end();
723 }
724 //-----------------------------------------------------------------------------
725 bool USERS_IMPL::IsIPInUse(uint32_t ip, const std::string & login, CONST_USER_PTR * user) const
726 {
727 STG_LOCKER lock(&mutex);
728 std::list<USER_IMPL>::const_iterator iter;
729 iter = users.begin();
730 while (iter != users.end())
731     {
732     if (iter->GetLogin() != login &&
733         !iter->GetProperty().ips.Get().IsAnyIP() &&
734         iter->GetProperty().ips.Get().IsIPInIPS(ip))
735         {
736         if (user != NULL)
737             *user = &(*iter);
738         return true;
739         }
740     ++iter;
741     }
742 return false;
743 }
744 //-----------------------------------------------------------------------------
745 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
746 {
747 STG_LOCKER lock(&mutex);
748 onAddNotifiers.insert(n);
749 }
750 //-----------------------------------------------------------------------------
751 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
752 {
753 STG_LOCKER lock(&mutex);
754 onAddNotifiers.erase(n);
755 }
756 //-----------------------------------------------------------------------------
757 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
758 {
759 STG_LOCKER lock(&mutex);
760 onDelNotifiers.insert(n);
761 }
762 //-----------------------------------------------------------------------------
763 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
764 {
765 STG_LOCKER lock(&mutex);
766 onDelNotifiers.erase(n);
767 }
768 //-----------------------------------------------------------------------------
769 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
770 {
771 STG_LOCKER lock(&mutex);
772 onAddNotifiersImpl.insert(n);
773 }
774 //-----------------------------------------------------------------------------
775 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
776 {
777 STG_LOCKER lock(&mutex);
778 onAddNotifiersImpl.erase(n);
779 }
780 //-----------------------------------------------------------------------------
781 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
782 {
783 STG_LOCKER lock(&mutex);
784 onDelNotifiersImpl.insert(n);
785 }
786 //-----------------------------------------------------------------------------
787 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
788 {
789 STG_LOCKER lock(&mutex);
790 onDelNotifiersImpl.erase(n);
791 }
792 //-----------------------------------------------------------------------------
793 int USERS_IMPL::OpenSearch()
794 {
795 STG_LOCKER lock(&mutex);
796 handle++;
797 searchDescriptors[handle] = users.begin();
798 return handle;
799 }
800 //-----------------------------------------------------------------------------
801 int USERS_IMPL::SearchNext(int h, USER_PTR * user)
802 {
803     USER_IMPL * ptr = NULL;
804     if (SearchNext(h, &ptr))
805         return -1;
806     *user = ptr;
807     return 0;
808 }
809 //-----------------------------------------------------------------------------
810 int USERS_IMPL::SearchNext(int h, USER_IMPL ** user)
811 {
812 STG_LOCKER lock(&mutex);
813
814 if (searchDescriptors.find(h) == searchDescriptors.end())
815     {
816     WriteServLog("USERS. Incorrect search handle.");
817     return -1;
818     }
819
820 if (searchDescriptors[h] == users.end())
821     return -1;
822
823 while (searchDescriptors[h]->GetDeleted())
824     {
825     ++searchDescriptors[h];
826     if (searchDescriptors[h] == users.end())
827         {
828         return -1;
829         }
830     }
831
832 *user = &(*searchDescriptors[h]);
833
834 ++searchDescriptors[h];
835
836 return 0;
837 }
838 //-----------------------------------------------------------------------------
839 int USERS_IMPL::CloseSearch(int h)
840 {
841 STG_LOCKER lock(&mutex);
842 if (searchDescriptors.find(h) != searchDescriptors.end())
843     {
844     searchDescriptors.erase(searchDescriptors.find(h));
845     return 0;
846     }
847
848 WriteServLog("USERS. Incorrect search handle.");
849 return -1;
850 }
851 //-----------------------------------------------------------------------------
852 void USERS_IMPL::AddUserIntoIndexes(user_iter user)
853 {
854 STG_LOCKER lock(&mutex);
855 loginIndex.insert(make_pair(user->GetLogin(), user));
856 }
857 //-----------------------------------------------------------------------------
858 void USERS_IMPL::DelUserFromIndexes(user_iter user)
859 {
860 STG_LOCKER lock(&mutex);
861 loginIndex.erase(user->GetLogin());
862 }
863 //-----------------------------------------------------------------------------
864 bool USERS_IMPL::TimeToWriteDetailStat(const struct tm & t)
865 {
866 int statTime = settings->GetDetailStatWritePeriod();
867
868 switch (statTime)
869     {
870     case dsPeriod_1:
871         if (t.tm_min == 0)
872             return true;
873         break;
874     case dsPeriod_1_2:
875         if (t.tm_min % 30 == 0)
876             return true;
877         break;
878     case dsPeriod_1_4:
879         if (t.tm_min % 15 == 0)
880             return true;
881         break;
882     case dsPeriod_1_6:
883         if (t.tm_min % 10 == 0)
884             return true;
885         break;
886     }
887 return false;
888 }