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>
23 $Date: 2010/11/03 10:50:03 $
31 #include "stg/user_stat.h"
32 #include "stg/user_conf.h"
33 #include "stg/user_ips.h"
34 #include "stg/user_property.h"
36 #include "stg/message.h"
37 #include "stg/noncopyable.h"
38 #include "stg/os_int.h"
39 #include "stg/const.h"
48 //-----------------------------------------------------------------------------
53 #ifdef USE_ABSTRACT_SETTINGS
58 //-----------------------------------------------------------------------------
59 class USER_ID_GENERATOR {
60 friend class USER_IMPL;
62 USER_ID_GENERATOR() {}
63 int GetNextID() { static int id = 0; return id++; }
65 //-----------------------------------------------------------------------------
66 class CHG_PASSIVE_NOTIFIER : public PROPERTY_NOTIFIER_BASE<int>,
69 CHG_PASSIVE_NOTIFIER(USER_IMPL * u) : user(u) {}
70 void Notify(const int & oldPassive, const int & newPassive);
75 //-----------------------------------------------------------------------------
76 class CHG_DISABLED_NOTIFIER : public PROPERTY_NOTIFIER_BASE<int>,
79 CHG_DISABLED_NOTIFIER(USER_IMPL * u) : user(u) {}
80 void Notify(const int & oldValue, const int & newValue);
85 //-----------------------------------------------------------------------------
86 class CHG_TARIFF_NOTIFIER : public PROPERTY_NOTIFIER_BASE<std::string>,
89 CHG_TARIFF_NOTIFIER(USER_IMPL * u) : user(u) {}
90 void Notify(const std::string & oldTariff, const std::string & newTariff);
95 //-----------------------------------------------------------------------------
96 class CHG_CASH_NOTIFIER : public PROPERTY_NOTIFIER_BASE<double>,
99 CHG_CASH_NOTIFIER(USER_IMPL * u) : user(u) {}
100 void Notify(const double & oldCash, const double & newCash);
105 //-----------------------------------------------------------------------------
106 class CHG_IPS_NOTIFIER : public PROPERTY_NOTIFIER_BASE<USER_IPS>,
107 private NONCOPYABLE {
109 CHG_IPS_NOTIFIER(USER_IMPL * u) : user(u) {}
110 void Notify(const USER_IPS & oldIPs, const USER_IPS & newIPs);
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;
123 #ifdef USE_ABSTRACT_SETTINGS
124 USER_IMPL(const SETTINGS * settings,
126 const TARIFFS * tariffs,
127 const ADMIN * sysAdmin,
130 USER_IMPL(const SETTINGS_IMPL * settings,
132 const TARIFFS * tariffs,
133 const ADMIN * sysAdmin,
136 USER_IMPL(const USER_IMPL & u);
137 virtual ~USER_IMPL();
143 int WriteMonthStat();
145 const std::string & GetLogin() const { return login; }
146 void SetLogin(std::string const & l);
148 uint32_t GetCurrIP() const { return currIP; }
149 time_t GetCurrIPModificationTime() const { return currIP.ModificationTime(); }
151 void AddCurrIPBeforeNotifier(CURR_IP_NOTIFIER * notifier);
152 void DelCurrIPBeforeNotifier(const CURR_IP_NOTIFIER * notifier);
154 void AddCurrIPAfterNotifier(CURR_IP_NOTIFIER * notifier);
155 void DelCurrIPAfterNotifier(const CURR_IP_NOTIFIER * notifier);
157 void AddConnectedBeforeNotifier(CONNECTED_NOTIFIER * notifier);
158 void DelConnectedBeforeNotifier(const CONNECTED_NOTIFIER * notifier);
160 void AddConnectedAfterNotifier(CONNECTED_NOTIFIER * notifier);
161 void DelConnectedAfterNotifier(const CONNECTED_NOTIFIER * notifier);
163 int GetID() const { return id; }
165 double GetPassiveTimePart() const;
166 void ResetPassiveTime() { passiveTime = 0; }
167 void SetPassiveTimeAsNewUser();
169 int WriteDetailStat(bool hard = false);
171 const TARIFF * GetTariff() const { return tariff; }
172 void ResetNextTariff() { nextTariff = ""; }
174 #ifdef TRAFF_STAT_WITH_PORTS
175 void AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len);
176 void AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len);
178 void AddTraffStatU(int dir, uint32_t ip, uint32_t len);
179 void AddTraffStatD(int dir, uint32_t ip, uint32_t len);
182 const DIR_TRAFF & GetSessionUpload() const { return sessionUpload; }
183 const DIR_TRAFF & GetSessionDownload() const { return sessionDownload; }
184 time_t GetSessionUploadModificationTime() const { return sessionUploadModTime; }
185 time_t GetSessionDownloadModificationTime() const { return sessionDownloadModTime; }
187 bool GetConnected() const { return connected; }
188 time_t GetConnectedModificationTime() const { return connected.ModificationTime(); }
189 const std::string & GetLastDisconnectReason() const { return lastDisconnectReason; }
190 int GetAuthorized() const { return static_cast<int>(authorizedBy.size()); }
191 time_t GetAuthorizedModificationTime() const { return authorizedModificationTime; }
192 int Authorize(uint32_t ip, uint32_t enabledDirs, const AUTH * auth);
193 void Unauthorize(const AUTH * auth,
194 const std::string & reason = std::string());
195 bool IsAuthorizedBy(const AUTH * auth) const;
196 std::vector<std::string> GetAuthorizers() const;
198 int AddMessage(STG_MSG * msg);
200 void UpdatePingTime(time_t t = 0);
201 time_t GetPingTime() const { return pingTime; }
205 const std::string & GetStrError() const { return errorStr; }
207 USER_PROPERTIES & GetProperty() { return property; }
208 const USER_PROPERTIES & GetProperty() const { return property; }
210 void SetDeleted() { deleted = true; }
211 bool GetDeleted() const { return deleted; }
213 time_t GetLastWriteStatTime() const { return lastWriteStat; }
215 void MidnightResetSessionStat();
216 void ProcessDayFee();
217 void ProcessDayFeeSpread();
218 void ProcessNewMonth();
219 void ProcessDailyFee();
222 std::string GetEnabledDirs() const;
227 virtual std::string GetParamValue(const std::string & name) const;
230 USER_IMPL & operator=(const USER_IMPL & rvalue);
235 USER_PROPERTIES property;
236 STG_LOGGER & WriteServLog;
238 void Connect(bool fakeConnect = false);
239 void Disconnect(bool fakeDisconnect, const std::string & reason);
240 int SaveMonthStat(int month, int year);
242 void SetPrepaidTraff();
244 int SendMessage(STG_MSG & msg) const;
247 time_t lastScanMessages;
252 USER_PROPERTY<bool> connected;
254 bool enabledDirs[DIR_NUM];
256 USER_ID_GENERATOR userIDGenerator;
258 uint32_t __currIP; // Current user's ip
259 USER_PROPERTY<uint32_t> currIP;
261 uint32_t lastIPForDisconnect; // User's ip after unauth but before disconnect
262 std::string lastDisconnectReason;
266 const ADMIN * sysAdmin;
269 const TARIFFS * tariffs;
270 const TARIFF * tariff;
272 TRAFF_STAT traffStat;
273 std::pair<time_t, TRAFF_STAT> traffStatSaved;
275 #ifdef USE_ABSTRACT_SETTINGS
276 const SETTINGS * settings;
278 const SETTINGS_IMPL * settings;
281 std::set<const AUTH *> authorizedBy;
282 time_t authorizedModificationTime;
284 std::list<STG_MSG> messages;
288 time_t lastWriteStat;
289 time_t lastWriteDetailedStat;
292 USER_PROPERTY<double> & cash;
293 USER_PROPERTY<DIR_TRAFF> & up;
294 USER_PROPERTY<DIR_TRAFF> & down;
295 USER_PROPERTY<double> & lastCashAdd;
296 USER_PROPERTY<time_t> & passiveTime;
297 USER_PROPERTY<time_t> & lastCashAddTime;
298 USER_PROPERTY<double> & freeMb;
299 USER_PROPERTY<time_t> & lastActivityTime;
300 USER_PROPERTY<std::string> & password;
301 USER_PROPERTY<int> & passive;
302 USER_PROPERTY<int> & disabled;
303 USER_PROPERTY<int> & disabledDetailStat;
304 USER_PROPERTY<int> & alwaysOnline;
305 USER_PROPERTY<std::string> & tariffName;
306 USER_PROPERTY<std::string> & nextTariff;
307 USER_PROPERTY<std::string> & address;
308 USER_PROPERTY<std::string> & note;
309 USER_PROPERTY<std::string> & group;
310 USER_PROPERTY<std::string> & email;
311 USER_PROPERTY<std::string> & phone;
312 USER_PROPERTY<std::string> & realName;
313 USER_PROPERTY<double> & credit;
314 USER_PROPERTY<time_t> & creditExpire;
315 USER_PROPERTY<USER_IPS> & ips;
316 USER_PROPERTY<std::string> & userdata0;
317 USER_PROPERTY<std::string> & userdata1;
318 USER_PROPERTY<std::string> & userdata2;
319 USER_PROPERTY<std::string> & userdata3;
320 USER_PROPERTY<std::string> & userdata4;
321 USER_PROPERTY<std::string> & userdata5;
322 USER_PROPERTY<std::string> & userdata6;
323 USER_PROPERTY<std::string> & userdata7;
324 USER_PROPERTY<std::string> & userdata8;
325 USER_PROPERTY<std::string> & userdata9;
329 DIR_TRAFF sessionUpload;
330 DIR_TRAFF sessionDownload;
331 time_t sessionUploadModTime;
332 time_t sessionDownloadModTime;
334 CHG_PASSIVE_NOTIFIER passiveNotifier;
335 CHG_DISABLED_NOTIFIER disabledNotifier;
336 CHG_TARIFF_NOTIFIER tariffNotifier;
337 CHG_CASH_NOTIFIER cashNotifier;
338 CHG_IPS_NOTIFIER ipNotifier;
340 mutable pthread_mutex_t mutex;
342 std::string errorStr;
344 //-----------------------------------------------------------------------------
346 typedef USER_IMPL * USER_IMPL_PTR;