]> git.stg.codes - stg.git/blob - projects/stargazer/user_impl.h
Rename BASE_AUTH and BASE_STORE to AUTH and STORE
[stg.git] / projects / stargazer / user_impl.h
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  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 /*
22  $Revision: 1.48 $
23  $Date: 2010/11/03 10:50:03 $
24  $Author: faust $
25  */
26
27 #ifndef USER_IMPL_H
28 #define USER_IMPL_H
29
30 #include <ctime>
31 #include <list>
32 #include <string>
33 #include <set>
34
35 #include "user.h"
36 #include "os_int.h"
37 #include "stg_const.h"
38 #include "user_stat.h"
39 #include "user_conf.h"
40 #include "user_ips.h"
41 #include "user_property.h"
42 #include "auth.h"
43 #include "stg_message.h"
44 #include "noncopyable.h"
45
46 //-----------------------------------------------------------------------------
47 class TARIFF;
48 class TARIFFS;
49 class ADMIN;
50 class USER_IMPL;
51 //-----------------------------------------------------------------------------
52 class USER_ID_GENERATOR {
53 friend class USER_IMPL;
54 private:
55     USER_ID_GENERATOR() {}
56     int GetNextID() { static int id = 0; return id++; }
57 };
58 //-----------------------------------------------------------------------------
59 class CHG_PASSIVE_NOTIFIER : public PROPERTY_NOTIFIER_BASE<int>,
60                              private NONCOPYABLE {
61 public:
62     CHG_PASSIVE_NOTIFIER(USER_IMPL * u) : user(u) {}
63     void Notify(const int & oldPassive, const int & newPassive);
64
65 private:
66     USER_IMPL * user;
67 };
68 //-----------------------------------------------------------------------------
69 class CHG_TARIFF_NOTIFIER : public PROPERTY_NOTIFIER_BASE<std::string>,
70                             private NONCOPYABLE {
71 public:
72     CHG_TARIFF_NOTIFIER(USER_IMPL * u) : user(u) {}
73     void Notify(const std::string & oldTariff, const std::string & newTariff);
74
75 private:
76     USER_IMPL * user;
77 };
78 //-----------------------------------------------------------------------------
79 class CHG_CASH_NOTIFIER : public PROPERTY_NOTIFIER_BASE<double>,
80                           private NONCOPYABLE {
81 public:
82     CHG_CASH_NOTIFIER(USER_IMPL * u) : user(u) {}
83     void Notify(const double & oldCash, const double & newCash);
84
85 private:
86     USER_IMPL * user;
87 };
88 //-----------------------------------------------------------------------------
89 class CHG_IP_NOTIFIER : public PROPERTY_NOTIFIER_BASE<uint32_t>,
90                         private NONCOPYABLE {
91 public:
92     CHG_IP_NOTIFIER(USER_IMPL * u) : user(u) {}
93     void Notify(const uint32_t & oldCash, const uint32_t & newCash);
94
95 private:
96     USER_IMPL * user;
97 };
98 //-----------------------------------------------------------------------------
99 class USER_IMPL : public USER {
100 friend class CHG_PASSIVE_NOTIFIER;
101 friend class CHG_TARIFF_NOTIFIER;
102 friend class CHG_CASH_NOTIFIER;
103 friend class CHG_IP_NOTIFIER;
104 public:
105     USER_IMPL(const SETTINGS * settings,
106               const STORE * store,
107               const TARIFFS * tariffs,
108               const ADMIN & sysAdmin,
109               const USERS * u);
110     USER_IMPL(const USER_IMPL & u);
111     virtual ~USER_IMPL();
112
113     int             ReadConf();
114     int             ReadStat();
115     int             WriteConf();
116     int             WriteStat();
117     int             WriteMonthStat();
118
119     std::string const & GetLogin() const { return login; }
120     void            SetLogin(std::string const & l);
121
122     uint32_t        GetCurrIP() const { return currIP; }
123     time_t          GetCurrIPModificationTime() const { return currIP.ModificationTime(); }
124
125     void            AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
126     void            DelCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
127
128     void            AddCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
129     void            DelCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
130
131     int             GetID() const { return id; }
132
133     double          GetPassiveTimePart() const;
134     void            ResetPassiveTime() { passiveTime = 0; }
135     void            SetPassiveTimeAsNewUser();
136
137     int             WriteDetailStat(bool hard = false);
138
139     const TARIFF *  GetTariff() const { return tariff; }
140     void            ResetNextTariff() { nextTariff = ""; }
141
142     #ifdef TRAFF_STAT_WITH_PORTS
143     void            AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len);
144     void            AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len);
145     #else
146     void            AddTraffStatU(int dir, uint32_t ip, uint32_t len);
147     void            AddTraffStatD(int dir, uint32_t ip, uint32_t len);
148     #endif
149
150     const DIR_TRAFF & GetSessionUpload() const { return sessionUpload; }
151     const DIR_TRAFF & GetSessionDownload() const { return sessionDownload; }
152
153     bool            GetConnected() const { return connected; }
154     time_t          GetConnectedModificationTime() const { return connected.ModificationTime(); }
155     int             GetAuthorized() const { return authorizedBy.size(); }
156     int             Authorize(uint32_t ip, uint32_t enabledDirs, const AUTH * auth);
157     void            Unauthorize(const AUTH * auth);
158     bool            IsAuthorizedBy(const AUTH * auth) const;
159
160     int             AddMessage(STG_MSG * msg);
161
162     void            UpdatePingTime(time_t t = 0);
163     time_t          GetPingTime() const { return pingTime; }
164
165     void            PrintUser() const;
166     void            Run();
167
168     const std::string & GetStrError() const { return errorStr; }
169
170     USER_PROPERTIES & GetProperty() { return property; };
171     const USER_PROPERTIES & GetProperty() const { return property; };
172
173     void            SetDeleted() { deleted = true; }
174     bool            GetDeleted() const { return deleted; }
175
176     time_t          GetLastWriteStatTime() const { return lastWriteStat; }
177
178     void            MidnightResetSessionStat();
179     void            ProcessDayFee();
180     void            SetPrepaidTraff();
181     void            ProcessDayFeeSpread();
182     void            ProcessNewMonth();
183
184     bool            IsInetable();
185     std::string          GetEnabledDirs();
186
187     void            OnAdd();
188     void            OnDelete();
189
190 private:
191     const USERS *   users;
192     USER_PROPERTIES property;
193     STG_LOGGER &    WriteServLog;
194
195     void            Connect(bool fakeConnect = false);
196     void            Disconnect(bool fakeDisconnect, const std::string & reason);
197     int             SaveMonthStat(int month, int year);
198
199     int             SendMessage(STG_MSG & msg) const;
200     void            ScanMessage();
201
202     time_t          lastScanMessages;
203
204     std::string     login;
205     int             id;
206     bool            __connected;
207     USER_PROPERTY<bool> connected;
208
209     bool            enabledDirs[DIR_NUM];
210
211     USER_ID_GENERATOR userIDGenerator;
212
213     uint32_t        __currIP; // ôÅËÕÝÉÊ ÁÄÒÅÓ ÐÏÌØÚÏ×ÁÔÅÌÑ
214     USER_PROPERTY<uint32_t> currIP;
215
216     /*
217     ë ÔÏÍÕ ÍÏÍÅÎÔÕ ËÁË ÍÙ ÕÖÅ ÎÅ Á×ÔÏÒÉÚÏ×ÁÎÉÙ, ÎÏ ÅÝÅ ÎÅ ×ÙÐÏÌÎÅΠDisconnect,
218     currIP ÕÖÅ ÓÂÒÏÛÅÎ. ðÏÓÌÅÄÎÅÅ ÚÎÁÞÅÎÉÅ currIP ÓÏÈÒÁÎÑÅÍ × lastIPForDisconnect
219     */
220     uint32_t        lastIPForDisconnect;
221
222     time_t          pingTime;
223
224     const ADMIN &   sysAdmin;
225     const STORE *   store;
226
227     const TARIFFS * tariffs;
228     const TARIFF *  tariff;
229
230     TRAFF_STAT      traffStat;
231     std::pair<time_t, TRAFF_STAT> traffStatSaved;
232
233     const SETTINGS * settings;
234
235     std::set<const AUTH *> authorizedBy;
236
237     std::list<STG_MSG> messages;
238
239     bool            deleted;
240
241     time_t          lastWriteStat;           // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÓÔÁÔÉÓÔÉËÉ
242     time_t          lastWriteDeatiledStat;   // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÄÅÔÁÌØÎÏÊ ÓÔÁÔÉÓÔÉËÉ
243
244     // Properties
245     USER_PROPERTY<double>         & cash;
246     USER_PROPERTY<DIR_TRAFF>      & up;
247     USER_PROPERTY<DIR_TRAFF>      & down;
248     USER_PROPERTY<double>         & lastCashAdd;
249     USER_PROPERTY<time_t>         & passiveTime;
250     USER_PROPERTY<time_t>         & lastCashAddTime;
251     USER_PROPERTY<double>         & freeMb;
252     USER_PROPERTY<time_t>         & lastActivityTime;
253     USER_PROPERTY<std::string>    & password;
254     USER_PROPERTY<int>            & passive;
255     USER_PROPERTY<int>            & disabled;
256     USER_PROPERTY<int>            & disabledDetailStat;
257     USER_PROPERTY<int>            & alwaysOnline;
258     USER_PROPERTY<std::string>    & tariffName;
259     USER_PROPERTY<std::string>    & nextTariff;
260     USER_PROPERTY<std::string>    & address;
261     USER_PROPERTY<std::string>    & note;
262     USER_PROPERTY<std::string>    & group;
263     USER_PROPERTY<std::string>    & email;
264     USER_PROPERTY<std::string>    & phone;
265     USER_PROPERTY<std::string>    & realName;
266     USER_PROPERTY<double>         & credit;
267     USER_PROPERTY<time_t>         & creditExpire;
268     USER_PROPERTY<USER_IPS>       & ips;
269     USER_PROPERTY<std::string>    & userdata0;
270     USER_PROPERTY<std::string>    & userdata1;
271     USER_PROPERTY<std::string>    & userdata2;
272     USER_PROPERTY<std::string>    & userdata3;
273     USER_PROPERTY<std::string>    & userdata4;
274     USER_PROPERTY<std::string>    & userdata5;
275     USER_PROPERTY<std::string>    & userdata6;
276     USER_PROPERTY<std::string>    & userdata7;
277     USER_PROPERTY<std::string>    & userdata8;
278     USER_PROPERTY<std::string>    & userdata9;
279
280     // End properties
281
282     DIR_TRAFF                sessionUpload;
283     DIR_TRAFF                sessionDownload;
284
285     CHG_PASSIVE_NOTIFIER     passiveNotifier;
286     CHG_TARIFF_NOTIFIER      tariffNotifier;
287     CHG_CASH_NOTIFIER        cashNotifier;
288     CHG_IP_NOTIFIER          ipNotifier;
289
290     mutable pthread_mutex_t  mutex;
291
292     std::string              errorStr;
293 };
294 //-----------------------------------------------------------------------------
295
296 #endif //USER_H