]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parser_get_user.cpp
Better handling of errors from server. Refactoring.
[stg.git] / stglibs / srvconf.lib / parser_get_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 "stg/parser_get_user.h"
23
24 #include "stg/common.h"
25
26 #include <map>
27 #include <utility>
28 #include <cstddef>
29
30 #include <strings.h>
31
32 template <>
33 bool GetValue<PARSER_GET_USER::STAT>(const char ** attr, PARSER_GET_USER::STAT & value)
34 {
35 if (!attr)
36     return false;
37 std::map<std::string, long long *> props;
38 for (size_t i = 0; i < DIR_NUM; ++i)
39     {
40     props.insert(std::pair<std::string, long long *>("su" + x2str(i), &value.su[i]));
41     props.insert(std::pair<std::string, long long *>("sd" + x2str(i), &value.sd[i]));
42     props.insert(std::pair<std::string, long long *>("mu" + x2str(i), &value.mu[i]));
43     props.insert(std::pair<std::string, long long *>("md" + x2str(i), &value.md[i]));
44     }
45 size_t pos = 0;
46 while (attr[pos])
47     {
48         std::string name(ToLower(attr[pos++]));
49         std::map<std::string, long long *>::iterator it(props.find(name));
50         if (it != props.end())
51             if (str2x(attr[pos++], *it->second) < 0)
52                 return false;
53     }
54 return true;
55 }
56
57 PARSER_GET_USER::PARSER_GET_USER()
58     : callback(NULL),
59       data(NULL),
60       depth(0)
61 {
62     AddParser(propertyParsers, "login", info.login);
63     AddParser(propertyParsers, "password", info.password);
64     AddParser(propertyParsers, "cash", info.cash);
65     AddParser(propertyParsers, "credit", info.credit);
66     AddParser(propertyParsers, "creditExpire", info.creditExpire);
67     AddParser(propertyParsers, "lastCash", info.lastCash);
68     AddParser(propertyParsers, "prepaidTraff", info.prepaidTraff);
69     AddParser(propertyParsers, "down", info.down);
70     AddParser(propertyParsers, "passive", info.passive);
71     AddParser(propertyParsers, "disableDetailStat", info.disableDetailStat);
72     AddParser(propertyParsers, "connected", info.connected);
73     AddParser(propertyParsers, "alwaysOnline", info.alwaysOnline);
74     AddParser(propertyParsers, "currIP", info.ip, GetIPValue);
75     AddParser(propertyParsers, "ip", info.ips);
76     AddParser(propertyParsers, "tariff", info.tariff);
77     AddParser(propertyParsers, "group", info.group, GetEncodedValue);
78     AddParser(propertyParsers, "note", info.note, GetEncodedValue);
79     AddParser(propertyParsers, "email", info.email, GetEncodedValue);
80     AddParser(propertyParsers, "name", info.name, GetEncodedValue);
81     AddParser(propertyParsers, "address", info.address, GetEncodedValue);
82     AddParser(propertyParsers, "phone", info.phone, GetEncodedValue);
83     AddParser(propertyParsers, "traff", info.stat);
84
85     for (size_t i = 0; i < USERDATA_NUM; ++i)
86         AddParser(propertyParsers, "userData" + x2str(i), info.userData[i], GetEncodedValue);
87 }
88 //-----------------------------------------------------------------------------
89 PARSER_GET_USER::~PARSER_GET_USER()
90 {
91     PROPERTY_PARSERS::iterator it(propertyParsers.begin());
92     while (it != propertyParsers.end())
93         delete (it++)->second;
94 }
95 //-----------------------------------------------------------------------------
96 int PARSER_GET_USER::ParseStart(const char * el, const char ** attr)
97 {
98 depth++;
99 if (depth == 1)
100     ParseUser(el, attr);
101
102 if (depth == 2)
103     ParseUserParams(el, attr);
104
105 return 0;
106 }
107 //-----------------------------------------------------------------------------
108 void PARSER_GET_USER::ParseEnd(const char * /*el*/)
109 {
110 depth--;
111 if (depth == 0)
112     {
113     if (callback)
114         callback(error.empty(), error, info, data);
115     error.clear();
116     }
117 }
118 //-----------------------------------------------------------------------------
119 void PARSER_GET_USER::ParseUser(const char * el, const char ** attr)
120 {
121 if (strcasecmp(el, "user") == 0)
122     if (strcasecmp(attr[1], "error") == 0)
123         error = "User not found.";
124 }
125 //-----------------------------------------------------------------------------
126 void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr)
127 {
128 if (!TryParse(propertyParsers, ToLower(el), attr))
129     error = "Invalid parameter.";
130 }
131 //-----------------------------------------------------------------------------
132 void PARSER_GET_USER::SetCallback(CALLBACK f, void * d)
133 {
134 callback = f;
135 data = d;
136 }