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