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