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