]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_users.h
Solved some TODOs.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_users.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  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #ifndef __STG_SGCONFIG_PARSER_USERS_H__
23 #define __STG_SGCONFIG_PARSER_USERS_H__
24
25 #include "parser.h"
26
27 #include "stg/user_conf.h"
28 #include "stg/user_stat.h"
29 #include "stg/common.h"
30 #include "stg/resetable.h"
31
32 #include <string>
33
34 class USERS;
35 class USER;
36 class TARIFFS;
37 class ADMIN;
38 class STORE;
39
40 namespace STG
41 {
42 namespace PARSER
43 {
44
45 class GET_USERS: public BASE_PARSER
46 {
47     public:
48         class FACTORY : public BASE_PARSER::FACTORY
49         {
50             public:
51                 FACTORY(USERS & users) : m_users(users) {}
52                 virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_USERS(admin, m_users); }
53                 static void Register(REGISTRY & registry, USERS & users)
54                 { registry[ToLower(tag)] = new FACTORY(users); }
55             private:
56                 USERS & m_users;
57         };
58
59         static const char * tag;
60
61         GET_USERS(const ADMIN & admin, USERS & users)
62             : BASE_PARSER(admin, tag), m_users(users),
63               m_lastUserUpdateTime(0) {}
64         int Start(void * data, const char * el, const char ** attr);
65
66     private:
67         USERS & m_users;
68         time_t m_lastUserUpdateTime;
69
70         void CreateAnswer();
71 };
72
73 class GET_USER: public BASE_PARSER
74 {
75     public:
76         class FACTORY : public BASE_PARSER::FACTORY
77         {
78             public:
79                 FACTORY(const USERS & users) : m_users(users) {}
80                 virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_USER(admin, m_users); }
81                 static void Register(REGISTRY & registry, const USERS & users)
82                 { registry[ToLower(tag)] = new FACTORY(users); }
83             private:
84                 const USERS & m_users;
85         };
86
87         static const char * tag;
88
89         GET_USER(const ADMIN & admin, const USERS & users)
90             : BASE_PARSER(admin, tag), m_users(users) {}
91         int Start(void * data, const char * el, const char ** attr);
92
93     private:
94         const USERS & m_users;
95         std::string m_login;
96
97         void CreateAnswer();
98 };
99
100 class ADD_USER: public BASE_PARSER
101 {
102     public:
103         class FACTORY : public BASE_PARSER::FACTORY
104         {
105             public:
106                 FACTORY(USERS & users) : m_users(users) {}
107                 virtual BASE_PARSER * create(const ADMIN & admin) { return new ADD_USER(admin, m_users); }
108                 static void Register(REGISTRY & registry, USERS & users)
109                 { registry[ToLower(tag)] = new FACTORY(users); }
110             private:
111                 USERS & m_users;
112         };
113
114         static const char * tag;
115
116         ADD_USER(const ADMIN & admin, USERS & users)
117             : BASE_PARSER(admin, tag), m_users(users) {}
118         int Start(void * data, const char * el, const char ** attr);
119
120     private:
121         USERS & m_users;
122         std::string m_login;
123
124         void CreateAnswer();
125 };
126
127 class CHG_USER: public BASE_PARSER
128 {
129     public:
130         class FACTORY : public BASE_PARSER::FACTORY
131         {
132             public:
133                 FACTORY(USERS & users, STORE & store, const TARIFFS & tariffs)
134                     : m_users(users), m_store(store), m_tariffs(tariffs)
135                 {}
136                 virtual BASE_PARSER * create(const ADMIN & admin) { return new CHG_USER(admin, m_users, m_store, m_tariffs); }
137                 static void Register(REGISTRY & registry, USERS & users, STORE & store, const TARIFFS & tariffs)
138                 { registry[ToLower(tag)] = new FACTORY(users, store, tariffs); }
139             private:
140                 USERS & m_users;
141                 STORE & m_store;
142                 const TARIFFS & m_tariffs;
143         };
144
145         static const char * tag;
146
147         CHG_USER(const ADMIN & admin, USERS & users,
148                  STORE & store, const TARIFFS & tariffs)
149             : BASE_PARSER(admin, tag),
150               m_users(users),
151               m_store(store),
152               m_tariffs(tariffs),
153               m_cashMustBeAdded(false),
154               m_res(0) {}
155
156         int Start(void * data, const char * el, const char ** attr);
157
158     private:
159         USERS & m_users;
160         STORE & m_store;
161         const TARIFFS & m_tariffs;
162         USER_STAT_RES m_usr;
163         USER_CONF_RES m_ucr;
164         RESETABLE<uint64_t> m_upr[DIR_NUM];
165         RESETABLE<uint64_t> m_downr[DIR_NUM];
166         std::string m_cashMsg;
167         std::string m_login;
168         bool m_cashMustBeAdded;
169         int m_res;
170
171         int ApplyChanges();
172         void CreateAnswer();
173 };
174
175 class DEL_USER: public BASE_PARSER
176 {
177     public:
178         class FACTORY : public BASE_PARSER::FACTORY
179         {
180             public:
181                 FACTORY(USERS & users) : m_users(users) {}
182                 virtual BASE_PARSER * create(const ADMIN & admin) { return new DEL_USER(admin, m_users); }
183                 static void Register(REGISTRY & registry, USERS & users)
184                 { registry[ToLower(tag)] = new FACTORY(users); }
185             private:
186                 USERS & m_users;
187         };
188
189         static const char * tag;
190
191         DEL_USER(const ADMIN & admin, USERS & users)
192             : BASE_PARSER(admin, tag), m_users(users), res(0), u(NULL) {}
193         int Start(void * data, const char * el, const char ** attr);
194         int End(void * data, const char * el);
195
196     private:
197         USERS & m_users;
198         int res;
199         USER * u;
200
201         void CreateAnswer();
202 };
203
204 class CHECK_USER: public BASE_PARSER
205 {
206     public:
207         class FACTORY : public BASE_PARSER::FACTORY
208         {
209             public:
210                 FACTORY(const USERS & users) : m_users(users) {}
211                 virtual BASE_PARSER * create(const ADMIN & admin) { return new CHECK_USER(admin, m_users); }
212                 static void Register(REGISTRY & registry, const USERS & users)
213                 { registry[ToLower(tag)] = new FACTORY(users); }
214             private:
215                 const USERS & m_users;
216         };
217
218         static const char * tag;
219
220         CHECK_USER(const ADMIN & admin, const USERS & users)
221             : BASE_PARSER(admin, tag), m_users(users) {}
222         int Start(void * data, const char * el, const char ** attr);
223         int End(void * data, const char * el);
224
225     private:
226         const USERS & m_users;
227
228         void CreateAnswer(const char * error);
229         void CreateAnswer() {} // dummy
230 };
231
232 } // namespace PARSER
233 } // namespace STG
234
235 #endif