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 $
36 #include "stg/os_int.h"
37 #include "stg/const.h"
38 #include "stg/user_stat.h"
39 #include "stg/user_conf.h"
40 #include "stg/user_ips.h"
41 #include "stg/user_property.h"
43 #include "stg/message.h"
44 #include "stg/noncopyable.h"
46 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 class USER_ID_GENERATOR {
54 friend class USER_IMPL;
56 USER_ID_GENERATOR() {}
57 int GetNextID() { static int id = 0; return id++; }
59 //-----------------------------------------------------------------------------
60 class CHG_PASSIVE_NOTIFIER : public PROPERTY_NOTIFIER_BASE<int>,
63 CHG_PASSIVE_NOTIFIER(USER_IMPL * u) : user(u) {}
64 void Notify(const int & oldPassive, const int & newPassive);
69 //-----------------------------------------------------------------------------
70 class CHG_TARIFF_NOTIFIER : public PROPERTY_NOTIFIER_BASE<std::string>,
73 CHG_TARIFF_NOTIFIER(USER_IMPL * u) : user(u) {}
74 void Notify(const std::string & oldTariff, const std::string & newTariff);
79 //-----------------------------------------------------------------------------
80 class CHG_CASH_NOTIFIER : public PROPERTY_NOTIFIER_BASE<double>,
83 CHG_CASH_NOTIFIER(USER_IMPL * u) : user(u) {}
84 void Notify(const double & oldCash, const double & newCash);
89 //-----------------------------------------------------------------------------
90 class CHG_IP_NOTIFIER : public PROPERTY_NOTIFIER_BASE<uint32_t>,
93 CHG_IP_NOTIFIER(USER_IMPL * u) : user(u) {}
94 void Notify(const uint32_t & oldCash, const uint32_t & newCash);
99 //-----------------------------------------------------------------------------
100 class USER_IMPL : public USER {
101 friend class CHG_PASSIVE_NOTIFIER;
102 friend class CHG_TARIFF_NOTIFIER;
103 friend class CHG_CASH_NOTIFIER;
104 friend class CHG_IP_NOTIFIER;
106 USER_IMPL(const SETTINGS_IMPL * settings,
108 const TARIFFS * tariffs,
109 const ADMIN * sysAdmin,
111 USER_IMPL(const USER_IMPL & u);
112 virtual ~USER_IMPL();
118 int WriteMonthStat();
120 const std::string & GetLogin() const { return login; }
121 void SetLogin(std::string const & l);
123 uint32_t GetCurrIP() const { return currIP; }
124 time_t GetCurrIPModificationTime() const { return currIP.ModificationTime(); }
126 void AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
127 void DelCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
129 void AddCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
130 void DelCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
132 void AddConnectedBeforeNotifier(PROPERTY_NOTIFIER_BASE<bool> *);
133 void DelConnectedBeforeNotifier(PROPERTY_NOTIFIER_BASE<bool> *);
135 void AddConnectedAfterNotifier(PROPERTY_NOTIFIER_BASE<bool> *);
136 void DelConnectedAfterNotifier(PROPERTY_NOTIFIER_BASE<bool> *);
138 int GetID() const { return id; }
140 double GetPassiveTimePart() const;
141 void ResetPassiveTime() { passiveTime = 0; }
142 void SetPassiveTimeAsNewUser();
144 int WriteDetailStat(bool hard = false);
146 const TARIFF * GetTariff() const { return tariff; }
147 void ResetNextTariff() { nextTariff = ""; }
149 #ifdef TRAFF_STAT_WITH_PORTS
150 void AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len);
151 void AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len);
153 void AddTraffStatU(int dir, uint32_t ip, uint32_t len);
154 void AddTraffStatD(int dir, uint32_t ip, uint32_t len);
157 const DIR_TRAFF & GetSessionUpload() const { return sessionUpload; }
158 const DIR_TRAFF & GetSessionDownload() const { return sessionDownload; }
160 bool GetConnected() const { return connected; }
161 time_t GetConnectedModificationTime() const { return connected.ModificationTime(); }
162 int GetAuthorized() const { return authorizedBy.size(); }
163 int Authorize(uint32_t ip, uint32_t enabledDirs, const AUTH * auth);
164 void Unauthorize(const AUTH * auth);
165 bool IsAuthorizedBy(const AUTH * auth) const;
167 int AddMessage(STG_MSG * msg);
169 void UpdatePingTime(time_t t = 0);
170 time_t GetPingTime() const { return pingTime; }
172 void PrintUser() const;
175 const std::string & GetStrError() const { return errorStr; }
177 USER_PROPERTIES & GetProperty() { return property; };
178 const USER_PROPERTIES & GetProperty() const { return property; };
180 void SetDeleted() { deleted = true; }
181 bool GetDeleted() const { return deleted; }
183 time_t GetLastWriteStatTime() const { return lastWriteStat; }
185 void MidnightResetSessionStat();
186 void ProcessDayFee();
187 void SetPrepaidTraff();
188 void ProcessDayFeeSpread();
189 void ProcessNewMonth();
192 std::string GetEnabledDirs();
199 USER_PROPERTIES property;
200 STG_LOGGER & WriteServLog;
202 void Connect(bool fakeConnect = false);
203 void Disconnect(bool fakeDisconnect, const std::string & reason);
204 int SaveMonthStat(int month, int year);
206 int SendMessage(STG_MSG & msg) const;
209 time_t lastScanMessages;
214 USER_PROPERTY<bool> connected;
216 bool enabledDirs[DIR_NUM];
218 USER_ID_GENERATOR userIDGenerator;
220 uint32_t __currIP; // Current user's ip
221 USER_PROPERTY<uint32_t> currIP;
223 uint32_t lastIPForDisconnect; // User's ip after unauth but before disconnect
227 const ADMIN * sysAdmin;
230 const TARIFFS * tariffs;
231 const TARIFF * tariff;
233 TRAFF_STAT traffStat;
234 std::pair<time_t, TRAFF_STAT> traffStatSaved;
236 const SETTINGS_IMPL * settings;
238 std::set<const AUTH *> authorizedBy;
240 std::list<STG_MSG> messages;
244 time_t lastWriteStat;
245 time_t lastWriteDetailedStat;
248 USER_PROPERTY<double> & cash;
249 USER_PROPERTY<DIR_TRAFF> & up;
250 USER_PROPERTY<DIR_TRAFF> & down;
251 USER_PROPERTY<double> & lastCashAdd;
252 USER_PROPERTY<time_t> & passiveTime;
253 USER_PROPERTY<time_t> & lastCashAddTime;
254 USER_PROPERTY<double> & freeMb;
255 USER_PROPERTY<time_t> & lastActivityTime;
256 USER_PROPERTY<std::string> & password;
257 USER_PROPERTY<int> & passive;
258 USER_PROPERTY<int> & disabled;
259 USER_PROPERTY<int> & disabledDetailStat;
260 USER_PROPERTY<int> & alwaysOnline;
261 USER_PROPERTY<std::string> & tariffName;
262 USER_PROPERTY<std::string> & nextTariff;
263 USER_PROPERTY<std::string> & address;
264 USER_PROPERTY<std::string> & note;
265 USER_PROPERTY<std::string> & group;
266 USER_PROPERTY<std::string> & email;
267 USER_PROPERTY<std::string> & phone;
268 USER_PROPERTY<std::string> & realName;
269 USER_PROPERTY<double> & credit;
270 USER_PROPERTY<time_t> & creditExpire;
271 USER_PROPERTY<USER_IPS> & ips;
272 USER_PROPERTY<std::string> & userdata0;
273 USER_PROPERTY<std::string> & userdata1;
274 USER_PROPERTY<std::string> & userdata2;
275 USER_PROPERTY<std::string> & userdata3;
276 USER_PROPERTY<std::string> & userdata4;
277 USER_PROPERTY<std::string> & userdata5;
278 USER_PROPERTY<std::string> & userdata6;
279 USER_PROPERTY<std::string> & userdata7;
280 USER_PROPERTY<std::string> & userdata8;
281 USER_PROPERTY<std::string> & userdata9;
285 DIR_TRAFF sessionUpload;
286 DIR_TRAFF sessionDownload;
288 CHG_PASSIVE_NOTIFIER passiveNotifier;
289 CHG_TARIFF_NOTIFIER tariffNotifier;
290 CHG_CASH_NOTIFIER cashNotifier;
291 CHG_IP_NOTIFIER ipNotifier;
293 mutable pthread_mutex_t mutex;
295 std::string errorStr;
297 //-----------------------------------------------------------------------------
299 typedef USER_IMPL * USER_IMPL_PTR;