]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp
Split sgconfig parsers into seaparate files.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_admins.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 "parser_admins.h"
23
24 #include "stg/admins.h"
25 #include "stg/common.h"
26
27 #include <strings.h> // strcasecmp
28
29 using STG::PARSER::GET_ADMINS;
30 using STG::PARSER::ADD_ADMIN;
31 using STG::PARSER::DEL_ADMIN;
32 using STG::PARSER::CHG_ADMIN;
33
34 void GET_ADMINS::CreateAnswer()
35 {
36     const PRIV * priv = currAdmin.GetPriv();
37     if (!priv->adminChg)
38     {
39         answer = "<Error Result=\"Error. Access denied.\"/>";
40         return;
41     }
42
43     answer.clear();
44
45     answer += GetOpenTag();
46     ADMIN_CONF ac;
47     int h = m_admins.OpenSearch();
48
49     while (m_admins.SearchNext(h, &ac) == 0)
50     {
51         unsigned int p = (ac.priv.userStat << 0) +
52                          (ac.priv.userConf << 2) +
53                          (ac.priv.userCash << 4) +
54                          (ac.priv.userPasswd << 6) +
55                          (ac.priv.userAddDel << 8) +
56                          (ac.priv.adminChg << 10) +
57                          (ac.priv.tariffChg << 12);
58         answer += "<admin login=\"" + ac.login + "\" priv=\"" + x2str(p) + "\"/>";
59     }
60     m_admins.CloseSearch(h);
61     answer += GetCloseTag();
62 }
63
64 int DEL_ADMIN::Start(void *, const char * el, const char ** attr)
65 {
66     if (strcasecmp(el, tag.c_str()) == 0)
67     {
68         admin = attr[1];
69         return 0;
70     }
71     return -1;
72 }
73
74 void DEL_ADMIN::CreateAnswer()
75 {
76     if (m_admins.Del(admin, &currAdmin) == 0)
77         answer = "<" + tag + " Result=\"Ok\"/>";
78     else
79         answer = "<" + tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
80 }
81
82 int ADD_ADMIN::Start(void *, const char *el, const char **attr)
83 {
84     if (strcasecmp(el, tag.c_str()) == 0)
85     {
86         admin = attr[1];
87         return 0;
88     }
89     return -1;
90 }
91
92 void ADD_ADMIN::CreateAnswer()
93 {
94     if (m_admins.Add(admin, &currAdmin) == 0)
95         answer = "<" + tag + " Result=\"Ok\"/>";
96     else
97         answer = "<" + tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
98 }
99
100 int CHG_ADMIN::Start(void *, const char * el, const char ** attr)
101 {
102     if (strcasecmp(el, tag.c_str()) == 0)
103     {
104         for (size_t i = 0; i < 6; i += 2)
105         {
106             printfd(__FILE__, "PARSER_CHG_ADMIN::attr[%d] = %s\n", i, attr[i]);
107             if (attr[i] == NULL)
108                 break;
109
110             if (strcasecmp(attr[i], "Login") == 0)
111             {
112                 login = attr[i + 1];
113                 continue;
114             }
115
116             if (strcasecmp(attr[i], "Priv") == 0)
117             {
118                 privAsString = attr[i + 1];
119                 continue;
120             }
121
122             if (strcasecmp(attr[i], "Password") == 0)
123             {
124                 password = attr[i + 1];
125                 continue;
126             }
127         }
128
129         return 0;
130     }
131     return -1;
132 }
133
134 void CHG_ADMIN::CreateAnswer()
135 {
136     if (!login.empty())
137     {
138         ADMIN * origAdmin = NULL;
139
140         if (m_admins.Find(login, &origAdmin))
141         {
142             answer = "<" + tag + " Result = \"Admin '" + login + "' is not found.\"/>";
143             return;
144         }
145
146         ADMIN_CONF conf(origAdmin->GetConf());
147
148         if (!password.empty())
149             conf.password = password.data();
150
151         if (!privAsString.empty())
152         {
153             int p = 0;
154             if (str2x(privAsString.data().c_str(), p) < 0)
155             {
156                 answer = "<" + tag + " Result = \"Incorrect parameter Priv.\"/>";
157                 return;
158             }
159
160             conf.priv.FromInt(p);
161         }
162
163         if (m_admins.Change(conf, &currAdmin) != 0)
164             answer = "<" + tag + " Result = \"" + m_admins.GetStrError() + "\"/>";
165         else
166             answer = "<" + tag + " Result = \"Ok\"/>";
167     }
168     else
169         answer = "<" + tag + " Result = \"Incorrect parameter login.\"/>";
170 }