]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/get_user.cpp
Fixes in paths.
[stg.git] / stglibs / srvconf.lib / parsers / 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 "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 using namespace STG;
33
34 namespace STG
35 {
36
37 template <>
38 bool GetValue<GET_USER::STAT>(const char ** attr, GET_USER::STAT & value)
39 {
40 if (!attr)
41     return false;
42 std::map<std::string, long long *> props;
43 for (size_t i = 0; i < DIR_NUM; ++i)
44     {
45     props.insert(std::pair<std::string, long long *>("su" + x2str(i), &value.su[i]));
46     props.insert(std::pair<std::string, long long *>("sd" + x2str(i), &value.sd[i]));
47     props.insert(std::pair<std::string, long long *>("mu" + x2str(i), &value.mu[i]));
48     props.insert(std::pair<std::string, long long *>("md" + x2str(i), &value.md[i]));
49     }
50 size_t pos = 0;
51 while (attr[pos])
52     {
53         std::string name(ToLower(attr[pos++]));
54         std::map<std::string, long long *>::iterator it(props.find(name));
55         if (it != props.end())
56             if (str2x(attr[pos++], *it->second) < 0)
57                 return false;
58     }
59 return true;
60 }
61
62 }
63
64 GET_USER::PARSER::PARSER()
65     : callback(NULL),
66       data(NULL),
67       depth(0),
68       parsingAnswer(false)
69 {
70     AddParser(propertyParsers, "login", info.login);
71     AddParser(propertyParsers, "password", info.password);
72     AddParser(propertyParsers, "cash", info.cash);
73     AddParser(propertyParsers, "credit", info.credit);
74     AddParser(propertyParsers, "creditExpire", info.creditExpire);
75     AddParser(propertyParsers, "lastCash", info.lastCash);
76     AddParser(propertyParsers, "prepaidTraff", info.prepaidTraff);
77     AddParser(propertyParsers, "down", info.down);
78     AddParser(propertyParsers, "passive", info.passive);
79     AddParser(propertyParsers, "disableDetailStat", info.disableDetailStat);
80     AddParser(propertyParsers, "connected", info.connected);
81     AddParser(propertyParsers, "aonline", info.alwaysOnline);
82     AddParser(propertyParsers, "currIP", info.ip, GetIPValue);
83     AddParser(propertyParsers, "ip", info.ips);
84     AddParser(propertyParsers, "tariff", info.tariff);
85     AddParser(propertyParsers, "group", info.group, GetEncodedValue);
86     AddParser(propertyParsers, "note", info.note, GetEncodedValue);
87     AddParser(propertyParsers, "email", info.email, GetEncodedValue);
88     AddParser(propertyParsers, "name", info.name, GetEncodedValue);
89     AddParser(propertyParsers, "address", info.address, GetEncodedValue);
90     AddParser(propertyParsers, "phone", info.phone, GetEncodedValue);
91     AddParser(propertyParsers, "traff", info.stat);
92
93     for (size_t i = 0; i < USERDATA_NUM; ++i)
94         AddParser(propertyParsers, "userData" + x2str(i), info.userData[i], GetEncodedValue);
95 }
96 //-----------------------------------------------------------------------------
97 GET_USER::PARSER::~PARSER()
98 {
99     PROPERTY_PARSERS::iterator it(propertyParsers.begin());
100     while (it != propertyParsers.end())
101         delete (it++)->second;
102 }
103 //-----------------------------------------------------------------------------
104 int GET_USER::PARSER::ParseStart(const char * el, const char ** attr)
105 {
106 depth++;
107 if (depth == 1)
108     ParseUser(el, attr);
109
110 if (depth == 2 && parsingAnswer)
111     ParseUserParams(el, attr);
112
113 return 0;
114 }
115 //-----------------------------------------------------------------------------
116 void GET_USER::PARSER::ParseEnd(const char * /*el*/)
117 {
118 depth--;
119 if (depth == 0 && parsingAnswer)
120     {
121     if (callback)
122         callback(error.empty(), error, info, data);
123     error.clear();
124     parsingAnswer = false;
125     }
126 }
127 //-----------------------------------------------------------------------------
128 void GET_USER::PARSER::ParseUser(const char * el, const char ** attr)
129 {
130 if (strcasecmp(el, "user") == 0)
131     if (attr && attr[0] && attr[1])
132         {
133         if (strcasecmp(attr[1], "error") == 0)
134             {
135             if (attr[2] && attr[3])
136                 error = attr[3];
137             else
138                 error = "User not found.";
139             }
140         else
141             parsingAnswer = true;
142         }
143     else
144         parsingAnswer = true;
145 }
146 //-----------------------------------------------------------------------------
147 void GET_USER::PARSER::ParseUserParams(const char * el, const char ** attr)
148 {
149 if (!TryParse(propertyParsers, ToLower(el), attr))
150     error = "Invalid parameter.";
151 }
152 //-----------------------------------------------------------------------------
153 void GET_USER::PARSER::SetCallback(CALLBACK f, void * d)
154 {
155 callback = f;
156 data = d;
157 }