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 "stg/user_conf.h"
25 #include "stg/user_stat.h"
37 void appendResetable(std::ostream & stream, const std::string & name, const T & value)
40 stream << "<" << name << " value=\"" << value.data() << "\"/>";
44 void appendResetable(std::ostream & stream, const std::string & name, size_t suffix, const T & value)
47 stream << "<" << name << suffix << " value=\"" << value.data() << "\"/>";
50 } // namespace anonymous
52 CHG_USER::PARSER::PARSER(SIMPLE::CALLBACK f, void * d)
58 //-----------------------------------------------------------------------------
59 int CHG_USER::PARSER::ParseStart(const char *el, const char **attr)
64 if (strcasecmp(el, "SetUser") == 0)
65 ParseAnswer(el, attr);
66 else if (strcasecmp(el, "DelUser") == 0)
67 ParseAnswer(el, attr);
68 else if (strcasecmp(el, "AddUser") == 0)
69 ParseAnswer(el, attr);
73 //-----------------------------------------------------------------------------
74 void CHG_USER::PARSER::ParseEnd(const char *)
78 //-----------------------------------------------------------------------------
79 void CHG_USER::PARSER::ParseAnswer(const char * /*el*/, const char ** attr)
83 if (attr && attr[0] && attr[1])
84 callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data);
86 callback(false, "Invalid response.", data);
89 std::string CHG_USER::Serialize(const USER_CONF_RES & conf, const USER_STAT_RES & stat)
91 std::ostringstream stream;
95 appendResetable(stream, "credit", conf.credit);
96 appendResetable(stream, "creditExpire", conf.creditExpire);
97 appendResetable(stream, "password", conf.password);
98 appendResetable(stream, "down", conf.disabled); // TODO: down -> disabled
99 appendResetable(stream, "passive", conf.passive);
100 appendResetable(stream, "disableDetailStat", conf.disabledDetailStat); // TODO: disable -> disabled
101 appendResetable(stream, "aonline", conf.alwaysOnline); // TODO: aonline -> alwaysOnline
102 appendResetable(stream, "ip", conf.ips); // TODO: ip -> ips
104 if (!conf.nextTariff.empty())
105 stream << "<tariff delayed=\"" << conf.nextTariff.data() << "\"/>";
106 else if (!conf.tariffName.empty())
107 stream << "<tariff now=\"" << conf.nextTariff.data() << "\"/>";
109 appendResetable(stream, "note", conf.note);
110 appendResetable(stream, "name", conf.realName); // TODO: name -> realName
111 appendResetable(stream, "address", conf.address);
112 appendResetable(stream, "email", conf.email);
113 appendResetable(stream, "phone", conf.phone);
114 appendResetable(stream, "group", conf.group);
116 for (size_t i = 0; i < conf.userdata.size(); ++i)
117 appendResetable(stream, "userdata", i, conf.userdata[i]);
121 if (!stat.cashAdd.empty())
122 stream << "<cash add=\"" << stat.cashAdd.data().first << "\" msg=\"" << stat.cashAdd.data().second << "\"/>";
123 else if (!stat.cashSet.empty())
124 stream << "<cash set=\"" << stat.cashAdd.data().first << "\" msg=\"" << stat.cashAdd.data().second << "\"/>";
126 appendResetable(stream, "freeMb", stat.freeMb);
128 std::ostringstream traff;
129 for (size_t i = 0; i < stat.sessionUp.size(); ++i)
130 if (!stat.sessionUp[i].empty())
131 traff << " SU" << i << "=\"" << stat.sessionUp[i].data() << "\"";
132 for (size_t i = 0; i < stat.sessionDown.size(); ++i)
133 if (!stat.sessionDown[i].empty())
134 traff << " SD" << i << "=\"" << stat.sessionDown[i].data() << "\"";
135 for (size_t i = 0; i < stat.monthUp.size(); ++i)
136 if (!stat.monthUp[i].empty())
137 traff << " MU" << i << "=\"" << stat.monthUp[i].data() << "\"";
138 for (size_t i = 0; i < stat.monthDown.size(); ++i)
139 if (!stat.monthDown[i].empty())
140 traff << " MD" << i << "=\"" << stat.monthDown[i].data() << "\"";
142 std::string traffData = traff.str();
143 if (!traffData.empty())
144 stream << "<traff" << traffData << "/>";