From: Naffanya Date: Wed, 29 Jan 2014 21:04:05 +0000 (+0200) Subject: Search parameter in map case insensitive X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/b3139bf3f37b3b0244efea8b4b5e5a7d0bc90095?hp=0e90b234f60ed61562ed710190ed1a62ba82e31f Search parameter in map case insensitive --- diff --git a/include/stg/user.h b/include/stg/user.h index a8f61105..8dc761e3 100644 --- a/include/stg/user.h +++ b/include/stg/user.h @@ -98,7 +98,7 @@ public: virtual time_t GetLastWriteStatTime() const = 0; virtual bool IsInetable() = 0; - virtual std::string GetEnabledDirs() = 0; + virtual std::string GetEnabledDirs() const = 0; virtual void OnAdd() = 0; virtual void OnDelete() = 0; diff --git a/include/stg/user_property.h b/include/stg/user_property.h index 1716296a..398c4579 100644 --- a/include/stg/user_property.h +++ b/include/stg/user_property.h @@ -12,12 +12,14 @@ $Author: faust $ #include #include #include +#include #include #include #include "stg/logger.h" #include "stg/locker.h" #include "stg/scriptexecuter.h" +#include "stg/common.h" #include "store.h" #include "admin.h" @@ -25,10 +27,14 @@ $Author: faust $ #include "noncopyable.h" extern volatile time_t stgTime; - +//----------------------------------------------------------------------------- +class USER_PROPERTY_BASE { +public: + virtual std::string ToString() const = 0; +}; //----------------------------------------------------------------------------- template -class USER_PROPERTY { +class USER_PROPERTY : public USER_PROPERTY_BASE { public: USER_PROPERTY(varT & val); virtual ~USER_PROPERTY(); @@ -68,7 +74,8 @@ public: bool isPassword, bool isStat, STG_LOGGER & logger, - const std::string & sd); + const std::string & sd, + std::map & properties); virtual ~USER_PROPERTY_LOGGED() {} USER_PROPERTY_LOGGED * GetPointer() throw() { return this; } @@ -118,6 +125,7 @@ private: USER_STAT stat; USER_CONF conf; + std::map properties; public: USER_PROPERTIES(const std::string & sd); @@ -130,6 +138,9 @@ public: void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; } + std::string GetPropertyValue(const std::string & name) const; + bool Exists(const std::string & name) const; + USER_PROPERTY_LOGGED cash; USER_PROPERTY_LOGGED up; USER_PROPERTY_LOGGED down; @@ -268,7 +279,8 @@ USER_PROPERTY_LOGGED::USER_PROPERTY_LOGGED(varT & val, bool isPass, bool isSt, STG_LOGGER & logger, - const std::string & sd) + const std::string & sd, + std::map & properties) : USER_PROPERTY(val), stgLogger(logger), @@ -277,6 +289,7 @@ USER_PROPERTY_LOGGED::USER_PROPERTY_LOGGED(varT & val, name(n), scriptsDir(sd) { +properties.insert(std::make_pair(ToLower(name), this)); } //------------------------------------------------------------------------- template @@ -376,6 +389,23 @@ else //------------------------------------------------------------------------- //------------------------------------------------------------------------- //------------------------------------------------------------------------- +inline +std::string USER_PROPERTIES::GetPropertyValue(const std::string & name) const +{ +std::map::const_iterator it = properties.find(name); +if (it == properties.end()) + return ""; +return it->second->ToString(); +} +//----------------------------------------------------------------------------- +inline +bool USER_PROPERTIES::Exists(const std::string & name) const +{ +return properties.find(name) != properties.end(); +} +//------------------------------------------------------------------------- +//------------------------------------------------------------------------- +//------------------------------------------------------------------------- template inline std::ostream & operator<< (std::ostream & stream, const USER_PROPERTY & value) @@ -384,9 +414,10 @@ return stream << value.ConstData(); } //----------------------------------------------------------------------------- template +inline std::string USER_PROPERTY::ToString() const { -std::stringstream stream; +std::ostringstream stream; stream << value; return stream.str(); } diff --git a/include/stg/users.h b/include/stg/users.h index 1a8d7a43..9f516aef 100644 --- a/include/stg/users.h +++ b/include/stg/users.h @@ -64,7 +64,6 @@ public: virtual int Start() = 0; virtual int Stop() = 0; - }; #endif diff --git a/projects/stargazer/main.cpp b/projects/stargazer/main.cpp index 1e4c677e..200fd2a6 100644 --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@ -271,6 +271,7 @@ if (settings->ReadSettings()) WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str()); exit(1); } + #ifndef NO_DAEMON std::string startFile(settings->GetConfDir() + START_FILE); #endif diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index 3bc90792..725bc3f1 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -429,7 +429,7 @@ for(it = rsSettings.GetUserParams().begin(); it != rsSettings.GetUserParams().end(); ++it) { - std::string parameter(GetUserParam(rsu.user, *it)); + std::string parameter(rsu.user->GetParamValue(it->c_str())); if (params.length() + parameter.length() > RS_PARAMS_LEN - 1) break; params += parameter + " "; @@ -518,86 +518,6 @@ for (size_t i = 0; i < netRouters.size(); ++i) return std::vector(); } //----------------------------------------------------------------------------- -std::string REMOTE_SCRIPT::GetUserParam(USER_PTR u, const std::string & paramName) const -{ -std::string value = ""; -if (strcasecmp(paramName.c_str(), "cash") == 0) - strprintf(&value, "%f", u->GetProperty().cash.Get()); -else -if (strcasecmp(paramName.c_str(), "freeMb") == 0) - strprintf(&value, "%f", u->GetProperty().freeMb.Get()); -else -if (strcasecmp(paramName.c_str(), "passive") == 0) - strprintf(&value, "%d", u->GetProperty().passive.Get()); -else -if (strcasecmp(paramName.c_str(), "disabled") == 0) - strprintf(&value, "%d", u->GetProperty().disabled.Get()); -else -if (strcasecmp(paramName.c_str(), "alwaysOnline") == 0) - strprintf(&value, "%d", u->GetProperty().alwaysOnline.Get()); -else -if (strcasecmp(paramName.c_str(), "tariffName") == 0 || - strcasecmp(paramName.c_str(), "tariff") == 0) - value = "\"" + u->GetProperty().tariffName.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "nextTariff") == 0) - value = "\"" + u->GetProperty().nextTariff.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "address") == 0) - value = "\"" + u->GetProperty().address.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "note") == 0) - value = "\"" + u->GetProperty().note.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "group") == 0) - value = "\"" + u->GetProperty().group.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "email") == 0) - value = "\"" + u->GetProperty().email.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "realName") == 0) - value = "\"" + u->GetProperty().realName.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "credit") == 0) - strprintf(&value, "%f", u->GetProperty().credit.Get()); -else -if (strcasecmp(paramName.c_str(), "userdata0") == 0) - value = "\"" + u->GetProperty().userdata0.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "userdata1") == 0) - value = "\"" + u->GetProperty().userdata1.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "userdata2") == 0) - value = "\"" + u->GetProperty().userdata2.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "userdata3") == 0) - value = "\"" + u->GetProperty().userdata3.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "userdata4") == 0) - value = "\"" + u->GetProperty().userdata4.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "userdata5") == 0) - value = "\"" + u->GetProperty().userdata5.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "userdata6") == 0) - value = "\"" + u->GetProperty().userdata6.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "userdata7") == 0) - value = "\"" + u->GetProperty().userdata7.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "userdata8") == 0) - value = "\"" + u->GetProperty().userdata8.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "userdata9") == 0) - value = "\"" + u->GetProperty().userdata9.Get() + "\""; -else -if (strcasecmp(paramName.c_str(), "enabledDirs") == 0) - value = u->GetEnabledDirs(); -else - printfd(__FILE__, "Unknown value name: %s\n", paramName.c_str()); -return value; -} -//----------------------------------------------------------------------------- void REMOTE_SCRIPT::SetUserNotifiers(USER_PTR u) { ipNotifierList.push_front(RS::IP_NOTIFIER(*this, u)); diff --git a/projects/stargazer/plugins/other/rscript/rscript.h b/projects/stargazer/plugins/other/rscript/rscript.h index 8d22b458..bc6aa92f 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.h +++ b/projects/stargazer/plugins/other/rscript/rscript.h @@ -213,7 +213,6 @@ private: std::vector IP2Routers(uint32_t ip); bool GetUsers(); - std::string GetUserParam(USER_PTR u, const std::string & paramName) const; void SetUserNotifiers(USER_PTR u); void UnSetUserNotifiers(USER_PTR u); diff --git a/projects/stargazer/user_impl.cpp b/projects/stargazer/user_impl.cpp index 3b2489ad..7f6ff352 100644 --- a/projects/stargazer/user_impl.cpp +++ b/projects/stargazer/user_impl.cpp @@ -821,7 +821,7 @@ if (settings->GetShowFeeInCash() || tariff == NULL) return (cash - tariff->GetFee() >= -credit); } //----------------------------------------------------------------------------- -std::string USER_IMPL::GetEnabledDirs() +std::string USER_IMPL::GetEnabledDirs() const { //STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -1511,39 +1511,22 @@ while (it != messages.end()) //----------------------------------------------------------------------------- std::string USER_IMPL::GetParamValue(const std::string & name) const { -if (name == "freeMb") return property.freeMb.ToString(); -if (name == "passive") return property.passive.ToString(); -if (name == "disabled") return property.disabled.ToString(); -if (name == "alwaysOnline") return property.alwaysOnline.ToString(); -if (name == "tariffName") return property.tariffName; -if (name == "nextTariff") return property.nextTariff; -if (name == "address") return property.address; -if (name == "note") return property.note; -if (name == "group") return property.group; -if (name == "email") return property.email; -if (name == "phone") return property.phone; -if (name == "realName") return property.realName; -if (name == "credit") return property.credit.ToString(); -if (name == "userdata0") return property.userdata0; -if (name == "userdata1") return property.userdata1; -if (name == "userdata2") return property.userdata2; -if (name == "userdata3") return property.userdata3; -if (name == "userdata4") return property.userdata4; -if (name == "userdata5") return property.userdata5; -if (name == "userdata6") return property.userdata6; -if (name == "userdata7") return property.userdata7; -if (name == "userdata8") return property.userdata8; -if (name == "userdata9") return property.userdata9; -if (name == "cash") return property.cash.ToString(); -if (name == "id") - { - std::stringstream stream; - stream << id; - return stream.str();; - } -if (name == "login") return login; -if (name == "ip") return currIP.ToString(); -return ""; + if (name == "id") + { + std::ostringstream stream; + stream << id; + return stream.str(); + } + if (name == "login") return login; + if (name == "currIP") return currIP.ToString(); + if (name == "enabledDirs") return GetEnabledDirs(); + if (property.Exists(name)) + return property.GetPropertyValue(name); + else + { + WriteServLog("User’s parameter '%s' does not exist.", name.c_str()); + return ""; + } } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- diff --git a/projects/stargazer/user_impl.h b/projects/stargazer/user_impl.h index 150f2a8b..5b51fc5d 100644 --- a/projects/stargazer/user_impl.h +++ b/projects/stargazer/user_impl.h @@ -218,7 +218,7 @@ public: void ProcessDailyFee(); bool IsInetable(); - std::string GetEnabledDirs(); + std::string GetEnabledDirs() const; void OnAdd(); void OnDelete(); diff --git a/projects/stargazer/user_property.cpp b/projects/stargazer/user_property.cpp index f5f5eeea..a0c974bc 100644 --- a/projects/stargazer/user_property.cpp +++ b/projects/stargazer/user_property.cpp @@ -3,40 +3,39 @@ USER_PROPERTIES::USER_PROPERTIES(const std::string & sd) : stat(), conf(), - cash (stat.cash, "cash", false, true, GetStgLogger(), sd), - up (stat.monthUp, "upload", false, true, GetStgLogger(), sd), - down (stat.monthDown, "download", false, true, GetStgLogger(), sd), - lastCashAdd (stat.lastCashAdd, "lastCashAdd", false, true, GetStgLogger(), sd), - passiveTime (stat.passiveTime, "passiveTime", false, true, GetStgLogger(), sd), - lastCashAddTime (stat.lastCashAddTime, "lastCashAddTime", false, true, GetStgLogger(), sd), - freeMb (stat.freeMb, "freeMb", false, true, GetStgLogger(), sd), - lastActivityTime(stat.lastActivityTime, "lastActivityTime", false, true, GetStgLogger(), sd), + cash (stat.cash, "cash", false, true, GetStgLogger(), sd, properties), + up (stat.monthUp, "upload", false, true, GetStgLogger(), sd, properties), + down (stat.monthDown, "download", false, true, GetStgLogger(), sd, properties), + lastCashAdd (stat.lastCashAdd, "lastCashAdd", false, true, GetStgLogger(), sd, properties), + passiveTime (stat.passiveTime, "passiveTime", false, true, GetStgLogger(), sd, properties), + lastCashAddTime (stat.lastCashAddTime, "lastCashAddTime", false, true, GetStgLogger(), sd, properties), + freeMb (stat.freeMb, "freeMb", false, true, GetStgLogger(), sd, properties), + lastActivityTime(stat.lastActivityTime, "lastActivityTime", false, true, GetStgLogger(), sd, properties), - - password (conf.password, "password", true, false, GetStgLogger(), sd), - passive (conf.passive, "passive", false, false, GetStgLogger(), sd), - disabled (conf.disabled, "disabled", false, false, GetStgLogger(), sd), - disabledDetailStat(conf.disabledDetailStat, "DisabledDetailStat", false, false, GetStgLogger(), sd), - alwaysOnline(conf.alwaysOnline, "alwaysOnline", false, false, GetStgLogger(), sd), - tariffName (conf.tariffName, "tariff", false, false, GetStgLogger(), sd), - nextTariff (conf.nextTariff, "new tariff", false, false, GetStgLogger(), sd), - address (conf.address, "address", false, false, GetStgLogger(), sd), - note (conf.note, "note", false, false, GetStgLogger(), sd), - group (conf.group, "group", false, false, GetStgLogger(), sd), - email (conf.email, "email", false, false, GetStgLogger(), sd), - phone (conf.phone, "phone", false, false, GetStgLogger(), sd), - realName (conf.realName, "realName", false, false, GetStgLogger(), sd), - credit (conf.credit, "credit", false, false, GetStgLogger(), sd), - creditExpire(conf.creditExpire, "creditExpire", false, false, GetStgLogger(), sd), - ips (conf.ips, "IP", false, false, GetStgLogger(), sd), - userdata0 (conf.userdata[0], "userdata0", false, false, GetStgLogger(), sd), - userdata1 (conf.userdata[1], "userdata1", false, false, GetStgLogger(), sd), - userdata2 (conf.userdata[2], "userdata2", false, false, GetStgLogger(), sd), - userdata3 (conf.userdata[3], "userdata3", false, false, GetStgLogger(), sd), - userdata4 (conf.userdata[4], "userdata4", false, false, GetStgLogger(), sd), - userdata5 (conf.userdata[5], "userdata5", false, false, GetStgLogger(), sd), - userdata6 (conf.userdata[6], "userdata6", false, false, GetStgLogger(), sd), - userdata7 (conf.userdata[7], "userdata7", false, false, GetStgLogger(), sd), - userdata8 (conf.userdata[8], "userdata8", false, false, GetStgLogger(), sd), - userdata9 (conf.userdata[9], "userdata9", false, false, GetStgLogger(), sd) + password (conf.password, "password", true, false, GetStgLogger(), sd, properties), + passive (conf.passive, "passive", false, false, GetStgLogger(), sd, properties), + disabled (conf.disabled, "disabled", false, false, GetStgLogger(), sd, properties), + disabledDetailStat(conf.disabledDetailStat, "DisabledDetailStat", false, false, GetStgLogger(), sd, properties), + alwaysOnline(conf.alwaysOnline, "alwaysOnline", false, false, GetStgLogger(), sd, properties), + tariffName (conf.tariffName, "tariff", false, false, GetStgLogger(), sd, properties), + nextTariff (conf.nextTariff, "new tariff", false, false, GetStgLogger(), sd, properties), + address (conf.address, "address", false, false, GetStgLogger(), sd, properties), + note (conf.note, "note", false, false, GetStgLogger(), sd, properties), + group (conf.group, "group", false, false, GetStgLogger(), sd, properties), + email (conf.email, "email", false, false, GetStgLogger(), sd, properties), + phone (conf.phone, "phone", false, false, GetStgLogger(), sd, properties), + realName (conf.realName, "realName", false, false, GetStgLogger(), sd, properties), + credit (conf.credit, "credit", false, false, GetStgLogger(), sd, properties), + creditExpire(conf.creditExpire, "creditExpire", false, false, GetStgLogger(), sd, properties), + ips (conf.ips, "ips", false, false, GetStgLogger(), sd, properties), + userdata0 (conf.userdata[0], "userdata0", false, false, GetStgLogger(), sd, properties), + userdata1 (conf.userdata[1], "userdata1", false, false, GetStgLogger(), sd, properties), + userdata2 (conf.userdata[2], "userdata2", false, false, GetStgLogger(), sd, properties), + userdata3 (conf.userdata[3], "userdata3", false, false, GetStgLogger(), sd, properties), + userdata4 (conf.userdata[4], "userdata4", false, false, GetStgLogger(), sd, properties), + userdata5 (conf.userdata[5], "userdata5", false, false, GetStgLogger(), sd, properties), + userdata6 (conf.userdata[6], "userdata6", false, false, GetStgLogger(), sd, properties), + userdata7 (conf.userdata[7], "userdata7", false, false, GetStgLogger(), sd, properties), + userdata8 (conf.userdata[8], "userdata8", false, false, GetStgLogger(), sd, properties), + userdata9 (conf.userdata[9], "userdata9", false, false, GetStgLogger(), sd, properties) {}