added get_userlist function to WHOHandler.c and wrote some test code
[NeonServV5.git] / IRCParser.c
1
2 #include "IRCParser.h"
3 #include "UserNode.h"
4 #include "ChanNode.h"
5 #include "ChanUser.h"
6 #include "IRCEvents.h"
7 #include "ClientSocket.h"
8 #include "WHOHandler.h"
9
10 struct irc_cmd *irc_commands = NULL;
11
12 static void parse_line(struct ClientSocket *client, char *line);
13 static void register_irc_function(char *command, irc_cmd_t *func);
14 static void parse_raw(struct ClientSocket *client, char *from, char *cmd, char **argv, int argc);
15
16 int parse_lines(struct ClientSocket *client, char *lines, int len) {
17     int i, used = 0;
18     char *line = lines;
19     for(i = 0; i < len; i++) {
20         if(lines[i] == '\r') //just zero it out :D
21             lines[i] = 0;
22         if(lines[i] == '\n') {
23             lines[i] = 0;
24             parse_line(client, line);
25             line = lines+(i+1);
26             used = i+1;
27         }
28     }
29     return used;
30 }
31
32 static void parse_line(struct ClientSocket *client, char *line) {
33     int argc = 0;
34     char *argv[MAXNUMPARAMS];
35     printf("[recv %lu] %s\n", (unsigned long) strlen(line), line);
36     if(line[0] == ':')
37         line++;
38     else
39         argv[argc++] = NULL;
40     while(*line) {
41         //skip leading spaces
42         while (*line == ' ')
43             *line++ = 0;
44         if (*line == ':') {
45            //the rest is a single parameter
46            argv[argc++] = line + 1;
47         }
48         argv[argc++] = line;
49         if (argc >= MAXNUMPARAMS)
50             break;
51         while (*line != ' ' && *line)
52             line++;
53     }
54     if(argc >= 2) {
55         parse_raw(client, argv[0], argv[1], argv+2, argc-2);
56     }
57 }
58
59 static void register_irc_function(char *command, irc_cmd_t *func) {
60     struct irc_cmd *irc_cmd = malloc(sizeof(*irc_cmd));
61     if (!irc_cmd)
62     {
63         perror("malloc() failed");
64         return;
65     }
66     irc_cmd->cmd = command;
67     irc_cmd->func = func;
68     irc_cmd->next = irc_commands;
69     irc_commands = irc_cmd;
70 }
71
72 static void parse_raw(struct ClientSocket *client, char *from, char *cmd, char **argv, int argc) {
73     struct irc_cmd *irc_cmd;
74     int ret = 0;
75     for(irc_cmd = irc_commands; irc_cmd; irc_cmd = irc_cmd->next) {
76         if(!stricmp(irc_cmd->cmd, cmd)) {
77             ret = irc_cmd->func(client, from, argv, argc);
78             break;
79         }
80     }
81     if(!ret) {
82         //fprintf(stderr,"PARSE ERROR: %s", cmd); commented out right now - we're still developing and this is very spammy
83     }
84 }
85
86 static USERLIST_CALLBACK(got_channel_userlist) {
87     putsock(client, "PRIVMSG %s :[BOT JOIN] Users on this Channel:", chan->name);
88     struct ChanUser *chanuser;
89     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
90         putsock(client, "PRIVMSG %s :  %s!%s@%s [%s]  rights: %d", chanuser->user->nick, chanuser->user->ident, chanuser->user->host, ((chanuser->user->flags & USERFLAG_ISAUTHED) ? chanuser->user->auth : "0"), chanuser->flags);
91     }
92 }
93
94 static IRC_CMD(raw_001) {
95     client->flags |= SOCKET_FLAG_READY;
96     putsock(client, "JOIN #pktest");
97     return 1;
98 }
99
100 static IRC_CMD(raw_join) {
101     if(from == NULL || argc < 1) return 0;
102     struct UserNode *user = getUserByMask(from);
103     if(user == NULL) {
104         user = addUserMask(from);
105     }
106     struct ChanNode *chan = getChanByName(argv[0]);
107     if(chan == NULL) {
108         chan = addChannel(argv[0]);
109         //request member list
110         addChanUser(chan, user); //it must be a bot
111         get_userlist(chan, got_channel_userlist);
112     } else if(!isUserOnChan(user, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
113         struct ChanUser *chanuser = addChanUser(chan, user);
114         event_join(chanuser);
115     }
116     return 1;
117 }
118
119 static IRC_CMD(raw_part) {
120     if(from == NULL || argc < 2) return 0;
121     struct UserNode *user = getUserByMask(from);
122     if(user == NULL) return 0;
123     struct ChanNode *chan = getChanByName(argv[0]);
124     if(chan == NULL) return 0;
125     if(isUserOnChan(user, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
126         struct ChanUser *chanuser = getChanUser(user, chan);
127         delChanUser(chanuser, 0); //we need to free the chanuser manually!
128         event_part(chanuser, argv[1]);
129         free(chanuser);
130         if(user->flags & USERFLAG_ISBOT) {
131             //check if theres another bot in the channel - otherwise free it
132             
133         }
134     }
135     if(user->channel == NULL && !(user->flags & USERFLAG_ISBOT)) {
136         //remove the user
137         delUser(user, 1);
138     }
139     
140     return 1;
141 }
142
143 static IRC_CMD(raw_quit) {
144     if(from == NULL || argc < 2) return 0;
145     struct UserNode *user = getUserByMask(from);
146     if(user == NULL) return 0;
147     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
148     event_quit(user, argv[1]);
149     if(user->flags & USERFLAG_ISBOT) {
150         //check if there are other bots in the users channel - otherwise free them
151         
152     }
153     delUser(user, 1); //now we fully free the user
154     return 1;
155 }
156
157 static IRC_CMD(raw_kick) {
158     if(from == NULL || argc < 3) return 0;
159     struct UserNode *user = getUserByMask(from);
160     struct UserNode *target = getUserByNick(argv[1]);
161     if(user == NULL || target == NULL) return 0;
162     struct ChanNode *chan = getChanByName(argv[0]);
163     if(chan == NULL) return 0;
164     if(isUserOnChan(target, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
165         struct ChanUser *chanuser = getChanUser(target, chan);
166         delChanUser(chanuser, 0); //we need to free the chanuser manually!
167         event_kick(user, chanuser, argv[1]);
168         free(chanuser);
169         if(target->flags & USERFLAG_ISBOT) {
170             //check if theres another bot in the channel - otherwise free it
171             
172         }
173     }
174     if(target->channel == NULL && !(target->flags & USERFLAG_ISBOT)) {
175         //remove the user
176         delUser(user, 1);
177     }
178     return 1;
179 }
180
181 static IRC_CMD(raw_ping) {
182     if(argc == 0) return 0;
183     putsock(client, "PONG :%s", argv[0]);
184     return 1;
185 }
186
187 static IRC_CMD(raw_354) {
188     recv_whohandler_354(client, argv, argc);
189     return 1;
190 }
191
192 static IRC_CMD(raw_315) {
193     recv_whohandler_315(client, argv, argc);
194     return 1;
195 }
196
197 void parser_init() {
198     //all the raws we receive...
199     register_irc_function("001", raw_001);
200     register_irc_function("354", raw_354);
201     register_irc_function("315", raw_315);
202     register_irc_function("KICK", raw_kick);
203     register_irc_function("JOIN", raw_join);
204     register_irc_function("PART", raw_part);
205     register_irc_function("QUIT", raw_quit);
206     register_irc_function("PING", raw_ping);
207 }