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