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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
24 #include "stg/user_stat.h"
25 #include "stg/user_conf.h"
26 #include "stg/user_ips.h"
27 #include "stg/user_property.h"
29 #include "stg/message.h"
30 #include "stg/noncopyable.h"
31 #include "stg/const.h"
43 //-----------------------------------------------------------------------------
48 #ifdef USE_ABSTRACT_SETTINGS
53 //-----------------------------------------------------------------------------
54 class UserImpl : public User
57 #ifdef USE_ABSTRACT_SETTINGS
58 using Settings = STG::Settings;
60 using Settings = STG::SettingsImpl;
62 UserImpl(const Settings * settings,
64 const Tariffs * tariffs,
65 const Admin * sysAdmin,
67 const Services & svcs);
68 UserImpl(const UserImpl & u);
73 int WriteConf() override;
74 int WriteStat() override;
77 const std::string & GetLogin() const override { return login; }
78 void SetLogin(std::string const & l);
80 int GetID() const override { return id; }
82 double GetPassiveTimePart() const override;
83 void ResetPassiveTime() { passiveTime = 0; }
84 void SetPassiveTimeAsNewUser();
86 int WriteDetailStat(bool hard = false);
88 const Tariff * GetTariff() const override { return tariff; }
89 void ResetNextTariff() override { nextTariff = ""; }
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);
95 void AddTraffStatU(int dir, uint32_t ip, uint32_t len);
96 void AddTraffStatD(int dir, uint32_t ip, uint32_t len);
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; }
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;
113 int AddMessage(Message * msg) override;
115 void UpdatePingTime(time_t t = 0) override;
116 time_t GetPingTime() const override { return pingTime; }
120 const std::string & GetStrError() const override { return errorStr; }
122 UserProperties & GetProperties() override { return properties; }
123 const UserProperties & GetProperties() const override { return properties; }
125 void SetDeleted() override { deleted = true; }
126 bool GetDeleted() const override { return deleted; }
128 time_t GetLastWriteStatTime() const override { return lastWriteStat; }
130 void MidnightResetSessionStat();
131 void ProcessDayFee();
132 void ProcessDayFeeSpread();
133 void ProcessNewMonth();
134 void ProcessDailyFee();
135 void ProcessServices();
137 bool IsInetable() override;
138 std::string GetEnabledDirs() const override;
140 void OnAdd() override;
141 void OnDelete() override;
143 virtual std::string GetParamValue(const std::string & name) const override;
146 UserImpl & operator=(const UserImpl & rvalue);
151 UserProperties properties;
152 STG::Logger& WriteServLog;
154 void Connect(bool fakeConnect = false);
155 void Disconnect(bool fakeDisconnect, const std::string & reason);
156 int SaveMonthStat(int month, int year);
158 void SetPrepaidTraff();
160 int SendMessage(Message & msg) const;
163 time_t lastScanMessages;
168 bool enabledDirs[DIR_NUM];
170 uint32_t lastIPForDisconnect; // User's ip after unauth but before disconnect
171 std::string lastDisconnectReason;
175 const Admin * sysAdmin;
178 const Tariffs * tariffs;
179 const Tariff * tariff;
181 const Services & m_services;
184 std::pair<time_t, TraffStat> traffStatSaved;
186 const Settings * settings;
188 std::set<const Auth *> authorizedBy;
189 time_t authorizedModificationTime;
191 std::vector<Message> messages;
195 time_t lastWriteStat;
196 time_t lastWriteDetailedStat;
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;
236 DirTraff sessionUpload;
237 DirTraff sessionDownload;
238 time_t sessionUploadModTime;
239 time_t sessionDownloadModTime;
241 ScopedConnection m_beforePassiveConn;
242 void onPassiveChange(int oldVal, int newVal);
244 ScopedConnection m_afterDisabledConn;
245 void onDisabledChange(int oldVal, int newVal);
247 ScopedConnection m_beforeTariffConn;
248 void onTariffChange(const std::string& oldVal, const std::string& newVal);
250 ScopedConnection m_beforeCashConn;
251 void onCashChange(double oldVal, double newVal);
253 ScopedConnection m_afterIPConn;
254 void onIPChange(const UserIPs& oldVal, const UserIPs& newVal);
256 mutable pthread_mutex_t mutex;
258 std::string errorStr;
260 //-----------------------------------------------------------------------------