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     void            ResetDetailStat();
 
 148     int             SwapDetailStat();
 
 149     int             WriteDetailStat();
 
 151     const TARIFF *  GetTariff() const { return tariff; }
 
 152     void            ResetNextTariff() { nextTariff = ""; }
 
 154     #ifdef TRAFF_STAT_WITH_PORTS
 
 155     void            AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len);
 
 156     void            AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len);
 
 158     void            AddTraffStatU(int dir, uint32_t ip, uint32_t len);
 
 159     void            AddTraffStatD(int dir, uint32_t ip, uint32_t len);
 
 162     const DIR_TRAFF & GetSessionUpload() const { return sessionUpload; }
 
 163     const DIR_TRAFF & GetSessionDownload() const { return sessionDownload; }
 
 165     bool            GetConnected() const { return connected; }
 
 166     time_t          GetConnectedModificationTime() const { return connected.ModificationTime(); }
 
 167     int             GetAuthorized() const { return authorizedBy.size(); }
 
 168     int             Authorize(uint32_t ip, const string & iface, uint32_t enabledDirs, const BASE_AUTH * auth);
 
 169     void            Unauthorize(const BASE_AUTH * auth);
 
 170     bool            IsAuthorizedBy(const BASE_AUTH * auth) const;
 
 174     int             AddMessage(STG_MSG * msg);
 
 176     void            UpdatePingTime(time_t t = 0);
 
 177     time_t          GetPingTime() const { return pingTime; }
 
 179     void            PrintUser() const;
 
 182     const string &  GetStrError() const { return errorStr; }
 
 184     USER_PROPERTIES property;
 
 186     void            SetDeleted() { deleted = true; }
 
 187     bool            GetDeleted() const { return deleted; }
 
 189     time_t          GetLastWriteStatTime() const { return lastWriteStat; }
 
 191     void            MidnightResetSessionStat();
 
 192     void            ProcessDayFee();
 
 193     void            SetPrepaidTraff();
 
 194     void            ProcessDayFeeSpread();
 
 195     void            ProcessNewMonth();
 
 198     string          GetEnabledDirs();
 
 201     STG_LOGGER &    WriteServLog;
 
 203     void            Connect(bool fakeConnect = false);
 
 204     void            Disconnect(bool fakeDisconnect, const std::string & reason);
 
 205     int             SaveMonthStat(int month, int year);
 
 207     int             SendMessage(const STG_MSG & msg);
 
 208     int             RemoveMessage(uint64_t) { return 0; }
 
 210     time_t          lastScanMessages;
 
 215     USER_PROPERTY<bool> connected;
 
 217     bool            enabledDirs[DIR_NUM];
 
 219     USER_ID_GENERATOR userIDGenerator;
 
 221     uint32_t        __currIP; // ôÅËÕÝÉÊ ÁÄÒÅÓ ÐÏÌØÚÏ×ÁÔÅÌÑ
 
 222     USER_PROPERTY<uint32_t> currIP;
 
 225     ë ÔÏÍÕ ÍÏÍÅÎÔÕ ËÁË ÍÙ ÕÖÅ ÎÅ Á×ÔÏÒÉÚÏ×ÁÎÉÙ, ÎÏ ÅÝÅ ÎÅ ×ÙÐÏÌÎÅΠDisconnect,
 
 226     currIP ÕÖÅ ÓÂÒÏÛÅÎ. ðÏÓÌÅÄÎÅÅ ÚÎÁÞÅÎÉÅ currIP ÓÏÈÒÁÎÑÅÍ × lastIPForDisconnect
 
 228     uint32_t        lastIPForDisconnect;
 
 232     const ADMIN     sysAdmin;
 
 233     const BASE_STORE * store;
 
 235     const TARIFFS * tariffs;
 
 236     const TARIFF *  tariff;
 
 238     map<IP_DIR_PAIR, STAT_NODE>     traffStatInternal[2];
 
 239     map<IP_DIR_PAIR, STAT_NODE> *   traffStat;
 
 240     map<IP_DIR_PAIR, STAT_NODE> *   traffStatToWrite;
 
 243     const SETTINGS * settings;
 
 245     set<const BASE_AUTH *> authorizedBy;
 
 247     const map<uint32_t, user_iter> * ipIndex;
 
 251     time_t          lastWriteStat;           // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÓÔÁÔÉÓÔÉËÉ
 
 252     time_t          lastWriteDeatiledStat;   // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÄÅÔÁÌØÎÏÊ ÓÔÁÔÉÓÔÉËÉ
 
 253     time_t          lastSwapDeatiledStat;    // ÷ÒÅÍÑ ÐÏÓÌÅÄÎÅÊ ÚÁÐÉÓÉ ÄÅÔÁÌØÎÏÊ ÓÔÁÔÉÓÔÉËÉ
 
 255     bool            writeFreeMbTraffCost;
 
 258     USER_PROPERTY<double>         & cash;
 
 259     USER_PROPERTY<DIR_TRAFF>      & up;
 
 260     USER_PROPERTY<DIR_TRAFF>      & down;
 
 261     USER_PROPERTY<double>         & lastCashAdd;
 
 262     USER_PROPERTY<time_t>         & passiveTime;
 
 263     USER_PROPERTY<time_t>         & lastCashAddTime;
 
 264     USER_PROPERTY<double>         & freeMb;
 
 265     USER_PROPERTY<time_t>         & lastActivityTime;
 
 266     USER_PROPERTY<string>         & password;
 
 267     USER_PROPERTY<int>            & passive;
 
 268     USER_PROPERTY<int>            & disabled;
 
 269     USER_PROPERTY<int>            & disabledDetailStat;
 
 270     USER_PROPERTY<int>            & alwaysOnline;
 
 271     USER_PROPERTY<string>         & tariffName;
 
 272     USER_PROPERTY<string>         & nextTariff;
 
 273     USER_PROPERTY<string>         & address;
 
 274     USER_PROPERTY<string>         & note;
 
 275     USER_PROPERTY<string>         & group;
 
 276     USER_PROPERTY<string>         & email;
 
 277     USER_PROPERTY<string>         & phone;
 
 278     USER_PROPERTY<string>         & realName;
 
 279     USER_PROPERTY<double>         & credit;
 
 280     USER_PROPERTY<time_t>         & creditExpire;
 
 281     USER_PROPERTY<USER_IPS>       & ips;
 
 282     USER_PROPERTY<string>         & userdata0;
 
 283     USER_PROPERTY<string>         & userdata1;
 
 284     USER_PROPERTY<string>         & userdata2;
 
 285     USER_PROPERTY<string>         & userdata3;
 
 286     USER_PROPERTY<string>         & userdata4;
 
 287     USER_PROPERTY<string>         & userdata5;
 
 288     USER_PROPERTY<string>         & userdata6;
 
 289     USER_PROPERTY<string>         & userdata7;
 
 290     USER_PROPERTY<string>         & userdata8;
 
 291     USER_PROPERTY<string>         & userdata9;
 
 295     DIR_TRAFF                sessionUpload;
 
 296     DIR_TRAFF                sessionDownload;
 
 298     CHG_PASSIVE_NOTIFIER     passiveNotifier;
 
 299     CHG_TARIFF_NOTIFIER      tariffNotifier;
 
 300     CHG_CASH_NOTIFIER        cashNotifier;
 
 301     CHG_IP_NOTIFIER          ipNotifier;
 
 303     mutable pthread_mutex_t  mutex;
 
 307 //-----------------------------------------------------------------------------