1 /* IRCParser.c - NeonServ v5.2
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "IRCParser.h"
22 #include "IRCEvents.h"
23 #include "ClientSocket.h"
24 #include "WHOHandler.h"
30 struct irc_cmd *irc_commands = NULL;
32 static void parse_line(struct ClientSocket *client, char *line);
33 static void register_irc_function(char *command, irc_cmd_t *func);
34 static void parse_raw(struct ClientSocket *client, char *from, char *cmd, char **argv, int argc);
36 int parse_lines(struct ClientSocket *client, char *lines, int len) {
39 for(i = 0; i < len; i++) {
40 if(lines[i] == '\r') //just zero it out :D
42 if(lines[i] == '\n') {
44 parse_line(client, line);
52 static void parse_line(struct ClientSocket *client, char *line) {
54 char *argv[MAXNUMPARAMS];
55 printf("[recv %lu] %s\n", (unsigned long) strlen(line), line);
65 //the rest is a single parameter
66 argv[argc++] = line + 1;
70 if (argc >= MAXNUMPARAMS)
72 while (*line != ' ' && *line)
76 parse_raw(client, argv[0], argv[1], argv+2, argc-2);
80 static void register_irc_function(char *command, irc_cmd_t *func) {
81 struct irc_cmd *irc_cmd = malloc(sizeof(*irc_cmd));
84 perror("malloc() failed");
87 irc_cmd->cmd = command;
89 irc_cmd->next = irc_commands;
90 irc_commands = irc_cmd;
93 static void parse_raw(struct ClientSocket *client, char *from, char *cmd, char **argv, int argc) {
94 struct irc_cmd *irc_cmd;
96 for(irc_cmd = irc_commands; irc_cmd; irc_cmd = irc_cmd->next) {
97 if(!stricmp(irc_cmd->cmd, cmd)) {
98 ret = irc_cmd->func(client, from, argv, argc);
103 event_raw(client, from, cmd, argv, argc);
105 fprintf(stderr,"PARSE ERROR: %s\n", cmd);
109 static USERLIST_CALLBACK(got_channel_userlist) {
110 struct ChanUser *chanuser = data;
111 event_join(chanuser);
114 static IRC_CMD(raw_001) {
115 client->flags |= SOCKET_FLAG_READY;
116 event_bot_ready(client);
120 static int is_firstBotSeeUser(struct ClientSocket *client, struct UserNode *user) {
121 struct ClientSocket *bot, *pref_bot = NULL, *unpref_bot = NULL;
122 struct ChanUser *chanuser;
124 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
126 for(chanuser = getUserChannels(bot->user, NULL); chanuser; chanuser = getUserChannels(bot->user, chanuser)) {
127 if(isUserOnChan(user, chanuser->chan)) {
133 if(bot->flags & SOCKET_FLAG_PREFERRED) {
139 bot = (pref_bot ? pref_bot : unpref_bot);
146 static IRC_CMD(raw_join) {
147 if(from == NULL || argc < 1) return 0;
148 struct UserNode *user = getUserByMask(from);
149 struct ChanNode *chan = getChanByName(argv[0]);
150 if(!chan && !(user->flags & USERFLAG_ISBOT)) return 0;
151 if(chan && chan->chanbot != client->user) return 1; //we ignore it - but it's not a parse error
153 user = addUserMask(from);
156 chan = addChannel(argv[0]);
157 //request member list
158 chan->chanbot = user;
159 struct ChanUser *chanuser = addChanUser(chan, user); //it must be a bot
160 get_userlist(chan, got_channel_userlist, chanuser);
161 putsock(client, "MODE %s", chan->name);
162 putsock(client, "MODE %s +b", chan->name);
163 } else if(!isUserOnChan(user, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
164 struct ChanUser *chanuser = addChanUser(chan, user);
165 event_join(chanuser);
170 static IRC_CMD(raw_part) {
171 if(from == NULL || argc < 1) return 0;
172 struct UserNode *user = getUserByMask(from);
173 if(user == NULL) return 0;
174 struct ChanNode *chan = getChanByName(argv[0]);
175 if(chan == NULL) return 0;
176 if(chan->chanbot != client->user) return 1; //we ignore it - but it's not a parse error
177 if(isUserOnChan(user, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
178 struct ChanUser *chanuser = getChanUser(user, chan);
179 delChanUser(chanuser, 0); //we need to free the chanuser manually!
180 event_part(chanuser, (argc > 1 ? argv[1] : NULL));
181 freeChanUser(chanuser);
182 if(chan->chanbot == user) {
183 //check if theres another bot in the channel - otherwise free it
184 checkChannelVisibility(chan);
187 if(user->channel == NULL && !(user->flags & USERFLAG_ISBOT)) {
194 static IRC_CMD(raw_quit) {
195 if(from == NULL || argc < 1) return 0;
196 struct UserNode *user = getUserByMask(from);
197 if(user == NULL) return 0;
198 if(!is_firstBotSeeUser(client, user)) return 1; //we ignore it - but it's not a parse error
199 delUser(user, 0); //a little bit crazy, but we want to delete the user on the channel's userlists - but not the users channel list
200 event_quit(user, argv[0]);
201 if(user->flags & USERFLAG_ISBOT) {
202 //check if there are other bots in the users channel - otherwise free them
203 struct ChanUser *chanuser, *next;
204 for(chanuser = getUserChannels(user, NULL); chanuser; chanuser = next) {
205 next = getUserChannels(user, chanuser);
206 if(chanuser->chan->chanbot == user)
207 checkChannelVisibility(chanuser->chan);
209 //search the user representing the bot in the world of IRC
210 struct ClientSocket *bot;
211 for(bot = getBots(0, NULL); bot; bot = getBots(0, bot)) {
212 if(bot->user == user) {
218 delUser(user, 1); //now we fully free the user
222 void bot_disconnect(struct ClientSocket *client) {
223 struct UserNode *user = client->user;
224 struct ChanUser *chanuser, *next;
227 event_quit(user, "disconnected");
228 for(chanuser = getUserChannels(user, NULL); chanuser; chanuser = next) {
229 next = getUserChannels(user, chanuser);
230 if(chanuser->chan->chanbot == user)
231 checkChannelVisibility(chanuser->chan);
232 freeChanUser(chanuser);
234 user->channel = NULL;
235 delUser(user, 1); //now we fully free the user
240 static IRC_CMD(raw_kick) {
241 if(from == NULL || argc < 3) return 0;
242 struct UserNode *user = getUserByMask(from);
243 struct UserNode *target = getUserByNick(argv[1]);
244 struct ChanNode *chan = getChanByName(argv[0]);
245 if(chan == NULL || target == NULL) return 0;
246 if(chan->chanbot != client->user) return 1; //we ignore it - but it's not a parse error
247 if(isUserOnChan(target, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
249 user = createTempUser(from);
250 user->flags |= USERFLAG_ISTMPUSER;
252 struct ChanUser *chanuser = getChanUser(target, chan);
253 delChanUser(chanuser, 0); //we need to free the chanuser manually!
254 event_kick(user, chanuser, argv[1]);
255 freeChanUser(chanuser);
256 if(chanuser->chan->chanbot == user) {
257 //check if theres another bot in the channel - otherwise free it
258 checkChannelVisibility(chan);
261 if(target->channel == NULL && !(target->flags & USERFLAG_ISBOT)) {
268 static IRC_CMD(raw_topic) {
269 if(from == NULL || argc < 2) return 0;
270 struct UserNode *user = getUserByMask(from);
271 struct ChanNode *chan = getChanByName(argv[0]);
272 if(chan == NULL) return 0;
273 if(chan->chanbot != client->user) return 1; //just ignore it to prevent event duplicates
275 user = createTempUser(from);
276 user->flags |= USERFLAG_ISTMPUSER;
278 event_topic(user, chan, argv[1]);
279 strcpy(chan->topic, argv[1]);
283 static IRC_CMD(raw_privmsg) {
284 if(from == NULL || argc < 2) return 0;
285 struct UserNode *user = getUserByMask(from);
287 user = createTempUser(from);
288 user->flags |= USERFLAG_ISTMPUSER;
290 if(argv[0][0] == '#') { //Channel message
291 struct ChanNode *chan = getChanByName(argv[0]);
292 if(chan && chan->chanbot == client->user) {
293 if(argv[1][0] == '\001') {
294 char *cmd = &argv[1][1];
295 char *text = strstr(cmd, " ");
299 if(strlen(text) && text[strlen(text)-1] == '\001')
300 text[strlen(text)-1] = '\0';
301 } else if(strlen(cmd) && cmd[strlen(cmd)-1] == '\001')
302 cmd[strlen(cmd)-1] = '\0';
303 event_chanctcp(user, chan, cmd, text);
305 event_chanmsg(user, chan, argv[1]);
308 struct UserNode *target = getUserByNick(argv[0]);
310 if(argv[1][0] == '\001') {
311 char *cmd = &argv[1][1];
312 char *text = strstr(cmd, " ");
316 if(strlen(text) && text[strlen(text)-1] == '\001')
317 text[strlen(text)-1] = '\0';
318 } else if(strlen(cmd) && cmd[strlen(cmd)-1] == '\001')
319 cmd[strlen(cmd)-1] = '\0';
320 event_privctcp(user, target, cmd, text);
322 event_privmsg(user, target, argv[1]);
328 static IRC_CMD(raw_notice) {
329 if(from == NULL || argc < 2) return 0;
330 struct UserNode *user = getUserByMask(from);
332 user = createTempUser(from);
333 user->flags |= USERFLAG_ISTMPUSER;
335 if(argv[0][0] == '#') { //Channel notice
336 struct ChanNode *chan = getChanByName(argv[0]);
337 if(chan && chan->chanbot == client->user)
338 event_channotice(user, chan, argv[1]);
340 struct UserNode *target = getUserByNick(argv[0]);
342 event_privnotice(user, target, argv[1]);
347 static IRC_CMD(raw_nick) {
348 if(from == NULL || argc == 0) return 0;
349 struct UserNode *user = getUserByMask(from);
350 if(user == NULL) return 0;
351 if(!is_firstBotSeeUser(client, user)) return 1; //we ignore it - but it's not a parse error
352 event_nick(user, argv[0]);
353 renameUser(user, argv[0]);
357 static IRC_CMD(raw_ping) {
358 if(argc == 0) return 0;
359 putsock(client, "PONG :%s", argv[0]);
363 static IRC_CMD(raw_354) {
364 recv_whohandler_354(client, argv, argc);
368 static IRC_CMD(raw_315) {
369 recv_whohandler_315(client, argv, argc);
373 static IRC_CMD(raw_324) { //MODE LIST
374 //Watchcat #pktest +stnzN
375 if(from == NULL || argc < 3) return 0;
376 struct ChanNode *chan = getChanByName(argv[1]);
377 if(chan == NULL) return 0;
378 parseModes(chan->modes, argv[2], argv+3, argc-3);
382 static IRC_CMD(raw_invite) {
383 if(from == NULL || argc < 2) return 0;
384 struct UserNode *user = getUserByMask(from);
386 user = createTempUser(from);
387 user->flags |= USERFLAG_ISTMPUSER;
389 event_invite(client, user, argv[1]);
393 static IRC_CMD(raw_mode) {
394 if(from == NULL || argc < 2) return 0;
395 struct UserNode *user = getUserByMask(from);
397 user = createTempUser(from);
398 user->flags |= USERFLAG_ISTMPUSER;
400 if(argv[0][0] == '#') {
402 struct ChanNode *chan = getChanByName(argv[0]);
404 if(chan->chanbot != client->user) return 1;
405 event_mode(user, chan, argv[1], argv+2, argc-2);
406 parseModes(chan->modes, argv[1], argv+2, argc-2);
413 static IRC_CMD(raw_367) {
414 //Watchcat #pktest pk911!*@* TestBot!~bot@pktest.user.WebGamesNet 1315863279
415 struct ChanNode *chan = getChanByName(argv[1]);
418 while((ban = getMatchingChannelBan(chan, argv[2]))) {
419 removeChannelBan(ban);
421 addChannelBan(chan, argv[2]);
426 //all the raws we receive...
427 register_irc_function("001", raw_001);
428 register_irc_function("324", raw_324);
429 register_irc_function("367", raw_367);
430 register_irc_function("INVITE", raw_invite);
431 register_irc_function("NOTICE", raw_notice);
432 register_irc_function("TOPIC", raw_topic);
433 register_irc_function("KICK", raw_kick);
434 register_irc_function("PART", raw_part);
435 register_irc_function("QUIT", raw_quit);
436 register_irc_function("JOIN", raw_join);
437 register_irc_function("MODE", raw_mode);
438 register_irc_function("NICK", raw_nick);
439 register_irc_function("354", raw_354);
440 register_irc_function("315", raw_315);
441 register_irc_function("PING", raw_ping);
442 register_irc_function("PRIVMSG", raw_privmsg);
446 struct irc_cmd *cmd, *next;
447 for(cmd = irc_commands; cmd; cmd = next) {
453 void reply(struct ClientSocket *client, struct UserNode *user, const char *text, ...) {
454 const char *reply_format = get_language_string(user, text);
455 if(reply_format == NULL)
457 loadUserSettings(user);
458 char formatBuf[MAXLEN];
459 sprintf(formatBuf, "%s %s :%s", ((user->flags & USERFLAG_REPLY_PRIVMSG) ? "PRIVMSG" : "NOTICE"), user->nick, reply_format);
461 char sendBuf[MAXLEN];
463 if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
465 va_start(arg_list, text);
466 pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
468 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
470 sendBuf[pos+1] = '\0';
471 write_socket(client, sendBuf, pos+1);
474 char* merge_argv(char **argv, int start, int end) {
475 return merge_argv_char(argv, start, end, ' ');
478 char* merge_argv_char(char **argv, int start, int end, char seperator) {
481 for(i = start; i < end; i++) {