]> git.stg.codes - stg.git/blob - projects/sgconf_xml/main.cpp
Variable scope reduction.
[stg.git] / projects / sgconf_xml / main.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  */
20
21 /*
22 $Revision: 1.2 $
23 $Author: nobunaga $
24 $Date: 2008/01/05 12:11:34 $
25 */
26
27 #include "stg/common.h"
28 #include "stg/netunit.h"
29 #include "request.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <getopt.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <string.h>
39
40 #define FN_LEN          (512)
41 #define REQ_STR_LEN     (300)
42 char fileName[FN_LEN];
43 char strReq[2048];
44
45 int ParseReply(void * data, list<string> * ans);
46
47 struct option long_options[] = {
48 {"server",      1, 0, 's'},  //Server
49 {"port",        1, 0, 'p'},  //Port
50 {"admin",       1, 0, 'a'},  //Admin
51 {"admin_pass",  1, 0, 'w'},  //passWord
52 {"file",        1, 0, 'f'},  //File
53 {"strreq",      1, 0, 'r'},  //String request
54 {0, 0, 0, 0}};
55
56 //-----------------------------------------------------------------------------
57 int CheckLogin(const char * login)
58 {
59 for (int i = 0; i < (int)strlen(login); i++)
60     {
61     if (!(( login[i] >= 'a' && login[i] <= 'z')
62           || (login[i] >= 'A' && login[i] <= 'Z')
63           || (login[i] >= '0' && login[i] <= '9')
64           ||  login[i] == '_'
65           ||  login[i] == '-'))
66         {
67         return 1;
68         }
69     }
70 return 0;
71 }
72 //-----------------------------------------------------------------------------
73 short int ParseServerPort(const char * p)
74 {
75 int port;
76 if (str2x(p, port) != 0)
77     {
78     printf("Incorresct server port %s\n", p);
79     exit(NETWORK_ERR_CODE);
80     }
81 return(short)port;
82 }
83 //-----------------------------------------------------------------------------
84 char * ParseAdminLogin(char * adm)
85 {
86 if (CheckLogin(adm))
87     {
88     printf("Incorrect admin login %s\n", adm);
89     exit(PARAMETER_PARSING_ERR_CODE);
90     }
91 return adm;
92 }
93 //-----------------------------------------------------------------------------
94 char * ParsePassword(char * pass)
95 {
96 if (strlen(pass) >= ADM_PASSWD_LEN)
97     {
98     printf("Password too big %s\n", pass);
99     exit(PARAMETER_PARSING_ERR_CODE);
100     }
101
102 return pass;
103 }
104 //-----------------------------------------------------------------------------
105 void CreateRequest(REQUEST * req, char * r)
106 {
107 r[0] = 0;
108
109 if (!req->strReq.empty())
110     {
111     char str[10024];
112     sprintf(str, "%s", req->strReq.const_data().c_str());
113     strcat(r, str);
114     return;
115     } else
116     {
117     FILE *f = fopen(fileName, "rt");
118     if (!f)
119         {
120         printf("Can't open request file\n");
121         exit(PARAMETER_PARSING_ERR_CODE);
122         }
123
124     char ts[REQ_STR_LEN];
125     while (fgets(ts, REQ_STR_LEN, f))
126         {
127         strncat(r, ts, REQ_STR_LEN);
128         }
129     fclose(f);
130     }
131 }
132 //-----------------------------------------------------------------------------
133 int Process(REQUEST * r)
134 {
135 int ret;
136 char str[2048];
137
138 NETTRANSACT nt;
139 nt.SetServer(r->server.const_data().c_str());
140 nt.SetServerPort(r->port.const_data());
141 nt.SetLogin(r->admLogin.const_data().c_str());
142 nt.SetPassword(r->admPasswd.const_data().c_str());
143 nt.SetRxCallback(NULL, ParseReply);
144
145 CreateRequest(r, str);
146
147 if ((ret = nt.Connect()) != st_ok)
148     {
149     printf("%s\n", nt.GetError().c_str());
150     return ret;
151     }
152 if ((ret = nt.Transact(str)) != st_ok)
153     {
154     printf("%s\n", nt.GetError().c_str());
155     return ret;
156     }
157 if ((ret = nt.Disconnect()) != st_ok)
158     {
159     printf("%s\n", nt.GetError().c_str());
160     return ret;
161     }
162
163 printf("<!-- Ok -->\n");
164 return 0;
165 }
166 //-----------------------------------------------------------------------------
167 int CheckParameters(REQUEST * req)
168 {
169 int a = !req->admLogin.empty()
170         && !req->admPasswd.empty()
171         && !req->server.empty()
172         && !req->port.empty();
173
174 int b = !req->fileReq.empty()
175         || !req->strReq.empty();
176
177 return a && b;
178 }
179 //-----------------------------------------------------------------------------
180 void Usage()
181 {
182 printf("Sgconf version: 1.05.9_XML\n\n");
183
184 printf("Use: sgconf -s <server> -p <port> -a <admin> -w <admin_pass> -r <request_string>\n");
185 printf("Use: sgconf -s <server> -p <port> -a <admin> -w <admin_pass> -f <request_file>\n\n");
186
187 printf("Request file or string content:\n\n");
188
189 printf("  <GetServerInfo/>\n\n");
190
191 printf("  <GetTariffs/>\n");
192 printf("  <AddTariff name=\"NEW_TARIFF\"/>\n");
193 printf("  <DelTariff name=\"DELETED_TARIFF\"/>\n\n");
194
195 printf("  <SetTariff name=\"TARIFF\"/>\n");
196 printf("    <Time[0...9] value=\"HH:MM-HH:MM\"/>   Day-Night time for each DIR\n");
197 printf("    <PriceDayA value=\"PriceDayA0/PriceDayA1/PriceDayA2/PriceDayA3/PriceDayA4/PriceDayA5/PriceDayA6/PriceDayA7/PriceDayA8/PriceDayA9\"/>\n");
198 printf("    <PriceDayB value=\"PriceDayB0/PriceDayB1/PriceDayB2/PriceDayB3/PriceDayB4/PriceDayB5/PriceDayB6/PriceDayB7/PriceDayB8/PriceDayB9\"/>\n");
199 printf("    <PriceNightA value=\"PriceNightA0/PriceNightA1/PriceNightA2/PriceNightA3/PriceNightA4/PriceNightA5/PriceNightA6/PriceNightA7/PriceNightA8/PriceNightA9\"/>\n");
200 printf("    <PriceNightB value=\"PriceNightB0/PriceNightB1/PriceNightB2/PriceNightB3/PriceNightB4/PriceNightB5/PriceNightB6/PriceNightB7/PriceNightB8/PriceNightB9\"/>\n");
201 printf("    <SinglePrice value=\"SinglePrice0/SinglePrice1/SinglePrice2/SinglePrice3/SinglePrice4/SinglePrice5/SinglePrice6/SinglePrice7/SinglePrice8/SinglePrice9\"/>\n");
202 printf("    <NoDiscount value=\"NoDiscount0/NoDiscount1/NoDiscount2/NoDiscount3/NoDiscount4/NoDiscount5/NoDiscount6/NoDiscount7/NoDiscount8/NoDiscount9\"/>\n");
203 printf("    <Threshold value=\"NEW_Threshold\"/>\n");
204 printf("    <Fee value=\"NEW_Fee\"/>\n");
205 printf("    <PassiveCost value=\"NEW_PassiveCost\"/>\n");
206 printf("    <Free value=\"NEW_Free\"/>\n");
207 printf("    <TraffType value=\"NEW_TraffType\"/>   New TraffType value: [up|down|up+down|max]\n");
208 printf("  </SetTariff/>\n\n");
209
210 printf("  <GetAdmins/>\n");
211 printf("  <AddAdmin login=\"LOGIN\"/>\n");
212 printf("  <DelAdmin login=\"LOGIN\"/>\n");
213 printf("  <ChgAdmin login=\"LOGIN\" priv=\"NEW_PRIV\" password=\"NEW_PASSWORD\"/>\n\n");
214
215 printf("  <GetUsers/>\n");
216 printf("  <GetUser login=\"LOGIN\"/>\n");
217 printf("  <AddUser login=\"LOGIN\"/>\n");
218 printf("  <DelUser login=\"LOGIN\"/>\n");
219 printf("  <CheckUser login=\"LOGIN\" password=\"PASSWORD\"/>   Checking login and password in database. Return Ok or Err.\n\n");
220
221 printf("  <SetUser>\n");
222 printf("    <login value=\"LOGIN\" />\n");
223 printf("    <ip value=\"NEW_IP\" />\n");
224 printf("    <password value=\"NEW_Password\" />\n");
225 printf("    <tariff [ delayed | now ]=\"NEW_Tariff\" />   delayed - change tariff from 1st day of new month; now - change tariff NOW.\n");
226 printf("    <group value=\"NEW_Group\" />   Encode12() -> value\n");
227 printf("    <name value=\"NEW_RealName\" />   Encode12() -> value\n");
228 printf("    <address value=\"NEW_Address\" />   Encode12() -> value\n");
229 printf("    <phone value=\"NEW_Phone\" />   Encode12() -> value\n");
230 printf("    <email value=\"NEW_Email\" />   Encode12() -> value\n");
231 printf("    <note value=\"NEW_Note\" />   Encode12() -> value\n");
232 printf("    <userdata[0...9] value=\"NEW_Userdata[0...9]\" />   Encode12() -> value\n");
233 printf("    <cash [ add | set ]=\"Cash\" msg=\"MESSAGE\" />   add - add money on account; set - set money on account; Message - message for log\n");
234 printf("    <credit value=\"NEW_Credit\" />\n");
235 printf("    <CreditExpire value=\"NEW_CreditExpire\" />\n");
236 printf("    <freemb value=\"NEW_FreeMB\" />\n");
237 printf("    <aonline value=\"AlwaysOnline\" />   1 - turn ON AlwaysOnline; 0 - turn OFF AlwaysOnline\n");
238 printf("    <down value=\"Down\" />   1 - turn ON Down; 0 - turn OFF Down\n");
239 printf("    <passive value=\"Passive\" />   1 - turn ON Passive; 0 - turn OFF Passive\n");
240 printf("    <traff MU[0...9]=\"NEW_MU[0...9]\" MD[0...9]=\"NEW_MD[0...9]\" />   MU[0...9] - Set upload traffic value; MU[0...9] - Set download traffic value; \n");
241 printf("  </SetUser>\n\n");
242
243 printf("  <Message login=\"LOGIN\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"MESSAGE\" />\n");
244 }
245 //---------------------------------------------------------------------------
246 int main(int argc, char **argv)
247 {
248 REQUEST req;
249
250 while (1)
251     {
252     int option_index = -1;
253
254     int c = getopt_long(argc, argv, "s:p:a:w:f:r:", long_options, &option_index);
255     if (c == -1)
256         break;
257
258     switch (c)
259         {
260         case 's': //server
261             req.server = optarg;
262             break;
263
264         case 'p': //port
265             req.port = ParseServerPort(optarg);
266             break;
267
268         case 'a': //admin
269             req.admLogin = ParseAdminLogin(optarg);
270             break;
271
272         case 'w': //admin password
273             req.admPasswd = ParsePassword(optarg);
274             break;
275
276         case 'f': //file
277             strcpy(fileName,optarg);
278             req.fileReq = 1;
279             break;
280
281         case 'r': //string request
282             req.strReq = optarg;
283             break;
284
285         case '?':
286             break;
287
288         default:
289             printf ("?? getopt returned character code 0%o ??\n", c);
290         }
291     }
292
293 if (optind < argc)
294     {
295     printf ("non-option ARGV-elements: ");
296     while (optind < argc)
297         printf ("%s ", argv[optind++]);
298     printf ("\n");
299     exit(PARAMETER_PARSING_ERR_CODE);
300     }
301
302 if (CheckParameters(&req) == 0)
303     {
304     Usage();
305     exit(PARAMETER_PARSING_ERR_CODE);
306     }
307
308 Process(&req);
309
310 return 0;
311 }
312 //-----------------------------------------------------------------------------
313