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