]> git.stg.codes - stg.git/blob - libs/srvconf/parsers/chg_user.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / libs / srvconf / 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 CHG_USER::PARSER::PARSER(SIMPLE::CALLBACK f, void * d, const std::string & e)
38     : callback(f),
39       data(d),
40       encoding(e),
41       depth(0)
42 {
43 }
44 //-----------------------------------------------------------------------------
45 int CHG_USER::PARSER::ParseStart(const char *el, const char **attr)
46 {
47 depth++;
48 if (depth == 1)
49     {
50     if (strcasecmp(el, "SetUser") == 0)
51         ParseAnswer(el, attr);
52     else if (strcasecmp(el, "DelUser") == 0)
53         ParseAnswer(el, attr);
54     else if (strcasecmp(el, "AddUser") == 0)
55         ParseAnswer(el, attr);
56     }
57 return 0;
58 }
59 //-----------------------------------------------------------------------------
60 void CHG_USER::PARSER::ParseEnd(const char *)
61 {
62 depth--;
63 }
64 //-----------------------------------------------------------------------------
65 void CHG_USER::PARSER::ParseAnswer(const char * /*el*/, const char ** attr)
66 {
67 if (!callback)
68     return;
69 if (attr && attr[0] && attr[1])
70     callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data);
71 else
72     callback(false, "Invalid response.", data);
73 }
74
75 std::string CHG_USER::Serialize(const USER_CONF_RES & conf, const USER_STAT_RES & stat, const std::string & encoding)
76 {
77 std::ostringstream stream;
78
79 // Conf
80
81 appendResetableTag(stream, "credit", conf.credit);
82 appendResetableTag(stream, "creditExpire", conf.creditExpire);
83 appendResetableTag(stream, "password", conf.password);
84 appendResetableTag(stream, "down", conf.disabled); // TODO: down -> disabled
85 appendResetableTag(stream, "passive", conf.passive);
86 appendResetableTag(stream, "disableDetailStat", conf.disabledDetailStat); // TODO: disable -> disabled
87 appendResetableTag(stream, "aonline", conf.alwaysOnline); // TODO: aonline -> alwaysOnline
88 appendResetableTag(stream, "ip", conf.ips); // TODO: ip -> ips
89
90 if (!conf.nextTariff.empty())
91     stream << "<tariff delayed=\"" << conf.nextTariff.data() << "\"/>";
92 else if (!conf.tariffName.empty())
93     stream << "<tariff now=\"" << conf.tariffName.data() << "\"/>";
94
95 appendResetableTag(stream, "note", MaybeEncode(MaybeIconv(conf.note, encoding, "koi8-ru")));
96 appendResetableTag(stream, "name", MaybeEncode(MaybeIconv(conf.realName, encoding, "koi8-ru"))); // TODO: name -> realName
97 appendResetableTag(stream, "address", MaybeEncode(MaybeIconv(conf.address, encoding, "koi8-ru")));
98 appendResetableTag(stream, "email", MaybeEncode(MaybeIconv(conf.email, encoding, "koi8-ru")));
99 appendResetableTag(stream, "phone", MaybeEncode(MaybeIconv(conf.phone, encoding, "cp1251")));
100 appendResetableTag(stream, "group", MaybeEncode(MaybeIconv(conf.group, encoding, "koi8-ru")));
101 appendResetableTag(stream, "corp", conf.corp);
102
103 for (size_t i = 0; i < conf.userdata.size(); ++i)
104     appendResetableTag(stream, "userdata", i, MaybeEncode(MaybeIconv(conf.userdata[i], encoding, "koi8-ru")));
105
106 if (!conf.services.empty())
107     {
108     stream << "<services>";
109     for (size_t i = 0; i < conf.services.data().size(); ++i)
110         stream << "<service name=\"" << conf.services.data()[i] << "\"/>";
111     stream << "</services>";
112     }
113
114 // Stat
115
116 if (!stat.cashAdd.empty())
117     stream << "<cash add=\"" << stat.cashAdd.data().first << "\" msg=\"" << IconvString(Encode12str(stat.cashAdd.data().second), encoding, "koi8-ru") << "\"/>";
118 else if (!stat.cashSet.empty())
119     stream << "<cash set=\"" << stat.cashSet.data().first << "\" msg=\"" << IconvString(Encode12str(stat.cashSet.data().second), encoding, "koi8-ru") << "\"/>";
120
121 appendResetableTag(stream, "freeMb", stat.freeMb);
122
123 std::ostringstream traff;
124 for (size_t i = 0; i < stat.sessionUp.size(); ++i)
125     if (!stat.sessionUp[i].empty())
126         traff << " SU" << i << "=\"" << stat.sessionUp[i].data() << "\"";
127 for (size_t i = 0; i < stat.sessionDown.size(); ++i)
128     if (!stat.sessionDown[i].empty())
129         traff << " SD" << i << "=\"" << stat.sessionDown[i].data() << "\"";
130 for (size_t i = 0; i < stat.monthUp.size(); ++i)
131     if (!stat.monthUp[i].empty())
132         traff << " MU" << i << "=\"" << stat.monthUp[i].data() << "\"";
133 for (size_t i = 0; i < stat.monthDown.size(); ++i)
134     if (!stat.monthDown[i].empty())
135         traff << " MD" << i << "=\"" << stat.monthDown[i].data() << "\"";
136
137 std::string traffData = traff.str();
138 if (!traffData.empty())
139     stream << "<traff" << traffData << "/>";
140
141 std::cerr << stream.str() << "\n";
142 return stream.str();
143 }