]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/chg_user.cpp
Merge branch 'fix-gts-auth-errors'
[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
29 #include <sstream>
30
31 #include <strings.h>
32
33 using namespace STG;
34
35 CHG_USER::PARSER::PARSER(SIMPLE::CALLBACK f, void * d)
36     : callback(f),
37       data(d),
38       depth(0)
39 {
40 }
41 //-----------------------------------------------------------------------------
42 int CHG_USER::PARSER::ParseStart(const char *el, const char **attr)
43 {
44 depth++;
45 if (depth == 1)
46     {
47     if (strcasecmp(el, "SetUser") == 0)
48         ParseAnswer(el, attr);
49     else if (strcasecmp(el, "DelUser") == 0)
50         ParseAnswer(el, attr);
51     else if (strcasecmp(el, "AddUser") == 0)
52         ParseAnswer(el, attr);
53     }
54 return 0;
55 }
56 //-----------------------------------------------------------------------------
57 void CHG_USER::PARSER::ParseEnd(const char *)
58 {
59 depth--;
60 }
61 //-----------------------------------------------------------------------------
62 void CHG_USER::PARSER::ParseAnswer(const char * /*el*/, const char ** attr)
63 {
64 if (!callback)
65     return;
66 if (attr && attr[0] && attr[1])
67     callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data);
68 else
69     callback(false, "Invalid response.", data);
70 }
71
72 std::string CHG_USER::Serialize(const USER_CONF_RES & conf, const USER_STAT_RES & stat)
73 {
74 std::ostringstream stream;
75
76 // Conf
77
78 appendResetable(stream, "credit", conf.credit);
79 appendResetable(stream, "creditExpire", conf.creditExpire);
80 appendResetable(stream, "password", conf.password);
81 appendResetable(stream, "down", conf.disabled); // TODO: down -> disabled
82 appendResetable(stream, "passive", conf.passive);
83 appendResetable(stream, "disableDetailStat", conf.disabledDetailStat); // TODO: disable -> disabled
84 appendResetable(stream, "aonline", conf.alwaysOnline); // TODO: aonline -> alwaysOnline
85 appendResetable(stream, "ip", conf.ips); // TODO: ip -> ips
86
87 if (!conf.nextTariff.empty())
88     stream << "<tariff delayed=\"" << conf.nextTariff.data() << "\"/>";
89 else if (!conf.tariffName.empty())
90     stream << "<tariff now=\"" << conf.nextTariff.data() << "\"/>";
91
92 appendResetable(stream, "note", conf.note);
93 appendResetable(stream, "name", conf.realName); // TODO: name -> realName
94 appendResetable(stream, "address", conf.address);
95 appendResetable(stream, "email", conf.email);
96 appendResetable(stream, "phone", conf.phone);
97 appendResetable(stream, "group", conf.group);
98
99 for (size_t i = 0; i < conf.userdata.size(); ++i)
100     appendResetable(stream, "userdata", i, conf.userdata[i]);
101
102 // Stat
103
104 if (!stat.cashAdd.empty())
105     stream << "<cash add=\"" << stat.cashAdd.data().first << "\" msg=\"" << stat.cashAdd.data().second << "\"/>";
106 else if (!stat.cashSet.empty())
107     stream << "<cash set=\"" << stat.cashAdd.data().first << "\" msg=\"" << stat.cashAdd.data().second << "\"/>";
108
109 appendResetable(stream, "freeMb", stat.freeMb);
110
111 std::ostringstream traff;
112 for (size_t i = 0; i < stat.sessionUp.size(); ++i)
113     if (!stat.sessionUp[i].empty())
114         traff << " SU" << i << "=\"" << stat.sessionUp[i].data() << "\"";
115 for (size_t i = 0; i < stat.sessionDown.size(); ++i)
116     if (!stat.sessionDown[i].empty())
117         traff << " SD" << i << "=\"" << stat.sessionDown[i].data() << "\"";
118 for (size_t i = 0; i < stat.monthUp.size(); ++i)
119     if (!stat.monthUp[i].empty())
120         traff << " MU" << i << "=\"" << stat.monthUp[i].data() << "\"";
121 for (size_t i = 0; i < stat.monthDown.size(); ++i)
122     if (!stat.monthDown[i].empty())
123         traff << " MD" << i << "=\"" << stat.monthDown[i].data() << "\"";
124
125 std::string traffData = traff.str();
126 if (!traffData.empty())
127     stream << "<traff" << traffData << "/>";
128
129 return stream.str();
130 }