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 CHG_PASSIVE_NOTIFIER : public PropertyNotifierBase<int> {
56 explicit CHG_PASSIVE_NOTIFIER(UserImpl * u) : user(u) {}
57 void Notify(const int & oldPassive, const int & newPassive);
62 //-----------------------------------------------------------------------------
63 class CHG_DISABLED_NOTIFIER : public PropertyNotifierBase<int> {
65 explicit CHG_DISABLED_NOTIFIER(UserImpl * u) : user(u) {}
66 void Notify(const int & oldValue, const int & newValue);
71 //-----------------------------------------------------------------------------
72 class CHG_TARIFF_NOTIFIER : public PropertyNotifierBase<std::string> {
74 explicit CHG_TARIFF_NOTIFIER(UserImpl * u) : user(u) {}
75 void Notify(const std::string & oldTariff, const std::string & newTariff);
80 //-----------------------------------------------------------------------------
81 class CHG_CASH_NOTIFIER : public PropertyNotifierBase<double> {
83 explicit CHG_CASH_NOTIFIER(UserImpl * u) : user(u) {}
84 void Notify(const double & oldCash, const double & newCash);
89 //-----------------------------------------------------------------------------
90 class CHG_IPS_NOTIFIER : public PropertyNotifierBase<UserIPs> {
92 explicit CHG_IPS_NOTIFIER(UserImpl * u) : user(u) {}
93 void Notify(const UserIPs & oldIPs, const UserIPs & newIPs);
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;
106 #ifdef USE_ABSTRACT_SETTINGS
107 using Settings = STG::Settings;
109 using Settings = STG::SettingsImpl;
111 UserImpl(const Settings * settings,
113 const Tariffs * tariffs,
114 const Admin * sysAdmin,
116 const Services & svcs);
117 UserImpl(const UserImpl & u);
122 int WriteConf() override;
123 int WriteStat() override;
124 int WriteMonthStat();
126 const std::string & GetLogin() const override { return login; }
127 void SetLogin(std::string const & l);
129 uint32_t GetCurrIP() const override{ return currIP; }
130 time_t GetCurrIPModificationTime() const override { return currIP.ModificationTime(); }
132 void AddCurrIPBeforeNotifier(CURR_IP_NOTIFIER * notifier) override;
133 void DelCurrIPBeforeNotifier(const CURR_IP_NOTIFIER * notifier) override;
135 void AddCurrIPAfterNotifier(CURR_IP_NOTIFIER * notifier) override;
136 void DelCurrIPAfterNotifier(const CURR_IP_NOTIFIER * notifier) override;
138 void AddConnectedBeforeNotifier(CONNECTED_NOTIFIER * notifier) override;
139 void DelConnectedBeforeNotifier(const CONNECTED_NOTIFIER * notifier) override;
141 void AddConnectedAfterNotifier(CONNECTED_NOTIFIER * notifier) override;
142 void DelConnectedAfterNotifier(const CONNECTED_NOTIFIER * notifier) override;
144 int GetID() const override { return id; }
146 double GetPassiveTimePart() const override;
147 void ResetPassiveTime() { passiveTime = 0; }
148 void SetPassiveTimeAsNewUser();
150 int WriteDetailStat(bool hard = false);
152 const Tariff * GetTariff() const override { return tariff; }
153 void ResetNextTariff() override { nextTariff = ""; }
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);
159 void AddTraffStatU(int dir, uint32_t ip, uint32_t len);
160 void AddTraffStatD(int dir, uint32_t ip, uint32_t len);
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; }
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;
179 int AddMessage(Message * msg) override;
181 void UpdatePingTime(time_t t = 0) override;
182 time_t GetPingTime() const override { return pingTime; }
186 const std::string & GetStrError() const override { return errorStr; }
188 UserProperties & GetProperties() override { return properties; }
189 const UserProperties & GetProperties() const override { return properties; }
191 void SetDeleted() override { deleted = true; }
192 bool GetDeleted() const override { return deleted; }
194 time_t GetLastWriteStatTime() const override { return lastWriteStat; }
196 void MidnightResetSessionStat();
197 void ProcessDayFee();
198 void ProcessDayFeeSpread();
199 void ProcessNewMonth();
200 void ProcessDailyFee();
201 void ProcessServices();
203 bool IsInetable() override;
204 std::string GetEnabledDirs() const override;
206 void OnAdd() override;
207 void OnDelete() override;
209 virtual std::string GetParamValue(const std::string & name) const override;
212 UserImpl & operator=(const UserImpl & rvalue);
217 UserProperties properties;
218 STG::Logger& WriteServLog;
220 void Connect(bool fakeConnect = false);
221 void Disconnect(bool fakeDisconnect, const std::string & reason);
222 int SaveMonthStat(int month, int year);
224 void SetPrepaidTraff();
226 int SendMessage(Message & msg) const;
229 time_t lastScanMessages;
234 UserProperty<bool> connected;
236 bool enabledDirs[DIR_NUM];
238 uint32_t __currIP; // Current user's ip
239 UserProperty<uint32_t> currIP;
241 uint32_t lastIPForDisconnect; // User's ip after unauth but before disconnect
242 std::string lastDisconnectReason;
246 const Admin * sysAdmin;
249 const Tariffs * tariffs;
250 const Tariff * tariff;
252 const Services & m_services;
255 std::pair<time_t, TraffStat> traffStatSaved;
257 const Settings * settings;
259 std::set<const Auth *> authorizedBy;
260 time_t authorizedModificationTime;
262 std::vector<Message> messages;
266 time_t lastWriteStat;
267 time_t lastWriteDetailedStat;
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;
307 DirTraff sessionUpload;
308 DirTraff sessionDownload;
309 time_t sessionUploadModTime;
310 time_t sessionDownloadModTime;
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;
318 mutable pthread_mutex_t mutex;
320 std::string errorStr;
322 //-----------------------------------------------------------------------------