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