]> git.stg.codes - stg.git/blob - projects/stargazer/user_impl.h
Initialize some fields via initializer lists in USER_IMPL and
[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 "stg/user.h"
36 #include "stg/os_int.h"
37 #include "stg/const.h"
38 #include "stg/user_stat.h"
39 #include "stg/user_conf.h"
40 #include "stg/user_ips.h"
41 #include "stg/user_property.h"
42 #include "stg/auth.h"
43 #include "stg/message.h"
44 #include "stg/noncopyable.h"
45
46 //-----------------------------------------------------------------------------
47 class TARIFF;
48 class TARIFFS;
49 class ADMIN;
50 class USER_IMPL;
51 #ifdef USE_ABSTRACT_SETTINGS
52 class SETTINGS;
53 #else
54 class SETTINGS_IMPL;
55 #endif
56 //-----------------------------------------------------------------------------
57 class USER_ID_GENERATOR {
58 friend class USER_IMPL;
59 private:
60     USER_ID_GENERATOR() {}
61     int GetNextID() { static int id = 0; return id++; }
62 };
63 //-----------------------------------------------------------------------------
64 class CHG_PASSIVE_NOTIFIER : public PROPERTY_NOTIFIER_BASE<int>,
65                              private NONCOPYABLE {
66 public:
67     CHG_PASSIVE_NOTIFIER(USER_IMPL * u) : user(u) {}
68     void Notify(const int & oldPassive, const int & newPassive);
69
70 private:
71     USER_IMPL * user;
72 };
73 //-----------------------------------------------------------------------------
74 class CHG_TARIFF_NOTIFIER : public PROPERTY_NOTIFIER_BASE<std::string>,
75                             private NONCOPYABLE {
76 public:
77     CHG_TARIFF_NOTIFIER(USER_IMPL * u) : user(u) {}
78     void Notify(const std::string & oldTariff, const std::string & newTariff);
79
80 private:
81     USER_IMPL * user;
82 };
83 //-----------------------------------------------------------------------------
84 class CHG_CASH_NOTIFIER : public PROPERTY_NOTIFIER_BASE<double>,
85                           private NONCOPYABLE {
86 public:
87     CHG_CASH_NOTIFIER(USER_IMPL * u) : user(u) {}
88     void Notify(const double & oldCash, const double & newCash);
89
90 private:
91     USER_IMPL * user;
92 };
93 //-----------------------------------------------------------------------------
94 class CHG_IPS_NOTIFIER : public PROPERTY_NOTIFIER_BASE<USER_IPS>,
95                          private NONCOPYABLE {
96 public:
97     CHG_IPS_NOTIFIER(USER_IMPL * u) : user(u) {}
98     void Notify(const USER_IPS & oldIPs, const USER_IPS & newIPs);
99
100 private:
101     USER_IMPL * user;
102 };
103 //-----------------------------------------------------------------------------
104 class USER_IMPL : public USER {
105 friend class CHG_PASSIVE_NOTIFIER;
106 friend class CHG_TARIFF_NOTIFIER;
107 friend class CHG_CASH_NOTIFIER;
108 friend class CHG_IPS_NOTIFIER;
109 public:
110 #ifdef USE_ABSTRACT_SETTINGS
111     USER_IMPL(const SETTINGS * settings,
112               const STORE * store,
113               const TARIFFS * tariffs,
114               const ADMIN * sysAdmin,
115               const USERS * u);
116 #else
117     USER_IMPL(const SETTINGS_IMPL * settings,
118               const STORE * store,
119               const TARIFFS * tariffs,
120               const ADMIN * sysAdmin,
121               const USERS * u);
122 #endif
123     USER_IMPL(const USER_IMPL & u);
124     virtual ~USER_IMPL();
125
126     int             ReadConf();
127     int             ReadStat();
128     int             WriteConf();
129     int             WriteStat();
130     int             WriteMonthStat();
131
132     const std::string & GetLogin() const { return login; }
133     void            SetLogin(std::string const & l);
134
135     uint32_t        GetCurrIP() const { return currIP; }
136     time_t          GetCurrIPModificationTime() const { return currIP.ModificationTime(); }
137
138     void            AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
139     void            DelCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
140
141     void            AddCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
142     void            DelCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
143
144     void            AddConnectedBeforeNotifier(PROPERTY_NOTIFIER_BASE<bool> *);
145     void            DelConnectedBeforeNotifier(PROPERTY_NOTIFIER_BASE<bool> *);
146
147     void            AddConnectedAfterNotifier(PROPERTY_NOTIFIER_BASE<bool> *);
148     void            DelConnectedAfterNotifier(PROPERTY_NOTIFIER_BASE<bool> *);
149
150     int             GetID() const { return id; }
151
152     double          GetPassiveTimePart() const;
153     void            ResetPassiveTime() { passiveTime = 0; }
154     void            SetPassiveTimeAsNewUser();
155
156     int             WriteDetailStat(bool hard = false);
157
158     const TARIFF *  GetTariff() const { return tariff; }
159     void            ResetNextTariff() { nextTariff = ""; }
160
161     #ifdef TRAFF_STAT_WITH_PORTS
162     void            AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len);
163     void            AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len);
164     #else
165     void            AddTraffStatU(int dir, uint32_t ip, uint32_t len);
166     void            AddTraffStatD(int dir, uint32_t ip, uint32_t len);
167     #endif
168
169     const DIR_TRAFF & GetSessionUpload() const { return sessionUpload; }
170     const DIR_TRAFF & GetSessionDownload() const { return sessionDownload; }
171
172     bool            GetConnected() const { return connected; }
173     time_t          GetConnectedModificationTime() const { return connected.ModificationTime(); }
174     int             GetAuthorized() const { return authorizedBy.size(); }
175     int             Authorize(uint32_t ip, uint32_t enabledDirs, const AUTH * auth);
176     void            Unauthorize(const AUTH * auth);
177     bool            IsAuthorizedBy(const AUTH * auth) const;
178
179     int             AddMessage(STG_MSG * msg);
180
181     void            UpdatePingTime(time_t t = 0);
182     time_t          GetPingTime() const { return pingTime; }
183
184     void            PrintUser() const;
185     void            Run();
186
187     const std::string & GetStrError() const { return errorStr; }
188
189     USER_PROPERTIES & GetProperty() { return property; };
190     const USER_PROPERTIES & GetProperty() const { return property; };
191
192     void            SetDeleted() { deleted = true; }
193     bool            GetDeleted() const { return deleted; }
194
195     time_t          GetLastWriteStatTime() const { return lastWriteStat; }
196
197     void            MidnightResetSessionStat();
198     void            ProcessDayFee();
199     void            ProcessDayFeeSpread();
200     void            ProcessNewMonth();
201
202     bool            IsInetable();
203     std::string     GetEnabledDirs();
204
205     void            OnAdd();
206     void            OnDelete();
207
208 private:
209     USER_IMPL & operator=(const USER_IMPL & rvalue);
210
211     const USERS *   users;
212     USER_PROPERTIES property;
213     STG_LOGGER &    WriteServLog;
214
215     void            Connect(bool fakeConnect = false);
216     void            Disconnect(bool fakeDisconnect, const std::string & reason);
217     int             SaveMonthStat(int month, int year);
218
219     void            SetPrepaidTraff();
220
221     int             SendMessage(STG_MSG & msg) const;
222     void            ScanMessage();
223
224     time_t          lastScanMessages;
225
226     std::string     login;
227     int             id;
228     bool            __connected;
229     USER_PROPERTY<bool> connected;
230
231     bool            enabledDirs[DIR_NUM];
232
233     USER_ID_GENERATOR userIDGenerator;
234
235     uint32_t        __currIP; // Current user's ip
236     USER_PROPERTY<uint32_t> currIP;
237
238     uint32_t        lastIPForDisconnect; // User's ip after unauth but before disconnect
239
240     time_t          pingTime;
241
242     const ADMIN *   sysAdmin;
243     const STORE *   store;
244
245     const TARIFFS * tariffs;
246     const TARIFF *  tariff;
247
248     TRAFF_STAT      traffStat;
249     std::pair<time_t, TRAFF_STAT> traffStatSaved;
250
251 #ifdef USE_ABSTRACT_SETTINGS
252     const SETTINGS * settings;
253 #else
254     const SETTINGS_IMPL * settings;
255 #endif
256
257     std::set<const AUTH *> authorizedBy;
258
259     std::list<STG_MSG> messages;
260
261     bool            deleted;
262
263     time_t          lastWriteStat;
264     time_t          lastWriteDetailedStat;
265
266     // Properties
267     USER_PROPERTY<double>         & cash;
268     USER_PROPERTY<DIR_TRAFF>      & up;
269     USER_PROPERTY<DIR_TRAFF>      & down;
270     USER_PROPERTY<double>         & lastCashAdd;
271     USER_PROPERTY<time_t>         & passiveTime;
272     USER_PROPERTY<time_t>         & lastCashAddTime;
273     USER_PROPERTY<double>         & freeMb;
274     USER_PROPERTY<time_t>         & lastActivityTime;
275     USER_PROPERTY<std::string>    & password;
276     USER_PROPERTY<int>            & passive;
277     USER_PROPERTY<int>            & disabled;
278     USER_PROPERTY<int>            & disabledDetailStat;
279     USER_PROPERTY<int>            & alwaysOnline;
280     USER_PROPERTY<std::string>    & tariffName;
281     USER_PROPERTY<std::string>    & nextTariff;
282     USER_PROPERTY<std::string>    & address;
283     USER_PROPERTY<std::string>    & note;
284     USER_PROPERTY<std::string>    & group;
285     USER_PROPERTY<std::string>    & email;
286     USER_PROPERTY<std::string>    & phone;
287     USER_PROPERTY<std::string>    & realName;
288     USER_PROPERTY<double>         & credit;
289     USER_PROPERTY<time_t>         & creditExpire;
290     USER_PROPERTY<USER_IPS>       & ips;
291     USER_PROPERTY<std::string>    & userdata0;
292     USER_PROPERTY<std::string>    & userdata1;
293     USER_PROPERTY<std::string>    & userdata2;
294     USER_PROPERTY<std::string>    & userdata3;
295     USER_PROPERTY<std::string>    & userdata4;
296     USER_PROPERTY<std::string>    & userdata5;
297     USER_PROPERTY<std::string>    & userdata6;
298     USER_PROPERTY<std::string>    & userdata7;
299     USER_PROPERTY<std::string>    & userdata8;
300     USER_PROPERTY<std::string>    & userdata9;
301
302     // End properties
303
304     DIR_TRAFF                sessionUpload;
305     DIR_TRAFF                sessionDownload;
306
307     CHG_PASSIVE_NOTIFIER     passiveNotifier;
308     CHG_TARIFF_NOTIFIER      tariffNotifier;
309     CHG_CASH_NOTIFIER        cashNotifier;
310     CHG_IPS_NOTIFIER         ipNotifier;
311
312     mutable pthread_mutex_t  mutex;
313
314     std::string              errorStr;
315 };
316 //-----------------------------------------------------------------------------
317
318 typedef USER_IMPL * USER_IMPL_PTR;
319
320 #endif //USER_H