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