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