]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/get_user.cpp
Merge branch 'stg-2.409-radius'
[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
29 #include <strings.h>
30
31 using namespace STG;
32
33 namespace STG
34 {
35
36 template <>
37 bool GetValue<GET_USER::STAT>(const char ** attr, GET_USER::STAT & value, const std::string & /*attrName*/)
38 {
39 if (!attr)
40     return false;
41 std::map<std::string, long long *> props;
42 for (size_t i = 0; i < DIR_NUM; ++i)
43     {
44     props.insert(std::pair<std::string, long long *>("su" + unsigned2str(i), &value.su[i]));
45     props.insert(std::pair<std::string, long long *>("sd" + unsigned2str(i), &value.sd[i]));
46     props.insert(std::pair<std::string, long long *>("mu" + unsigned2str(i), &value.mu[i]));
47     props.insert(std::pair<std::string, long long *>("md" + unsigned2str(i), &value.md[i]));
48     }
49 size_t pos = 0;
50 while (attr[pos])
51     {
52         std::string name(ToLower(attr[pos++]));
53         std::map<std::string, long long *>::iterator it(props.find(name));
54         if (it != props.end())
55             if (str2x(attr[pos++], *it->second) < 0)
56                 return false;
57     }
58 return true;
59 }
60
61 }
62
63 GET_USER::PARSER::PARSER(CALLBACK f, void * d, const std::string & e)
64     : callback(f),
65       data(d),
66       encoding(e),
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.lastCashAdd);
76     AddParser(propertyParsers, "lastTimeCash", info.lastCashAddTime);
77     AddParser(propertyParsers, "freeMb", info.prepaidTraff);
78     AddParser(propertyParsers, "down", info.disabled);
79     AddParser(propertyParsers, "passive", info.passive);
80     AddParser(propertyParsers, "disableDetailStat", info.disableDetailStat);
81     AddParser(propertyParsers, "status", info.connected);
82     AddParser(propertyParsers, "aonline", info.alwaysOnline);
83     AddParser(propertyParsers, "currIP", info.ip, GetIPValue);
84     AddParser(propertyParsers, "ip", info.ips);
85     AddParser(propertyParsers, "tariff", info.tariff);
86     AddParser(propertyParsers, "group", info.group, "koi8-ru", GetEncodedValue);
87     AddParser(propertyParsers, "note", info.note, "koi8-ru", GetEncodedValue);
88     AddParser(propertyParsers, "email", info.email, "koi8-ru", GetEncodedValue);
89     AddParser(propertyParsers, "name", info.name, "koi8-ru", GetEncodedValue);
90     AddParser(propertyParsers, "address", info.address, "koi8-ru", GetEncodedValue);
91     AddParser(propertyParsers, "phone", info.phone, "cp1251", GetEncodedValue);
92     AddParser(propertyParsers, "corp", info.corp);
93     AddParser(propertyParsers, "traff", info.stat);
94     AddParser(propertyParsers, "pingTime", info.pingTime);
95     AddParser(propertyParsers, "lastActivityTime", info.lastActivityTime);
96
97     for (size_t i = 0; i < USERDATA_NUM; ++i)
98         AddParser(propertyParsers, "userData" + unsigned2str(i), info.userData[i], "koi8-ru", GetEncodedValue);
99 }
100 //-----------------------------------------------------------------------------
101 GET_USER::PARSER::~PARSER()
102 {
103     PROPERTY_PARSERS::iterator it(propertyParsers.begin());
104     while (it != propertyParsers.end())
105         delete (it++)->second;
106 }
107 //-----------------------------------------------------------------------------
108 int GET_USER::PARSER::ParseStart(const char * el, const char ** attr)
109 {
110 depth++;
111 if (depth == 1)
112     ParseUser(el, attr);
113
114 if (depth == 2 && parsingAnswer)
115     ParseUserParams(el, attr);
116
117 if (depth == 3 && parsingAnswer)
118     {
119     ParseAuthBy(el, attr);
120     ParseServices(el, attr);
121     }
122
123 return 0;
124 }
125 //-----------------------------------------------------------------------------
126 void GET_USER::PARSER::ParseEnd(const char * /*el*/)
127 {
128 depth--;
129 if (depth == 0 && parsingAnswer)
130     {
131     if (callback)
132         callback(error.empty(), error, info, data);
133     error.clear();
134     parsingAnswer = false;
135     }
136 }
137 //-----------------------------------------------------------------------------
138 void GET_USER::PARSER::ParseUser(const char * el, const char ** attr)
139 {
140 if (strcasecmp(el, "user") == 0)
141     {
142     if (attr && attr[0] && attr[1])
143         {
144         if (strcasecmp(attr[1], "error") == 0)
145             {
146             if (attr[2] && attr[3])
147                 error = attr[3];
148             else
149                 error = "User not found.";
150             }
151         else if (strcasecmp(attr[0], "login") == 0 && attr[1])
152             info.login = attr[1];
153         }
154     parsingAnswer = true;
155     }
156 }
157 //-----------------------------------------------------------------------------
158 void GET_USER::PARSER::ParseUserParams(const char * el, const char ** attr)
159 {
160 if (strcasecmp(el, "AuthorizedBy") != 0 &&
161     !TryParse(propertyParsers, ToLower(el), attr, encoding))
162     error = "Invalid parameter.";
163 else if (strcasecmp(el, "Services") != 0 &&
164     !TryParse(propertyParsers, ToLower(el), attr, encoding))
165     error = "Invalid parameter.";
166 }
167 //-----------------------------------------------------------------------------
168 void GET_USER::PARSER::ParseAuthBy(const char * el, const char ** attr)
169 {
170 if (strcasecmp(el, "Auth") == 0 &&
171     attr && attr[0] && attr[1] &&
172     strcasecmp(attr[0], "name") == 0)
173     info.authBy.push_back(attr[1]);
174 }
175 //-----------------------------------------------------------------------------
176 void GET_USER::PARSER::ParseServices(const char * el, const char ** attr)
177 {
178 if (strcasecmp(el, "Service") == 0 &&
179     attr && attr[0] && attr[1] &&
180     strcasecmp(attr[0], "name") == 0)
181     info.services.push_back(attr[1]);
182 }