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