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 $
37 #include "stg_const.h"
38 #include "user_stat.h"
39 #include "user_conf.h"
41 #include "user_property.h"
42 #include "base_auth.h"
43 #include "stg_message.h"
44 #include "noncopyable.h"
48 //-----------------------------------------------------------------------------
53 typedef list<USER>::iterator user_iter;
54 typedef list<USER>::const_iterator const_user_iter;
55 //-----------------------------------------------------------------------------
56 class USER_ID_GENERATOR
60 USER_ID_GENERATOR() {}
61 int GetNextID() { static int id = 0; return id++; }
63 //-----------------------------------------------------------------------------
64 class CHG_PASSIVE_NOTIFIER : public PROPERTY_NOTIFIER_BASE<int>,
68 CHG_PASSIVE_NOTIFIER(USER * u) : user(u) {}
69 void Notify(const int & oldPassive, const int & newPassive);
74 //-----------------------------------------------------------------------------
75 class CHG_TARIFF_NOTIFIER : public PROPERTY_NOTIFIER_BASE<string>,
79 CHG_TARIFF_NOTIFIER(USER * u) : user(u) {}
80 void Notify(const string & oldTariff, const string & newTariff);
85 //-----------------------------------------------------------------------------
86 class CHG_CASH_NOTIFIER : public PROPERTY_NOTIFIER_BASE<double>,
90 CHG_CASH_NOTIFIER(USER * u) : user(u) {}
91 void Notify(const double & oldCash, const double & newCash);
96 //-----------------------------------------------------------------------------
97 class CHG_IP_NOTIFIER : public PROPERTY_NOTIFIER_BASE<uint32_t>,
101 CHG_IP_NOTIFIER(USER * u) : user(u) {}
102 void Notify(const uint32_t & oldCash, const uint32_t & newCash);
107 //-----------------------------------------------------------------------------
110 friend class CHG_PASSIVE_NOTIFIER;
111 friend class CHG_TARIFF_NOTIFIER;
112 friend class CHG_CASH_NOTIFIER;
113 friend class CHG_IP_NOTIFIER;
115 USER(const SETTINGS * settings,
116 const BASE_STORE * store,
117 const TARIFFS * tariffs,
118 const ADMIN & sysAdmin,
119 const map<uint32_t, user_iter> * ipIndex);
120 USER(const USER & u);
127 int WriteMonthStat();
129 string const & GetLogin() const { return login; }
130 void SetLogin(string const & l);
132 uint32_t GetCurrIP() const { return currIP; }
133 time_t GetCurrIPModificationTime() const { return currIP.ModificationTime(); }
135 void AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
136 void DelCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
138 void AddCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
139 void DelCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> *);
141 int GetID() const { return id; }
143 double GetPassiveTimePart() const;
144 void ResetPassiveTime() { passiveTime = 0; }
145 void SetPassiveTimeAsNewUser();
147 int WriteDetailStat(bool hard = false);
149 const TARIFF * GetTariff() const { return tariff; }
150 void ResetNextTariff() { nextTariff = ""; }
152 #ifdef TRAFF_STAT_WITH_PORTS
153 void AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len);
154 void AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len);
156 void AddTraffStatU(int dir, uint32_t ip, uint32_t len);
157 void AddTraffStatD(int dir, uint32_t ip, uint32_t len);
160 const DIR_TRAFF & GetSessionUpload() const { return sessionUpload; }
161 const DIR_TRAFF & GetSessionDownload() const { return sessionDownload; }
163 bool GetConnected() const { return connected; }
164 time_t GetConnectedModificationTime() const { return connected.ModificationTime(); }
165 int GetAuthorized() const { return authorizedBy.size(); }
166 int Authorize(uint32_t ip, const string & iface, uint32_t enabledDirs, const BASE_AUTH * auth);
167 void Unauthorize(const BASE_AUTH * auth);
168 bool IsAuthorizedBy(const BASE_AUTH * auth) const;
172 int AddMessage(STG_MSG * msg);
174 void UpdatePingTime(time_t t = 0);
175 time_t GetPingTime() const { return pingTime; }
177 void PrintUser() const;
180 const string & GetStrError() const { return errorStr; }
182 USER_PROPERTIES property;
184 void SetDeleted() { deleted = true; }
185 bool GetDeleted() const { return deleted; }
187 time_t GetLastWriteStatTime() const { return lastWriteStat; }
189 void MidnightResetSessionStat();
190 void ProcessDayFee();
191 void SetPrepaidTraff();
192 void ProcessDayFeeSpread();
193 void ProcessNewMonth();
196 string GetEnabledDirs();
199 STG_LOGGER & WriteServLog;
201 void Connect(bool fakeConnect = false);
202 void Disconnect(bool fakeDisconnect, const std::string & reason);
203 int SaveMonthStat(int month, int year);
205 int SendMessage(const STG_MSG & msg);
207 time_t lastScanMessages;
212 USER_PROPERTY<bool> connected;
214 bool enabledDirs[DIR_NUM];
216 USER_ID_GENERATOR userIDGenerator;
218 uint32_t __currIP; // ôÅËÕÝÉÊ ÁÄÒÅÓ ÐÏÌØÚÏ×ÁÔÅÌÑ
219 USER_PROPERTY<uint32_t> currIP;
222 ë ÔÏÍÕ ÍÏÍÅÎÔÕ ËÁË ÍÙ ÕÖÅ ÎÅ Á×ÔÏÒÉÚÏ×ÁÎÉÙ, ÎÏ ÅÝÅ ÎÅ ×ÙÐÏÌÎÅÎ Disconnect,
223 currIP ÕÖÅ ÓÂÒÏÛÅÎ. ðÏÓÌÅÄÎÅÅ ÚÎÁÞÅÎÉÅ currIP ÓÏÈÒÁÎÑÅÍ × lastIPForDisconnect
225 uint32_t lastIPForDisconnect;
229 const ADMIN sysAdmin;
230 const BASE_STORE * store;
232 const TARIFFS * tariffs;
233 const TARIFF * tariff;
235 TRAFF_STAT traffStat;
236 std::pair<time_t, TRAFF_STAT> traffStatSaved;
238 const SETTINGS * settings;
240 set<const BASE_AUTH *> authorizedBy;
242 const map<uint32_t, user_iter> * ipIndex;
244 list<STG_MSG> messages;
248 time_t lastWriteStat; // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÓÔÁÔÉÓÔÉËÉ
249 time_t lastWriteDeatiledStat; // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÄÅÔÁÌØÎÏÊ ÓÔÁÔÉÓÔÉËÉ
251 bool writeFreeMbTraffCost;
254 USER_PROPERTY<double> & cash;
255 USER_PROPERTY<DIR_TRAFF> & up;
256 USER_PROPERTY<DIR_TRAFF> & down;
257 USER_PROPERTY<double> & lastCashAdd;
258 USER_PROPERTY<time_t> & passiveTime;
259 USER_PROPERTY<time_t> & lastCashAddTime;
260 USER_PROPERTY<double> & freeMb;
261 USER_PROPERTY<time_t> & lastActivityTime;
262 USER_PROPERTY<string> & password;
263 USER_PROPERTY<int> & passive;
264 USER_PROPERTY<int> & disabled;
265 USER_PROPERTY<int> & disabledDetailStat;
266 USER_PROPERTY<int> & alwaysOnline;
267 USER_PROPERTY<string> & tariffName;
268 USER_PROPERTY<string> & nextTariff;
269 USER_PROPERTY<string> & address;
270 USER_PROPERTY<string> & note;
271 USER_PROPERTY<string> & group;
272 USER_PROPERTY<string> & email;
273 USER_PROPERTY<string> & phone;
274 USER_PROPERTY<string> & realName;
275 USER_PROPERTY<double> & credit;
276 USER_PROPERTY<time_t> & creditExpire;
277 USER_PROPERTY<USER_IPS> & ips;
278 USER_PROPERTY<string> & userdata0;
279 USER_PROPERTY<string> & userdata1;
280 USER_PROPERTY<string> & userdata2;
281 USER_PROPERTY<string> & userdata3;
282 USER_PROPERTY<string> & userdata4;
283 USER_PROPERTY<string> & userdata5;
284 USER_PROPERTY<string> & userdata6;
285 USER_PROPERTY<string> & userdata7;
286 USER_PROPERTY<string> & userdata8;
287 USER_PROPERTY<string> & userdata9;
291 DIR_TRAFF sessionUpload;
292 DIR_TRAFF sessionDownload;
294 CHG_PASSIVE_NOTIFIER passiveNotifier;
295 CHG_TARIFF_NOTIFIER tariffNotifier;
296 CHG_CASH_NOTIFIER cashNotifier;
297 CHG_IP_NOTIFIER ipNotifier;
299 mutable pthread_mutex_t mutex;
303 //-----------------------------------------------------------------------------