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