2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
24 #include "optional_utils.h"
26 #include "stg/user_conf.h"
27 #include "stg/user_stat.h"
28 #include "stg/common.h"
36 ChgUser::Parser::Parser(Simple::Callback f, void* d, const std::string& e)
43 //-----------------------------------------------------------------------------
44 int ChgUser::Parser::ParseStart(const char* el, const char** attr)
49 if (strcasecmp(el, "SetUser") == 0)
50 ParseAnswer(el, attr);
51 else if (strcasecmp(el, "DelUser") == 0)
52 ParseAnswer(el, attr);
53 else if (strcasecmp(el, "AddUser") == 0)
54 ParseAnswer(el, attr);
58 //-----------------------------------------------------------------------------
59 void ChgUser::Parser::ParseEnd(const char* /*unused*/)
63 //-----------------------------------------------------------------------------
64 void ChgUser::Parser::ParseAnswer(const char* /*el*/, const char** attr)
68 if (attr && attr[0] && attr[1])
69 callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data);
71 callback(false, "Invalid response.", data);
74 std::string ChgUser::serialize(const UserConfOpt& conf, const UserStatOpt& stat, const std::string& encoding)
76 std::ostringstream stream;
80 appendResetableTag(stream, "credit", conf.credit);
81 appendResetableTag(stream, "creditExpire", conf.creditExpire);
82 appendResetableTag(stream, "password", conf.password);
83 appendResetableTag(stream, "down", conf.disabled); // TODO: down -> disabled
84 appendResetableTag(stream, "passive", conf.passive);
85 appendResetableTag(stream, "disableDetailStat", conf.disabledDetailStat); // TODO: disable -> disabled
86 appendResetableTag(stream, "aonline", conf.alwaysOnline); // TODO: aonline -> alwaysOnline
87 appendResetableTag(stream, "ip", conf.ips); // TODO: ip -> ips
90 stream << "<tariff delayed=\"" << conf.nextTariff.value() << "\"/>";
91 else if (conf.tariffName)
92 stream << "<tariff now=\"" << conf.tariffName.value() << "\"/>";
94 appendResetableTag(stream, "note", maybeEncode(maybeIconv(conf.note, encoding, "koi8-ru")));
95 appendResetableTag(stream, "name", maybeEncode(maybeIconv(conf.realName, encoding, "koi8-ru"))); // TODO: name -> realName
96 appendResetableTag(stream, "address", maybeEncode(maybeIconv(conf.address, encoding, "koi8-ru")));
97 appendResetableTag(stream, "email", maybeEncode(maybeIconv(conf.email, encoding, "koi8-ru")));
98 appendResetableTag(stream, "phone", maybeEncode(maybeIconv(conf.phone, encoding, "cp1251")));
99 appendResetableTag(stream, "group", maybeEncode(maybeIconv(conf.group, encoding, "koi8-ru")));
100 appendResetableTag(stream, "corp", conf.corp);
102 for (size_t i = 0; i < conf.userdata.size(); ++i)
103 appendResetableTag(stream, "userdata", i, maybeEncode(maybeIconv(conf.userdata[i], encoding, "koi8-ru")));
107 stream << "<services>";
108 for (const auto& service : conf.services.value())
109 stream << "<service name=\"" << service << "\"/>";
110 stream << "</services>";
116 stream << "<cash add=\"" << stat.cashAdd.value().first << "\" msg=\"" << IconvString(Encode12str(stat.cashAdd.value().second), encoding, "koi8-ru") << "\"/>";
117 else if (stat.cashSet)
118 stream << "<cash set=\"" << stat.cashSet.value().first << "\" msg=\"" << IconvString(Encode12str(stat.cashSet.value().second), encoding, "koi8-ru") << "\"/>";
120 appendResetableTag(stream, "freeMb", stat.freeMb);
122 std::ostringstream traff;
123 for (size_t i = 0; i < stat.sessionUp.size(); ++i)
124 if (stat.sessionUp[i])
125 traff << " SU" << i << "=\"" << stat.sessionUp[i].value() << "\"";
126 for (size_t i = 0; i < stat.sessionDown.size(); ++i)
127 if (stat.sessionDown[i])
128 traff << " SD" << i << "=\"" << stat.sessionDown[i].value() << "\"";
129 for (size_t i = 0; i < stat.monthUp.size(); ++i)
131 traff << " MU" << i << "=\"" << stat.monthUp[i].value() << "\"";
132 for (size_t i = 0; i < stat.monthDown.size(); ++i)
133 if (stat.monthDown[i])
134 traff << " MD" << i << "=\"" << stat.monthDown[i].value() << "\"";
136 std::string traffData = traff.str();
137 if (!traffData.empty())
138 stream << "<traff" << traffData << "/>";