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