]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_services.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_services.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 : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "parser_services.h"
22
23 #include "stg/services.h"
24
25 #include <strings.h> // strcasecmp
26
27 using STG::PARSER::GET_SERVICES;
28 using STG::PARSER::GET_SERVICE;
29 using STG::PARSER::ADD_SERVICE;
30 using STG::PARSER::DEL_SERVICE;
31 using STG::PARSER::CHG_SERVICE;
32
33 const char * GET_SERVICES::tag = "GetServices";
34 const char * GET_SERVICE::tag  = "AddService";
35 const char * ADD_SERVICE::tag  = "AddService";
36 const char * DEL_SERVICE::tag  = "DelService";
37 const char * CHG_SERVICE::tag  = "SetService";
38
39 void GET_SERVICES::CreateAnswer()
40 {
41     // TODO: no priviledges implemented yet
42     /*const PRIV * priv = m_currAdmin.GetPriv();
43     if (!priv->serviceChg)
44     {
45         m_answer = "<Error Result=\"Error. Access denied.\"/>";
46         return;
47     }*/
48
49     m_answer = "<Services>";
50     SERVICE_CONF conf;
51     int h = m_services.OpenSearch();
52     while (m_services.SearchNext(h, &conf) == 0)
53     {
54         m_answer += "<Service name=\"" + conf.name +
55                     "\" comment=\"" + Encode12str(conf.comment) +
56                     "\" cost=\"" + x2str(conf.cost) +
57                     "\" payDay=\"" + x2str(conf.payDay) + "\"/>";
58     }
59     m_services.CloseSearch(h);
60     m_answer += "</Services>";
61 }
62
63 int GET_SERVICE::Start(void *, const char * el, const char ** attr)
64 {
65     if (strcasecmp(el, m_tag.c_str()) == 0)
66     {
67         m_name = attr[1];
68         return 0;
69     }
70     return -1;
71 }
72
73 void GET_SERVICE::CreateAnswer()
74 {
75     // TODO: no priviledges implemented yet
76     /*const PRIV * priv = m_currAdmin.GetPriv();
77     if (!priv->serviceChg)
78     {
79         m_answer = "<Error Result=\"Error. Access denied.\"/>";
80         return;
81     }*/
82
83     SERVICE_CONF conf;
84     if (!m_services.Find(m_name, &conf))
85         m_answer = "<Error result=\"Service '" + m_name + "' does not exist.\"/>";
86     else
87         m_answer += "<" + m_tag + " name=\"" + conf.name +
88                     "\" comment=\"" + Encode12str(conf.comment) +
89                     "\" cost=\"" + x2str(conf.cost) +
90                     "\" payDay=\"" + x2str(conf.payDay) + "\"/>";
91 }
92
93 int ADD_SERVICE::Start(void *, const char * el, const char ** attr)
94 {
95     if (strcasecmp(el, m_tag.c_str()) == 0)
96     {
97         m_name = attr[1];
98         return 0;
99     }
100     return -1;
101 }
102
103 void ADD_SERVICE::CreateAnswer()
104 {
105     SERVICE_CONF conf(m_name);
106     if (m_services.Add(conf, &m_currAdmin) == 0)
107         m_answer = "<" + m_tag + " result=\"Ok\"/>";
108     else
109         m_answer = "<" + m_tag + " result=\"" + m_services.GetStrError() + "\"/>";
110 }
111
112 int DEL_SERVICE::Start(void *, const char * el, const char ** attr)
113 {
114     if (strcasecmp(el, m_tag.c_str()) == 0)
115     {
116         m_name = attr[1];
117         return 0;
118     }
119     return -1;
120 }
121
122 void DEL_SERVICE::CreateAnswer()
123 {
124     if (m_services.Del(m_name, &m_currAdmin) == 0)
125         m_answer = "<" + m_tag + " result=\"Ok\"/>";
126     else
127         m_answer = "<" + m_tag + " result=\"" + m_services.GetStrError() + "\"/>";
128 }
129
130 int CHG_SERVICE::Start(void *, const char * el, const char ** attr)
131 {
132     if (strcasecmp(el, m_tag.c_str()) == 0)
133     {
134         for (size_t i = 0; i < 8; i += 2)
135         {
136             if (attr[i] == NULL)
137                 break;
138
139             if (strcasecmp(attr[i], "name") == 0)
140             {
141                 m_service.name = attr[i + 1];
142                 continue;
143             }
144
145             if (strcasecmp(attr[i], "comment") == 0)
146             {
147                 m_service.comment = Decode21str(attr[i + 1]);
148                 continue;
149             }
150
151             if (strcasecmp(attr[i], "cost") == 0)
152             {
153                 double cost = 0;
154                 if (str2x(attr[i + 1], cost) == 0)
155                     m_service.cost = cost;
156                 else
157                     printfd(__FILE__, "Bad cast from '%s' to double\n", attr[i + 1]);
158                 // TODO: log it
159                 continue;
160             }
161
162             if (strcasecmp(attr[i], "payDay") == 0)
163             {
164                 unsigned payDay;
165                 if (str2x(attr[i + 1], payDay) == 0)
166                     m_service.payDay = payDay;
167                 else
168                     printfd(__FILE__, "Bad cast from '%s' to unsigned\n", attr[i + 1]);
169                 // TODO: log it
170                 continue;
171             }
172         }
173
174         return 0;
175     }
176     return -1;
177 }
178
179 void CHG_SERVICE::CreateAnswer()
180 {
181     if (m_service.name.empty())
182     {
183         m_answer = "<" + m_tag + " result=\"Empty service name.\"/>";
184         return;
185     }
186
187     if (!m_services.Exists(m_service.name.const_data()))
188     {
189         m_answer = "<" + m_tag + " result = \"Service '" + m_service.name.const_data() + "' does not exist.\"/>";
190         return;
191     }
192
193     SERVICE_CONF orig;
194     m_services.Find(m_service.name.const_data(), &orig);
195
196     SERVICE_CONF_RES conf(orig);
197     conf.Splice(m_service);
198
199     if (m_services.Change(conf.GetData(), &m_currAdmin) != 0)
200         m_answer = "<" + m_tag + " result = \"" + m_services.GetStrError() + "\"/>";
201     else
202         m_answer = "<" + m_tag + " result = \"Ok\"/>";
203 }