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 "resetable_utils.h"
26 #include "stg/user_conf.h"
27 #include "stg/user_stat.h"
28 #include "stg/common.h"
40 RESETABLE<std::string> MaybeEncode(const RESETABLE<std::string> & value)
42 RESETABLE<std::string> res;
44 res = Encode12str(value.data());
48 RESETABLE<std::string> MaybeIconv(const RESETABLE<std::string> & value, const std::string & fromEncoding, const std::string & toEncoding)
50 RESETABLE<std::string> res;
52 res = IconvString(value.data(), fromEncoding, toEncoding);
58 CHG_USER::PARSER::PARSER(SIMPLE::CALLBACK f, void * d)
64 //-----------------------------------------------------------------------------
65 int CHG_USER::PARSER::ParseStart(const char *el, const char **attr)
70 if (strcasecmp(el, "SetUser") == 0)
71 ParseAnswer(el, attr);
72 else if (strcasecmp(el, "DelUser") == 0)
73 ParseAnswer(el, attr);
74 else if (strcasecmp(el, "AddUser") == 0)
75 ParseAnswer(el, attr);
79 //-----------------------------------------------------------------------------
80 void CHG_USER::PARSER::ParseEnd(const char *)
84 //-----------------------------------------------------------------------------
85 void CHG_USER::PARSER::ParseAnswer(const char * /*el*/, const char ** attr)
89 if (attr && attr[0] && attr[1])
90 callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data);
92 callback(false, "Invalid response.", data);
95 std::string CHG_USER::Serialize(const USER_CONF_RES & conf, const USER_STAT_RES & stat, const std::string & encoding)
97 std::ostringstream stream;
101 appendResetable(stream, "credit", conf.credit);
102 appendResetable(stream, "creditExpire", conf.creditExpire);
103 appendResetable(stream, "password", conf.password);
104 appendResetable(stream, "down", conf.disabled); // TODO: down -> disabled
105 appendResetable(stream, "passive", conf.passive);
106 appendResetable(stream, "disableDetailStat", conf.disabledDetailStat); // TODO: disable -> disabled
107 appendResetable(stream, "aonline", conf.alwaysOnline); // TODO: aonline -> alwaysOnline
108 appendResetable(stream, "ip", conf.ips); // TODO: ip -> ips
110 if (!conf.nextTariff.empty())
111 stream << "<tariff delayed=\"" << conf.nextTariff.data() << "\"/>";
112 else if (!conf.tariffName.empty())
113 stream << "<tariff now=\"" << conf.tariffName.data() << "\"/>";
115 appendResetable(stream, "note", MaybeIconv(MaybeEncode(conf.note), "koi8-ru", encoding));
116 appendResetable(stream, "name", MaybeIconv(MaybeEncode(conf.realName), "koi8-ru", encoding)); // TODO: name -> realName
117 appendResetable(stream, "address", MaybeIconv(MaybeEncode(conf.address), "koi8-ru", encoding));
118 appendResetable(stream, "email", MaybeIconv(MaybeEncode(conf.email), "koi8-ru", encoding));
119 appendResetable(stream, "phone", MaybeIconv(MaybeEncode(conf.phone), "koi8-ru", encoding));
120 appendResetable(stream, "group", MaybeIconv(MaybeEncode(conf.group), "koi8-ru", encoding));
121 appendResetable(stream, "corp", conf.corp);
123 for (size_t i = 0; i < conf.userdata.size(); ++i)
124 appendResetable(stream, "userdata", i, MaybeIconv(MaybeEncode(conf.userdata[i]), "koi8-ru", encoding));
126 if (!conf.services.empty())
128 stream << "<services>";
129 for (size_t i = 0; i < conf.services.data().size(); ++i)
130 stream << "<service name=\"" << conf.services.data()[i] << "\"/>";
131 stream << "</services>";
136 if (!stat.cashAdd.empty())
137 stream << "<cash add=\"" << stat.cashAdd.data().first << "\" msg=\"" << IconvString(Encode12str(stat.cashAdd.data().second), "koi8-ru", encoding) << "\"/>";
138 else if (!stat.cashSet.empty())
139 stream << "<cash set=\"" << stat.cashSet.data().first << "\" msg=\"" << IconvString(Encode12str(stat.cashSet.data().second), "koi8-ru", encoding) << "\"/>";
141 appendResetable(stream, "freeMb", stat.freeMb);
143 std::ostringstream traff;
144 for (size_t i = 0; i < stat.sessionUp.size(); ++i)
145 if (!stat.sessionUp[i].empty())
146 traff << " SU" << i << "=\"" << stat.sessionUp[i].data() << "\"";
147 for (size_t i = 0; i < stat.sessionDown.size(); ++i)
148 if (!stat.sessionDown[i].empty())
149 traff << " SD" << i << "=\"" << stat.sessionDown[i].data() << "\"";
150 for (size_t i = 0; i < stat.monthUp.size(); ++i)
151 if (!stat.monthUp[i].empty())
152 traff << " MU" << i << "=\"" << stat.monthUp[i].data() << "\"";
153 for (size_t i = 0; i < stat.monthDown.size(); ++i)
154 if (!stat.monthDown[i].empty())
155 traff << " MD" << i << "=\"" << stat.monthDown[i].data() << "\"";
157 std::string traffData = traff.str();
158 if (!traffData.empty())
159 stream << "<traff" << traffData << "/>";
161 std::cerr << stream.str() << "\n";