]> git.stg.codes - stg.git/blob - projects/stargazer/user_impl.h
7e14311ce6b09854b1cad7fcd5eee310d6cb4c59
[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 class Tariffs;
46 class Admin;
47 class UserImpl;
48 #ifdef USE_ABSTRACT_SETTINGS
49 struct Settings;
50 #else
51 class SettingsImpl;
52 #endif
53 //-----------------------------------------------------------------------------
54 class UserImpl : public User
55 {
56     public:
57 #ifdef USE_ABSTRACT_SETTINGS
58         using Settings = STG::Settings;
59 #else
60         using Settings = STG::SettingsImpl;
61 #endif
62         UserImpl(const Settings * settings,
63                   const Store * store,
64                   const Tariffs * tariffs,
65                   const Admin * sysAdmin,
66                   const Users * u,
67                   const Services & svcs);
68         UserImpl(const UserImpl & u);
69         virtual ~UserImpl();
70
71         int             ReadConf();
72         int             ReadStat();
73         int             WriteConf() override;
74         int             WriteStat() override;
75         int             WriteMonthStat();
76
77         const std::string & GetLogin() const override { return login; }
78         void            SetLogin(std::string const & l);
79
80         int             GetID() const override { return id; }
81
82         double          GetPassiveTimePart() const override;
83         void            ResetPassiveTime() { passiveTime = 0; }
84         void            SetPassiveTimeAsNewUser();
85
86         int             WriteDetailStat(bool hard = false);
87
88         const Tariff *  GetTariff() const override { return tariff; }
89         void            ResetNextTariff() override { nextTariff = ""; }
90
91         #ifdef TRAFF_STAT_WITH_PORTS
92         void            AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len);
93         void            AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len);
94         #else
95         void            AddTraffStatU(int dir, uint32_t ip, uint32_t len);
96         void            AddTraffStatD(int dir, uint32_t ip, uint32_t len);
97         #endif
98
99         const DirTraff & GetSessionUpload() const override { return sessionUpload; }
100         const DirTraff & GetSessionDownload() const override { return sessionDownload; }
101         time_t GetSessionUploadModificationTime() const override { return sessionUploadModTime; }
102         time_t GetSessionDownloadModificationTime() const override { return sessionDownloadModTime; }
103
104         const std::string & GetLastDisconnectReason() const override { return lastDisconnectReason; }
105         int             GetAuthorized() const override { return static_cast<int>(authorizedBy.size()); }
106         time_t          GetAuthorizedModificationTime() const override { return authorizedModificationTime; }
107         int             Authorize(uint32_t ip, uint32_t enabledDirs, const Auth * auth);
108         void            Unauthorize(const Auth * auth,
109                                     const std::string & reason = std::string());
110         bool            IsAuthorizedBy(const Auth * auth) const override;
111         std::vector<std::string> GetAuthorizers() const override;
112
113         int             AddMessage(Message * msg) override;
114
115         void            UpdatePingTime(time_t t = 0) override;
116         time_t          GetPingTime() const override { return pingTime; }
117
118         void            Run() override;
119
120         const std::string & GetStrError() const override { return errorStr; }
121
122         UserProperties & GetProperties() override { return properties; }
123         const UserProperties & GetProperties() const override { return properties; }
124
125         void            SetDeleted() override { deleted = true; }
126         bool            GetDeleted() const override { return deleted; }
127
128         time_t          GetLastWriteStatTime() const override { return lastWriteStat; }
129
130         void            MidnightResetSessionStat();
131         void            ProcessDayFee();
132         void            ProcessDayFeeSpread();
133         void            ProcessNewMonth();
134         void            ProcessDailyFee();
135         void            ProcessServices();
136
137         bool            IsInetable() override;
138         std::string     GetEnabledDirs() const override;
139
140         void            OnAdd() override;
141         void            OnDelete() override;
142
143         virtual std::string GetParamValue(const std::string & name) const override;
144
145     private:
146         UserImpl & operator=(const UserImpl & rvalue);
147
148         void            Init();
149
150         const Users*   users;
151         UserProperties properties;
152         STG::Logger&   WriteServLog;
153
154         void            Connect(bool fakeConnect = false);
155         void            Disconnect(bool fakeDisconnect, const std::string & reason);
156         int             SaveMonthStat(int month, int year);
157
158         void            SetPrepaidTraff();
159
160         int             SendMessage(Message & msg) const;
161         void            ScanMessage();
162
163         time_t          lastScanMessages;
164
165         std::string     login;
166         int             id;
167
168         bool            enabledDirs[DIR_NUM];
169
170         uint32_t        lastIPForDisconnect; // User's ip after unauth but before disconnect
171         std::string     lastDisconnectReason;
172
173         time_t          pingTime;
174
175         const Admin *   sysAdmin;
176         const Store *   store;
177
178         const Tariffs * tariffs;
179         const Tariff *  tariff;
180
181         const Services & m_services;
182
183         TraffStat      traffStat;
184         std::pair<time_t, TraffStat> traffStatSaved;
185
186         const Settings * settings;
187
188         std::set<const Auth *> authorizedBy;
189         time_t          authorizedModificationTime;
190
191         std::vector<Message> messages;
192
193         bool            deleted;
194
195         time_t          lastWriteStat;
196         time_t          lastWriteDetailedStat;
197
198         // Properties
199         UserProperty<double>         & cash;
200         UserProperty<DirTraff>      & up;
201         UserProperty<DirTraff>      & down;
202         UserProperty<double>         & lastCashAdd;
203         UserProperty<time_t>         & passiveTime;
204         UserProperty<time_t>         & lastCashAddTime;
205         UserProperty<double>         & freeMb;
206         UserProperty<time_t>         & lastActivityTime;
207         UserProperty<std::string>    & password;
208         UserProperty<int>            & passive;
209         UserProperty<int>            & disabled;
210         UserProperty<int>            & disabledDetailStat;
211         UserProperty<int>            & alwaysOnline;
212         UserProperty<std::string>    & tariffName;
213         UserProperty<std::string>    & nextTariff;
214         UserProperty<std::string>    & address;
215         UserProperty<std::string>    & note;
216         UserProperty<std::string>    & group;
217         UserProperty<std::string>    & email;
218         UserProperty<std::string>    & phone;
219         UserProperty<std::string>    & realName;
220         UserProperty<double>         & credit;
221         UserProperty<time_t>         & creditExpire;
222         UserProperty<UserIPs>       & ips;
223         UserProperty<std::string>    & userdata0;
224         UserProperty<std::string>    & userdata1;
225         UserProperty<std::string>    & userdata2;
226         UserProperty<std::string>    & userdata3;
227         UserProperty<std::string>    & userdata4;
228         UserProperty<std::string>    & userdata5;
229         UserProperty<std::string>    & userdata6;
230         UserProperty<std::string>    & userdata7;
231         UserProperty<std::string>    & userdata8;
232         UserProperty<std::string>    & userdata9;
233
234         // End properties
235
236         DirTraff                sessionUpload;
237         DirTraff                sessionDownload;
238         time_t                   sessionUploadModTime;
239         time_t                   sessionDownloadModTime;
240
241         ScopedConnection m_beforePassiveConn;
242         void onPassiveChange(int oldVal, int newVal);
243
244         ScopedConnection m_afterDisabledConn;
245         void onDisabledChange(int oldVal, int newVal);
246
247         ScopedConnection m_beforeTariffConn;
248         void onTariffChange(const std::string& oldVal, const std::string& newVal);
249
250         ScopedConnection m_beforeCashConn;
251         void onCashChange(double oldVal, double newVal);
252
253         ScopedConnection m_afterIPConn;
254         void onIPChange(const UserIPs& oldVal, const UserIPs& newVal);
255
256         mutable pthread_mutex_t  mutex;
257
258         std::string              errorStr;
259 };
260 //-----------------------------------------------------------------------------
261
262 }