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