]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp
Split sgconfig parsers into seaparate files.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_message.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_message.h"
23
24 #include "stg/users.h"
25 #include "stg/common.h"
26
27 extern volatile time_t stgTime; // So sad...
28
29 using STG::PARSER::SEND_MESSAGE;
30
31 int SEND_MESSAGE::Start(void *, const char *el, const char **attr)
32 {
33     if (strcasecmp(el, tag.c_str()) != 0)
34         return -1;
35
36     for (size_t i = 0; i < 14; i++)
37         if (attr[i] == NULL)
38         {
39             m_result = res_params_error;
40             CreateAnswer();
41             printfd(__FILE__, "To few parameters\n");
42             return 0;
43         }
44
45     for (size_t i = 0; i < 14; i += 2)
46     {
47         if (strcasecmp(attr[i], "login") == 0)
48             ParseLogins(attr[i + 1]);
49
50         if (strcasecmp(attr[i], "MsgVer") == 0)
51         {
52             str2x(attr[i + 1], m_msg.header.ver);
53             if (m_msg.header.ver != 1)
54                 m_result = res_params_error;
55         }
56
57         if (strcasecmp(attr[i], "MsgType") == 0)
58         {
59             str2x(attr[i + 1], m_msg.header.type);
60             if (m_msg.header.type != 1)
61                 m_result = res_params_error;
62         }
63
64         if (strcasecmp(attr[i], "Repeat") == 0)
65         {
66             str2x(attr[i + 1], m_msg.header.repeat);
67             if (m_msg.header.repeat < 0)
68                 m_result = res_params_error;
69         }
70
71         if (strcasecmp(attr[i], "RepeatPeriod") == 0)
72             str2x(attr[i + 1], m_msg.header.repeatPeriod);
73
74         if (strcasecmp(attr[i], "ShowTime") == 0)
75             str2x(attr[i + 1], m_msg.header.showTime);
76
77         if (strcasecmp(attr[i], "Text") == 0)
78         {
79             Decode21str(m_msg.text, attr[i + 1]);
80             m_result = res_ok;
81         }
82     }
83     return 0;
84 }
85
86 int SEND_MESSAGE::End(void *, const char *el)
87 {
88     if (strcasecmp(el, tag.c_str()) != 0)
89         return -1;
90
91     m_result = res_unknown;
92     for (unsigned i = 0; i < m_logins.size(); i++)
93     {
94         if (m_users.FindByName(m_logins[i], &m_user))
95         {
96             printfd(__FILE__, "User not found. %s\n", m_logins[i].c_str());
97             continue;
98         }
99         m_msg.header.creationTime = static_cast<unsigned int>(stgTime);
100         m_user->AddMessage(&m_msg);
101         m_result = res_ok;
102     }
103     CreateAnswer();
104     return 0;
105 }
106
107 int SEND_MESSAGE::ParseLogins(const char * login)
108 {
109     char * p;
110     char * l = new char[strlen(login) + 1];
111     strcpy(l, login);
112     p = strtok(l, ":");
113     m_logins.clear();
114     while (p)
115     {
116         m_logins.push_back(p);
117         p = strtok(NULL, ":");
118     }
119
120     delete[] l;
121     return 0;
122 }
123
124 void SEND_MESSAGE::CreateAnswer()
125 {
126     switch (m_result)
127     {
128         case res_ok:
129             answer = "<SendMessageResult value=\"ok\"/>";
130             break;
131         case res_params_error:
132             answer = "<SendMessageResult value=\"Parameters error.\"/>";
133             break;
134         case res_unknown:
135             answer = "<SendMessageResult value=\"Unknown user.\"/>";
136             break;
137     }
138 }