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