]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/chg_user.cpp
Added encoding of some strings.
[stg.git] / stglibs / srvconf.lib / parsers / chg_user.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "chg_user.h"
23
24 #include "resetable_utils.h"
25
26 #include "stg/user_conf.h"
27 #include "stg/user_stat.h"
28 #include "stg/common.h"
29
30 #include <sstream>
31 #include <iostream>
32
33 #include <strings.h>
34
35 using namespace STG;
36
37 namespace
38 {
39
40 RESETABLE<std::string> MaybeEncode(const RESETABLE<std::string> & value)
41 {
42 RESETABLE<std::string> res;
43 if (!value.empty())
44     res = Encode12str(value.data());
45 return res;
46 }
47
48 }
49
50 CHG_USER::PARSER::PARSER(SIMPLE::CALLBACK f, void * d)
51     : callback(f),
52       data(d),
53       depth(0)
54 {
55 }
56 //-----------------------------------------------------------------------------
57 int CHG_USER::PARSER::ParseStart(const char *el, const char **attr)
58 {
59 depth++;
60 if (depth == 1)
61     {
62     if (strcasecmp(el, "SetUser") == 0)
63         ParseAnswer(el, attr);
64     else if (strcasecmp(el, "DelUser") == 0)
65         ParseAnswer(el, attr);
66     else if (strcasecmp(el, "AddUser") == 0)
67         ParseAnswer(el, attr);
68     }
69 return 0;
70 }
71 //-----------------------------------------------------------------------------
72 void CHG_USER::PARSER::ParseEnd(const char *)
73 {
74 depth--;
75 }
76 //-----------------------------------------------------------------------------
77 void CHG_USER::PARSER::ParseAnswer(const char * /*el*/, const char ** attr)
78 {
79 if (!callback)
80     return;
81 if (attr && attr[0] && attr[1])
82     callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data);
83 else
84     callback(false, "Invalid response.", data);
85 }
86
87 std::string CHG_USER::Serialize(const USER_CONF_RES & conf, const USER_STAT_RES & stat)
88 {
89 std::ostringstream stream;
90
91 // Conf
92
93 appendResetable(stream, "credit", conf.credit);
94 appendResetable(stream, "creditExpire", conf.creditExpire);
95 appendResetable(stream, "password", conf.password);
96 appendResetable(stream, "down", conf.disabled); // TODO: down -> disabled
97 appendResetable(stream, "passive", conf.passive);
98 appendResetable(stream, "disableDetailStat", conf.disabledDetailStat); // TODO: disable -> disabled
99 appendResetable(stream, "aonline", conf.alwaysOnline); // TODO: aonline -> alwaysOnline
100 appendResetable(stream, "ip", conf.ips); // TODO: ip -> ips
101
102 if (!conf.nextTariff.empty())
103     stream << "<tariff delayed=\"" << conf.nextTariff.data() << "\"/>";
104 else if (!conf.tariffName.empty())
105     stream << "<tariff now=\"" << conf.nextTariff.data() << "\"/>";
106
107 appendResetable(stream, "note", MaybeEncode(conf.note));
108 appendResetable(stream, "name", MaybeEncode(conf.realName)); // TODO: name -> realName
109 appendResetable(stream, "address", MaybeEncode(conf.address));
110 appendResetable(stream, "email", MaybeEncode(conf.email));
111 appendResetable(stream, "phone", MaybeEncode(conf.phone));
112 appendResetable(stream, "group", MaybeEncode(conf.group));
113 appendResetable(stream, "corp", conf.group);
114
115 for (size_t i = 0; i < conf.userdata.size(); ++i)
116     appendResetable(stream, "userdata", i, MaybeEncode(conf.userdata[i]));
117
118 if (!conf.services.empty())
119     {
120     stream << "<services>";
121     for (size_t i = 0; i < conf.services.data().size(); ++i)
122         stream << "<service name=\"" << conf.services.data()[i] << "\"/>";
123     stream << "</services>";
124     }
125
126 // Stat
127
128 if (!stat.cashAdd.empty())
129     stream << "<cash add=\"" << stat.cashAdd.data().first << "\" msg=\"" << Encode12str(stat.cashAdd.data().second) << "\"/>";
130 else if (!stat.cashSet.empty())
131     stream << "<cash set=\"" << stat.cashAdd.data().first << "\" msg=\"" << Encode12str(stat.cashAdd.data().second) << "\"/>";
132
133 appendResetable(stream, "freeMb", stat.freeMb);
134
135 std::ostringstream traff;
136 for (size_t i = 0; i < stat.sessionUp.size(); ++i)
137     if (!stat.sessionUp[i].empty())
138         traff << " SU" << i << "=\"" << stat.sessionUp[i].data() << "\"";
139 for (size_t i = 0; i < stat.sessionDown.size(); ++i)
140     if (!stat.sessionDown[i].empty())
141         traff << " SD" << i << "=\"" << stat.sessionDown[i].data() << "\"";
142 for (size_t i = 0; i < stat.monthUp.size(); ++i)
143     if (!stat.monthUp[i].empty())
144         traff << " MU" << i << "=\"" << stat.monthUp[i].data() << "\"";
145 for (size_t i = 0; i < stat.monthDown.size(); ++i)
146     if (!stat.monthDown[i].empty())
147         traff << " MD" << i << "=\"" << stat.monthDown[i].data() << "\"";
148
149 std::string traffData = traff.str();
150 if (!traffData.empty())
151     stream << "<traff" << traffData << "/>";
152
153 std::cerr << stream.str() << "\n";
154 return stream.str();
155 }