392459b852cfab4c594bc06873f5f2836899e026
[NextIRCd.git] / src / ircd_parse.c
1 /* ircd_client.c - NextIRCd
2  * Copyright (C) 2012-2013  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "tools.h"
19 #include "IOHandler/IOSockets.h"
20
21 typedef int cmd_client_t(struct Client *client, char *argv[], int argc);
22 typedef int cmd_auth_t(struct Auth *auth, char *argv[], int argc);
23 typedef int cmd_server_t(struct Server *server, char *argv[], int argc);
24
25 static struct ServerMsg parse_static_srvmsg;
26
27 #define PARSE_CLIFLAG_OPONLY 0x01
28
29 //include all commands
30 #include "cmd_ping.h"
31
32
33 struct {
34         struct {
35                 const char *client;
36                 const char *server;
37         } tokens;
38         struct {
39                 cmd_client_t *function;
40                 unsigned int min_argc : 8;
41                 unsigned int max_argc : 8;
42                 unsigned int flags : 8;
43         } client;
44         struct {
45                 cmd_auth_t *function;
46                 unsigned int min_argc : 8;
47                 unsigned int max_argc : 8;
48                 unsigned int flags : 8;
49         } auth;
50         struct {
51                 cmd_server_t *function;
52                 unsigned int min_argc : 8;
53                 unsigned int max_argc : 8;
54                 unsigned int flags : 8;
55         } server;
56         
57 } parse_command_list {
58         {{"PING", "P"}, /* Ping Command */
59                 {NULL, 0, 1, 0}, /* Client */
60                 {NULL, 0, 1, 0}, /* Unauthed */
61                 {NULL, 0, 1, 0}  /* Server */
62         },
63         
64         {{NULL, NULL},{NULL, 0, 0, 0},{NULL, 0, 0, 0},{NULL, 0, 0, 0}}
65 };
66
67 static char *parse_irc_token(char **data) {
68         //find next " "
69         int i;
70         char *token = *data;
71         for(i = 0; *data[i]; i++) {
72                 if(*data[i] != ' ') {
73                         *data[i] = '\0';
74                         *data += i+1;
75                         break;
76                 }
77         }
78         return token;
79 }
80
81 static char **parse_irc_params(char *data, int *argc, int maxargs) {
82         if(maxargs == 0) {
83                 *argc = 0;
84                 return NULL;
85         }
86     char **argv = calloc(maxargs, sizeof(*argv));
87     while(*data) {
88         //skip leading spaces
89         while (*line == ' ')
90             *line++ = 0;
91         if (*line == ':') {
92            //the rest is a single parameter
93            argv[*argc++] = line + 1;
94            break;
95         }
96         argv[*argc++] = line;
97         if (argc >= maxargs)
98             break;
99         while (*line != ' ' && *line)
100             line++;
101     }
102     return argv;
103 }
104
105 /* Client Data Format
106 * <TOKEN> <arg1> <arg2>
107 */
108 void parse_client_data(struct Client *client, char *data) {
109     char *token = parse_irc_token(&data);
110         int found = 0;
111         int i;
112         for(i = 0; (parse_command_list[i].tokens.client ||  parse_command_list[i].tokens.server); i++) {
113                 if(stricmp(parse_command_list[i].tokens.client, token)) {
114                         found = 1;
115                         break;
116                 }
117         }
118         if(found && !parse_command_list[i].client.function) {
119                 // reply 462 You may not reregister
120         } else if(found) {
121                 int argc;
122                 char **argv = parse_irc_params(data, &argc, parse_command_list[i].client.max_argc);
123                 if(argc < parse_command_list[i].client.min_argc) {
124                         // reply 461 Not enough parameters
125                 } else {
126                         int ret = parse_command_list[i].client.function(client, argv, argc);
127                         if(ret) {
128                                 // error occurred!
129                                 // do something?
130                         }
131                 }
132                 free(argv);
133         } else {
134                 // reply 421 Unknown command
135         }
136 }
137
138 void parse_unauth_data(struct Auth *auth, char *data) {
139         char *token = parse_irc_token(&data);
140         int found = 0;
141         int i;
142         for(i = 0; (parse_command_list[i].tokens.client ||  parse_command_list[i].tokens.server); i++) {
143                 if(stricmp(parse_command_list[i].tokens.client, token)) {
144                         found = 1;
145                         break;
146                 }
147         }
148         if(found && !parse_command_list[i].auth.function) {
149                 // reply 451 Register first.
150         } else if(found) {
151                 int argc;
152                 char **argv = parse_irc_params(data, &argc, parse_command_list[i].auth.max_argc);
153                 if(argc < parse_command_list[i].auth.min_argc) {
154                         // reply 461 Not enough parameters
155                 } else {
156                         int ret = parse_command_list[i].auth.function(auth, argv, argc);
157                         if(ret) {
158                                 // error occurred!
159                                 // do something?
160                         }
161                 }
162                 free(argv);
163         } else {
164                 // reply 421 Unknown command
165         }
166 }
167
168 /* Unicast                  Multicast                 Broadcast
169 * <src-srvnum> 4 Bytes      <source>     4 Bytes      <source>     4 Bytes
170 * <length>     2 Bytes      <length>     2 Bytes      <length>     2 Bytes
171 * <flags>      1 Byte       <flags>      1 Byte       <flags>      1 Byte
172 * <token>      x Bytes      <token>      x Bytes      <token>      x Bytes
173 * " "          1 Byte       " "          1 Byte       " "          1 Byte
174 * <dst-srvnum> 4 Bytes      <servers>    1 Byte       <ttl>        1 Byte
175 *                           <dst-srv01>  4 Bytes      
176 *                           <dst-srv02>  4 Bytes      
177 * <args>                    <args>                    <args>
178 */
179 void parse_server_data(struct Server *server, struct IOSocketBuffer *buffer) {
180         struct ServerMsg *srvmsg = &parse_static_srvmsg;
181         unsigned char *buf = (unsigned char *)buffer->buffer;
182         unsigned int buflen, require_more = 0;
183         int i;
184         
185         while(buffer->bufpos >= 9 && !require_more) { //require at least 9 Bytes
186                 srvmsg->source.srvnum = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
187                 srvmsg->resolved_source = 0;
188                 srvmsg->msglen = ((buf[4] << 8) | buf[5]);
189                 srvmsg->msgflags = buf[6];
190                 srvmsg->msgtype  = (buf[6] & SERVERMSG_TYPEMASK);
191                 
192                 if(buffer->bufpos < srvmsg->msglen) {
193                         require_more = 1;
194                         break;
195                 }
196                 
197                 for(i = 0; buffer->bufpos > i+7; i++) {
198                         if(buf[i+7] == ' ') {
199                                 if(i >= MAXSERVERTOKENLEN)
200                                         srvmsg->token[MAXSERVERTOKENLEN] = '\0';
201                                 else
202                                         srvmsg->token[i] = '\0';
203                                 break;
204                         }
205                         if(i >= MAXSERVERTOKENLEN)
206                                 continue;
207                         srvmsg->token[i] = buf[i+7];
208                 }
209                 if(buf[i+7] != ' ') {
210                         require_more = 1;
211                         break;
212                 }
213                 buf += i+8;
214                 buflen = buffer->bufpos - (i+8);
215                 
216                 switch(srvmsg->msgtype) {
217                 case SERVERMSG_TYPE_UNICAST:
218                         if(buflen < 4) {
219                                 require_more = 1;
220                                 break;
221                         }
222                         srvmsg->destinations = malloc(sizeof(struct ServerMsgDstMap));
223                         srvmsg->destinations->dstcount = 1;
224                         srvmsg->destinations->dst[0].destination.srvnum = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
225                         srvmsg->destinations->dst[0].destination.resolved_destination = 0;
226                         break;
227                 case SERVERMSG_TYPE_MULTICAST:
228                         if(buflen < 5) {
229                                 require_more = 1;
230                                 break;
231                         }
232                         unsigned int srvcount = buf[0];
233                         buf++;
234                         buflen--;
235                         if(buflen < (srvcount * 4)) {
236                                 require_more = 1;
237                                 break;
238                         }
239                         srvmsg->destinations = malloc(sizeof(struct ServerMsgDstMap) + (sizeof(struct ServerMsgDestination) * (srvcount - 1)));
240                         srvmsg->destinations->dstcount = srvcount;
241                         for(i = 0; i < srvcount; i++) {
242                                 srvmsg->destinations->dst[i].destination.srvnum = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
243                                 srvmsg->destinations->dst[i].destination.resolved_destination = 0;
244                                 buf += 4;
245                                 buflen -= 4;
246                         }
247                         break;
248                 case SERVERMSG_TYPE_BROADCAST:
249                         if(buflen == 0) {
250                                 require_more = 1;
251                                 break;
252                         }
253                         srvmsg->destinations = NULL;
254                         srvmsg->msgttl = buf[0];
255                         buf++;
256                         buflen--;
257                         break;
258                 default:
259                         // PARSE ERROR
260                         goto parse_server_data_finish;
261                 }
262                 
263                 srvmsg->arglen = srvmsg->msglen - (buf - buffer->buffer);
264                 srvmsg->args = buf;
265                 
266                 int found = 0;
267                 for(i = 0; (parse_command_list[i].tokens.client ||  parse_command_list[i].tokens.server); i++) {
268                         if(stricmp(parse_command_list[i].tokens.server, srvmsg->token)) {
269                                 found = 1;
270                                 break;
271                         }
272                 }
273                 if(!found)
274                         goto parse_server_data_finish;
275                 
276                 //exec command function
277                 
278         
279                 parse_server_data_finish:
280                 if(srvmsg->destinations)
281                         free(srvmsg->destinations);
282                 
283                 if(srvmsg->msglen > buffer->bufpos) {
284                         memmove(buffer->buffer, buffer->buffer + srvmsg->msglen, srvmsg->msglen - buffer->bufpos);
285                         buffer->bufpos -= srvmsg->msglen;
286                 } else 
287                         srvmsg->bufpos = 0;
288         }
289 }
290