]> git.stg.codes - stg.git/blob - projects/stargazer/user.h
Добавление исходников
[stg.git] / projects / stargazer / user.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     void            ResetDetailStat();
148     int             SwapDetailStat();
149     int             WriteDetailStat();
150
151     const TARIFF *  GetTariff() const { return tariff; }
152     void            ResetNextTariff() { nextTariff = ""; }
153
154     #ifdef TRAFF_STAT_WITH_PORTS
155     void            AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len);
156     void            AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len);
157     #else
158     void            AddTraffStatU(int dir, uint32_t ip, uint32_t len);
159     void            AddTraffStatD(int dir, uint32_t ip, uint32_t len);
160     #endif
161
162     const DIR_TRAFF & GetSessionUpload() const { return sessionUpload; }
163     const DIR_TRAFF & GetSessionDownload() const { return sessionDownload; }
164
165     bool            GetConnected() const { return connected; }
166     time_t          GetConnectedModificationTime() const { return connected.ModificationTime(); }
167     int             GetAuthorized() const { return authorizedBy.size(); }
168     int             Authorize(uint32_t ip, const string & iface, uint32_t enabledDirs, const BASE_AUTH * auth);
169     void            Unauthorize(const BASE_AUTH * auth);
170     bool            IsAuthorizedBy(const BASE_AUTH * auth) const;
171     void            OnAdd();
172     void            OnDelete();
173
174     int             AddMessage(STG_MSG * msg);
175
176     void            UpdatePingTime(time_t t = 0);
177     time_t          GetPingTime() const { return pingTime; }
178
179     void            PrintUser() const;
180     void            Run();
181
182     const string &  GetStrError() const { return errorStr; }
183
184     USER_PROPERTIES property;
185
186     void            SetDeleted() { deleted = true; }
187     bool            GetDeleted() const { return deleted; }
188
189     time_t          GetLastWriteStatTime() const { return lastWriteStat; }
190
191     void            MidnightResetSessionStat();
192     void            ProcessDayFee();
193     void            SetPrepaidTraff();
194     void            ProcessDayFeeSpread();
195     void            ProcessNewMonth();
196
197     bool            IsInetable();
198     string          GetEnabledDirs();
199
200 private:
201     STG_LOGGER &    WriteServLog;
202
203     void            Connect(bool fakeConnect = false);
204     void            Disconnect(bool fakeDisconnect, const std::string & reason);
205     int             SaveMonthStat(int month, int year);
206
207     int             SendMessage(const STG_MSG & msg);
208     int             RemoveMessage(uint64_t) { return 0; }
209     int             ScanMessage();
210     time_t          lastScanMessages;
211
212     string          login;
213     int             id;
214     bool            __connected;
215     USER_PROPERTY<bool> connected;
216
217     bool            enabledDirs[DIR_NUM];
218
219     USER_ID_GENERATOR userIDGenerator;
220
221     uint32_t        __currIP; // ôÅËÕÝÉÊ ÁÄÒÅÓ ÐÏÌØÚÏ×ÁÔÅÌÑ
222     USER_PROPERTY<uint32_t> currIP;
223
224     /*
225     ë ÔÏÍÕ ÍÏÍÅÎÔÕ ËÁË ÍÙ ÕÖÅ ÎÅ Á×ÔÏÒÉÚÏ×ÁÎÉÙ, ÎÏ ÅÝÅ ÎÅ ×ÙÐÏÌÎÅΠDisconnect,
226     currIP ÕÖÅ ÓÂÒÏÛÅÎ. ðÏÓÌÅÄÎÅÅ ÚÎÁÞÅÎÉÅ currIP ÓÏÈÒÁÎÑÅÍ × lastIPForDisconnect
227     */
228     uint32_t        lastIPForDisconnect;
229
230     time_t          pingTime;
231
232     const ADMIN     sysAdmin;
233     const BASE_STORE * store;
234
235     const TARIFFS * tariffs;
236     const TARIFF *  tariff;
237
238     map<IP_DIR_PAIR, STAT_NODE>     traffStatInternal[2];
239     map<IP_DIR_PAIR, STAT_NODE> *   traffStat;
240     map<IP_DIR_PAIR, STAT_NODE> *   traffStatToWrite;
241     int                             traffStatInUse;
242
243     const SETTINGS * settings;
244
245     set<const BASE_AUTH *> authorizedBy;
246
247     const map<uint32_t, user_iter> * ipIndex;
248
249     bool            deleted;
250
251     time_t          lastWriteStat;           // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÓÔÁÔÉÓÔÉËÉ
252     time_t          lastWriteDeatiledStat;   // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÄÅÔÁÌØÎÏÊ ÓÔÁÔÉÓÔÉËÉ
253     time_t          lastSwapDeatiledStat;    // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÄÅÔÁÌØÎÏÊ ÓÔÁÔÉÓÔÉËÉ
254
255     bool            writeFreeMbTraffCost;
256
257     // Properties
258     USER_PROPERTY<double>         & cash;
259     USER_PROPERTY<DIR_TRAFF>      & up;
260     USER_PROPERTY<DIR_TRAFF>      & down;
261     USER_PROPERTY<double>         & lastCashAdd;
262     USER_PROPERTY<time_t>         & passiveTime;
263     USER_PROPERTY<time_t>         & lastCashAddTime;
264     USER_PROPERTY<double>         & freeMb;
265     USER_PROPERTY<time_t>         & lastActivityTime;
266     USER_PROPERTY<string>         & password;
267     USER_PROPERTY<int>            & passive;
268     USER_PROPERTY<int>            & disabled;
269     USER_PROPERTY<int>            & disabledDetailStat;
270     USER_PROPERTY<int>            & alwaysOnline;
271     USER_PROPERTY<string>         & tariffName;
272     USER_PROPERTY<string>         & nextTariff;
273     USER_PROPERTY<string>         & address;
274     USER_PROPERTY<string>         & note;
275     USER_PROPERTY<string>         & group;
276     USER_PROPERTY<string>         & email;
277     USER_PROPERTY<string>         & phone;
278     USER_PROPERTY<string>         & realName;
279     USER_PROPERTY<double>         & credit;
280     USER_PROPERTY<time_t>         & creditExpire;
281     USER_PROPERTY<USER_IPS>       & ips;
282     USER_PROPERTY<string>         & userdata0;
283     USER_PROPERTY<string>         & userdata1;
284     USER_PROPERTY<string>         & userdata2;
285     USER_PROPERTY<string>         & userdata3;
286     USER_PROPERTY<string>         & userdata4;
287     USER_PROPERTY<string>         & userdata5;
288     USER_PROPERTY<string>         & userdata6;
289     USER_PROPERTY<string>         & userdata7;
290     USER_PROPERTY<string>         & userdata8;
291     USER_PROPERTY<string>         & userdata9;
292
293     // End properties
294
295     DIR_TRAFF                sessionUpload;
296     DIR_TRAFF                sessionDownload;
297
298     CHG_PASSIVE_NOTIFIER     passiveNotifier;
299     CHG_TARIFF_NOTIFIER      tariffNotifier;
300     CHG_CASH_NOTIFIER        cashNotifier;
301     CHG_IP_NOTIFIER          ipNotifier;
302
303     mutable pthread_mutex_t  mutex;
304
305     string                   errorStr;
306 };
307 //-----------------------------------------------------------------------------
308
309 #endif //USER_H