]> git.stg.codes - stg.git/blob - libs/srvconf/parsers/get_user.cpp
Public interfaces: part 1
[stg.git] / libs / srvconf / 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<GetUser::Stat>(const char** attr, GetUser::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" + std::to_string(i), &value.su[i]));
45         props.insert(std::pair<std::string, long long*>("sd" + std::to_string(i), &value.sd[i]));
46         props.insert(std::pair<std::string, long long*>("mu" + std::to_string(i), &value.mu[i]));
47         props.insert(std::pair<std::string, long long*>("md" + std::to_string(i), &value.md[i]));
48     }
49     size_t pos = 0;
50     while (attr[pos])
51     {
52         const auto it = props.find(ToLower(attr[pos++]));
53         if (it != props.end())
54             if (str2x(attr[pos++], *it->second) < 0)
55                 return false;
56     }
57     return true;
58 }
59
60 }
61
62 GetUser::Parser::Parser(Callback f, void* d, const std::string& e)
63     : callback(f),
64       data(d),
65       encoding(e),
66       depth(0),
67       parsingAnswer(false)
68 {
69     addParser(propertyParsers, "login", info.login);
70     addParser(propertyParsers, "password", info.password);
71     addParser(propertyParsers, "cash", info.cash);
72     addParser(propertyParsers, "credit", info.credit);
73     addParser(propertyParsers, "creditExpire", info.creditExpire);
74     addParser(propertyParsers, "lastCash", info.lastCashAdd);
75     addParser(propertyParsers, "lastTimeCash", info.lastCashAddTime);
76     addParser(propertyParsers, "freeMb", info.prepaidTraff);
77     addParser(propertyParsers, "down", info.disabled);
78     addParser(propertyParsers, "passive", info.passive);
79     addParser(propertyParsers, "disableDetailStat", info.disableDetailStat);
80     addParser(propertyParsers, "status", 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, "koi8-ru", getEncodedValue);
86     addParser(propertyParsers, "note", info.note, "koi8-ru", getEncodedValue);
87     addParser(propertyParsers, "email", info.email, "koi8-ru", getEncodedValue);
88     addParser(propertyParsers, "name", info.name, "koi8-ru", getEncodedValue);
89     addParser(propertyParsers, "address", info.address, "koi8-ru", getEncodedValue);
90     addParser(propertyParsers, "phone", info.phone, "cp1251", getEncodedValue);
91     addParser(propertyParsers, "corp", info.corp);
92     addParser(propertyParsers, "traff", info.stat);
93     addParser(propertyParsers, "pingTime", info.pingTime);
94     addParser(propertyParsers, "lastActivityTime", info.lastActivityTime);
95
96     for (size_t i = 0; i < USERDATA_NUM; ++i)
97         addParser(propertyParsers, "userData" + std::to_string(i), info.userData[i], "koi8-ru", getEncodedValue);
98 }
99 //-----------------------------------------------------------------------------
100 GetUser::Parser::~Parser()
101 {
102     auto it = propertyParsers.begin();
103     while (it != propertyParsers.end())
104         delete (it++)->second;
105 }
106 //-----------------------------------------------------------------------------
107 int GetUser::Parser::ParseStart(const char* el, const char** attr)
108 {
109     depth++;
110     if (depth == 1)
111         ParseUser(el, attr);
112     else if (depth == 2 && parsingAnswer)
113         ParseUserParams(el, attr);
114     else if (depth == 3 && parsingAnswer)
115     {
116         ParseAuthBy(el, attr);
117         ParseServices(el, attr);
118     }
119
120     return 0;
121 }
122 //-----------------------------------------------------------------------------
123 void GetUser::Parser::ParseEnd(const char* /*el*/)
124 {
125     depth--;
126     if (depth == 0 && parsingAnswer)
127     {
128         if (callback)
129             callback(error.empty(), error, info, data);
130         error.clear();
131         parsingAnswer = false;
132     }
133 }
134 //-----------------------------------------------------------------------------
135 void GetUser::Parser::ParseUser(const char* el, const char** attr)
136 {
137     if (strcasecmp(el, "user") == 0)
138     {
139         if (attr && attr[0] && attr[1])
140         {
141             if (strcasecmp(attr[1], "error") == 0)
142             {
143                 if (attr[2] && attr[3])
144                     error = attr[3];
145                 else
146                     error = "User not found.";
147             }
148             else if (strcasecmp(attr[0], "login") == 0 && attr[1])
149                 info.login = attr[1];
150         }
151         parsingAnswer = true;
152     }
153 }
154 //-----------------------------------------------------------------------------
155 void GetUser::Parser::ParseUserParams(const char* el, const char** attr)
156 {
157     if (strcasecmp(el, "AuthorizedBy") != 0 &&
158         !tryParse(propertyParsers, ToLower(el), attr, encoding))
159         error = "Invalid parameter.";
160     else if (strcasecmp(el, "Services") != 0 &&
161         !tryParse(propertyParsers, ToLower(el), attr, encoding))
162         error = "Invalid parameter.";
163 }
164 //-----------------------------------------------------------------------------
165 void GetUser::Parser::ParseAuthBy(const char* el, const char** attr)
166 {
167     if (strcasecmp(el, "Auth") == 0 &&
168         attr && attr[0] && attr[1] &&
169         strcasecmp(attr[0], "name") == 0)
170         info.authBy.push_back(attr[1]);
171 }
172 //-----------------------------------------------------------------------------
173 void GetUser::Parser::ParseServices(const char* el, const char** attr)
174 {
175     if (strcasecmp(el, "Service") == 0 &&
176         attr && attr[0] && attr[1] &&
177         strcasecmp(attr[0], "name") == 0)
178         info.services.push_back(attr[1]);
179 }