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