]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_admin.cpp
Massive refactoring.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_admin.cpp
1 #include <stdio.h>
2 #include <string.h>
3
4 #include "parser.h"
5
6 //-----------------------------------------------------------------------------
7 //  GET ADMINS
8 //-----------------------------------------------------------------------------
9 int PARSER_GET_ADMINS::ParseStart(void *, const char *el, const char **)
10 {
11 if (strcasecmp(el, "GetAdmins") == 0)
12     {
13     return 0;
14     }
15 return -1;
16 }
17 //-----------------------------------------------------------------------------
18 int PARSER_GET_ADMINS::ParseEnd(void *, const char *el)
19 {
20 if (strcasecmp(el, "GetAdmins") == 0)
21     {
22     CreateAnswer();
23     return 0;
24     }
25 return -1;
26 }
27 //-----------------------------------------------------------------------------
28 void PARSER_GET_ADMINS::CreateAnswer()
29 {
30 const PRIV * priv = currAdmin->GetPriv();
31 if (!priv->adminChg)
32     {
33     //answerList->clear();
34     answerList->erase(answerList->begin(), answerList->end());
35
36     answerList->push_back("<Error Result=\"Error. Access denied.\"/>");
37     return;
38     }
39
40 std::string s;
41 //answerList->clear();
42 answerList->erase(answerList->begin(), answerList->end());
43
44 answerList->push_back("<Admins>");
45 ADMIN_CONF ac;
46 int h = admins->OpenSearch();
47
48 unsigned int p;
49 while (admins->SearchNext(h, &ac) == 0)
50     {
51     //memcpy(&p, &ac.priv, sizeof(unsigned int));
52     p = (ac.priv.userStat << 0) +
53         (ac.priv.userConf << 2) +
54         (ac.priv.userCash << 4) +
55         (ac.priv.userPasswd << 6) +
56         (ac.priv.userAddDel << 8) +
57         (ac.priv.adminChg << 10) +
58         (ac.priv.tariffChg << 12);
59     strprintf(&s, "<admin login=\"%s\" priv=\"%d\"/>", ac.login.c_str(), p);
60     answerList->push_back(s);
61     }
62 admins->CloseSearch(h);
63 answerList->push_back("</Admins>");
64 }
65 //-----------------------------------------------------------------------------
66
67 //-----------------------------------------------------------------------------
68 //  DEL ADMIN
69 //-----------------------------------------------------------------------------
70 int PARSER_DEL_ADMIN::ParseStart(void *, const char *el, const char **attr)
71 {
72 strError = "";
73 if (strcasecmp(el, "DelAdmin") == 0)
74     {
75     adminToDel = attr[1];
76     return 0;
77     }
78 return -1;
79 }
80 //-----------------------------------------------------------------------------
81 int PARSER_DEL_ADMIN::ParseEnd(void *, const char *el)
82 {
83 if (strcasecmp(el, "DelAdmin") == 0)
84     {
85     CreateAnswer();
86     return 0;
87     }
88 return -1;
89 }
90 //-----------------------------------------------------------------------------
91 void PARSER_DEL_ADMIN::CreateAnswer()
92 {
93 //answerList->clear();
94 answerList->erase(answerList->begin(), answerList->end());
95
96 if (admins->Del(adminToDel, currAdmin) == 0)
97     {
98     answerList->push_back("<DelAdmin Result=\"Ok\"/>");
99     }
100 else
101     {
102     std::string s;
103     strprintf(&s, "<DelAdmin Result=\"Error. %s\"/>", admins->GetStrError().c_str());
104     answerList->push_back(s);
105     }
106 }
107 //-----------------------------------------------------------------------------
108 int PARSER_DEL_ADMIN::CheckAttr(const char **attr)
109 {
110 /*  <DelAdmin login=\"admin\">
111  *  attr[0] = "login" (word login)
112  *  attr[1] = login, value of login
113  *  attr[2] = NULL                  */
114
115 if (strcasecmp(attr[0], "login") == 0 && attr[1] && !attr[2])
116     {
117     return 0;
118     }
119 return -1;
120 }
121 //-----------------------------------------------------------------------------
122 //  ADD ADMIN
123 //-----------------------------------------------------------------------------
124 int PARSER_ADD_ADMIN::ParseStart(void *, const char *el, const char **attr)
125 {
126 if (strcasecmp(el, "AddAdmin") == 0)
127     {
128     adminToAdd = attr[1];
129     return 0;
130     }
131 return -1;
132 }
133 //-----------------------------------------------------------------------------
134 int PARSER_ADD_ADMIN::ParseEnd(void *, const char *el)
135 {
136 //answerList->clear();
137 answerList->erase(answerList->begin(), answerList->end());
138
139 if (strcasecmp(el, "AddAdmin") == 0)
140     {
141     CreateAnswer();
142     return 0;
143     }
144 return -1;
145 }
146 //-----------------------------------------------------------------------------
147 void PARSER_ADD_ADMIN::CreateAnswer()
148 {
149 //answerList->clear();
150 answerList->erase(answerList->begin(), answerList->end());
151
152 if (admins->Add(adminToAdd, currAdmin) == 0)
153     {
154     answerList->push_back("<AddAdmin Result=\"Ok\"/>");
155     }
156 else
157     {
158     std::string s;
159     strprintf(&s, "<AddAdmin Result=\"Error. %s\"/>", admins->GetStrError().c_str());
160     answerList->push_back(s);
161     }
162 }
163 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
165 //  CHG ADMIN
166 //-----------------------------------------------------------------------------
167 int PARSER_CHG_ADMIN::ParseStart(void *, const char *el, const char **attr)
168 {
169 if (strcasecmp(el, "ChgAdmin") == 0)
170     {
171     for (int i = 0; i < 6; i+=2)
172         {
173         printfd(__FILE__, "PARSER_CHG_ADMIN::attr[%d] = %s\n", i, attr[i]);
174         if (attr[i] == NULL)
175             break;
176
177         if (strcasecmp(attr[i], "Login") == 0)
178             {
179             login = attr[i + 1];
180             continue;
181             }
182
183         if (strcasecmp(attr[i], "Priv") == 0)
184             {
185             privAsString = attr[i + 1];
186             continue;
187             }
188
189         if (strcasecmp(attr[i], "Password") == 0)
190             {
191             password = attr[i + 1];
192             continue;
193             }
194         }
195
196     return 0;
197     }
198 return -1;
199 }
200 //-----------------------------------------------------------------------------
201 int PARSER_CHG_ADMIN::ParseEnd(void *, const char *el)
202 {
203 if (strcasecmp(el, "ChgAdmin") == 0)
204     {
205     CreateAnswer();
206     return 0;
207     }
208 return -1;
209 }
210 //-----------------------------------------------------------------------------
211 void PARSER_CHG_ADMIN::CreateAnswer()
212 {
213 answerList->erase(answerList->begin(), answerList->end());
214
215
216 if (!login.res_empty())
217     {
218     ADMIN * origAdmin = NULL;
219
220     if (admins->Find(login, &origAdmin))
221         {
222         answerList->push_back(std::string("<ChgAdmin Result = \"Admin '") + login.data() + "' is not found.\"/>");
223         return;
224         }
225
226     ADMIN_CONF conf(origAdmin->GetConf());
227
228     if (!password.res_empty())
229         conf.password = password.data();
230
231     if (!privAsString.res_empty())
232         {
233         int p = 0;
234         if (str2x(privAsString.data().c_str(), p) < 0)
235             {
236             answerList->push_back("<ChgAdmin Result = \"Incorrect parameter Priv.\"/>");
237             return;
238             }
239
240         conf.priv.FromInt(p);
241         }
242
243     if (admins->Change(conf, currAdmin) != 0)
244         {
245         std::string s;
246         strprintf(&s, "<ChgAdmin Result = \"%s\"/>", admins->GetStrError().c_str());
247         answerList->push_back(s);
248         }
249     else
250         {
251         answerList->push_back("<ChgAdmin Result = \"Ok\"/>");
252         }
253     }
254 else
255     {
256     answerList->push_back("<ChgAdmin Result = \"Incorrect parameter login.\"/>");
257     }
258 }
259 //-----------------------------------------------------------------------------*/
260