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