]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser.h
81bd454b92067814713060dd58293d6fb2393e9c
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser.h
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  */
20
21 #ifndef PARSER_H
22 #define PARSER_H
23
24 #include "stg/message.h"
25 #include "stg/tariff_conf.h"
26 #include "stg/resetable.h"
27 #include "stg/const.h"
28
29 #include <string>
30 #include <vector>
31
32 class TARIFFS;
33 class SETTINGS;
34 class STORE;
35 class ADMINS;
36 class ADMIN;
37 class USERS;
38 class USER;
39 class USER_STAT_RES;
40 class USER_CONF_RES;
41
42 //-----------------------------------------------------------------------------
43 class BASE_PARSER {
44 public:
45     BASE_PARSER()
46         : strError(),
47           admins(NULL),
48           users(NULL),
49           tariffs(NULL),
50           store(NULL),
51           settings(NULL),
52           currAdmin(NULL),
53           depth(0)
54     {}
55     virtual ~BASE_PARSER() {}
56     virtual int ParseStart(void *data, const char *el, const char **attr) = 0;
57     virtual int ParseEnd(void *data, const char *el) = 0;
58     virtual void Reset() { answer.clear(); depth = 0; }
59
60     void SetUsers(USERS * u) { users = u; }
61     void SetAdmins(ADMINS * a) { admins = a; }
62     void SetTariffs(TARIFFS * t) { tariffs = t; }
63     void SetStore(STORE * s) { store = s; }
64     void SetStgSettings(const SETTINGS * s) { settings = s; }
65
66     void SetCurrAdmin(ADMIN & cua) { currAdmin = &cua; }
67     const std::string & GetStrError() const { return strError; }
68     const std::string & GetAnswer() const { return answer; }
69
70 protected:
71     BASE_PARSER(const BASE_PARSER & rvalue);
72     BASE_PARSER & operator=(const BASE_PARSER & rvalue);
73
74     std::string      strError;
75     ADMINS *         admins;
76     USERS *          users;
77     TARIFFS *        tariffs;
78     STORE *          store;
79     const SETTINGS * settings;
80     ADMIN          * currAdmin;
81     int              depth;
82     std::string      answer;
83 };
84 //-----------------------------------------------------------------------------
85 class PARSER_GET_ADMINS: public BASE_PARSER {
86 public:
87     int ParseStart(void *data, const char *el, const char **attr);
88     int ParseEnd(void *data, const char *el);
89
90 private:
91     void CreateAnswer();
92 };
93 //-----------------------------------------------------------------------------
94 class PARSER_ADD_ADMIN: public BASE_PARSER {
95 public:
96         PARSER_ADD_ADMIN() : BASE_PARSER(), adminToAdd() {}
97     int ParseStart(void *data, const char *el, const char **attr);
98     int ParseEnd(void *data, const char *el);
99
100 private:
101     std::string adminToAdd;
102
103     void CreateAnswer();
104 };
105 //-----------------------------------------------------------------------------
106 class PARSER_DEL_ADMIN: public BASE_PARSER {
107 public:
108         PARSER_DEL_ADMIN() : BASE_PARSER(), adminToDel() {}
109     int ParseStart(void *data, const char *el, const char **attr);
110     int ParseEnd(void *data, const char *el);
111
112 private:
113     std::string adminToDel;
114
115     void CreateAnswer();
116 };
117 //-----------------------------------------------------------------------------
118 class PARSER_CHG_ADMIN: public BASE_PARSER {
119 public:
120         PARSER_CHG_ADMIN() : BASE_PARSER(), login(), password(), privAsString() {}
121     int ParseStart(void *data, const char *el, const char **attr);
122     int ParseEnd(void *data, const char *el);
123
124 private:
125     RESETABLE<std::string> login;
126     RESETABLE<std::string> password;
127     RESETABLE<std::string> privAsString;
128
129     void CreateAnswer();
130 };
131 //-----------------------------------------------------------------------------
132 class PARSER_GET_SERVER_INFO: public BASE_PARSER {
133 public:
134     int ParseStart(void *data, const char *el, const char **attr);
135     int ParseEnd(void *data, const char *el);
136
137 private:
138     void CreateAnswer();
139 };
140 //-----------------------------------------------------------------------------
141 class PARSER_GET_USER: public BASE_PARSER {
142 public:
143         PARSER_GET_USER() : BASE_PARSER(), login() {}
144         ~PARSER_GET_USER() {}
145     int ParseStart(void *data, const char *el, const char **attr);
146     int ParseEnd(void *data, const char *el);
147
148 private:
149     std::string login;
150
151     void CreateAnswer();
152 };
153 //-----------------------------------------------------------------------------
154 class PARSER_GET_USERS: public BASE_PARSER {
155 public:
156         PARSER_GET_USERS() : BASE_PARSER(), lastUserUpdateTime(0), lastUpdateFound(false) {}
157     int ParseStart(void *data, const char *el, const char **attr);
158     int ParseEnd(void *data, const char *el);
159
160 private:
161     time_t lastUserUpdateTime;
162     bool lastUpdateFound;
163
164     void CreateAnswer();
165 };
166 //-----------------------------------------------------------------------------
167 class PARSER_GET_TARIFFS: public BASE_PARSER {
168 public:
169     int ParseStart(void *data, const char *el, const char **attr);
170     int ParseEnd(void *data, const char *el);
171
172 private:
173     void CreateAnswer();
174 };
175 //-----------------------------------------------------------------------------
176 class PARSER_ADD_TARIFF: public BASE_PARSER {
177 public:
178         PARSER_ADD_TARIFF() : BASE_PARSER(), tariffToAdd() {}
179     int ParseStart(void *data, const char *el, const char **attr);
180     int ParseEnd(void *data, const char *el);
181
182 private:
183     std::string tariffToAdd;
184
185     void CreateAnswer();
186 };
187 //-----------------------------------------------------------------------------
188 class PARSER_DEL_TARIFF: public BASE_PARSER {
189 public:
190         PARSER_DEL_TARIFF() : BASE_PARSER(), tariffToDel() {}
191     int ParseStart(void *data, const char *el, const char **attr);
192     int ParseEnd(void *data, const char *el);
193
194 private:
195     std::string tariffToDel;
196
197     void CreateAnswer();
198 };
199 //-----------------------------------------------------------------------------
200 class PARSER_CHG_TARIFF: public BASE_PARSER {
201 public:
202         PARSER_CHG_TARIFF() : BASE_PARSER(), td() {}
203     int ParseStart(void *data, const char *el, const char **attr);
204     int ParseEnd(void *data, const char *el);
205
206 private:
207     TARIFF_DATA_RES td;
208
209     int ParseSlashedIntParams(int paramsNum, const std::string & s, int * params);
210     int ParseSlashedDoubleParams(int paramsNum, const std::string & s, double * params);
211     int CheckTariffData();
212     int AplayChanges();
213     void CreateAnswer();
214 };
215 //-----------------------------------------------------------------------------/
216 class PARSER_ADD_USER: public BASE_PARSER {
217 public:
218         PARSER_ADD_USER() : BASE_PARSER(), login() {}
219         ~PARSER_ADD_USER() {}
220     int ParseStart(void *data, const char *el, const char **attr);
221     int ParseEnd(void *data, const char *el);
222
223 private:
224     std::string login;
225
226     int CheckUserData();
227     void CreateAnswer();
228     void Reset();
229 };
230 //-----------------------------------------------------------------------------
231 class PARSER_CHG_USER: public BASE_PARSER {
232 public:
233         PARSER_CHG_USER();
234         ~PARSER_CHG_USER();
235     int ParseStart(void *data, const char *el, const char **attr);
236     int ParseEnd(void *data, const char *el);
237
238 private:
239     USER_STAT_RES * usr;
240     USER_CONF_RES * ucr;
241     RESETABLE<uint64_t> * upr;
242     RESETABLE<uint64_t> * downr;
243     std::string cashMsg;
244     std::string login;
245     bool cashMustBeAdded;
246     int res;
247
248     PARSER_CHG_USER(const PARSER_CHG_USER & rvalue);
249     PARSER_CHG_USER & operator=(const PARSER_CHG_USER & rvalue);
250
251     std::string EncChar2String(const char *);
252     int AplayChanges();
253     void CreateAnswer();
254     void Reset();
255 };
256 //-----------------------------------------------------------------------------
257 class PARSER_DEL_USER: public BASE_PARSER {
258 public:
259         PARSER_DEL_USER() : BASE_PARSER(), res(0), u(NULL) {}
260     int ParseStart(void *data, const char *el, const char **attr);
261     int ParseEnd(void *data, const char *el);
262
263 private:
264     int res;
265     USER * u;
266
267     PARSER_DEL_USER(const PARSER_DEL_USER & rvalue);
268     PARSER_DEL_USER & operator=(const PARSER_DEL_USER & rvalue);
269
270     void CreateAnswer();
271 };
272 //-----------------------------------------------------------------------------
273 class PARSER_CHECK_USER: public BASE_PARSER {
274 public:
275         PARSER_CHECK_USER() : BASE_PARSER() {}
276     int ParseStart(void *data, const char *el, const char **attr);
277     int ParseEnd(void *data, const char *el);
278
279 private:
280     void CreateAnswer(const char * error);
281 };
282 //-----------------------------------------------------------------------------
283 class PARSER_SEND_MESSAGE: public BASE_PARSER {
284 public:
285         PARSER_SEND_MESSAGE() : BASE_PARSER(), logins(), result(0), msg(), u(NULL) {}
286     int ParseStart(void *data, const char *el, const char **attr);
287     int ParseEnd(void *data, const char *el);
288
289 private:
290     enum {res_ok, res_params_error, res_unknown};
291     std::vector<std::string> logins;
292     int result;
293     STG_MSG msg;
294     USER * u;
295
296     PARSER_SEND_MESSAGE(const PARSER_SEND_MESSAGE & rvalue);
297     PARSER_SEND_MESSAGE & operator=(const PARSER_SEND_MESSAGE & rvalue);
298
299     int ParseLogins(const char * logins);
300     void CreateAnswer();
301 };
302 //-----------------------------------------------------------------------------
303 #endif //PARSER_H