]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp
Split sgconfig parsers into seaparate files.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_users.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_users.h"
23
24 #include "stg/users.h"
25 #include "stg/tariffs.h"
26 #include "stg/user_property.h"
27 #include "stg/user_conf.h"
28 #include "stg/user_stat.h"
29 #include "stg/common.h"
30
31 #include <cstdio>
32
33 using STG::PARSER::GET_USERS;
34 using STG::PARSER::GET_USER;
35 using STG::PARSER::ADD_USER;
36 using STG::PARSER::DEL_USER;
37 using STG::PARSER::CHG_USER;
38 using STG::PARSER::CHECK_USER;
39
40 namespace
41 {
42
43 std::string UserToXML(const USER & user, bool loginInStart, bool showPass, time_t lastTime = 0)
44 {
45     std::string answer;
46
47     if (loginInStart)
48         answer += "<User result=\"ok\">";
49     else
50         answer += "<User result=\"ok\" login=\"" + user.GetLogin() + "\">";
51
52     answer += "<Login value=\"" + user.GetLogin() + "\"/>";
53
54     if (user.GetProperty().password.ModificationTime() > lastTime)
55     {
56         if (showPass)
57             answer += "<Password value=\"" + user.GetProperty().password.Get() + "\" />";
58         else
59             answer += "<Password value=\"++++++\"/>";
60     }
61
62     if (user.GetProperty().cash.ModificationTime() > lastTime)
63         answer += "<Cash value=\"" + x2str(user.GetProperty().cash.Get()) + "\"/>";
64     if (user.GetProperty().freeMb.ModificationTime() > lastTime)
65         answer += "<FreeMb value=\"" + x2str(user.GetProperty().freeMb.Get()) + "\"/>";
66     if (user.GetProperty().credit.ModificationTime() > lastTime)
67         answer += "<Credit value=\"" + x2str(user.GetProperty().credit.Get()) + "\"/>";
68
69     if (user.GetProperty().nextTariff.Get() != "")
70     {
71         if (user.GetProperty().tariffName.ModificationTime() > lastTime ||
72             user.GetProperty().nextTariff.ModificationTime() > lastTime)
73             answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "/" + user.GetProperty().nextTariff.Get() + "\"/>";
74     }
75     else
76     {
77         if (user.GetProperty().tariffName.ModificationTime() > lastTime)
78             answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "\"/>";
79     }
80
81     if (user.GetProperty().note.ModificationTime() > lastTime)
82         answer += "<Note value=\"" + Encode12str(user.GetProperty().note) + "\"/>";
83     if (user.GetProperty().phone.ModificationTime() > lastTime)
84         answer += "<Phone value=\"" + Encode12str(user.GetProperty().phone) + "\"/>";
85     if (user.GetProperty().address.ModificationTime() > lastTime)
86         answer += "<Address value=\"" + Encode12str(user.GetProperty().address) + "\"/>";
87     if (user.GetProperty().email.ModificationTime() > lastTime)
88         answer += "<Email value=\"" + Encode12str(user.GetProperty().email) + "\"/>";
89
90     std::vector<const USER_PROPERTY_LOGGED<std::string> *> userdata;
91     userdata.push_back(user.GetProperty().userdata0.GetPointer());
92     userdata.push_back(user.GetProperty().userdata1.GetPointer());
93     userdata.push_back(user.GetProperty().userdata2.GetPointer());
94     userdata.push_back(user.GetProperty().userdata3.GetPointer());
95     userdata.push_back(user.GetProperty().userdata4.GetPointer());
96     userdata.push_back(user.GetProperty().userdata5.GetPointer());
97     userdata.push_back(user.GetProperty().userdata6.GetPointer());
98     userdata.push_back(user.GetProperty().userdata7.GetPointer());
99     userdata.push_back(user.GetProperty().userdata8.GetPointer());
100     userdata.push_back(user.GetProperty().userdata9.GetPointer());
101
102     for (size_t i = 0; i < userdata.size(); i++)
103         if (userdata[i]->ModificationTime() > lastTime)
104             answer += "<UserData" + x2str(i) + " value=\"" + Encode12str(userdata[i]->Get()) + "\" />";
105
106     if (user.GetProperty().realName.ModificationTime() > lastTime)
107         answer += "<Name value=\"" + Encode12str(user.GetProperty().realName) + "\"/>";
108     if (user.GetProperty().group.ModificationTime() > lastTime)
109         answer += "<Group value=\"" + Encode12str(user.GetProperty().group) + "\"/>";
110     if (user.GetConnectedModificationTime() > lastTime)
111         answer += std::string("<Status value=\"") + (user.GetConnected() ? "1" : "0") + "\"/>";
112     if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime)
113         answer += std::string("<AOnline value=\"") + (user.GetProperty().alwaysOnline.Get() ? "1" : "0") + "\"/>";
114     if (user.GetCurrIPModificationTime() > lastTime)
115         answer += "<CurrIP value=\"" + inet_ntostring(user.GetCurrIP()) + "\"/>";
116     if (user.GetPingTime() > lastTime)
117         answer += "<PingTime value=\"" + x2str(user.GetPingTime()) + "\"/>";
118     if (user.GetProperty().ips.ModificationTime() > lastTime)
119         answer += "<IP value=\"" + user.GetProperty().ips.Get().GetIpStr() + "\"/>";
120
121     answer += "<Traff";
122     const DIR_TRAFF & upload(user.GetProperty().down.Get());
123     const DIR_TRAFF & download(user.GetProperty().up.Get());
124     if (user.GetProperty().up.ModificationTime() > lastTime)
125         for (size_t j = 0; j < DIR_NUM; j++)
126             answer += " MU" + x2str(j) + "=\"" + x2str(upload[j]) + "\"";
127     if (user.GetProperty().down.ModificationTime() > lastTime)
128         for (size_t j = 0; j < DIR_NUM; j++)
129             answer += " MD" + x2str(j) + "=\"" + x2str(download[j]) + "\"";
130     if (user.GetSessionUploadModificationTime() > lastTime)
131         for (size_t j = 0; j < DIR_NUM; j++)
132             answer += " SU" + x2str(j) + "=\"" + x2str(user.GetSessionUpload()[j]) + "\"";
133     if (user.GetSessionDownloadModificationTime() > lastTime)
134         for (size_t j = 0; j < DIR_NUM; j++)
135             answer += " SD" + x2str(j) + "=\"" + x2str(user.GetSessionDownload()[j]) + "\"";
136     answer += "/>";
137
138     if (user.GetProperty().disabled.ModificationTime() > lastTime)
139         answer += std::string("<Down value=\"") + (user.GetProperty().disabled.Get() ? "1" : "0") + "\"/>";
140     if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime)
141         answer += std::string("<DisableDetailStat value=\"") + (user.GetProperty().disabledDetailStat.Get() ? "1" : "0") + "\"/>";
142     if (user.GetProperty().passive.ModificationTime() > lastTime)
143         answer += std::string("<Passive value=\"") + (user.GetProperty().passive.Get() ? "1" : "0") + "\"/>";
144     if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime)
145         answer += "<LastCash value=\"" + x2str(user.GetProperty().lastCashAdd.Get()) + "\"/>";
146     if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime)
147         answer += "<LastTimeCash value=\"" + x2str(user.GetProperty().lastCashAddTime.Get()) + "\"/>";
148     if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime)
149         answer += "<LastActivityTime value=\"" + x2str(user.GetProperty().lastActivityTime.Get()) + "\"/>";
150     if (user.GetProperty().creditExpire.ModificationTime() > lastTime)
151         answer += "<CreditExpire value=\"" + x2str(user.GetProperty().creditExpire.Get()) + "\"/>";
152
153     if (lastTime == 0)
154     {
155         answer += "<AuthorizedBy>";
156         std::vector<std::string> list(user.GetAuthorizers());
157         for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
158             answer += "<Auth name=\"" + *it + "\"/>";
159         answer += "</AuthorizedBy>";
160     }
161
162     answer += "</User>";
163
164     return answer;
165 }
166
167 } // namespace anonymous
168
169 int GET_USER::Start(void *, const char * el, const char ** attr)
170 {
171     if (strcasecmp(el, tag.c_str()) != 0)
172         return -1;
173
174     if (attr[1] == NULL)
175         return -1;
176
177     login = attr[1];
178     return 0;
179 }
180
181 void GET_USER::CreateAnswer()
182 {
183     CONST_USER_PTR u;
184
185     if (m_users.FindByName(login, &u))
186         answer = "<" + tag + " result=\"error\" reason=\"User not found.\"/>";
187     else
188         answer = UserToXML(*u, false, currAdmin.GetPriv()->userConf || currAdmin.GetPriv()->userPasswd);
189 }
190
191 int GET_USERS::Start(void *, const char * el, const char ** attr)
192 {
193     lastUpdateFound = false;
194     if (strcasecmp(el, tag.c_str()) != 0)
195         return -1;
196
197     while (attr && *attr && *(attr + 1))
198     {
199         if (strcasecmp(*attr, "LastUpdate") == 0)
200         {
201             if (str2x(*(attr + 1), lastUserUpdateTime) == 0)
202                 lastUpdateFound = true;
203         }
204         ++attr;
205     }
206
207     return 0;
208 }
209
210 void GET_USERS::CreateAnswer()
211 {
212     int h = m_users.OpenSearch();
213     if (!h)
214     {
215         printfd(__FILE__, "m_users.OpenSearch() error\n");
216         return;
217     }
218
219     if (lastUpdateFound)
220         answer = "<" + tag + " LastUpdate=\"" + x2str(time(NULL)) + "\">";
221     else
222         answer = GetOpenTag();
223
224     USER_PTR u;
225
226     while (m_users.SearchNext(h, &u) == 0)
227         answer += UserToXML(*u, true, currAdmin.GetPriv()->userConf || currAdmin.GetPriv()->userPasswd, lastUserUpdateTime);
228
229     m_users.CloseSearch(h);
230
231     answer += GetCloseTag();
232 }
233
234 int ADD_USER::Start(void *, const char * el, const char ** attr)
235 {
236     depth++;
237
238     if (depth == 1)
239     {
240         if (strcasecmp(el, tag.c_str()) == 0)
241             return 0;
242     }
243     else
244     {
245         if (strcasecmp(el, "login") == 0)
246         {
247             login = attr[1];
248             return 0;
249         }
250     }
251     return -1;
252 }
253
254 int ADD_USER::End(void *, const char *el)
255 {
256     if (depth == 1)
257     {
258         if (strcasecmp(el, tag.c_str()) == 0)
259             return -1;
260         CreateAnswer();
261     }
262
263     --depth;
264     return 0;
265 }
266
267 void ADD_USER::CreateAnswer()
268 {
269     if (CheckUserData() == 0)
270         answer = "<" + tag + " result=\"ok\"/>";
271     else
272         answer = "<" + tag + " result=\"error\" reason=\"Access denied\"/>";
273 }
274
275 int ADD_USER::CheckUserData()
276 {
277     USER_PTR u;
278     if (m_users.FindByName(login, &u))
279         return m_users.Add(login, &currAdmin);
280
281     return -1;
282 }
283
284 CHG_USER::CHG_USER(const ADMIN & admin, USERS & users, const TARIFFS & tariffs)
285     : BASE_PARSER(admin, "SetUser"),
286       m_users(users),
287       m_tariffs(tariffs),
288       usr(new USER_STAT_RES),
289       ucr(new USER_CONF_RES),
290       upr(new RESETABLE<uint64_t>[DIR_NUM]),
291       downr(new RESETABLE<uint64_t>[DIR_NUM]),
292       cashMustBeAdded(false),
293       res(0)
294 {
295 }
296
297 CHG_USER::~CHG_USER()
298 {
299     delete usr;
300     delete ucr;
301     delete[] upr;
302     delete[] downr;
303 }
304
305 int CHG_USER::Start(void *, const char * el, const char ** attr)
306 {
307     depth++;
308
309     if (depth == 1)
310     {
311         if (strcasecmp(el, tag.c_str()) == 0)
312             return 0;
313     }
314     else
315     {
316         if (strcasecmp(el, "login") == 0)
317         {
318             login = attr[1];
319             return 0;
320         }
321
322         if (strcasecmp(el, "ip") == 0)
323         {
324             try
325             {
326                 ucr->ips = StrToIPS(attr[1]);
327             }
328             catch (...)
329             {
330                 printfd(__FILE__, "StrToIPS Error!\n");
331             }
332         }
333
334         if (strcasecmp(el, "password") == 0)
335         {
336             ucr->password = attr[1];
337             return 0;
338         }
339
340         if (strcasecmp(el, "address") == 0)
341         {
342             ucr->address = Decode21str(attr[1]);
343             return 0;
344         }
345
346         if (strcasecmp(el, "aonline") == 0)
347         {
348             ucr->alwaysOnline = (*(attr[1]) != '0');
349             return 0;
350         }
351
352         if (strcasecmp(el, "cash") == 0)
353         {
354             if (attr[2] && (strcasecmp(attr[2], "msg") == 0))
355                 cashMsg = Decode21str(attr[3]);
356
357             double cash;
358             if (strtodouble2(attr[1], cash) == 0)
359                 usr->cash = cash;
360
361             if (strcasecmp(attr[0], "set") == 0)
362                 cashMustBeAdded = false;
363
364             if (strcasecmp(attr[0], "add") == 0)
365                 cashMustBeAdded = true;
366
367             return 0;
368         }
369
370         if (strcasecmp(el, "CreditExpire") == 0)
371         {
372             long int creditExpire = 0;
373             if (str2x(attr[1], creditExpire) == 0)
374                 ucr->creditExpire = (time_t)creditExpire;
375
376             return 0;
377         }
378
379         if (strcasecmp(el, "credit") == 0)
380         {
381             double credit;
382             if (strtodouble2(attr[1], credit) == 0)
383                 ucr->credit = credit;
384             return 0;
385         }
386
387         if (strcasecmp(el, "freemb") == 0)
388         {
389             double freeMb;
390             if (strtodouble2(attr[1], freeMb) == 0)
391                 usr->freeMb = freeMb;
392             return 0;
393         }
394
395         if (strcasecmp(el, "down") == 0)
396         {
397             int down = 0;
398             if (str2x(attr[1], down) == 0)
399                 ucr->disabled = down;
400             return 0;
401         }
402
403         if (strcasecmp(el, "DisableDetailStat") == 0)
404         {
405             int disabledDetailStat = 0;
406             if (str2x(attr[1], disabledDetailStat) == 0)
407                 ucr->disabledDetailStat = disabledDetailStat;
408             return 0;
409         }
410
411         if (strcasecmp(el, "email") == 0)
412         {
413             ucr->email = Decode21str(attr[1]);
414             return 0;
415         }
416
417         for (int i = 0; i < USERDATA_NUM; i++)
418         {
419             char name[15];
420             sprintf(name, "userdata%d", i);
421             if (strcasecmp(el, name) == 0)
422             {
423                 ucr->userdata[i] = Decode21str(attr[1]);
424                 return 0;
425             }
426         }
427
428         if (strcasecmp(el, "group") == 0)
429         {
430             ucr->group = Decode21str(attr[1]);
431             return 0;
432         }
433
434         if (strcasecmp(el, "note") == 0)
435         {
436             ucr->note = Decode21str(attr[1]);
437             return 0;
438         }
439
440         if (strcasecmp(el, "passive") == 0)
441         {
442             int passive = 0;
443             if (str2x(attr[1], passive) == 0)
444                 ucr->passive = passive;
445             return 0;
446         }
447
448         if (strcasecmp(el, "phone") == 0)
449         {
450             ucr->phone = Decode21str(attr[1]);
451             return 0;
452         }
453
454         if (strcasecmp(el, "Name") == 0)
455         {
456             ucr->realName = Decode21str(attr[1]);
457             return 0;
458         }
459
460         if (strcasecmp(el, "traff") == 0)
461         {
462             int j = 0;
463             while (attr[j])
464             {
465                 int dir = attr[j][2] - '0';
466
467                 if (strncasecmp(attr[j], "md", 2) == 0)
468                 {
469                     uint64_t t = 0;
470                     str2x(attr[j + 1], t);
471                     downr[dir] = t;
472                 }
473                 if (strncasecmp(attr[j], "mu", 2) == 0)
474                 {
475                     uint64_t t = 0;
476                     str2x(attr[j + 1], t);
477                     upr[dir] = t;
478                 }
479                 j += 2;
480             }
481             return 0;
482         }
483
484         if (strcasecmp(el, "tariff") == 0)
485         {
486             if (strcasecmp(attr[0], "now") == 0)
487                 ucr->tariffName = attr[1];
488
489             if (strcasecmp(attr[0], "delayed") == 0)
490                 ucr->nextTariff = attr[1];
491
492             return 0;
493         }
494     }
495     return -1;
496 }
497
498 int CHG_USER::End(void *, const char *el)
499 {
500     if (depth == 1)
501     {
502         if (strcasecmp(el, tag.c_str()) != 0)
503             return -1;
504
505         ApplyChanges();
506         CreateAnswer();
507     }
508
509     depth--;
510     return 0;
511 }
512
513 void CHG_USER::CreateAnswer()
514 {
515     if (res == 0)
516         answer = "<" + tag + " result=\"ok\"/>";
517     else
518         answer = "<" + tag + " result=\"error\"/>";
519 }
520
521 int CHG_USER::ApplyChanges()
522 {
523     printfd(__FILE__, "PARSER_CHG_USER::ApplyChanges()\n");
524     USER_PTR u;
525
526     res = 0;
527     if (m_users.FindByName(login, &u))
528     {
529         res = -1;
530         return -1;
531     }
532
533     bool check = false;
534     bool alwaysOnline = u->GetProperty().alwaysOnline;
535     if (!ucr->alwaysOnline.empty())
536     {
537         check = true;
538         alwaysOnline = ucr->alwaysOnline.const_data();
539     }
540     bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP();
541     if (!ucr->ips.empty())
542     {
543         check = true;
544         onlyOneIP = ucr->ips.const_data().OnlyOneIP();
545     }
546
547     if (check && alwaysOnline && !onlyOneIP)
548     {
549         printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
550         GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", currAdmin.GetLogStr().c_str());
551         res = -1;
552         return -1;
553     }
554
555     for (size_t i = 0; i < ucr->ips.const_data().Count(); ++i)
556     {
557         CONST_USER_PTR user;
558         uint32_t ip = ucr->ips.const_data().operator[](i).ip;
559         if (m_users.IsIPInUse(ip, login, &user))
560         {
561             printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str());
562             GetStgLogger()("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", currAdmin.GetLogStr().c_str(), inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str());
563             res = -1;
564             return -1;
565         }
566     }
567
568     if (!ucr->ips.empty())
569         if (!u->GetProperty().ips.Set(ucr->ips.const_data(), &currAdmin, login, store))
570             res = -1;
571
572     if (!ucr->alwaysOnline.empty())
573         if (!u->GetProperty().alwaysOnline.Set(ucr->alwaysOnline.const_data(),
574                                           &currAdmin, login, store))
575             res = -1;
576
577     if (!ucr->address.empty())
578         if (!u->GetProperty().address.Set(ucr->address.const_data(), &currAdmin, login, store))
579             res = -1;
580
581     if (!ucr->creditExpire.empty())
582         if (!u->GetProperty().creditExpire.Set(ucr->creditExpire.const_data(),
583                                           &currAdmin, login, store))
584             res = -1;
585
586     if (!ucr->credit.empty())
587         if (!u->GetProperty().credit.Set(ucr->credit.const_data(), &currAdmin, login, store))
588             res = -1;
589
590     if (!usr->freeMb.empty())
591         if (!u->GetProperty().freeMb.Set(usr->freeMb.const_data(), &currAdmin, login, store))
592             res = -1;
593
594     if (!ucr->disabled.empty())
595         if (!u->GetProperty().disabled.Set(ucr->disabled.const_data(), &currAdmin, login, store))
596             res = -1;
597
598     if (!ucr->disabledDetailStat.empty())
599         if (!u->GetProperty().disabledDetailStat.Set(ucr->disabledDetailStat.const_data(), &currAdmin, login, store))
600             res = -1;
601
602     if (!ucr->email.empty())
603         if (!u->GetProperty().email.Set(ucr->email.const_data(), &currAdmin, login, store))
604             res = -1;
605
606     if (!ucr->group.empty())
607         if (!u->GetProperty().group.Set(ucr->group.const_data(), &currAdmin, login, store))
608             res = -1;
609
610     if (!ucr->note.empty())
611         if (!u->GetProperty().note.Set(ucr->note.const_data(), &currAdmin, login, store))
612             res = -1;
613
614     std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
615     userdata.push_back(u->GetProperty().userdata0.GetPointer());
616     userdata.push_back(u->GetProperty().userdata1.GetPointer());
617     userdata.push_back(u->GetProperty().userdata2.GetPointer());
618     userdata.push_back(u->GetProperty().userdata3.GetPointer());
619     userdata.push_back(u->GetProperty().userdata4.GetPointer());
620     userdata.push_back(u->GetProperty().userdata5.GetPointer());
621     userdata.push_back(u->GetProperty().userdata6.GetPointer());
622     userdata.push_back(u->GetProperty().userdata7.GetPointer());
623     userdata.push_back(u->GetProperty().userdata8.GetPointer());
624     userdata.push_back(u->GetProperty().userdata9.GetPointer());
625
626     for (int i = 0; i < (int)userdata.size(); i++)
627         if (!ucr->userdata[i].empty())
628             if(!userdata[i]->Set(ucr->userdata[i].const_data(), &currAdmin, login, store))
629                 res = -1;
630
631     if (!ucr->passive.empty())
632         if (!u->GetProperty().passive.Set(ucr->passive.const_data(), &currAdmin, login, store))
633             res = -1;
634
635     if (!ucr->password.empty())
636         if (!u->GetProperty().password.Set(ucr->password.const_data(), &currAdmin, login, store))
637             res = -1;
638
639     if (!ucr->phone.empty())
640         if (!u->GetProperty().phone.Set(ucr->phone.const_data(), &currAdmin, login, store))
641             res = -1;
642
643     if (!ucr->realName.empty())
644         if (!u->GetProperty().realName.Set(ucr->realName.const_data(), &currAdmin, login, store))
645             res = -1;
646
647
648     if (!usr->cash.empty())
649         if (cashMustBeAdded)
650         {
651             if (!u->GetProperty().cash.Set(usr->cash.const_data() + u->GetProperty().cash,
652                                            &currAdmin,
653                                            login,
654                                            store,
655                                            cashMsg))
656                 res = -1;
657             else
658                 if (!u->GetProperty().cash.Set(usr->cash.const_data(), &currAdmin, login, store, cashMsg))
659                     res = -1;
660         }
661
662
663     if (!ucr->tariffName.empty())
664     {
665         if (m_tariffs.FindByName(ucr->tariffName.const_data()))
666         {
667             if (!u->GetProperty().tariffName.Set(ucr->tariffName.const_data(), &currAdmin, login, store))
668                 res = -1;
669             u->ResetNextTariff();
670         }
671         else
672         {
673             //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
674             res = -1;
675         }
676     }
677
678     if (!ucr->nextTariff.empty())
679     {
680         if (m_tariffs.FindByName(ucr->nextTariff.const_data()))
681         {
682             if (!u->GetProperty().nextTariff.Set(ucr->nextTariff.const_data(), &currAdmin, login, store))
683                 res = -1;
684         }
685         else
686         {
687             //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
688             res = -1;
689         }
690     }
691
692     DIR_TRAFF up = u->GetProperty().up;
693     DIR_TRAFF down = u->GetProperty().down;
694     int upCount = 0;
695     int downCount = 0;
696     for (int i = 0; i < DIR_NUM; i++)
697     {
698         if (!upr[i].empty())
699         {
700             up[i] = upr[i].data();
701             upCount++;
702         }
703         if (!downr[i].empty())
704         {
705             down[i] = downr[i].data();
706             downCount++;
707         }
708     }
709
710     if (upCount)
711         if (!u->GetProperty().up.Set(up, &currAdmin, login, store))
712             res = -1;
713
714     if (downCount)
715         if (!u->GetProperty().down.Set(down, &currAdmin, login, store))
716             res = -1;
717
718     u->WriteConf();
719     u->WriteStat();
720
721     return 0;
722 }
723
724 int DEL_USER::Start(void *, const char *el, const char **attr)
725 {
726     res = 0;
727     if (strcasecmp(el, tag.c_str()) == 0)
728     {
729         if (attr[0] == NULL || attr[1] == NULL)
730         {
731             //CreateAnswer("Parameters error!");
732             CreateAnswer();
733             return 0;
734         }
735
736         if (m_users.FindByName(attr[1], &u))
737         {
738             res = 1;
739             CreateAnswer();
740             return 0;
741         }
742         CreateAnswer();
743         return 0;
744     }
745     return -1;
746 }
747
748 int DEL_USER::End(void *, const char *el)
749 {
750     if (strcasecmp(el, tag.c_str()) == 0)
751     {
752         if (!res)
753             m_users.Del(u->GetLogin(), &currAdmin);
754
755         return 0;
756     }
757     return -1;
758 }
759
760 void DEL_USER::CreateAnswer()
761 {
762     if (res)
763         answer = "<" + tag + " value=\"error\" reason=\"User not found\"/>";
764     else
765         answer = "<" + tag + " value=\"ok\"/>";
766 }
767
768 int CHECK_USER::Start(void *, const char *el, const char **attr)
769 {
770     if (strcasecmp(el, tag.c_str()) == 0)
771     {
772         if (attr[0] == NULL || attr[1] == NULL ||
773             attr[2] == NULL || attr[3] == NULL)
774         {
775             CreateAnswer("Invalid parameters.");
776             printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
777             return 0;
778         }
779
780         CONST_USER_PTR user;
781         if (m_users.FindByName(attr[1], &user))
782         {
783             CreateAnswer("User not found.");
784             printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
785             return 0;
786         }
787
788         if (strcmp(user->GetProperty().password.Get().c_str(), attr[3]))
789         {
790             CreateAnswer("Wrong password.");
791             printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
792             return 0;
793         }
794
795         CreateAnswer(NULL);
796         return 0;
797     }
798     return -1;
799 }
800
801 int CHECK_USER::End(void *, const char *el)
802 {
803     if (strcasecmp(el, tag.c_str()) == 0)
804         return 0;
805     return -1;
806 }
807
808 void CHECK_USER::CreateAnswer(const char * error)
809 {
810     if (error)
811         answer = "<" + tag + " value=\"Err\" reason=\"" + error + "\"/>";
812     else
813         answer = "<" + tag + " value=\"Ok\"/>";
814 }