added WHOHandler.c without any functions
[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     chan->flags |= CHANFLAG_RECEIVED_USERLIST;
88 }
89
90 static IRC_CMD(raw_001) {
91     client->flags |= SOCKET_FLAG_READY;
92     putsock(client, "JOIN #pktest");
93     return 1;
94 }
95
96 static IRC_CMD(raw_join) {
97     if(from == NULL || argc < 1) return 0;
98     struct UserNode *user = getUserByMask(from);
99     if(user == NULL) {
100         user = addUserMask(from);
101     }
102     struct ChanNode *chan = getChanByName(argv[0]);
103     if(chan == NULL) {
104         chan = addChannel(argv[0]);
105         //request member list
106         get_userlist(chan, got_channel_userlist);
107     } else if(!isUserOnChan(user, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
108         struct ChanUser *chanuser = addChanUser(chan, user);
109         event_join(chanuser);
110     }
111     return 1;
112 }
113
114 static IRC_CMD(raw_part) {
115     if(from == NULL || argc < 2) return 0;
116     struct UserNode *user = getUserByMask(from);
117     if(user == NULL) return 0;
118     struct ChanNode *chan = getChanByName(argv[0]);
119     if(chan == NULL) return 0;
120     if(isUserOnChan(user, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
121         struct ChanUser *chanuser = getChanUser(user, chan);
122         delChanUser(chanuser, 0); //we need to free the chanuser manually!
123         event_part(chanuser, argv[1]);
124         free(chanuser);
125         if(user->flags & USERFLAG_ISBOT) {
126             //check if theres another bot in the channel - otherwise free it
127             
128         }
129     }
130     if(user->channel == NULL && !(user->flags & USERFLAG_ISBOT)) {
131         //remove the user
132         delUser(user, 1);
133     }
134     
135     return 1;
136 }
137
138 static IRC_CMD(raw_quit) {
139     if(from == NULL || argc < 2) return 0;
140     struct UserNode *user = getUserByMask(from);
141     if(user == NULL) return 0;
142     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
143     event_quit(user, argv[1]);
144     if(user->flags & USERFLAG_ISBOT) {
145         //check if there are other bots in the users channel - otherwise free them
146         
147     }
148     delUser(user, 1); //now we fully free the user
149     return 1;
150 }
151
152 static IRC_CMD(raw_kick) {
153     if(from == NULL || argc < 3) return 0;
154     struct UserNode *user = getUserByMask(from);
155     struct UserNode *target = getUserByNick(argv[1]);
156     if(user == NULL || target == NULL) return 0;
157     struct ChanNode *chan = getChanByName(argv[0]);
158     if(chan == NULL) return 0;
159     if(isUserOnChan(target, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
160         struct ChanUser *chanuser = getChanUser(target, chan);
161         delChanUser(chanuser, 0); //we need to free the chanuser manually!
162         event_kick(user, chanuser, argv[1]);
163         free(chanuser);
164         if(target->flags & USERFLAG_ISBOT) {
165             //check if theres another bot in the channel - otherwise free it
166             
167         }
168     }
169     if(target->channel == NULL && !(target->flags & USERFLAG_ISBOT)) {
170         //remove the user
171         delUser(user, 1);
172     }
173     return 1;
174 }
175
176 static IRC_CMD(raw_ping) {
177     if(argc == 0) return 0;
178     putsock(client, "PONG :%s", argv[0]);
179     return 1;
180 }
181
182 static IRC_CMD(raw_354) {
183     recv_whohandler_354(client, argv, argc);
184     return 1;
185 }
186
187 static IRC_CMD(raw_315) {
188     recv_whohandler_315(client, argv, argc);
189     return 1;
190 }
191
192 void parser_init() {
193     //all the raws we receive...
194     register_irc_function("001", raw_001);
195     register_irc_function("354", raw_354);
196     register_irc_function("315", raw_315);
197     register_irc_function("KICK", raw_kick);
198     register_irc_function("JOIN", raw_join);
199     register_irc_function("PART", raw_part);
200     register_irc_function("QUIT", raw_quit);
201     register_irc_function("PING", raw_ping);
202 }