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=67eb91cd2fafdf89ba670706e684ab0cd74ae513 Search parameter in map case insensitive --- diff --git a/doc/xmlrpc/API-tariffs.xml b/doc/xmlrpc/API-tariffs.xml index d36b01dd..9b84889a 100644 --- a/doc/xmlrpc/API-tariffs.xml +++ b/doc/xmlrpc/API-tariffs.xml @@ -113,6 +113,17 @@ + + string period + + Периодичность снятия абонплаты: + + month — ежемесячное снятие + day — ежедневное снятие + + + + array of struct dirprices @@ -277,6 +288,17 @@ + + string period + + Периодичность снятия абонплаты: + + month — ежемесячное снятие + day — ежедневное снятие + + + + array of struct dirprices @@ -541,6 +563,17 @@ + + string period + + Периодичность снятия абонплаты: + + month — ежемесячное снятие + day — ежедневное снятие + + + + array of struct dirprices diff --git a/doc/xmlrpc/API-user.xml b/doc/xmlrpc/API-user.xml index fac4f2be..82f8e7ee 100644 --- a/doc/xmlrpc/API-user.xml +++ b/doc/xmlrpc/API-user.xml @@ -243,6 +243,7 @@ stargazer.set_user_cash, stargazer.chg_user_tariff stargazer.get_online_ips + stargazer.get_user_auth_by @@ -481,6 +482,7 @@ stargazer.set_user_cash, stargazer.chg_user_tariff stargazer.get_online_ips + stargazer.get_user_auth_by @@ -534,6 +536,7 @@ stargazer.set_user_cash, stargazer.chg_user_tariff stargazer.get_online_ips + stargazer.get_user_auth_by @@ -732,6 +735,7 @@ stargazer.set_user_cash, stargazer.chg_user_tariff stargazer.get_online_ips + stargazer.get_user_auth_by @@ -785,6 +789,7 @@ stargazer.set_user_cash, stargazer.chg_user_tariff stargazer.get_online_ips + stargazer.get_user_auth_by @@ -852,6 +857,7 @@ stargazer.set_user_cash, stargazer.chg_user_tariff stargazer.get_online_ips + stargazer.get_user_auth_by @@ -919,6 +925,7 @@ stargazer.add_user_cash, stargazer.chg_user_tariff stargazer.get_online_ips + stargazer.get_user_auth_by @@ -993,6 +1000,7 @@ stargazer.add_user_cash, stargazer.set_user_cash stargazer.get_online_ips + stargazer.get_user_auth_by @@ -1060,6 +1068,75 @@ stargazer.add_user_cash, stargazer.set_user_cash stargazer.chg_user_tariff + stargazer.get_user_auth_by + + + + + + stargazer.get_user_auth_by + + + stargazer.get_user_auth_by + Получение списка авторизаторов которыми авторизован пользователь + + + + + stargazer.get_user_auth_by + string cookie + string login + + + + + Description + Метод stargazer.get_user_auth_by позволяет получить список авторизаторов которыми в данный момент авторизован пользователь. + + + string cookie + + Авторизационный cookie. Для авторизации в системе используется метод stargazer.login + + + + string login + + Логин пользователя + + + + + Return Value + Возвращает структуру: + + + bool result + + Результат операции. true - успешно, false - неудача (неправильный или устаревший cookie). + + + + array of strings auths + + Список авторизаторов которыми авторизован пользователь. + + + + + + + See also + + stargazer.get_users, + stargazer.get_user, + stargazer.add_user, + stargazer.chg_user, + stargazer.del_user, + stargazer.add_user_cash, + stargazer.set_user_cash + stargazer.chg_user_tariff + stargazer.get_online_ips diff --git a/include/stg/admin_conf.h b/include/stg/admin_conf.h index df6d9e22..8b1a988e 100644 --- a/include/stg/admin_conf.h +++ b/include/stg/admin_conf.h @@ -7,9 +7,10 @@ #ifndef ADMIN_CONF_H #define ADMIN_CONF_H -#include - #include "os_int.h" +#include "resetable.h" + +#include #define ADM_LOGIN_LEN (32) #define ADM_PASSWD_LEN (32) @@ -75,6 +76,29 @@ struct ADMIN_CONF std::string password; }; //----------------------------------------------------------------------------- +struct ADMIN_CONF_RES +{ + ADMIN_CONF_RES(const ADMIN_CONF & conf) + : priv(conf.priv), + login(conf.login), + password(conf.password) + {} + ADMIN_CONF_RES(const ADMIN_CONF_RES & rhs) + : priv(rhs.priv), + login(rhs.login), + password(rhs.password) + {} + ADMIN_CONF_RES & operator=(const ADMIN_CONF_RES & rhs) + { + priv = rhs.priv; + login = rhs.login; + password = rhs.password; + return *this; + } + RESETABLE priv; + RESETABLE login; + RESETABLE password; +}; #include "admin_conf.inc.h" diff --git a/include/stg/const.h b/include/stg/const.h index e882200d..55f5b54b 100644 --- a/include/stg/const.h +++ b/include/stg/const.h @@ -80,8 +80,6 @@ #define NO_TARIFF_NAME "*_NO_TARIFF_*" #define NO_CORP_NAME "*_NO_CORP_*" -#define mega (1024 * 1024) - #define MONITOR_TIME_DELAY_SEC (60) #endif diff --git a/include/stg/corp_conf.h b/include/stg/corp_conf.h index d6cc2ebb..9bbc03d3 100644 --- a/include/stg/corp_conf.h +++ b/include/stg/corp_conf.h @@ -1,6 +1,28 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + #ifndef CORP_CONF_H #define CORP_CONF_H +#include "resetable.h" + #include struct CORP_CONF @@ -13,6 +35,31 @@ std::string name; double cash; }; +struct CORP_CONF_RES +{ +CORP_CONF_RES() + : name(), cash() +{} + +CORP_CONF_RES & operator=(const CORP_CONF & conf) +{ +name = conf.name; +cash = conf.cash; +return *this; +} + +CORP_CONF GetData() const +{ +CORP_CONF cc; +cc.name = name.data(); +cc.cash = cash.data(); +return cc; +} + +RESETABLE name; +RESETABLE cash; +}; + inline bool operator==(const CORP_CONF & a, const CORP_CONF & b) { diff --git a/include/stg/corporations.h b/include/stg/corporations.h index 2d07c6c6..e167301b 100644 --- a/include/stg/corporations.h +++ b/include/stg/corporations.h @@ -21,10 +21,10 @@ #ifndef CORPORATIONS_H #define CORPORATIONS_H -#include - #include "corp_conf.h" +#include + class ADMIN; class CORPORATIONS { diff --git a/include/stg/os_int.h b/include/stg/os_int.h index cc017b84..00ab3fad 100644 --- a/include/stg/os_int.h +++ b/include/stg/os_int.h @@ -31,7 +31,7 @@ #include #endif -#ifdef FREE_BSD5 +#if defined(FREE_BSD5) || defined(DARWIN) #include #endif diff --git a/include/stg/resetable.h b/include/stg/resetable.h index f3cecac8..c78d0247 100644 --- a/include/stg/resetable.h +++ b/include/stg/resetable.h @@ -1,9 +1,3 @@ - /* - $Revision: 1.9 $ - $Date: 2010/03/11 14:42:04 $ - $Author: faust $ - */ - /* * Copyright (c) 2001 by Peter Simons . * All rights reserved. @@ -15,8 +9,6 @@ // This is a wrapper class about variables where you want to keep // track of whether it has been assigened yet or not. -#include - template class RESETABLE { @@ -24,32 +16,31 @@ public: typedef T value_type; RESETABLE() : value(), is_set(false) {} + RESETABLE(const T & v) : value(v), is_set(true) {} - RESETABLE(const RESETABLE & rvalue) + RESETABLE(const RESETABLE & rvalue) : value(rvalue.value), is_set(rvalue.is_set) {} - RESETABLE(const value_type& val) : value(val), is_set(true) {} - - RESETABLE & operator=(const RESETABLE & rvalue) + RESETABLE & operator=(const RESETABLE & rhs) { - value = rvalue.value; - is_set = rvalue.is_set; + value = rhs.value; + is_set = rhs.is_set; return *this; } - RESETABLE & operator=(const value_type& rhs) + RESETABLE & operator=(const T & rhs) { value = rhs; is_set = true; return *this; } - const value_type & const_data() const throw() { return value; } - value_type & data() throw() { return value; } - operator const value_type&() const throw() { return value; } - bool res_empty() const throw() { return !is_set; } + const T & const_data() const throw() { return value; } + T & data() throw() { return value; } + const T & data() const throw() { return value; } + bool empty() const throw() { return !is_set; } void reset() throw() { is_set = false; } private: @@ -57,14 +48,4 @@ private: bool is_set; }; -template -std::ostream & operator<<(std::ostream & o, const RESETABLE & v); - -template -inline -std::ostream & operator<<(std::ostream & o, const RESETABLE & v) -{ - return o << v.const_data(); -} - #endif // RESETABLE_VARIABLE_H diff --git a/include/stg/service_conf.h b/include/stg/service_conf.h index ca58154b..473830e4 100644 --- a/include/stg/service_conf.h +++ b/include/stg/service_conf.h @@ -1,10 +1,31 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + #ifndef SERVICE_CONF_H #define SERVICE_CONF_H -#include - +#include "resetable.h" #include "os_int.h" +#include + struct SERVICE_CONF { SERVICE_CONF() @@ -30,6 +51,38 @@ double cost; uint8_t payDay; }; +struct SERVICE_CONF_RES +{ +SERVICE_CONF_RES() + : name(), comment(), + cost(), payDay() +{} + +SERVICE_CONF_RES & operator=(const SERVICE_CONF & conf) +{ +name = conf.name; +comment = conf.comment; +cost = conf.cost; +payDay = conf.payDay; +return *this; +} + +SERVICE_CONF GetData() const +{ +SERVICE_CONF sc; +sc.name = name.data(); +sc.comment = comment.data(); +sc.cost = cost.data(); +sc.payDay = payDay.data(); +return sc; +} + +RESETABLE name; +RESETABLE comment; +RESETABLE cost; +RESETABLE payDay; +}; + inline bool operator==(const SERVICE_CONF & a, const SERVICE_CONF & b) { diff --git a/include/stg/services.h b/include/stg/services.h index ddec31be..8c49f781 100644 --- a/include/stg/services.h +++ b/include/stg/services.h @@ -21,10 +21,10 @@ #ifndef SERVICES_H #define SERVICES_H -#include - #include "service_conf.h" +#include + class ADMIN; class SERVICES { diff --git a/include/stg/tariff.h b/include/stg/tariff.h index a5b1d2e9..d0bf20b6 100644 --- a/include/stg/tariff.h +++ b/include/stg/tariff.h @@ -21,15 +21,21 @@ #ifndef TARIFF_H #define TARIFF_H -#include +#include "os_int.h" #include +#include +#include -#include "os_int.h" -#include "tariff_conf.h" +struct TARIFF_DATA; class TARIFF { public: + enum PERIOD { DAY = 0, MONTH }; + + static std::string PeriodToString(PERIOD period); + static PERIOD StringToPeriod(const std::string& value); + virtual ~TARIFF() {} virtual double GetPriceWithTraffType(uint64_t up, uint64_t down, @@ -39,6 +45,7 @@ public: virtual double GetPassiveCost() const = 0; virtual double GetFee() const = 0; virtual double GetFree() const = 0; + virtual PERIOD GetPeriod() const = 0; virtual const std::string & GetName() const = 0; virtual void SetName(const std::string & name) = 0; @@ -49,4 +56,23 @@ public: virtual const TARIFF_DATA & GetTariffData() const = 0; }; +inline +std::string TARIFF::PeriodToString(TARIFF::PERIOD period) +{ +switch (period) + { + case DAY: return "day"; + case MONTH: return "month"; + } +return "month"; // Classic behaviour. +} + +inline +TARIFF::PERIOD TARIFF::StringToPeriod(const std::string& value) +{ +if (strcasecmp(value.c_str(), "day") == 0) + return DAY; +return MONTH; // Classic behaviour. +} + #endif diff --git a/include/stg/tariff_conf.h b/include/stg/tariff_conf.h index 5b2eebb9..6a296230 100644 --- a/include/stg/tariff_conf.h +++ b/include/stg/tariff_conf.h @@ -27,12 +27,13 @@ #ifndef TARIFF_CONF_H #define TARIFF_CONF_H -#include -#include - +#include "tariff.h" #include "resetable.h" #include "const.h" +#include +#include + //----------------------------------------------------------------------------- enum { @@ -102,21 +103,21 @@ struct DIRPRICE_DATA_RES return *this; } - DIRPRICE_DATA GetData() + DIRPRICE_DATA GetData() const { DIRPRICE_DATA dd; - dd.hDay = hDay; - dd.hNight = hNight; - dd.mDay = mDay; - dd.mNight = mNight; - dd.noDiscount = noDiscount; - dd.priceDayA = priceDayA; - dd.priceDayB = priceDayB; - - dd.priceNightA = priceNightA; - dd.priceNightB = priceNightB; - dd.singlePrice = singlePrice; - dd.threshold = threshold; + dd.hDay = hDay.data(); + dd.hNight = hNight.data(); + dd.mDay = mDay.data(); + dd.mNight = mNight.data(); + dd.noDiscount = noDiscount.data(); + dd.priceDayA = priceDayA.data(); + dd.priceDayB = priceDayB.data(); + + dd.priceNightA = priceNightA.data(); + dd.priceNightB = priceNightB.data(); + dd.singlePrice = singlePrice.data(); + dd.threshold = threshold.data(); return dd; } @@ -135,18 +136,20 @@ struct DIRPRICE_DATA_RES //----------------------------------------------------------------------------- struct TARIFF_CONF { - double fee; - double free; - int traffType; - double passiveCost; - std::string name; + double fee; + double free; + int traffType; + double passiveCost; + std::string name; + TARIFF::PERIOD period; TARIFF_CONF() : fee(0), free(0), traffType(TRAFF_UP_DOWN), passiveCost(0), - name() + name(), + period(TARIFF::MONTH) {} TARIFF_CONF(const std::string & n) @@ -154,7 +157,8 @@ struct TARIFF_CONF free(0), traffType(TRAFF_UP_DOWN), passiveCost(0), - name(n) + name(n), + period(TARIFF::MONTH) {} }; //----------------------------------------------------------------------------- @@ -165,7 +169,8 @@ struct TARIFF_CONF_RES free(), traffType(), passiveCost(), - name() + name(), + period() {} TARIFF_CONF_RES & operator=(const TARIFF_CONF & tc) @@ -175,25 +180,28 @@ struct TARIFF_CONF_RES traffType = tc.traffType; passiveCost = tc.passiveCost; name = tc.name; + period = tc.period; return *this; } - TARIFF_CONF GetData() + TARIFF_CONF GetData() const { TARIFF_CONF tc; - tc.fee = fee; - tc.free = free; - tc.name = name; - tc.passiveCost = passiveCost; - tc.traffType = traffType; + tc.fee = fee.data(); + tc.free = free.data(); + tc.name = name.data(); + tc.passiveCost = passiveCost.data(); + tc.traffType = traffType.data(); + tc.period = period.data(); return tc; } - RESETABLE fee; - RESETABLE free; - RESETABLE traffType; - RESETABLE passiveCost; - RESETABLE name; + RESETABLE fee; + RESETABLE free; + RESETABLE traffType; + RESETABLE passiveCost; + RESETABLE name; + RESETABLE period; }; //----------------------------------------------------------------------------- struct TARIFF_DATA @@ -234,7 +242,7 @@ struct TARIFF_DATA_RES dirPrice(DIR_NUM) {} - TARIFF_DATA GetData() + TARIFF_DATA GetData() const { TARIFF_DATA td; td.tariffConf = tariffConf.GetData(); diff --git a/include/stg/user.h b/include/stg/user.h index b7024859..8dc761e3 100644 --- a/include/stg/user.h +++ b/include/stg/user.h @@ -21,14 +21,16 @@ #ifndef USER_H #define USER_H -#include -#include - -#include "os_int.h" #include "notifer.h" #include "message.h" #include "tariff.h" #include "user_traff.h" +#include "os_int.h" + +#include +#include + +#include class USER_PROPERTIES; class AUTH; @@ -71,12 +73,12 @@ public: virtual bool GetConnected() const = 0; virtual time_t GetConnectedModificationTime() const = 0; + virtual const std::string & GetLastDisconnectReason() const = 0; virtual int GetAuthorized() const = 0; - /*virtual int Authorize(uint32_t ip, - uint32_t enabledDirs, - const AUTH * auth) = 0; - virtual void Unauthorize(const AUTH * auth) = 0;*/ + virtual time_t GetAuthorizedModificationTime() const = 0; + virtual bool IsAuthorizedBy(const AUTH * auth) const = 0; + virtual std::vector GetAuthorizers() const = 0; virtual int AddMessage(STG_MSG * msg) = 0; diff --git a/include/stg/user_conf.h b/include/stg/user_conf.h index 59933658..5d65fcee 100644 --- a/include/stg/user_conf.h +++ b/include/stg/user_conf.h @@ -77,7 +77,7 @@ struct USER_CONF_RES group(), credit(), nextTariff(), - userdata(USERDATA_NUM, RESETABLE()), + userdata(USERDATA_NUM), creditExpire(), ips() { @@ -100,37 +100,34 @@ struct USER_CONF_RES group = uc.group; credit = uc.credit; nextTariff = uc.nextTariff; - for (int i = 0; i < USERDATA_NUM; i++) - { - userdata[i] = uc.userdata[i]; - } + for (size_t i = 0; i < USERDATA_NUM; i++) userdata[i] = uc.userdata[i]; creditExpire = uc.creditExpire; ips = uc.ips; return *this; } - operator USER_CONF() const + USER_CONF GetData() const { USER_CONF uc; - uc.password = password; - uc.passive = passive; - uc.disabled = disabled; - uc.disabledDetailStat = disabledDetailStat; - uc.alwaysOnline = alwaysOnline; - uc.tariffName = tariffName; - uc.address = address; - uc.phone = phone; - uc.email = email; - uc.note = note; - uc.realName = realName; - uc.group = group; - uc.credit = credit; - uc.nextTariff = nextTariff; + uc.password = password.data(); + uc.passive = passive.data(); + uc.disabled = disabled.data(); + uc.disabledDetailStat = disabledDetailStat.data(); + uc.alwaysOnline = alwaysOnline.data(); + uc.tariffName = tariffName.data(); + uc.address = address.data(); + uc.phone = phone.data(); + uc.email = email.data(); + uc.note = note.data(); + uc.realName = realName.data(); + uc.group = group.data(); + uc.credit = credit.data(); + uc.nextTariff = nextTariff.data(); for (int i = 0; i < USERDATA_NUM; i++) { - uc.userdata[i] = userdata[i]; + uc.userdata[i] = userdata[i].data(); } - uc.creditExpire = creditExpire; - uc.ips = ips; + uc.creditExpire = creditExpire.data(); + uc.ips = ips.data(); return uc; } //------------------------------------------------------------------------- @@ -155,4 +152,3 @@ struct USER_CONF_RES }; //----------------------------------------------------------------------------- #endif - diff --git a/include/stg/user_ips.h b/include/stg/user_ips.h index e68190c5..e55f99fa 100644 --- a/include/stg/user_ips.h +++ b/include/stg/user_ips.h @@ -70,7 +70,8 @@ public: std::string GetIpStr() const; bool IsIPInIPS(uint32_t ip) const; bool OnlyOneIP() const; - int Count() const; + bool IsAnyIP() const; + size_t Count() const; void Add(const IP_MASK &im); void Erase(); @@ -129,9 +130,9 @@ return s.str(); } //----------------------------------------------------------------------------- inline -int USER_IPS::Count() const +size_t USER_IPS::Count() const { -return static_cast(ips.size()); +return ips.size(); } //----------------------------------------------------------------------------- inline @@ -172,6 +173,12 @@ return false; } //----------------------------------------------------------------------------- inline +bool USER_IPS::IsAnyIP() const +{ + return !ips.empty() && ips.front().ip == 0; +} +//----------------------------------------------------------------------------- +inline void USER_IPS::Add(const IP_MASK &im) { ips.push_back(im); diff --git a/include/stg/user_property.h b/include/stg/user_property.h index 7c0f4da1..398c4579 100644 --- a/include/stg/user_property.h +++ b/include/stg/user_property.h @@ -19,6 +19,7 @@ $Author: faust $ #include "stg/logger.h" #include "stg/locker.h" #include "stg/scriptexecuter.h" +#include "stg/common.h" #include "store.h" #include "admin.h" @@ -288,7 +289,7 @@ USER_PROPERTY_LOGGED::USER_PROPERTY_LOGGED(varT & val, name(n), scriptsDir(sd) { -properties.insert(std::make_pair(name, this)); +properties.insert(std::make_pair(ToLower(name), this)); } //------------------------------------------------------------------------- template diff --git a/include/stg/user_stat.h b/include/stg/user_stat.h index 54f11dec..ff020dc7 100644 --- a/include/stg/user_stat.h +++ b/include/stg/user_stat.h @@ -29,6 +29,8 @@ #include #include +#include +#include #include "os_int.h" #include "resetable.h" @@ -113,10 +115,11 @@ struct STAT_NODE //----------------------------------------------------------------------------- struct USER_STAT { - //USER_STAT & operator= (const USER_STAT_RES & usr); USER_STAT() - : up(), - down(), + : sessionUp(), + sessionDown(), + monthUp(), + monthDown(), cash(0), freeMb(0), lastCashAdd(0), @@ -125,8 +128,10 @@ struct USER_STAT lastActivityTime(0) {} - DIR_TRAFF up; - DIR_TRAFF down; + DIR_TRAFF sessionUp; + DIR_TRAFF sessionDown; + DIR_TRAFF monthUp; + DIR_TRAFF monthDown; double cash; double freeMb; double lastCashAdd; @@ -137,6 +142,8 @@ struct USER_STAT //----------------------------------------------------------------------------- typedef std::map TRAFF_STAT; //----------------------------------------------------------------------------- +typedef std::pair CASH_INFO; +//----------------------------------------------------------------------------- struct USER_STAT_RES { USER_STAT_RES() @@ -146,8 +153,10 @@ struct USER_STAT_RES lastCashAddTime(), passiveTime(), lastActivityTime(), - up(), - down() + sessionUp(), + sessionDown(), + monthUp(), + monthDown() {} USER_STAT_RES & operator= (const USER_STAT & us) @@ -158,32 +167,40 @@ struct USER_STAT_RES lastCashAddTime = us.lastCashAddTime; passiveTime = us.passiveTime; lastActivityTime = us.lastActivityTime; - up = us.up; - down = us.down; - return * this; + sessionUp = us.sessionUp; + sessionDown = us.sessionDown; + monthUp = us.monthUp; + monthDown = us.monthDown; + return *this; } - operator USER_STAT() const + USER_STAT GetData() const { USER_STAT us; - us.cash = cash; - us.freeMb = freeMb; - us.lastCashAdd = lastCashAdd; - us.lastCashAddTime = lastCashAddTime; - us.passiveTime = passiveTime; - us.lastActivityTime = lastActivityTime; - us.up = up; - us.down = down; + us.cash = cash.data(); + us.freeMb = freeMb.data(); + us.lastCashAdd = lastCashAdd.data(); + us.lastCashAddTime = lastCashAddTime.data(); + us.passiveTime = passiveTime.data(); + us.lastActivityTime = lastActivityTime.data(); + us.sessionUp = sessionUp.GetData(); + us.sessionDown = sessionDown.GetData(); + us.monthUp = monthUp.GetData(); + us.monthDown = monthDown.GetData(); return us; } RESETABLE cash; + RESETABLE cashAdd; + RESETABLE cashSet; RESETABLE freeMb; RESETABLE lastCashAdd; RESETABLE lastCashAddTime; RESETABLE passiveTime; RESETABLE lastActivityTime; - RESETABLE up; - RESETABLE down; + DIR_TRAFF_RES sessionUp; + DIR_TRAFF_RES sessionDown; + DIR_TRAFF_RES monthUp; + DIR_TRAFF_RES monthDown; }; //----------------------------------------------------------------------------- #endif diff --git a/include/stg/user_traff.h b/include/stg/user_traff.h index aee1171c..e1176b90 100644 --- a/include/stg/user_traff.h +++ b/include/stg/user_traff.h @@ -27,12 +27,13 @@ #ifndef USER_TRAFF_H #define USER_TRAFF_H -#include -#include - +#include "resetable.h" #include "const.h" #include "os_int.h" +#include +#include + enum TRAFF_DIRECTION {TRAFF_UPLOAD, TRAFF_DOWNLOAD}; class DIR_TRAFF @@ -43,64 +44,23 @@ public: typedef std::vector ContainerType; typedef ContainerType::size_type IndexType; - //------------------------------------------------------------------------- - DIR_TRAFF(); - DIR_TRAFF(const DIR_TRAFF & ts); - DIR_TRAFF & operator=(const DIR_TRAFF & ts); - ~DIR_TRAFF(); - uint64_t operator[](IndexType idx) const; - uint64_t & operator[](IndexType idx); - DIR_TRAFF operator+(const DIR_TRAFF & ts); + DIR_TRAFF() : traff(DIR_NUM) {} + DIR_TRAFF(const DIR_TRAFF & ts) : traff(ts.traff) {} + DIR_TRAFF & operator=(const DIR_TRAFF & ts) { traff = ts.traff; return *this; } + const uint64_t & operator[](IndexType idx) const { return traff[idx]; } + uint64_t & operator[](IndexType idx) { return traff[idx]; } + IndexType size() const { return traff.size(); } private: ContainerType traff; }; -//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -inline DIR_TRAFF::DIR_TRAFF() - : traff(DIR_NUM, 0) -{ -} -//----------------------------------------------------------------------------- -inline DIR_TRAFF::DIR_TRAFF(const DIR_TRAFF & ts) - : traff(ts.traff) -{ -} -//----------------------------------------------------------------------------- -inline DIR_TRAFF::~DIR_TRAFF() -{ -} -//----------------------------------------------------------------------------- -inline DIR_TRAFF & DIR_TRAFF::operator=(const DIR_TRAFF & ts) -{ -traff = ts.traff; -return *this; -} -//----------------------------------------------------------------------------- -inline uint64_t & DIR_TRAFF::operator[](IndexType idx) -{ -return traff[idx]; -} -//----------------------------------------------------------------------------- -inline uint64_t DIR_TRAFF::operator[](IndexType idx) const -{ -return traff[idx]; -} -//----------------------------------------------------------------------------- -inline DIR_TRAFF DIR_TRAFF::operator+(const DIR_TRAFF & ts) -{ -for (IndexType i = 0; i < DIR_NUM; i++) - { - traff[i] = traff[i] + ts.traff[i]; - } -return *this; -} -//----------------------------------------------------------------------------- -inline std::ostream & operator<<(std::ostream & o, const DIR_TRAFF & traff) +inline +std::ostream & operator<<(std::ostream & o, const DIR_TRAFF & traff) { bool first = true; -for (DIR_TRAFF::IndexType i = 0; i < DIR_NUM; ++i) +for (DIR_TRAFF::IndexType i = 0; i < traff.size(); ++i) { if (first) first = false; @@ -110,5 +70,36 @@ for (DIR_TRAFF::IndexType i = 0; i < DIR_NUM; ++i) } return o; } -//----------------------------------------------------------------------------- + +class DIR_TRAFF_RES +{ +public: + typedef RESETABLE value_type; + typedef RESETABLE ValueType; + typedef std::vector ContainerType; + typedef ContainerType::size_type IndexType; + + DIR_TRAFF_RES() : traff(DIR_NUM) {} + DIR_TRAFF_RES(const DIR_TRAFF & ts) + : traff(ts.size()) + { + for (IndexType i = 0; i < ts.size(); ++i) + traff[i] = ts[i]; + } + const ValueType & operator[](IndexType idx) const { return traff[idx]; } + ValueType & operator[](IndexType idx) { return traff[idx]; } + IndexType size() const { return traff.size(); } + DIR_TRAFF GetData() const + { + DIR_TRAFF res; + for (IndexType i = 0; i < traff.size(); ++i) + if (!traff[i].empty()) + res[i] = traff[i].data(); + return res; + } + +private: + ContainerType traff; +}; + #endif diff --git a/include/stg/users.h b/include/stg/users.h index 2ba23833..9f516aef 100644 --- a/include/stg/users.h +++ b/include/stg/users.h @@ -32,6 +32,7 @@ class USERS { public: virtual ~USERS() {} virtual int FindByName(const std::string & login, USER_PTR * user) = 0; + virtual int FindByName(const std::string & login, CONST_USER_PTR * user) const = 0; virtual bool TariffInUse(const std::string & tariffName) const = 0; @@ -46,13 +47,16 @@ public: virtual bool Authorize(const std::string & login, uint32_t ip, uint32_t enabledDirs, const AUTH * auth) = 0; - virtual bool Unauthorize(const std::string & login, const AUTH * auth) = 0; + virtual bool Unauthorize(const std::string & login, + const AUTH * auth, + const std::string & reason = std::string()) = 0; virtual int ReadUsers() = 0; virtual size_t Count() const = 0; virtual int FindByIPIdx(uint32_t ip, USER_PTR * user) const = 0; virtual bool IsIPInIndex(uint32_t ip) const = 0; + virtual bool IsIPInUse(uint32_t ip, const std::string & login, CONST_USER_PTR * user) const = 0; virtual int OpenSearch() = 0; virtual int SearchNext(int handle, USER_PTR * u) = 0; diff --git a/include/stg/utime.h b/include/stg/utime.h index 20da4e43..fe6b3f69 100644 --- a/include/stg/utime.h +++ b/include/stg/utime.h @@ -164,6 +164,11 @@ struct UTIME: public timeval { return tv_usec; } + + double AsDouble() const + { + return tv_sec + tv_usec * 1e-6; + } }; diff --git a/projects/rlm_stg/build b/projects/rlm_stg/build index 91fe7d9d..6459cdb9 100755 --- a/projects/rlm_stg/build +++ b/projects/rlm_stg/build @@ -53,15 +53,15 @@ fi if [ "$OS" = "unknown" ] then - echo "#############################################################################" - echo "# Sorry, but rlm_stg currently supported by Linux, FreeBSD 4.x, 5.x, 6.x #" - echo "#############################################################################" + printf "#############################################################################\n" + printf "# Sorry, but rlm_stg currently supported by Linux, FreeBSD 4.x, 5.x, 6.x #\n" + printf "#############################################################################\n" exit 1 fi -echo "#############################################################################" -echo " Building rlm_stg for $sys $release" -echo "#############################################################################" +printf "#############################################################################\n" +printf " Building rlm_stg for $sys $release\n" +printf "#############################################################################\n" STG_LIBS="crypto.lib common.lib" @@ -95,32 +95,32 @@ then CXX=g++ fi -echo -n "Checking CC... " +printf "Checking CC... " $CC --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CC not found" + printf "FAIL!\n" + printf "$CC not found\n" exit; fi -echo "found" -echo -n "Checking CXX... " +printf "found\n" +printf "Checking CXX... " $CXX --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CXX not found" + printf "FAIL!\n" + printf "$CXX not found\n" exit; fi -echo "found" +printf "found\n" -echo -n "Checking endianess... " -echo "int main() { int probe = 0x00000001; return *(char *)&probe; }" > build_check.c +printf "Checking endianess... " +printf "int main() { int probe = 0x00000001; return *(char *)&probe; }\n" > build_check.c $CC $CFLAGS $LDFLAGS build_check.c -o fake > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "Endianess checking failed" + printf "FAIL!\n" + printf "Endianess checking failed\n" exit; else ./fake @@ -129,39 +129,39 @@ else ARCH=le CXXFLAGS="$CXXFLAGS -DARCH_LE" CFLAGS="$CFLAGS -DARCH_LE" - echo "Little Endian" + printf "Little Endian\n" else ARCH=be CXXFLAGS="$CXXFLAGS -DARCH_BE" CFLAGS="$CFLAGS -DARCH_BE" - echo "Big Endian" + printf "Big Endian\n" fi fi rm -f fake -echo "OS=$OS" > $CONFFILE -echo "STG_TIME=yes" >> $CONFFILE -echo "DEBUG=$DEBUG" >> $CONFFILE -echo "DIR_BUILD=$BUILD_DIR" >> $CONFFILE -echo "DIR_LIB=\$(DIR_BUILD)/../../lib" >> $CONFFILE -echo "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs" >> $CONFFILE -echo "DIR_INCLUDE=\$(DIR_BUILD)/../../include" >> $CONFFILE -echo "ARCH=$ARCH" >> $CONFFILE -echo "DEFS=$DEFS" >> $CONFFILE -echo -n "STG_LIBS=" >> $CONFFILE +printf "OS=$OS\n" > $CONFFILE +printf "STG_TIME=yes\n" >> $CONFFILE +printf "DEBUG=$DEBUG\n" >> $CONFFILE +printf "DIR_BUILD=$BUILD_DIR\n" >> $CONFFILE +printf "DIR_LIB=\$(DIR_BUILD)/../../lib\n" >> $CONFFILE +printf "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs\n" >> $CONFFILE +printf "DIR_INCLUDE=\$(DIR_BUILD)/../../include\n" >> $CONFFILE +printf "ARCH=$ARCH\n" >> $CONFFILE +printf "DEFS=$DEFS\n" >> $CONFFILE +printf "STG_LIBS=" >> $CONFFILE for lib in $STG_LIBS do - echo -n "$lib " >> $CONFFILE + printf "$lib " >> $CONFFILE done -echo "" >> $CONFFILE -echo "CXXFLAGS=$CXXFLAGS" >> $CONFFILE -echo "CFLAGS=$CFLAGS" >> $CONFFILE -echo "LDFLAGS=$LDFLAGS" >> $CONFFILE -echo "LIB_THREAD=$LIB_THREAD" >> $CONFFILE -echo "PREFIX=$PREFIX" >> $CONFFILE -echo "BIN_MODE=$BIN_MODE" >> $CONFFILE -echo "DATA_MODE=$DATA_MODE" >> $CONFFILE -echo "DIR_MODE=$DIR_MODE" >> $CONFFILE -echo "OWNER=$OWNER" >> $CONFFILE +printf "\n" >> $CONFFILE +printf "CXXFLAGS=$CXXFLAGS\n" >> $CONFFILE +printf "CFLAGS=$CFLAGS\n" >> $CONFFILE +printf "LDFLAGS=$LDFLAGS\n" >> $CONFFILE +printf "LIB_THREAD=$LIB_THREAD\n" >> $CONFFILE +printf "PREFIX=$PREFIX\n" >> $CONFFILE +printf "BIN_MODE=$BIN_MODE\n" >> $CONFFILE +printf "DATA_MODE=$DATA_MODE\n" >> $CONFFILE +printf "DIR_MODE=$DIR_MODE\n" >> $CONFFILE +printf "OWNER=$OWNER\n" >> $CONFFILE $MAKE $MAKEOPTS diff --git a/projects/rscriptd/build b/projects/rscriptd/build index ea3914c7..c7aa090d 100755 --- a/projects/rscriptd/build +++ b/projects/rscriptd/build @@ -53,15 +53,15 @@ fi if [ "$OS" = "unknown" ] then - echo "#############################################################################" - echo "# Sorry, but rscriptd currently supported by Linux, FreeBSD 4.x, 5.x, 6.x #" - echo "#############################################################################" + printf "#############################################################################\n" + printf "# Sorry, but rscriptd currently supported by Linux, FreeBSD 4.x, 5.x, 6.x #\n" + printf "#############################################################################\n" exit 1 fi -echo "#############################################################################" -echo " Building rscriptd for $sys $release" -echo "#############################################################################" +printf "#############################################################################\n" +printf " Building rscriptd for $sys $release\n" +printf "#############################################################################\n" STG_LIBS="logger.lib locker.lib @@ -100,32 +100,32 @@ then CXX=g++ fi -echo -n "Checking CC... " +printf "Checking CC... " $CC --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CC not found" + printf "FAIL!\n" + printf "$CC not found\n" exit; fi -echo "found" -echo -n "Checking CXX... " +printf "found\n" +printf "Checking CXX... " $CXX --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CXX not found" + printf "FAIL!\n" + printf "$CXX not found\n" exit; fi -echo "found" +printf "found\n" -echo -n "Checking endianess... " -echo "int main() { int probe = 0x00000001; return *(char *)&probe; }" > build_check.c +printf "Checking endianess... " +printf "int main() { int probe = 0x00000001; return *(char *)&probe; }\n" > build_check.c $CC $CFLAGS $LDFLAGS build_check.c -o fake > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "Endianess checking failed" + printf "FAIL!\n" + printf "Endianess checking failed\n" exit; else ./fake @@ -134,41 +134,41 @@ else ARCH=le CXXFLAGS="$CXXFLAGS -DARCH_LE" CFLAGS="$CFLAGS -DARCH_LE" - echo "Little Endian" + printf "Little Endian\n" else ARCH=be CXXFLAGS="$CXXFLAGS -DARCH_BE" CFLAGS="$CFLAGS -DARCH_BE" - echo "Big Endian" + printf "Big Endian\n" fi fi rm -f fake rm -f build_check.c -echo "OS=$OS" > $CONFFILE -echo "STG_TIME=yes" >> $CONFFILE -echo "DEBUG=$DEBUG" >> $CONFFILE -echo "DIR_BUILD=$BUILD_DIR" >> $CONFFILE -echo "DIR_LIB=\$(DIR_BUILD)/../../lib" >> $CONFFILE -echo "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs" >> $CONFFILE -echo "DIR_INCLUDE=\$(DIR_BUILD)/../../include" >> $CONFFILE -echo "ARCH=$ARCH" >> $CONFFILE -echo "DEFS=$DEFS" >> $CONFFILE -echo -n "STG_LIBS=" >> $CONFFILE +printf "OS=$OS\n" > $CONFFILE +printf "STG_TIME=yes\n" >> $CONFFILE +printf "DEBUG=$DEBUG\n" >> $CONFFILE +printf "DIR_BUILD=$BUILD_DIR\n" >> $CONFFILE +printf "DIR_LIB=\$(DIR_BUILD)/../../lib\n" >> $CONFFILE +printf "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs\n" >> $CONFFILE +printf "DIR_INCLUDE=\$(DIR_BUILD)/../../include\n" >> $CONFFILE +printf "ARCH=$ARCH\n" >> $CONFFILE +printf "DEFS=$DEFS\n" >> $CONFFILE +printf "STG_LIBS=" >> $CONFFILE for lib in $STG_LIBS do - echo -n "$lib " >> $CONFFILE + printf "$lib " >> $CONFFILE done -echo "" >> $CONFFILE -echo "LIB_THREAD=$LIB_THREAD" >> $CONFFILE -echo "CXXFLAGS=$CXXFLAGS" >> $CONFFILE -echo "CFLAGS=$CFLAGS" >> $CONFFILE -echo "LDFLAGS=$LDFLAGS" >> $CONFFILE -echo "PREFIX=$PREFIX" >> $CONFFILE -echo "BIN_MODE=$BIN_MODE" >> $CONFFILE -echo "DATA_MODE=$DATA_MODE" >> $CONFFILE -echo "DIR_MODE=$DIR_MODE" >> $CONFFILE -echo "OWNER=$OWNER" >> $CONFFILE +printf "\n" >> $CONFFILE +printf "LIB_THREAD=$LIB_THREAD\n" >> $CONFFILE +printf "CXXFLAGS=$CXXFLAGS\n" >> $CONFFILE +printf "CFLAGS=$CFLAGS\n" >> $CONFFILE +printf "LDFLAGS=$LDFLAGS\n" >> $CONFFILE +printf "PREFIX=$PREFIX\n" >> $CONFFILE +printf "BIN_MODE=$BIN_MODE\n" >> $CONFFILE +printf "DATA_MODE=$DATA_MODE\n" >> $CONFFILE +printf "DIR_MODE=$DIR_MODE\n" >> $CONFFILE +printf "OWNER=$OWNER\n" >> $CONFFILE $MAKE $MAKEOPTS diff --git a/projects/sgauth/build b/projects/sgauth/build index 8b79bc52..5ea022a2 100755 --- a/projects/sgauth/build +++ b/projects/sgauth/build @@ -53,15 +53,15 @@ fi if [ "$OS" = "unknown" ] then - echo "#############################################################################" - echo "# Sorry, but sgauth currently supported by Linux, FreeBSD 4.x, 5.x, 6.x #" - echo "#############################################################################" + printf "#############################################################################\n" + printf "# Sorry, but sgauth currently supported by Linux, FreeBSD 4.x, 5.x, 6.x #\n" + printf "#############################################################################\n" exit 1 fi -echo "#############################################################################" -echo " Building sgauth for $sys $release" -echo "#############################################################################" +printf "#############################################################################\n" +printf " Building sgauth for $sys $release\n" +printf "#############################################################################\n" STG_LIBS="crypto.lib common.lib @@ -98,32 +98,32 @@ then CXX=g++ fi -echo -n "Checking CC... " +printf "Checking CC... " $CC --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CC not found" + printf "FAIL!\n" + printf "$CC not found\n" exit; fi -echo "found" -echo -n "Checking CXX... " +printf "found\n" +printf "Checking CXX... " $CXX --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CXX not found" + printf "FAIL!\n" + printf "$CXX not found\n" exit; fi -echo "found" +printf "found\n" -echo -n "Checking endianess... " -echo "int main() { int probe = 0x00000001; return *(char *)&probe; }" > build_check.c +printf "Checking endianess... " +printf "int main() { int probe = 0x00000001; return *(char *)&probe; }\n" > build_check.c $CC $CFLAGS $LDFLAGS build_check.c -o fake > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "Endianess checking failed" + printf "FAIL!\n" + printf "Endianess checking failed\n" exit; else ./fake @@ -132,40 +132,40 @@ else ARCH=le CXXFLAGS="$CXXFLAGS -DARCH_LE" CFLAGS="$CFLAGS -DARCH_LE" - echo "Little Endian" + printf "Little Endian\n" else ARCH=be CXXFLAGS="$CXXFLAGS -DARCH_BE" CFLAGS="$CFLAGS -DARCH_BE" - echo "Big Endian" + printf "Big Endian\n" fi fi rm -f fake rm -f build_check.c -echo "OS=$OS" > $CONFFILE -echo "STG_TIME=yes" >> $CONFFILE -echo "DEBUG=$DEBUG" >> $CONFFILE -echo "DIR_BUILD=$BUILD_DIR" >> $CONFFILE -echo "DIR_LIB=\$(DIR_BUILD)/../../lib" >> $CONFFILE -echo "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs" >> $CONFFILE -echo "DIR_INCLUDE=\$(DIR_BUILD)/../../include" >> $CONFFILE -echo "ARCH=$ARCH" >> $CONFFILE -echo "DEFS=$DEFS" >> $CONFFILE -echo -n "STG_LIBS=" >> $CONFFILE +printf "OS=$OS\n" > $CONFFILE +printf "STG_TIME=yes\n" >> $CONFFILE +printf "DEBUG=$DEBUG\n" >> $CONFFILE +printf "DIR_BUILD=$BUILD_DIR\n" >> $CONFFILE +printf "DIR_LIB=\$(DIR_BUILD)/../../lib\n" >> $CONFFILE +printf "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs\n" >> $CONFFILE +printf "DIR_INCLUDE=\$(DIR_BUILD)/../../include\n" >> $CONFFILE +printf "ARCH=$ARCH\n" >> $CONFFILE +printf "DEFS=$DEFS\n" >> $CONFFILE +printf "STG_LIBS=" >> $CONFFILE for lib in $STG_LIBS do - echo -n "$lib " >> $CONFFILE + printf "$lib " >> $CONFFILE done -echo "" >> $CONFFILE -echo "LIB_THREAD=$LIB_THREAD" >> $CONFFILE -echo "CXXFLAGS=$CXXFLAGS" >> $CONFFILE -echo "CFLAGS=$CFLAGS" >> $CONFFILE -echo "LDFLAGS=$LDFLAGS" >> $CONFFILE -echo "PREFIX=$PREFIX" >> $CONFFILE -echo "BIN_MODE=$BIN_MODE" >> $CONFFILE -echo "DATA_MODE=$DATA_MODE" >> $CONFFILE -echo "DIR_MODE=$DIR_MODE" >> $CONFFILE -echo "OWNER=$OWNER" >> $CONFFILE +printf "\n" >> $CONFFILE +printf "LIB_THREAD=$LIB_THREAD\n" >> $CONFFILE +printf "CXXFLAGS=$CXXFLAGS\n" >> $CONFFILE +printf "CFLAGS=$CFLAGS\n" >> $CONFFILE +printf "LDFLAGS=$LDFLAGS\n" >> $CONFFILE +printf "PREFIX=$PREFIX\n" >> $CONFFILE +printf "BIN_MODE=$BIN_MODE\n" >> $CONFFILE +printf "DATA_MODE=$DATA_MODE\n" >> $CONFFILE +printf "DIR_MODE=$DIR_MODE\n" >> $CONFFILE +printf "OWNER=$OWNER\n" >> $CONFFILE $MAKE $MAKEOPTS diff --git a/projects/sgauthstress/build b/projects/sgauthstress/build index 6f26a17e..2b145659 100755 --- a/projects/sgauthstress/build +++ b/projects/sgauthstress/build @@ -59,15 +59,15 @@ fi if [ "$OS" = "unknown" ] then - echo "################################################################################" - echo "# Sorry, but sgauthstress currently supported by Linux, FreeBSD 4.x-8.x #" - echo "################################################################################" + printf "################################################################################\n" + printf "# Sorry, but sgauthstress currently supported by Linux, FreeBSD 4.x-8.x #\n" + printf "################################################################################\n" exit 1 fi -echo "#############################################################################" -echo " Building sgauthstress for $sys $release" -echo "#############################################################################" +printf "#############################################################################\n" +printf " Building sgauthstress for $sys $release\n" +printf "#############################################################################\n" STG_LIBS="logger.lib locker.lib @@ -108,32 +108,32 @@ then CXX=g++ fi -echo -n "Checking CC... " +printf "Checking CC... " $CC --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CC not found" + printf "FAIL!\n" + printf "$CC not found\n" exit; fi -echo "found" -echo -n "Checking CXX... " +printf "found\n" +printf "Checking CXX... " $CXX --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CXX not found" + printf "FAIL!\n" + printf "$CXX not found\n" exit; fi -echo "found" +printf "found\n" -echo -n "Checking endianess... " -echo "int main() { int probe = 0x00000001; return *(char *)&probe; }" > build_check.c +printf "Checking endianess... " +printf "int main() { int probe = 0x00000001; return *(char *)&probe; }\n" > build_check.c $CC $CFLAGS $LDFLAGS build_check.c -o fake > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "Endianess checking failed" + printf "FAIL!\n" + printf "Endianess checking failed\n" exit; else ./fake @@ -142,103 +142,103 @@ else ARCH=le CXXFLAGS="$CXXFLAGS -DARCH_LE" CFLAGS="$CFLAGS -DARCH_LE" - echo "Little Endian" + printf "Little Endian\n" else ARCH=be CXXFLAGS="$CXXFLAGS -DARCH_BE" CFLAGS="$CFLAGS -DARCH_BE" - echo "Big Endian" + printf "Big Endian\n" fi fi rm -f fake rm -f build_check.c -echo -n "Checking for -lfbclient... " +printf "Checking for -lfbclient... " $CC $CFLAGS $LDFLAGS build_check.c -lfbclient $LIB_THREAD -o fake > /dev/null 2> /dev/null if [ $? != 0 ] then CHECK_FBCLIENT=no - echo "no" + printf "no\n" else CHECK_FBCLIENT=yes - echo "yes" + printf "yes\n" fi rm -f fake -echo -n "Checking for mysql_config... " +printf "Checking for mysql_config... " MYSQL_VERSION=`mysql_config --version 2> /dev/null` if [ $? != 0 ] then - echo "no"; - echo -n "Checking for -lmysqlclient... " + printf "no\n"; + printf "Checking for -lmysqlclient... " $CC $CFLAGS $LDFLAGS build_check.c -lmysqlclient_r $LIB_THREAD -o fake > /dev/null 2> /dev/null if [ $? != 0 ] then CHECK_MYSQLCLIENT=no - echo "no" + printf "no\n" else CHECK_MYSQLCLIENT=yes - echo "yes" + printf "yes\n" fi rm -f fake else - echo "yes" - echo -n "Checking for mysql_config --cflags... " + printf "yes\n" + printf "Checking for mysql_config --cflags... " MYSQL_CFLAGS=`mysql_config --cflags 2> /dev/null` if [ $? != 0 ] then CHECK_MYSQLCLIENT=no - echo "no" + printf "no\n" else - echo "[$MYSQL_CFLAGS]" - echo -n "Checking for mysql_config --libs_r... " + printf "[$MYSQL_CFLAGS]\n" + printf "Checking for mysql_config --libs_r... " MYSQL_LDFLAGS=`mysql_config --libs_r 2> /dev/null` if [ $? != 0 ] then CHECK_MYSQLCLIENT=no - echo "no" + printf "no\n" else CHECK_MYSQLCLIENT=yes - echo "[$MYSQL_LDFLAGS]" + printf "[$MYSQL_LDFLAGS]\n" fi fi fi -echo -n "Checking for pg_config... " +printf "Checking for pg_config... " PG_VERSION=`pg_config --version 2> /dev/null` if [ $? != 0 ] then - echo "no"; - echo -n "Checking for -lpq... " + printf "no\n"; + printf "Checking for -lpq... " $CC $CFLAGS $LDFLAGS build_check.c -lpq $LIB_THREAD -o fake > /dev/null 2> /dev/null if [ $? != 0 ] then CHECK_PQ=no - echo "no" + printf "no\n" else CHECK_PQ=yes - echo "yes" + printf "yes\n" fi rm -f fake else - echo "yes"; - echo -n "Checking for pg_config --includedir... " + printf "yes\n"; + printf "Checking for pg_config --includedir... " PG_CFLAGS=`pg_config --includedir 2> /dev/null` if [ $? != 0 ] then CHECK_PQ=no - echo "no" + printf "no\n" else - echo "[$PG_CFLAGS]" - echo -n "Checking for pg_config --libdir... " + printf "[$PG_CFLAGS]\n" + printf "Checking for pg_config --libdir... " PG_LDFLAGS=`pg_config --libdir 2> /dev/null` if [ $? != 0 ] then CHECK_PQ=no - echo "no" + printf "no\n" else CHECK_PQ=yes - echo "[$PG_LDFLAGS]" + printf "[$PG_LDFLAGS]\n" fi fi fi @@ -265,36 +265,36 @@ then store/mysql" fi -echo "OS=$OS" > $CONFFILE -echo "STG_TIME=yes" >> $CONFFILE -echo "DEBUG=$DEBUG" >> $CONFFILE -echo "DIR_BUILD=$BUILD_DIR" >> $CONFFILE -echo "DIR_LIB=\$(DIR_BUILD)/../../lib" >> $CONFFILE -echo "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs" >> $CONFFILE -echo "DIR_INCLUDE=\$(DIR_BUILD)/../../include" >> $CONFFILE -echo "DIR_MOD=\$(DIR_BUILD)/../stargazer/modules" >> $CONFFILE -echo "DIR_PLUGINS=\$(DIR_BUILD)/../stargazer/plugins" >> $CONFFILE -echo "ARCH=$ARCH" >> $CONFFILE -echo "DEFS=$DEFS" >> $CONFFILE -echo -n "STG_LIBS=" >> $CONFFILE +printf "OS=$OS\n" > $CONFFILE +printf "STG_TIME=yes\n" >> $CONFFILE +printf "DEBUG=$DEBUG\n" >> $CONFFILE +printf "DIR_BUILD=$BUILD_DIR\n" >> $CONFFILE +printf "DIR_LIB=\$(DIR_BUILD)/../../lib\n" >> $CONFFILE +printf "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs\n" >> $CONFFILE +printf "DIR_INCLUDE=\$(DIR_BUILD)/../../include\n" >> $CONFFILE +printf "DIR_MOD=\$(DIR_BUILD)/../stargazer/modules\n" >> $CONFFILE +printf "DIR_PLUGINS=\$(DIR_BUILD)/../stargazer/plugins\n" >> $CONFFILE +printf "ARCH=$ARCH\n" >> $CONFFILE +printf "DEFS=$DEFS\n" >> $CONFFILE +printf "STG_LIBS=" >> $CONFFILE for lib in $STG_LIBS do - echo -n "$lib " >> $CONFFILE + printf "$lib " >> $CONFFILE done -echo "" >> $CONFFILE -echo -n "PLUGINS=" >> $CONFFILE +printf "\n" >> $CONFFILE +printf "PLUGINS=" >> $CONFFILE for plugin in $PLUGINS do - echo -n "$plugin " >> $CONFFILE + printf "$plugin " >> $CONFFILE done -echo "" >> $CONFFILE -echo "LIB_THREAD=$LIB_THREAD" >> $CONFFILE -echo "CXXFLAGS=$CXXFLAGS" >> $CONFFILE -echo "CFLAGS=$CFLAGS" >> $CONFFILE -echo "LDFLAGS=$LDFLAGS" >> $CONFFILE -echo "PREFIX=$PREFIX" >> $CONFFILE -echo "BIN_MODE=$BIN_MODE" >> $CONFFILE -echo "DATA_MODE=$DATA_MODE" >> $CONFFILE -echo "DIR_MODE=$DIR_MODE" >> $CONFFILE -echo "OWNER=$OWNER" >> $CONFFILE +printf "\n" >> $CONFFILE +printf "LIB_THREAD=$LIB_THREAD\n" >> $CONFFILE +printf "CXXFLAGS=$CXXFLAGS\n" >> $CONFFILE +printf "CFLAGS=$CFLAGS\n" >> $CONFFILE +printf "LDFLAGS=$LDFLAGS\n" >> $CONFFILE +printf "PREFIX=$PREFIX\n" >> $CONFFILE +printf "BIN_MODE=$BIN_MODE\n" >> $CONFFILE +printf "DATA_MODE=$DATA_MODE\n" >> $CONFFILE +printf "DIR_MODE=$DIR_MODE\n" >> $CONFFILE +printf "OWNER=$OWNER\n" >> $CONFFILE $MAKE $MAKEOPTS diff --git a/projects/sgconf/Makefile b/projects/sgconf/Makefile index 297d3e20..0098ff5e 100644 --- a/projects/sgconf/Makefile +++ b/projects/sgconf/Makefile @@ -7,7 +7,9 @@ include ../../Makefile.conf PROG = sgconf SRCS = ./main.cpp \ - ./common_sg.cpp + ./common_sg.cpp \ + ./options.cpp \ + ./actions.cpp STGLIBS = conffiles \ srvconf \ @@ -41,7 +43,11 @@ OBJS = $(notdir $(patsubst %.cpp, %.o, $(patsubst %.c, %.o, $(SRCS)))) CXXFLAGS += $(DEFS) $(STGLIBS_INCS) $(SEARCH_DIRS) CFLAGS += $(DEFS) $(STGLIBS_INCS) $(SEARCH_DIRS) -LDFLAGS += -Wl,-E $(STGLIBS_LIBS) +LDFLAGS += $(STGLIBS_LIBS) + +ifneq ($(OS),darwin) +LDFLAGS += -Wl,-E +endif .PHONY: all clean distclean libs install uninstall install-bin install-data uninstall-bin uninstall-data all: libs $(PROG) ../../Makefile.conf diff --git a/projects/sgconf/action.h b/projects/sgconf/action.h new file mode 100644 index 00000000..0c1b4f92 --- /dev/null +++ b/projects/sgconf/action.h @@ -0,0 +1,55 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONF_ACTION_H__ +#define __STG_SGCONF_ACTION_H__ + +#include +#include + +namespace SGCONF +{ + +class OPTION_BLOCK; +struct PARSER_STATE; + +class ACTION +{ + public: + virtual ~ACTION() {} + + virtual ACTION * Clone() const = 0; + virtual std::string ParamDescription() const = 0; + virtual std::string DefaultDescription() const = 0; + virtual OPTION_BLOCK & Suboptions() = 0; + virtual PARSER_STATE Parse(int argc, char ** argv) = 0; + virtual void ParseValue(const std::string &) {} + + class ERROR : public std::runtime_error + { + public: + ERROR(const std::string & message) + : std::runtime_error(message.c_str()) {} + }; +}; + +} // namespace SGCONF + +#endif diff --git a/projects/sgconf/actions.cpp b/projects/sgconf/actions.cpp new file mode 100644 index 00000000..afa51621 --- /dev/null +++ b/projects/sgconf/actions.cpp @@ -0,0 +1,19 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h new file mode 100644 index 00000000..33a11347 --- /dev/null +++ b/projects/sgconf/actions.h @@ -0,0 +1,183 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONF_ACTIONS_H__ +#define __STG_SGCONF_ACTIONS_H__ + +#include "action.h" +#include "options.h" +#include "parser_state.h" + +#include "stg/common.h" +#include "stg/resetable.h" + +#include + +namespace SGCONF +{ + +typedef void (* FUNC0)(); + +template +class FUNC0_ACTION : public ACTION +{ + public: + FUNC0_ACTION(const F & func) : m_func(func) {} + + virtual ACTION * Clone() const { return new FUNC0_ACTION(*this); } + + virtual std::string ParamDescription() const { return ""; } + virtual std::string DefaultDescription() const { return ""; } + virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } + virtual PARSER_STATE Parse(int argc, char ** argv) + { + m_func(); + return PARSER_STATE(true, argc, argv); + } + + private: + F m_func; + OPTION_BLOCK m_suboptions; +}; + +template +inline +FUNC0_ACTION * MakeFunc0Action(F func) +{ +return new FUNC0_ACTION(func); +} + +template +class PARAM_ACTION : public ACTION +{ + public: + PARAM_ACTION(RESETABLE & param, + const T & defaultValue, + const std::string & paramDescription) + : m_param(param), + m_defaltValue(defaultValue), + m_description(paramDescription), + m_hasDefault(true) + {} + PARAM_ACTION(RESETABLE & param, + const std::string & paramDescription) + : m_param(param), + m_description(paramDescription), + m_hasDefault(false) + {} + + virtual ACTION * Clone() const { return new PARAM_ACTION(*this); } + + virtual std::string ParamDescription() const { return m_description; } + virtual std::string DefaultDescription() const; + virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } + virtual PARSER_STATE Parse(int argc, char ** argv); + virtual void ParseValue(const std::string & value); + + private: + RESETABLE & m_param; + T m_defaltValue; + std::string m_description; + bool m_hasDefault; + OPTION_BLOCK m_suboptions; +}; + +template +inline +std::string PARAM_ACTION::DefaultDescription() const +{ +return m_hasDefault ? " (default: '" + x2str(m_defaltValue) + "')" + : ""; +} + +template <> +inline +std::string PARAM_ACTION::DefaultDescription() const +{ +return m_hasDefault ? " (default: '" + m_defaltValue + "')" + : ""; +} + +template +inline +PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv) +{ +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); +T value; +if (str2x(*argv, value)) + throw ERROR(std::string("Bad argument: '") + *argv + "'"); +m_param = value; +return PARSER_STATE(false, --argc, ++argv); +} + +template +inline +void PARAM_ACTION::ParseValue(const std::string & stringValue) +{ +if (stringValue.empty()) + throw ERROR("Missing value."); +T value; +if (str2x(stringValue, value)) + throw ERROR(std::string("Bad value: '") + stringValue + "'"); +m_param = value; +} + +template <> +inline +void PARAM_ACTION::ParseValue(const std::string & stringValue) +{ +m_param = stringValue; +} + +template <> +inline +PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv) +{ +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); +m_param = *argv; +return PARSER_STATE(false, --argc, ++argv); +} + +template +inline +PARAM_ACTION * MakeParamAction(RESETABLE & param, + const T & defaultValue, + const std::string & paramDescription) +{ +return new PARAM_ACTION(param, defaultValue, paramDescription); +} + +template +inline +PARAM_ACTION * MakeParamAction(RESETABLE & param, + const std::string & paramDescription) +{ +return new PARAM_ACTION(param, paramDescription); +} + +} // namespace SGCONF + +#endif diff --git a/projects/sgconf/build b/projects/sgconf/build index a8a675be..d7b72363 100755 --- a/projects/sgconf/build +++ b/projects/sgconf/build @@ -51,17 +51,24 @@ then MAKE="gmake" fi +if [ "$sys" = "Darwin" ] +then + OS=darwin + ETC_DIR="./inst/freebsd/etc/stargazer" + MAKE="gmake" +fi + if [ "$OS" = "unknown" ] then - echo "#############################################################################" - echo "# Sorry, but sgconf currently supported by Linux, FreeBSD 4.x, 5.x, 6.x #" - echo "#############################################################################" + printf "#############################################################################\n" + printf "# Sorry, but sgconf currently supported by Linux, FreeBSD 4.x, 5.x, 6.x #\n" + printf "#############################################################################\n" exit 1 fi -echo "#############################################################################" -echo " Building sgconf for $sys $release" -echo "#############################################################################" +printf "#############################################################################\n" +printf " Building sgconf for $sys $release\n" +printf "#############################################################################\n" STG_LIBS="conffiles.lib crypto.lib @@ -77,12 +84,18 @@ else then DEFS="$DEFS -DFREE_BSD" else - DEFS="$DEFS -DFREE_BSD5" if [ "$OS" = "bsd7" ] then + DEFS="$DEFS -DFREE_BSD5" LIB_THREAD=-lpthread else - LIB_THREAD=-lc_r + if [ "$OS" == "darwin" ] + then + DEFS="$DEFS -DDARWIN" + LIB_THREAD=-lpthread + else + LIB_THREAD=-lc_r + fi fi fi fi @@ -97,32 +110,32 @@ then CXX=g++ fi -echo -n "Checking CC... " +printf "Checking CC... " $CC --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CC not found" + printf "FAIL!\n" + printf "$CC not found\n" exit; fi -echo "found" -echo -n "Checking CXX... " +printf "found\n" +printf "Checking CXX... " $CXX --version > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "$CXX not found" + printf "FAIL!\n" + printf "$CXX not found\n" exit; fi -echo "found" +printf "found\n" -echo -n "Checking endianess... " -echo "int main() { int probe = 0x00000001; return *(char *)&probe; }" > build_check.c +printf "Checking endianess... " +printf "int main() { int probe = 0x00000001; return *(char *)&probe; }\n" > build_check.c $CC $CFLAGS $LDFLAGS build_check.c -o fake > /dev/null 2> /dev/null if [ $? != 0 ] then - echo "FAIL!" - echo "Endianess checking failed" + printf "FAIL!\n" + printf "Endianess checking failed\n" exit; else ./fake @@ -131,61 +144,61 @@ else ARCH=le CXXFLAGS="$CXXFLAGS -DARCH_LE" CFLAGS="$CFLAGS -DARCH_LE" - echo "Little Endian" + printf "Little Endian\n" else ARCH=be CXXFLAGS="$CXXFLAGS -DARCH_BE" CFLAGS="$CFLAGS -DARCH_BE" - echo "Big Endian" + printf "Big Endian\n" fi fi rm -f fake -echo -n "Checking for -lexpat... " -echo "int main() { return 0; }" > build_check.c +printf "Checking for -lexpat... " +printf "int main() { return 0; }\n" > build_check.c $CC $CFLAGS $LDFLAGS build_check.c -lexpat -o fake > /dev/null 2> /dev/null if [ $? != 0 ] then CHECK_EXPAT=no - echo "no" + printf "no\n" else CHECK_EXPAT=yes - echo "yes" + printf "yes\n" fi rm -f fake rm -f build_check.c if [ "$CHECK_EXPAT" != "yes" ] then - echo "-lexpat not found!" + printf "-lexpat not found!\n" exit 1 fi -echo "OS=$OS" > $CONFFILE -echo "STG_TIME=yes" >> $CONFFILE -echo "DEBUG=$DEBUG" >> $CONFFILE -echo "DIR_BUILD=$BUILD_DIR" >> $CONFFILE -echo "DIR_LIB=\$(DIR_BUILD)/../../lib" >> $CONFFILE -echo "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs" >> $CONFFILE -echo "DIR_INCLUDE=\$(DIR_BUILD)/../../include" >> $CONFFILE -echo "ARCH=$ARCH" >> $CONFFILE -echo "CHECK_EXPAT=$CHECK_EXPAT" >> $CONFFILE -echo "DEFS=$DEFS" >> $CONFFILE -echo -n "STG_LIBS=" >> $CONFFILE +printf "OS=$OS\n" > $CONFFILE +printf "STG_TIME=yes\n" >> $CONFFILE +printf "DEBUG=$DEBUG\n" >> $CONFFILE +printf "DIR_BUILD=$BUILD_DIR\n" >> $CONFFILE +printf "DIR_LIB=\$(DIR_BUILD)/../../lib\n" >> $CONFFILE +printf "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs\n" >> $CONFFILE +printf "DIR_INCLUDE=\$(DIR_BUILD)/../../include\n" >> $CONFFILE +printf "ARCH=$ARCH\n" >> $CONFFILE +printf "CHECK_EXPAT=$CHECK_EXPAT\n" >> $CONFFILE +printf "DEFS=$DEFS\n" >> $CONFFILE +printf "STG_LIBS=" >> $CONFFILE for lib in $STG_LIBS do - echo -n "$lib " >> $CONFFILE + printf "$lib " >> $CONFFILE done -echo "" >> $CONFFILE -echo "LIB_THREAD=$LIB_THREAD" >> $CONFFILE -echo "CXXFLAGS=$CXXFLAGS" >> $CONFFILE -echo "CFLAGS=$CFLAGS" >> $CONFFILE -echo "LDFLAGS=$LDFLAGS" >> $CONFFILE -echo "PREFIX=$PREFIX" >> $CONFFILE -echo "BIN_MODE=$BIN_MODE" >> $CONFFILE -echo "DATA_MODE=$DATA_MODE" >> $CONFFILE -echo "DIR_MODE=$DIR_MODE" >> $CONFFILE -echo "OWNER=$OWNER" >> $CONFFILE +printf "\n" >> $CONFFILE +printf "LIB_THREAD=$LIB_THREAD\n" >> $CONFFILE +printf "CXXFLAGS=$CXXFLAGS\n" >> $CONFFILE +printf "CFLAGS=$CFLAGS\n" >> $CONFFILE +printf "LDFLAGS=$LDFLAGS\n" >> $CONFFILE +printf "PREFIX=$PREFIX\n" >> $CONFFILE +printf "BIN_MODE=$BIN_MODE\n" >> $CONFFILE +printf "DATA_MODE=$DATA_MODE\n" >> $CONFFILE +printf "DIR_MODE=$DIR_MODE\n" >> $CONFFILE +printf "OWNER=$OWNER\n" >> $CONFFILE $MAKE $MAKEOPTS diff --git a/projects/sgconf/common_sg.cpp b/projects/sgconf/common_sg.cpp index 3d015160..e407a716 100644 --- a/projects/sgconf/common_sg.cpp +++ b/projects/sgconf/common_sg.cpp @@ -25,21 +25,25 @@ */ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "stg/common.h" #include "sg_error_codes.h" #include "common_sg.h" #include "version_sg.h" -using namespace std; +#include "stg/common.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +using namespace STG; const int usageConf = 0; const int usageInfo = 1; @@ -47,18 +51,26 @@ const int usageInfo = 1; const int TO_KOI8 = 0; const int FROM_KOI8 = 1; //----------------------------------------------------------------------------- -struct GetUserCbData +struct ResultData +{ + bool result; + std::string reason; +}; +//----------------------------------------------------------------------------- +struct GetUserData { - void * data; - bool * result; + GetUserData(REQUEST & req, bool res) : request(req), result(res) {} + REQUEST & request; + bool result; + std::string reason; }; //--------------------------------------------------------------------------- struct HelpParams { - string setActionName; - string getActionName; - string valueName; - string valueParam; + std::string setActionName; + std::string getActionName; + std::string valueName; + std::string valueParam; }; //--------------------------------------------------------------------------- void Usage(int usageType) @@ -84,7 +96,7 @@ HelpParams hp[] = {"set credit expire", "get credit expire", "-E", ""}, {"set password", "get password", "-o", ""}, {"set prepaid traffic", "get prepaid traffic", "-e", ""}, - {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"}, + {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"}, {"set name", "get name", "-A", ""}, {"set note", "get note", "-N", ""}, {"set street address", "get street address", "-D", "
"}, @@ -122,6 +134,9 @@ printf("sgconf set -s -p -a -w -u -- printf("To get userdata<0...9> use:\n"); printf("sgconf get -s -p -a -w -u --ud0 [--ud1 ...]\n\n"); +printf("To get user's authorizers list use:\n"); +printf("sgconf get -s -p -a -w -u --authorized-by\n\n"); + printf("To send message use:\n"); printf("sgconf set -s -p -a -w -u -m \n\n"); @@ -164,7 +179,7 @@ short int ParseServerPort(const char * p) int port; if (str2x(p, port) != 0) { - printf("Incorresct server port %s\n", p); + printf("Incorrect server port %s\n", p); exit(NETWORK_ERR_CODE); } return (short)port; @@ -174,7 +189,7 @@ char * ParseAdminLogin(char * adm) { if (CheckLogin(adm)) { - printf("Incorresct admin login %s\n", adm); + printf("Incorrect admin login %s\n", adm); exit(PARAMETER_PARSING_ERR_CODE); } return adm; @@ -195,13 +210,13 @@ char * ParseUser(char * usr) { if (CheckLogin(usr)) { - printf("Incorresct user login %s\n", usr); + printf("Incorrect user login %s\n", usr); exit(PARAMETER_PARSING_ERR_CODE); } return usr; } //----------------------------------------------------------------------------- -void ConvertKOI8(const string & src, string * dst, int encType) +void ConvertKOI8(const std::string & src, std::string * dst, int encType) { iconv_t cd; char * ob = new char[src.size() * 2 + 1]; @@ -276,67 +291,68 @@ delete[] ob; delete[] ib; } //----------------------------------------------------------------------------- -void ConvertFromKOI8(const string & src, string * dst) +void ConvertFromKOI8(const std::string & src, std::string * dst) { ConvertKOI8(src, dst, FROM_KOI8); } //----------------------------------------------------------------------------- -void ConvertToKOI8(const string & src, string * dst) +void ResultCallback(bool result, const std::string & reason, void * d) { -ConvertKOI8(src, dst, TO_KOI8); +ResultData * data = static_cast(d); +data->result = result; +data->reason = reason; } //----------------------------------------------------------------------------- -int RecvSetUserAnswer(const char * ans, void * d) +void RecvAuthByData(bool result, const std::string & reason, + const AUTH_BY::INFO & list, void * d) { -GetUserCbData * gucbd; -gucbd = (GetUserCbData *)d; - -bool * result = gucbd->result; +ResultData * data = static_cast(d); +data->result = result; +data->reason = reason; -//REQUEST * req = (REQUEST *)gucbd->data; +if (!result) + return; -//printf("ans=%s\n", ans); -if (strcasecmp("Ok", ans) == 0) - *result = true; -else - *result = false; +for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) + std::cout << *it << "\n"; -return 0; +std::cout << std::endl; } //----------------------------------------------------------------------------- struct StringReqParams { - string name; - RESETABLE reqParam; - string * value; + std::string name; + RESETABLE reqParam; + const std::string * value; }; //----------------------------------------------------------------------------- -void RecvUserData(USERDATA * ud, void * d) +void GetUserCallback(bool result, const std::string& reason, const GET_USER::INFO & info, void * d) { -GetUserCbData * gucbd; -gucbd = (GetUserCbData *)d; - -bool * result = gucbd->result; +GetUserData * data = static_cast(d); +data->result = false; +data->reason = reason; -REQUEST * req = (REQUEST *)gucbd->data; +if (!result) + return; -if (ud->login == "") +if (info.login == "") { - *result = false; + data->result = false; + data->reason = "Invalid login."; return; } -if (!req->cash.res_empty()) - cout << "cash=" << ud->cash << endl; +if (!data->request.cash.empty()) + cout << "cash = " << info.cash << endl; -if (!req->credit.res_empty()) - cout << "credit=" << ud->credit << endl; +if (!data->request.credit.empty()) + cout << "credit = " << info.credit << endl; -if (!req->creditExpire.res_empty()) +if (!data->request.creditExpire.empty()) { char buf[32]; struct tm brokenTime; - time_t tt = ud->creditExpire; + time_t tt = info.creditExpire; brokenTime.tm_wday = 0; brokenTime.tm_yday = 0; @@ -349,151 +365,172 @@ if (!req->creditExpire.res_empty()) strftime(buf, 32, "%Y-%m-%d", &brokenTime); - cout << "creditExpire=" << buf << endl; + cout << "creditExpire = " << buf << endl; } -if (!req->down.res_empty()) - cout << "down=" << ud->down << endl; +if (!data->request.down.empty()) + cout << "down = " << info.down << endl; -if (!req->passive.res_empty()) - cout << "passive=" << ud->passive << endl; +if (!data->request.passive.empty()) + cout << "passive = " << info.passive << endl; -if (!req->disableDetailStat.res_empty()) - cout << "disableDetailStat=" << ud->disableDetailStat << endl; +if (!data->request.disableDetailStat.empty()) + cout << "disableDetailStat = " << info.disableDetailStat << endl; -if (!req->alwaysOnline.res_empty()) - cout << "alwaysOnline=" << ud->alwaysOnline << endl; +if (!data->request.alwaysOnline.empty()) + cout << "alwaysOnline = " << info.alwaysOnline << endl; -if (!req->prepaidTraff.res_empty()) - cout << "prepaidTraff=" << ud->prepaidTraff << endl; +if (!data->request.prepaidTraff.empty()) + cout << "prepaidTraff = " << info.prepaidTraff << endl; for (int i = 0; i < DIR_NUM; i++) { - if (!req->u[i].res_empty()) - cout << "u" << i << "=" << ud->stat.mu[i] << endl; - if (!req->d[i].res_empty()) - cout << "d" << i << "=" << ud->stat.md[i] << endl; + if (!data->request.sessionUpload[i].empty()) + cout << "session upload for dir " << i << " = " << info.stat.su[i] << endl; + if (!data->request.sessionDownload[i].empty()) + cout << "session download for dir " << i << "=" << info.stat.sd[i] << endl; + } + +for (int i = 0; i < DIR_NUM; i++) + { + if (!data->request.monthUpload[i].empty()) + cout << "month upload for dir " << i << " = " << info.stat.mu[i] << endl; + if (!data->request.monthDownload[i].empty()) + cout << "month download for dir " << i << " = " << info.stat.md[i] << endl; } for (int i = 0; i < USERDATA_NUM; i++) { - if (!req->ud[i].res_empty()) + if (!data->request.userData[i].empty()) { - string str; - ConvertFromKOI8(ud->userData[i], &str); - cout << "userdata" << i << "=" << str << endl; + std::string str; + ConvertFromKOI8(info.userData[i], &str); + cout << "user data " << i << " = " << str << endl; } } StringReqParams strReqParams[] = { - {"note", req->note, &ud->note}, - {"name", req->name, &ud->name}, - {"address", req->address, &ud->address}, - {"email", req->email, &ud->email}, - {"phone", req->phone, &ud->phone}, - {"group", req->group, &ud->group}, - {"tariff", req->tariff, &ud->tariff}, - {"password", req->usrPasswd, &ud->password}, - {"ip", req->ips, &ud->ips} // IP-address of user + {"note", data->request.note, &info.note}, + {"name", data->request.name, &info.name}, + {"address", data->request.address, &info.address}, + {"email", data->request.email, &info.email}, + {"phone", data->request.phone, &info.phone}, + {"group", data->request.group, &info.group}, + {"tariff", data->request.tariff, &info.tariff}, + {"password", data->request.usrPasswd, &info.password}, + {"ip", data->request.ips, &info.ips} // IP-address of user }; for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++) { - if (!strReqParams[i].reqParam.res_empty()) + if (!strReqParams[i].reqParam.empty()) { string str; ConvertFromKOI8(*strReqParams[i].value, &str); - cout << strReqParams[i].name << "=" << str << endl; + cout << strReqParams[i].name << " = " << str << endl; } } -*result = true; +data->result = true; } //----------------------------------------------------------------------------- -int ProcessSetUser(const std::string &server, - int port, - const std::string &admLogin, - const std::string &admPasswd, - const std::string &str, - void * data, - bool isMessage) +bool ProcessSetUser(const std::string & server, + int port, + const std::string & login, + const std::string & password, + const std::string & user, + const USER_CONF_RES & conf, + const USER_STAT_RES & stat) { -SERVCONF sc; +SERVCONF sc(server, port, login, password); -bool result = false; +ResultData data; +int res = sc.ChgUser(user, conf, stat, ResultCallback, &data); - -sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ -sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ -sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ -sc.SetAdmPassword(admPasswd.c_str()); - -// TODO Good variable name :) -GetUserCbData gucbd; - -gucbd.data = data; -gucbd.result = &result; - -if (isMessage) +if (res == st_ok && data.result) { - sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd); - sc.MsgUser(str.c_str()); + printf("Ok\n"); + return false; } + +printf("Error\n"); +if (res != st_ok) + printf("%s\n", sc.GetStrError().c_str()); else - { - sc.SetChgUserCb(RecvSetUserAnswer, &gucbd); - sc.ChgUser(str.c_str()); - } + printf("%s\n", data.reason.c_str()); +return true; +} +//----------------------------------------------------------------------------- +bool ProcessSendMessage(const std::string & server, uint16_t port, + const std::string & login, const std::string & password, + const std::string & user, const std::string & text) +{ +SERVCONF sc(server, port, login, password); + +ResultData data; +int res = sc.SendMessage(user, text, ResultCallback, &data); -if (result) +if (res == st_ok && data.result) { printf("Ok\n"); - return 0; + return true; } + +printf("Error\n"); +if (res != st_ok) + printf("%s\n", sc.GetStrError().c_str()); else + printf("%s\n", data.reason.c_str()); +return false; +} +//----------------------------------------------------------------------------- +bool ProcessGetUser(const std::string &server, + int port, + const std::string &admLogin, + const std::string &admPasswd, + const std::string &login, + REQUEST & request) +{ +SERVCONF sc(server, port, admLogin, admPasswd); + +GetUserData data(request, false); +bool res = (sc.GetUser(login.c_str(), GetUserCallback, &data) == st_ok); + +if (res && data.result) { - printf("Error\n"); - return -1; + printf("Ok\n"); + return true; } -return 0; +printf("Error\n"); +if (!res) + printf("%s\n", sc.GetStrError().c_str()); +else + printf("%s\n", data.reason.c_str()); +return false; } //----------------------------------------------------------------------------- -int ProcessGetUser(const std::string &server, +bool ProcessAuthBy(const std::string &server, int port, const std::string &admLogin, const std::string &admPasswd, - const std::string &login, - void * data) + const std::string &login) { -SERVCONF sc; - -bool result = false; - -sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ -sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ -sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ -sc.SetAdmPassword(admPasswd.c_str()); +SERVCONF sc(server, port, admLogin, admPasswd); -// TODO Good variable name :) -GetUserCbData gucbd; +ResultData data; +bool res = (sc.AuthBy(login.c_str(), RecvAuthByData, &data) == st_ok); -gucbd.data = data; -gucbd.result = &result; - -sc.SetGetUserDataRecvCb(RecvUserData, &gucbd); -sc.GetUser(login.c_str()); - -if (result) +if (res && data.result) { printf("Ok\n"); - return 0; - } -else - { - printf("Error\n"); - return -1; + return true; } -return 0; +printf("Error\n"); +if (!res) + printf("%s\n", sc.GetStrError().c_str()); +else + printf("%s\n", data.reason.c_str()); +return false; } //----------------------------------------------------------------------------- diff --git a/projects/sgconf/common_sg.h b/projects/sgconf/common_sg.h index a654748a..323a8cf8 100644 --- a/projects/sgconf/common_sg.h +++ b/projects/sgconf/common_sg.h @@ -28,11 +28,15 @@ #ifndef COMMON_SG_H #define COMMON_SG_H -#include - #include "stg/servconf.h" +#include "stg/servconf_types.h" #include "request.h" +#include + +struct USER_CONF_RES; +struct USER_STAT_RES; + void UsageConf(); void UsageInfo(); @@ -45,20 +49,29 @@ int CheckLogin(const char * login); void ConvertFromKOI8(const std::string & src, std::string * dst); void ConvertToKOI8(const std::string & src, std::string * dst); -int ProcessGetUser(const std::string &server, - int port, - const std::string &admLogin, - const std::string &admPasswd, - const std::string &login, - void * data); +bool ProcessGetUser(const std::string & server, + int port, + const std::string & admLogin, + const std::string & admPasswd, + const std::string & login, + REQUEST & request); -int ProcessSetUser(const std::string &server, +bool ProcessAuthBy(const std::string & server, int port, - const std::string &admLogin, - const std::string &admPasswd, - const std::string &str, - void * data, - bool isMessage = false); + const std::string & admLogin, + const std::string & admPasswd, + const std::string & login); -#endif +bool ProcessSetUser(const std::string & server, + int port, + const std::string & admLogin, + const std::string & admPasswd, + const std::string & user, + const USER_CONF_RES & conf, + const USER_STAT_RES & stat); +bool ProcessSendMessage(const std::string & server, uint16_t port, + const std::string & login, const std::string & password, + const std::string & user, const std::string & text); + +#endif diff --git a/projects/sgconf/config.h b/projects/sgconf/config.h new file mode 100644 index 00000000..9ec9616e --- /dev/null +++ b/projects/sgconf/config.h @@ -0,0 +1,75 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONF_CONFIG_H__ +#define __STG_SGCONF_CONFIG_H__ + +#include "stg/common.h" +#include "stg/resetable.h" +#include "stg/os_int.h" + +#include + +namespace SGCONF +{ + +struct CONFIG +{ + RESETABLE configFile; + RESETABLE server; + RESETABLE port; + RESETABLE userName; + RESETABLE userPass; + + CONFIG & operator=(const CONFIG & rhs) + { + if (!rhs.configFile.empty()) + configFile = rhs.configFile; + if (!rhs.server.empty()) + server = rhs.server; + if (!rhs.port.empty()) + port = rhs.port; + if (!rhs.userName.empty()) + userName = rhs.userName; + if (!rhs.userPass.empty()) + userPass = rhs.userPass; + return *this; + } + + std::string Serialize() const + { + std::string res("{ "); + if (!configFile.empty()) + res += "configFile: '" + configFile.data() + "'"; + if (!server.empty()) + res += ", server: '" + server.data() + "'"; + if (!port.empty()) + res += ", port: " + x2str(port.data()); + if (!userName.empty()) + res += ", userName: '" + userName.data() + "'"; + if (!userPass.empty()) + res += ", userPass: '" + userPass.data() + "'"; + return res + " }"; + } +}; + +} + +#endif diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 2aaacbc0..a0ef89a1 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -16,39 +16,248 @@ /* * Author : Boris Mikhailenko + * Author : Maxim Mamontov */ - /* - $Author: faust $ - $Revision: 1.25 $ - $Date: 2010/03/25 14:37:43 $ - */ +#include "request.h" +#include "common_sg.h" +#include "sg_error_codes.h" -#include -#include -#include -#include +#include "options.h" +#include "actions.h" +#include "config.h" + +#include "stg/user_conf.h" +#include "stg/user_stat.h" +#include "stg/common.h" #include #include #include +#include #include #include -#include #include -#include "stg/common.h" -#include "stg/netunit.h" -#include "request.h" -#include "common_sg.h" -#include "sg_error_codes.h" +#include +#include +#include +#include + +namespace +{ -using namespace std; +template +struct ARRAY_TYPE +{ +typedef typename T::value_type type; +}; -time_t stgTime; +template +struct ARRAY_TYPE +{ +typedef T type; +}; -int ParseReplyGet(void * data, list * ans); -//int ParseReplySet(void * data, list * ans); +template +struct ARRAY_TYPE +{ +typedef T type; +}; + +template +struct nullary_function +{ +typedef T result_type; +}; + +template +class binder0 : public nullary_function +{ + public: + binder0(const F & func, const typename F::argument_type & arg) + : m_func(func), m_arg(arg) {} + typename F::result_type operator()() const { return m_func(m_arg); } + private: + F m_func; + typename F::argument_type m_arg; +}; + +template +inline +binder0 bind0(const F & func, const typename F::argument_type & arg) +{ +return binder0(func, arg); +} + +template +class METHOD1_ADAPTER : public std::unary_function +{ + public: + METHOD1_ADAPTER(R (C::* func)(A), C & obj) : m_func(func), m_obj(obj) {} + R operator()(A arg) { return (m_obj.*m_func)(arg); } + private: + R (C::* m_func)(A); + C & m_obj; +}; + +template +class CONST_METHOD1_ADAPTER : public std::unary_function +{ + public: + CONST_METHOD1_ADAPTER(R (C::* func)(A) const, C & obj) : m_func(func), m_obj(obj) {} + R operator()(A arg) const { return (m_obj.*m_func)(arg); } + private: + R (C::* m_func)(A) const; + C & m_obj; +}; + +template +METHOD1_ADAPTER Method1Adapt(R (C::* func)(A), C & obj) +{ +return METHOD1_ADAPTER(func, obj); +} + +template +CONST_METHOD1_ADAPTER Method1Adapt(R (C::* func)(A) const, C & obj) +{ +return CONST_METHOD1_ADAPTER(func, obj); +} + +template +bool SetArrayItem(T & array, const char * index, const typename ARRAY_TYPE::type & value) +{ +size_t pos = 0; +if (str2x(index, pos)) + return false; +array[pos] = value; +return true; +} + +void Usage(); +void UsageAll(); +void UsageImpl(bool full); +void UsageConnection(); +void UsageAdmins(bool full); +void UsageTariffs(bool full); +void UsageUsers(bool full); +void UsageServices(bool full); +void UsageCorporations(bool full); + +void Version(); + +void ReadUserConfigFile(SGCONF::OPTION_BLOCK & block) +{ +std::vector paths; +const char * configHome = getenv("XDG_CONFIG_HOME"); +if (configHome == NULL) + { + const char * home = getenv("HOME"); + if (home == NULL) + return; + paths.push_back(std::string(home) + "/.config/sgconf/sgconf.conf"); + paths.push_back(std::string(home) + "/.sgconf/sgconf.conf"); + } +else + paths.push_back(std::string(configHome) + "/sgconf/sgconf.conf"); +for (std::vector::const_iterator it = paths.begin(); it != paths.end(); ++it) + if (access(it->c_str(), R_OK) == 0) + { + block.ParseFile(*it); + return; + } +} + +} // namespace anonymous + +namespace SGCONF +{ + +class CONFIG_ACTION : public ACTION +{ + public: + CONFIG_ACTION(CONFIG & config, + const std::string & paramDescription) + : m_config(config), + m_description(paramDescription) + {} + + virtual ACTION * Clone() const { return new CONFIG_ACTION(*this); } + + virtual std::string ParamDescription() const { return m_description; } + virtual std::string DefaultDescription() const { return ""; } + virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } + virtual PARSER_STATE Parse(int argc, char ** argv); + + private: + CONFIG & m_config; + std::string m_description; + OPTION_BLOCK m_suboptions; + + void ParseCredentials(const std::string & credentials); + void ParseHostAndPort(const std::string & hostAndPort); +}; + +PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv) +{ +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); +char * pos = strchr(*argv, '@'); +if (pos != NULL) + { + ParseCredentials(std::string(*argv, pos)); + ParseHostAndPort(std::string(pos + 1)); + } +else + { + ParseHostAndPort(std::string(*argv)); + } +return PARSER_STATE(false, --argc, ++argv); +} + +void CONFIG_ACTION::ParseCredentials(const std::string & credentials) +{ +std::string::size_type pos = credentials.find_first_of(':'); +if (pos != std::string::npos) + { + m_config.userName = credentials.substr(0, pos); + m_config.userPass = credentials.substr(pos + 1); + } +else + { + m_config.userName = credentials; + } +} + +void CONFIG_ACTION::ParseHostAndPort(const std::string & hostAndPort) +{ +std::string::size_type pos = hostAndPort.find_first_of(':'); +if (pos != std::string::npos) + { + m_config.server = hostAndPort.substr(0, pos); + uint16_t port = 0; + if (str2x(hostAndPort.substr(pos + 1), port)) + throw ERROR("Invalid port value: '" + hostAndPort.substr(pos + 1) + "'"); + m_config.port = port; + } +else + { + m_config.server = hostAndPort; + } +} + +inline +CONFIG_ACTION * MakeParamAction(CONFIG & config, + const std::string & paramDescription) +{ +return new CONFIG_ACTION(config, paramDescription); +} + +} // namespace SGCONF + +time_t stgTime; struct option long_options_get[] = { {"server", 1, 0, 's'}, //Server @@ -66,37 +275,12 @@ struct option long_options_get[] = { {"passive", 0, 0, 'i'}, //passive {"disable-stat",0, 0, 'S'}, //disable detail stat {"always-online",0, 0, 'O'}, //always online -{"u0", 0, 0, 500}, //U0 -{"u1", 0, 0, 501}, //U1 -{"u2", 0, 0, 502}, //U2 -{"u3", 0, 0, 503}, //U3 -{"u4", 0, 0, 504}, //U4 -{"u5", 0, 0, 505}, //U5 -{"u6", 0, 0, 506}, //U6 -{"u7", 0, 0, 507}, //U7 -{"u8", 0, 0, 508}, //U8 -{"u9", 0, 0, 509}, //U9 -{"d0", 0, 0, 600}, //D0 -{"d1", 0, 0, 601}, //D1 -{"d2", 0, 0, 602}, //D2 -{"d3", 0, 0, 603}, //D3 -{"d4", 0, 0, 604}, //D4 -{"d5", 0, 0, 605}, //D5 -{"d6", 0, 0, 606}, //D6 -{"d7", 0, 0, 607}, //D7 -{"d8", 0, 0, 608}, //D8 -{"d9", 0, 0, 609}, //D9 - -{"ud0", 0, 0, 700}, //UserData0 -{"ud1", 0, 0, 701}, //UserData1 -{"ud2", 0, 0, 702}, //UserData2 -{"ud3", 0, 0, 703}, //UserData3 -{"ud4", 0, 0, 704}, //UserData4 -{"ud5", 0, 0, 705}, //UserData5 -{"ud6", 0, 0, 706}, //UserData6 -{"ud7", 0, 0, 707}, //UserData7 -{"ud8", 0, 0, 708}, //UserData8 -{"ud9", 0, 0, 709}, //UserData9 +{"session-upload", 1, 0, 500}, //SU0 +{"session-download", 1, 0, 501}, //SD0 +{"month-upload", 1, 0, 502}, //MU0 +{"month-download", 1, 0, 503}, //MD0 + +{"user-data", 1, 0, 700}, //UserData0 {"prepaid", 0, 0, 'e'}, //prepaid traff {"create", 0, 0, 'n'}, //create @@ -108,7 +292,8 @@ struct option long_options_get[] = { {"email", 0, 0, 'L'}, //emaiL {"phone", 0, 0, 'P'}, //phone {"group", 0, 0, 'G'}, //Group -{"ip", 0, 0, 'I'}, //IP-address of user +{"ip", 0, 0, 'I'}, //IP-address of user +{"authorized-by",0, 0, 800}, //always online {0, 0, 0, 0}}; @@ -128,37 +313,12 @@ struct option long_options_set[] = { {"passive", 1, 0, 'i'}, //passive {"disable-stat",1, 0, 'S'}, //disable detail stat {"always-online",1, 0, 'O'}, //always online -{"u0", 1, 0, 500}, //U0 -{"u1", 1, 0, 501}, //U1 -{"u2", 1, 0, 502}, //U2 -{"u3", 1, 0, 503}, //U3 -{"u4", 1, 0, 504}, //U4 -{"u5", 1, 0, 505}, //U5 -{"u6", 1, 0, 506}, //U6 -{"u7", 1, 0, 507}, //U7 -{"u8", 1, 0, 508}, //U8 -{"u9", 1, 0, 509}, //U9 -{"d0", 1, 0, 600}, //D0 -{"d1", 1, 0, 601}, //D1 -{"d2", 1, 0, 602}, //D2 -{"d3", 1, 0, 603}, //D3 -{"d4", 1, 0, 604}, //D4 -{"d5", 1, 0, 605}, //D5 -{"d6", 1, 0, 606}, //D6 -{"d7", 1, 0, 607}, //D7 -{"d8", 1, 0, 608}, //D8 -{"d9", 1, 0, 609}, //D9 - -{"ud0", 1, 0, 700}, //UserData -{"ud1", 1, 0, 701}, //UserData1 -{"ud2", 1, 0, 702}, //UserData2 -{"ud3", 1, 0, 703}, //UserData3 -{"ud4", 1, 0, 704}, //UserData4 -{"ud5", 1, 0, 705}, //UserData5 -{"ud6", 1, 0, 706}, //UserData6 -{"ud7", 1, 0, 707}, //UserData7 -{"ud8", 1, 0, 708}, //UserData8 -{"ud9", 1, 0, 709}, //UserData9 +{"session-upload", 1, 0, 500}, //U0 +{"session-download", 1, 0, 501}, //U1 +{"month-upload", 1, 0, 502}, //U2 +{"month-download", 1, 0, 503}, //U3 + +{"user-data", 1, 0, 700}, //UserData {"prepaid", 1, 0, 'e'}, //prepaid traff {"create", 1, 0, 'n'}, //create @@ -170,40 +330,33 @@ struct option long_options_set[] = { {"email", 1, 0, 'L'}, //emaiL {"phone", 1, 0, 'P'}, //phone {"group", 1, 0, 'G'}, //Group -{"ip", 0, 0, 'I'}, //IP-address of user +{"ip", 0, 0, 'I'}, //IP-address of user {0, 0, 0, 0}}; //----------------------------------------------------------------------------- -double ParseCash(const char * c, string * message) +CASH_INFO ParseCash(const char * str) { //-c 123.45:log message -double cash; -char * msg; -char * str; -str = new char[strlen(c) + 1]; - -strncpy(str, c, strlen(c)); -str[strlen(c)] = 0; - -msg = strchr(str, ':'); - -if (msg) +std::string cashString; +std::string message; +const char * pos = strchr(str, ':'); +if (pos != NULL) { - *message = msg + 1; - str[msg - str] = 0; + cashString.append(str, pos); + message.append(pos + 1); } else - *message = ""; + cashString = str; -if (strtodouble2(str, cash) != 0) +double cash = 0; +if (strtodouble2(cashString, cash) != 0) { - printf("Incorrect cash value %s\n", c); + printf("Incorrect cash value %s\n", str); exit(PARAMETER_PARSING_ERR_CODE); } -delete[] str; -return cash; +return CASH_INFO(cash, message); } //----------------------------------------------------------------------------- double ParseCredit(const char * c) @@ -253,63 +406,24 @@ if (!(dp[1] == 0 && (dp[0] == '1' || dp[0] == '0'))) return dp[0] - '0'; } //----------------------------------------------------------------------------- -string ParseTariff(const char * t, int &chgType) +void ParseTariff(const char * str, RESETABLE & tariffName, RESETABLE & nextTariff) { -int l = strlen(t); -char * s; -s = new char[l]; -char * s1, * s2; -string ss; - -strcpy(s, t); - -s1 = strtok(s, ":"); - -if (strlen(s1) >= TARIFF_NAME_LEN) +const char * pos = strchr(str, ':'); +if (pos != NULL) { - printf("Tariff name too big %s\n", s1); - exit(PARAMETER_PARSING_ERR_CODE); - } - -//*tariff = s; - -if (CheckLogin(s1)) - { - printf("Incorrect tariff value %s\n", t); - exit(PARAMETER_PARSING_ERR_CODE); - } - -s2 = strtok(NULL, ":"); - -chgType = -1; - -if (s2 == NULL) - { - chgType = TARIFF_NOW; - ss = s; - delete[] s; - return ss; - } - - -if (strcmp(s2, "now") == 0) - chgType = TARIFF_NOW; - -if (strcmp(s2, "delayed") == 0) - chgType = TARIFF_DEL; - -if (strcmp(s2, "recalc") == 0) - chgType = TARIFF_REC; - -if (chgType < 0) - { - printf("Incorrect tariff value %s\n", t); - exit(PARAMETER_PARSING_ERR_CODE); + std::string tariff(str, pos); + if (strcmp(pos + 1, "now") == 0) + tariffName = tariff; + else if (strcmp(pos + 1, "delayed") == 0) + nextTariff = tariff; + else + { + printf("Incorrect tariff value '%s'. Should be '', ':now' or ':delayed'.\n", str); + exit(PARAMETER_PARSING_ERR_CODE); + } } - -ss = s; -delete[] s; -return ss; +else + tariffName = str; } //----------------------------------------------------------------------------- time_t ParseCreditExpire(const char * str) @@ -400,10 +514,10 @@ memset(str, 0, strLen); r[0] = 0; -if (!req->usrMsg.res_empty()) +if (!req->usrMsg.empty()) { string msg; - Encode12str(msg, req->usrMsg); + Encode12str(msg, req->usrMsg.data()); sprintf(str, "", req->login.const_data().c_str(), msg.c_str()); //sprintf(str, "\n", req->login, msg); strcat(r, str); @@ -429,25 +543,25 @@ if (req->createUser) strcat(r, "\n"); sprintf(str, "\n", req->login.const_data().c_str()); strcat(r, str); -if (!req->credit.res_empty()) +if (!req->credit.empty()) { sprintf(str, "\n", req->credit.const_data()); strcat(r, str); } -if (!req->creditExpire.res_empty()) +if (!req->creditExpire.empty()) { sprintf(str, "\n", req->creditExpire.const_data()); strcat(r, str); } -if (!req->prepaidTraff.res_empty()) +if (!req->prepaidTraff.empty()) { sprintf(str, "\n", req->prepaidTraff.const_data()); strcat(r, str); } -if (!req->cash.res_empty()) +if (!req->cash.empty()) { string msg; Encode12str(msg, req->message); @@ -455,7 +569,7 @@ if (!req->cash.res_empty()) strcat(r, str); } -if (!req->setCash.res_empty()) +if (!req->setCash.empty()) { string msg; Encode12str(msg, req->message); @@ -463,38 +577,38 @@ if (!req->setCash.res_empty()) strcat(r, str); } -if (!req->usrPasswd.res_empty()) +if (!req->usrPasswd.empty()) { sprintf(str, "\n", req->usrPasswd.const_data().c_str()); strcat(r, str); } -if (!req->down.res_empty()) +if (!req->down.empty()) { sprintf(str, "\n", req->down.const_data()); strcat(r, str); } -if (!req->passive.res_empty()) +if (!req->passive.empty()) { sprintf(str, "\n", req->passive.const_data()); strcat(r, str); } -if (!req->disableDetailStat.res_empty()) +if (!req->disableDetailStat.empty()) { sprintf(str, "\n", req->disableDetailStat.const_data()); strcat(r, str); } -if (!req->alwaysOnline.res_empty()) +if (!req->alwaysOnline.empty()) { sprintf(str, "\n", req->alwaysOnline.const_data()); strcat(r, str); } // IP-address of user -if (!req->ips.res_empty()) +if (!req->ips.empty()) { sprintf(str, "\n", req->ips.const_data().c_str()); strcat(r, str); @@ -504,7 +618,36 @@ int uPresent = false; int dPresent = false; for (int i = 0; i < DIR_NUM; i++) { - if (!req->u[i].res_empty()) + if (!req->monthUpload[i].empty()) + { + if (!uPresent && !dPresent) + { + sprintf(str, "monthUpload[i].const_data(); + //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data()); + sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str()); + strcat(r, str); + } + if (!req->monthDownload[i].empty()) + { + if (!uPresent && !dPresent) + { + sprintf(str, "monthDownload[i].const_data(); + sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str()); + strcat(r, str); + } + if (!req->sessionUpload[i].empty()) { if (!uPresent && !dPresent) { @@ -514,12 +657,12 @@ for (int i = 0; i < DIR_NUM; i++) } stringstream ss; - ss << req->u[i].const_data(); + ss << req->sessionUpload[i].const_data(); //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data()); sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str()); strcat(r, str); } - if (!req->d[i].res_empty()) + if (!req->sessionDownload[i].empty()) { if (!uPresent && !dPresent) { @@ -529,7 +672,7 @@ for (int i = 0; i < DIR_NUM; i++) } stringstream ss; - ss << req->d[i].const_data(); + ss << req->sessionDownload[i].const_data(); sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str()); strcat(r, str); } @@ -541,7 +684,7 @@ if (uPresent || dPresent) //printf("%s\n", r); -if (!req->tariff.res_empty()) +if (!req->tariff.empty()) { switch (req->chgTariff) { @@ -561,60 +704,60 @@ if (!req->tariff.res_empty()) } -if (!req->note.res_empty()) +if (!req->note.empty()) { string note; - Encode12str(note, req->note); + Encode12str(note, req->note.data()); sprintf(str, "", note.c_str()); strcat(r, str); } -if (!req->name.res_empty()) +if (!req->name.empty()) { string name; - Encode12str(name, req->name); + Encode12str(name, req->name.data()); sprintf(str, "", name.c_str()); strcat(r, str); } -if (!req->address.res_empty()) +if (!req->address.empty()) { string address; - Encode12str(address, req->address); + Encode12str(address, req->address.data()); sprintf(str, "
", address.c_str()); strcat(r, str); } -if (!req->email.res_empty()) +if (!req->email.empty()) { string email; - Encode12str(email, req->email); + Encode12str(email, req->email.data()); sprintf(str, "", email.c_str()); strcat(r, str); } -if (!req->phone.res_empty()) +if (!req->phone.empty()) { string phone; - Encode12str(phone, req->phone); + Encode12str(phone, req->phone.data()); sprintf(str, "", phone.c_str()); strcat(r, str); } -if (!req->group.res_empty()) +if (!req->group.empty()) { string group; - Encode12str(group, req->group); + Encode12str(group, req->group.data()); sprintf(str, "", group.c_str()); strcat(r, str); } for (int i = 0; i < USERDATA_NUM; i++) { - if (!req->ud[i].res_empty()) + if (!req->userData[i].empty()) { string ud; - Encode12str(ud, req->ud[i]); + Encode12str(ud, req->userData[i].data()); sprintf(str, "", i, ud.c_str()); strcat(r, str); } @@ -625,30 +768,32 @@ strcat(r, "\n"); //----------------------------------------------------------------------------- int CheckParameters(REQUEST * req) { -int u = false; -int d = false; -int ud = false; -int a = !req->admLogin.res_empty() - && !req->admPasswd.res_empty() - && !req->server.res_empty() - && !req->port.res_empty() - && !req->login.res_empty(); - -int b = !req->cash.res_empty() - || !req->setCash.res_empty() - || !req->credit.res_empty() - || !req->prepaidTraff.res_empty() - || !req->tariff.res_empty() - || !req->usrMsg.res_empty() - || !req->usrPasswd.res_empty() - - || !req->note.res_empty() - || !req->name.res_empty() - || !req->address.res_empty() - || !req->email.res_empty() - || !req->phone.res_empty() - || !req->group.res_empty() - || !req->ips.res_empty() // IP-address of user +bool su = false; +bool sd = false; +bool mu = false; +bool md = false; +bool ud = false; +bool a = !req->admLogin.empty() + && !req->admPasswd.empty() + && !req->server.empty() + && !req->port.empty() + && !req->login.empty(); + +bool b = !req->cash.empty() + || !req->setCash.empty() + || !req->credit.empty() + || !req->prepaidTraff.empty() + || !req->tariff.empty() + || !req->usrMsg.empty() + || !req->usrPasswd.empty() + + || !req->note.empty() + || !req->name.empty() + || !req->address.empty() + || !req->email.empty() + || !req->phone.empty() + || !req->group.empty() + || !req->ips.empty() // IP-address of user || !req->createUser || !req->deleteUser; @@ -656,25 +801,43 @@ int b = !req->cash.res_empty() for (int i = 0; i < DIR_NUM; i++) { - if (req->u[i].res_empty()) + if (req->sessionUpload[i].empty()) { - u = true; + su = true; break; } } for (int i = 0; i < DIR_NUM; i++) { - if (req->d[i].res_empty()) + if (req->sessionDownload[i].empty()) { - d = true; + sd = true; break; } } for (int i = 0; i < DIR_NUM; i++) { - if (req->ud[i].res_empty()) + if (req->monthUpload[i].empty()) + { + mu = true; + break; + } + } + +for (int i = 0; i < DIR_NUM; i++) + { + if (req->monthDownload[i].empty()) + { + md = true; + break; + } + } + +for (int i = 0; i < DIR_NUM; i++) + { + if (req->userData[i].empty()) { ud = true; break; @@ -683,7 +846,7 @@ for (int i = 0; i < DIR_NUM; i++) //printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud); -return a && (b || u || d || ud); +return a && (b || su || sd || mu || md || ud); } //----------------------------------------------------------------------------- int CheckParametersGet(REQUEST * req) @@ -696,7 +859,7 @@ int CheckParametersSet(REQUEST * req) return CheckParameters(req); } //----------------------------------------------------------------------------- -int mainGet(int argc, char **argv) +bool mainGet(int argc, char **argv) { int c; REQUEST req; @@ -791,10 +954,10 @@ while (1) case 'G': //Group req.group = " "; break; - - case 'I': //IP-address of user - req.ips = " "; - break; + + case 'I': //IP-address of user + req.ips = " "; + break; case 'S': //Detail stat status req.disableDetailStat = " "; @@ -805,50 +968,33 @@ while (1) break; case 500: //U + SetArrayItem(req.sessionUpload, optarg, 1); + //req.sessionUpload[optarg] = 1; + break; case 501: + SetArrayItem(req.sessionDownload, optarg, 1); + //req.sessionDownload[optarg] = 1; + break; case 502: + SetArrayItem(req.monthUpload, optarg, 1); + //req.monthUpload[optarg] = 1; + break; case 503: - case 504: - case 505: - case 506: - case 507: - case 508: - case 509: - //printf("U%d\n", c - 500); - req.u[c - 500] = 1; - break; - - case 600: //D - case 601: - case 602: - case 603: - case 604: - case 605: - case 606: - case 607: - case 608: - case 609: - //printf("D%d\n", c - 600); - req.d[c - 600] = 1; + SetArrayItem(req.monthDownload, optarg, 1); + //req.monthDownload[optarg] = 1; break; case 700: //UserData - case 701: - case 702: - case 703: - case 704: - case 705: - case 706: - case 707: - case 708: - case 709: - //printf("UD%d\n", c - 700); - req.ud[c - 700] = " "; + SetArrayItem(req.userData, optarg, std::string(" ")); + //req.userData[optarg] = " "; + break; + + case 800: + req.authBy = true; break; case '?': case ':': - //printf ("Unknown option \n"); missedOptionArg = true; break; @@ -873,10 +1019,13 @@ if (missedOptionArg || !CheckParametersGet(&req)) exit(PARAMETER_PARSING_ERR_CODE); } -return ProcessGetUser(req.server, req.port, req.admLogin, req.admPasswd, req.login, &req); +if (req.authBy) + return ProcessAuthBy(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data()); +else + return ProcessGetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req); } //----------------------------------------------------------------------------- -int mainSet(int argc, char **argv) +bool mainSet(int argc, char **argv) { string str; @@ -890,6 +1039,8 @@ const char * short_options_set = "s:p:a:w:u:c:r:t:m:o:d:i:e:v:nlN:A:D:L:P:G:I:S: int missedOptionArg = false; +USER_CONF_RES conf; +USER_STAT_RES stat; while (1) { int option_index = -1; @@ -919,7 +1070,7 @@ while (1) break; case 'o': //change user password - req.usrPasswd = ParsePassword(optarg); + conf.password = ParsePassword(optarg); break; case 'u': //user @@ -927,31 +1078,31 @@ while (1) break; case 'c': //add cash - req.cash = ParseCash(optarg, &req.message); + stat.cashAdd = ParseCash(optarg); break; case 'v': //set cash - req.setCash = ParseCash(optarg, &req.message); + stat.cashSet = ParseCash(optarg); break; case 'r': //credit - req.credit = ParseCredit(optarg); + conf.credit = ParseCredit(optarg); break; case 'E': //credit expire - req.creditExpire = ParseCreditExpire(optarg); + conf.creditExpire = ParseCreditExpire(optarg); break; case 'd': //down - req.down = ParseDownPassive(optarg); + conf.disabled = ParseDownPassive(optarg); break; case 'i': //passive - req.passive = ParseDownPassive(optarg); + conf.passive = ParseDownPassive(optarg); break; case 't': //tariff - req.tariff = ParseTariff(optarg, req.chgTariff); + ParseTariff(optarg, conf.tariffName, conf.nextTariff); break; case 'm': //message @@ -961,7 +1112,7 @@ while (1) break; case 'e': //Prepaid Traffic - req.prepaidTraff = ParsePrepaidTraffic(optarg); + stat.freeMb = ParsePrepaidTraffic(optarg); break; case 'n': //Create User @@ -974,98 +1125,70 @@ while (1) case 'N': //Note ParseAnyString(optarg, &str, "koi8-ru"); - req.note = str; + conf.note = str; break; case 'A': //nAme ParseAnyString(optarg, &str, "koi8-ru"); - req.name = str; + conf.realName = str; break; case 'D': //aDdress ParseAnyString(optarg, &str, "koi8-ru"); - req.address = str; + conf.address = str; break; case 'L': //emaiL ParseAnyString(optarg, &str, "koi8-ru"); - req.email = str; - //printf("EMAIL=%s\n", optarg); + conf.email = str; break; case 'P': //phone ParseAnyString(optarg, &str); - req.phone = str; + conf.phone = str; break; case 'G': //Group ParseAnyString(optarg, &str, "koi8-ru"); - req.group = str; + conf.group = str; break; case 'I': //IP-address of user ParseAnyString(optarg, &str); - req.ips = str; + conf.ips = StrToIPS(str); break; case 'S': - req.disableDetailStat = ParseDownPassive(optarg); + conf.disabledDetailStat = ParseDownPassive(optarg); break; case 'O': - req.alwaysOnline = ParseDownPassive(optarg); + conf.alwaysOnline = ParseDownPassive(optarg); break; case 500: //U + SetArrayItem(stat.sessionUp, optarg, ParseTraff(argv[optind++])); + break; case 501: + SetArrayItem(stat.sessionDown, optarg, ParseTraff(argv[optind++])); + break; case 502: + SetArrayItem(stat.monthUp, optarg, ParseTraff(argv[optind++])); + break; case 503: - case 504: - case 505: - case 506: - case 507: - case 508: - case 509: - //printf("U%d\n", c - 500); - req.u[c - 500] = ParseTraff(optarg); - break; - - case 600: //D - case 601: - case 602: - case 603: - case 604: - case 605: - case 606: - case 607: - case 608: - case 609: - //printf("D%d\n", c - 600); - req.d[c - 600] = ParseTraff(optarg); + SetArrayItem(stat.monthDown, optarg, ParseTraff(argv[optind++])); break; case 700: //UserData - case 701: - case 702: - case 703: - case 704: - case 705: - case 706: - case 707: - case 708: - case 709: - ParseAnyString(optarg, &str); - //printf("UD%d\n", c - 700); - req.ud[c - 700] = str; + ParseAnyString(argv[optind++], &str); + SetArrayItem(conf.userdata, optarg, str); break; case '?': - //printf("Missing option argument\n"); missedOptionArg = true; break; case ':': - //printf("Missing option argument\n"); missedOptionArg = true; break; @@ -1094,12 +1217,85 @@ const int rLen = 20000; char rstr[rLen]; memset(rstr, 0, rLen); -CreateRequestSet(&req, rstr); -return ProcessSetUser(req.server, req.port, req.admLogin, req.admPasswd, rstr, NULL, isMessage); +if (isMessage) + return ProcessSendMessage(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), req.usrMsg.data()); + +return ProcessSetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), conf, stat); } //----------------------------------------------------------------------------- int main(int argc, char **argv) { +SGCONF::CONFIG config; + +SGCONF::OPTION_BLOCKS blocks; +blocks.Add("General options") + .Add("c", "config", SGCONF::MakeParamAction(config.configFile, std::string("~/.config/stg/sgconf.conf"), ""), "override default config file") + .Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit") + .Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit") + .Add("v", "version", SGCONF::MakeFunc0Action(Version), "\t\tshow version information and exit"); +SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options") + .Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "
"), "\t\thost to connect") + .Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), ""), "\t\tport to connect") + .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), ""), "\tadministrative login") + .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, ""), "\tpassword for the administrative login") + .Add("a", "address", SGCONF::MakeParamAction(config, ""), "connection params as a single string in format: :@:"); + + +SGCONF::PARSER_STATE state(false, argc, argv); + +try +{ +state = blocks.Parse(--argc, ++argv); // Skipping self name +} +catch (const SGCONF::OPTION::ERROR& ex) +{ +std::cerr << ex.what() << "\n"; +return -1; +} + +if (state.stop) + return 0; + +if (state.argc > 0) + { + std::cerr << "Unknown option: '" << *state.argv << "'\n"; + return -1; + } + +try +{ +SGCONF::CONFIG configOverride(config); + +if (config.configFile.empty()) + { + const char * mainConfigFile = "/etc/sgconf/sgconf.conf"; + if (access(mainConfigFile, R_OK) == 0) + block.ParseFile(mainConfigFile); + ReadUserConfigFile(block); + } +else + { + block.ParseFile(config.configFile.data()); + } + +config = configOverride; +} +catch (const std::exception& ex) +{ +std::cerr << ex.what() << "\n"; +return -1; +} + +std::cerr << "Config: " << config.Serialize() << std::endl; + +return 0; + +if (argc < 2) + { + Usage(); + return 1; + } + if (argc <= 2) { UsageConf(); @@ -1114,7 +1310,9 @@ if (strcmp(argv[1], "get") == 0) else if (strcmp(argv[1], "set") == 0) { //printf("set\n"); - return mainSet(argc - 1, argv + 1); + if (mainSet(argc - 1, argv + 1) ) + return 0; + return -1; } else { @@ -1125,3 +1323,213 @@ return UNKNOWN_ERR_CODE; } //----------------------------------------------------------------------------- +namespace +{ + +void Usage() +{ +UsageImpl(false); +} + +void UsageAll() +{ +UsageImpl(true); +} + +void UsageImpl(bool full) +{ +std::cout << "sgconf is the Stargazer management utility.\n\n" + << "Usage:\n" + << "\tsgconf [options]\n\n" + << "General options:\n" + << "\t-c, --config \t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n" + << "\t-h, --help\t\t\t\tshow this help and exit\n" + << "\t--help-all\t\t\t\tshow full help and exit\n" + << "\t-v, --version\t\t\t\tshow version information and exit\n\n"; +UsageConnection(); +UsageAdmins(full); +UsageTariffs(full); +UsageUsers(full); +UsageServices(full); +UsageCorporations(full); +} +//----------------------------------------------------------------------------- +void UsageConnection() +{ +std::cout << "Connection options:\n" + << "\t-s, --server
\t\t\thost to connect (ip or domain name, default: \"localhost\")\n" + << "\t-p, --port \t\t\tport to connect (default: \"5555\")\n" + << "\t-u, --username \t\tadministrative login (default: \"admin\")\n" + << "\t-w, --userpass \t\tpassword for administrative login\n" + << "\t-a, --address \tconnection params as a single string in format: :@:\n\n"; +} +//----------------------------------------------------------------------------- +void UsageAdmins(bool full) +{ +std::cout << "Admins management options:\n" + << "\t--get-admins\t\t\t\tget a list of admins (subsequent options will define what to show)\n"; +if (full) + std::cout << "\t\t--login\t\t\t\tshow admin's login\n" + << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n"; +std::cout << "\t--get-admin\t\t\t\tget the information about admin\n"; +if (full) + std::cout << "\t\t--login \t\t\tlogin of the admin to show\n" + << "\t\t--priv\t\t\t\tshow admin's priviledges\n\n"; +std::cout << "\t--add-admin\t\t\t\tadd a new admin\n"; +if (full) + std::cout << "\t\t--login \t\t\tlogin of the admin to add\n" + << "\t\t--password \t\tpassword of the admin to add\n" + << "\t\t--priv \t\tpriviledges of the admin to add\n\n"; +std::cout << "\t--del-admin\t\t\t\tdelete an existing admin\n"; +if (full) + std::cout << "\t\t--login \t\t\tlogin of the admin to delete\n\n"; +std::cout << "\t--chg-admin\t\t\t\tchange an existing admin\n"; +if (full) + std::cout << "\t\t--login \t\t\tlogin of the admin to change\n" + << "\t\t--priv \t\tnew priviledges\n\n"; +} +//----------------------------------------------------------------------------- +void UsageTariffs(bool full) +{ +std::cout << "Tariffs management options:\n" + << "\t--get-tariffs\t\t\t\tget a list of tariffs (subsequent options will define what to show)\n"; +if (full) + std::cout << "\t\t--name\t\t\t\tshow tariff's name\n" + << "\t\t--fee\t\t\t\tshow tariff's fee\n" + << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n" + << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n" + << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n" + << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n"; +std::cout << "\t--get-tariff\t\t\t\tget the information about tariff\n"; +if (full) + std::cout << "\t\t--name \t\t\tname of the tariff to show\n" + << "\t\t--fee\t\t\t\tshow tariff's fee\n" + << "\t\t--free\t\t\t\tshow tariff's prepaid traffic in terms of cost\n" + << "\t\t--passive-cost\t\t\tshow tariff's cost of \"freeze\"\n" + << "\t\t--traff-type\t\t\tshow what type of traffix will be accounted by the tariff\n" + << "\t\t--dirs\t\t\t\tshow tarification rules for directions\n\n"; +std::cout << "\t--add-tariff\t\t\t\tadd a new tariff\n"; +if (full) + std::cout << "\t\t--name \t\t\tname of the tariff to add\n" + << "\t\t--fee \t\t\tstariff's fee\n" + << "\t\t--free \t\t\ttariff's prepaid traffic in terms of cost\n" + << "\t\t--passive-cost \t\ttariff's cost of \"freeze\"\n" + << "\t\t--traff-type \t\twhat type of traffi will be accounted by the tariff\n" + << "\t\t--times \t\t\tslash-separated list of \"day\" time-spans (in form \"hh:mm-hh:mm\") for each direction\n" + << "\t\t--prices-day-a \t\tslash-separated list of prices for \"day\" traffic before threshold for each direction\n" + << "\t\t--prices-night-a \tslash-separated list of prices for \"night\" traffic before threshold for each direction\n" + << "\t\t--prices-day-b \t\tslash-separated list of prices for \"day\" traffic after threshold for each direction\n" + << "\t\t--prices-night-b \tslash-separated list of prices for \"night\" traffic after threshold for each direction\n" + << "\t\t--single-prices \tslash-separated list of \"single price\" flags for each direction\n" + << "\t\t--no-discounts \t\tslash-separated list of \"no discount\" flags for each direction\n" + << "\t\t--thresholds \tslash-separated list of thresholds (in Mb) for each direction\n\n"; +std::cout << "\t--del-tariff\t\t\t\tdelete an existing tariff\n"; +if (full) + std::cout << "\t\t--name \t\t\tname of the tariff to delete\n\n"; +std::cout << "\t--chg-tariff\t\t\t\tchange an existing tariff\n"; +if (full) + std::cout << "\t\t--name \t\t\tname of the tariff to change\n" + << "\t\t--fee \t\t\tstariff's fee\n" + << "\t\t--free \t\t\ttariff's prepaid traffic in terms of cost\n" + << "\t\t--passive-cost \t\ttariff's cost of \"freeze\"\n" + << "\t\t--traff-type \t\twhat type of traffix will be accounted by the tariff\n" + << "\t\t--dir \t\t\tnumber of direction data to change\n" + << "\t\t\t--time