*** VERSION 5.2.0 ***
[NeonServV5.git] / src / WHOHandler.c
1 /* WHOHandler.c - NeonServ v5.2
2  * Copyright (C) 2011  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 "WHOHandler.h"
19 #include "ChanNode.h"
20 #include "UserNode.h"
21 #include "ChanUser.h"
22 #include "ModeNode.h"
23 #include "ClientSocket.h"
24
25 #define WHOQUEUETYPE_ISONQUEUE 0x01
26 #define WHOQUEUETYPE_USERLIST  0x02
27 #define WHOQUEUETYPE_USERAUTH  0x04
28 #define WHOQUEUETYPE_CHECKTYPE 0x07
29 #define WHOQUEUETYPE_FOUND     0x08
30
31 #define MAXCALLBACKS 3
32
33 struct WHOQueueEntry {
34     char type;
35     struct ClientSocket *client;
36     struct ChanNode *chan;
37     struct UserNode *user;
38     struct WHOQueueEntry *next;
39     void *callback[MAXCALLBACKS];
40     void *data[MAXCALLBACKS];
41 };
42
43 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
44
45 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
46     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
47     if (!entry)
48     {
49         perror("malloc() failed");
50         return NULL;
51     }
52     entry->next = NULL;
53     entry->client = client;
54     if(last_entry) {
55         last_entry->next = entry;
56         last_entry = entry;
57     } else {
58         last_entry = entry;
59         first_entry = entry;
60     }
61     return entry;
62 }
63
64 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
65     if(!first_entry) return NULL;
66     struct WHOQueueEntry *entry;
67     for(entry = first_entry; entry; entry = entry->next) {
68         if(entry->client == client)
69             break;
70     }
71     if(entry == NULL) return NULL;
72     if(freeEntry) {
73         if(entry == first_entry)
74             first_entry = entry->next;
75         if(entry == last_entry) {
76             struct WHOQueueEntry *last = NULL;
77             for(last = first_entry; last; last = last->next)
78                 if(last->next == NULL) break;
79             last_entry = last;
80         }
81     }
82     return entry;
83 }
84
85 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
86     struct ClientSocket *bot;
87     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
88         if(isUserOnChan(bot->user, chan))
89             break;
90     }
91     if(bot == NULL) return;
92     //check if we really need to who the channel
93     int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST));
94     if(!do_who) {
95         struct ChanUser *chanuser;
96         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
97             if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) {
98                 do_who = 1;
99                 break;
100             }
101         }
102     }
103     if(do_who) {
104         struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
105         entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
106         entry->chan = chan;
107         entry->callback[0] = callback;
108         int i;
109         for(i = 1; i < MAXCALLBACKS; i++)
110             entry->callback[i] = NULL;
111         entry->data[0] = data;
112         for(i = 1; i < MAXCALLBACKS; i++)
113             entry->data[i] = NULL;
114         putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
115     } else
116         callback(bot, chan, data);
117 }
118
119 void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data, int force) {
120     struct ClientSocket *bot;
121     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
122         if(isUserOnChan(bot->user, chan))
123             break;
124     }
125     if(bot == NULL) return;
126     //check if we really need to who the channel
127     //invisible users can only be present if chanmode +D or +d is set!
128     int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST))  || (isModeSet(chan->modes, 'd') || isModeSet(chan->modes, 'D'));
129     if(!do_who && force) {
130         struct ChanUser *chanuser;
131         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
132             if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) {
133                 do_who = 1;
134                 break;
135             }
136         }
137     }
138     if(do_who) {
139         struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
140         entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
141         entry->chan = chan;
142         entry->callback[0] = callback;
143         int i;
144         for(i = 1; i < MAXCALLBACKS; i++)
145             entry->callback[i] = NULL;
146         entry->data[0] = data;
147         for(i = 1; i < MAXCALLBACKS; i++)
148             entry->data[i] = NULL;
149         putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type);
150     } else
151         callback(bot, chan, data);
152 }
153
154 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
155     //check if we have already an active WHO for this user
156     struct WHOQueueEntry *entry;
157     for(entry = first_entry; entry; entry = entry->next) {
158         if((entry->type & WHOQUEUETYPE_USERAUTH) && entry->user == user) {
159             int i = 0;
160             for(i = 1; i < MAXCALLBACKS; i++) {
161                 if(!entry->callback[i]) {
162                     entry->callback[i] = callback;
163                     entry->data[i] = data;
164                     return;
165                 }
166             }
167         }
168     }
169     struct ClientSocket *bot;
170     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
171         if(bot->flags & SOCKET_FLAG_PREFERRED)
172             break;
173     }
174     if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
175     //check if we really need to who the user
176     if((user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT | USERFLAG_ISSERVER)) || (time(0) - user->last_who) <= REWHO_TIMEOUT) {
177         callback(bot, user->nick, user, data);
178         return;
179     }
180     entry = addWHOQueueEntry(bot);
181     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
182     entry->user = user;
183     entry->callback[0] = callback;
184     int i;
185     for(i = 1; i < MAXCALLBACKS; i++)
186         entry->callback[i] = NULL;
187     entry->data[0] = data;
188     for(i = 1; i < MAXCALLBACKS; i++)
189             entry->data[i] = NULL;
190     //WHO ".$user->getNick().",".$id." %tuhna,".$id
191     putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
192 }
193
194 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
195     int i;
196     if(argc < 2) return;
197     int type = atoi(argv[1]);
198     if(!type) return;
199     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
200     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
201     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
202     if(type & WHOQUEUETYPE_USERLIST) {
203         if(argc < 7) return;
204         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
205         struct ChanNode *chan = entry->chan;
206         //add the user toe the channel if he isn't added, yet and update its user data
207         //parse flags
208         int userflags = 0;
209         int chanuserflags = 0;
210         for(i = 0; i < strlen(argv[5]); i++) {
211             switch (argv[5][i]) {
212                 case '@':
213                     chanuserflags |= CHANUSERFLAG_OPPED;
214                     break;
215                 case '+':
216                     chanuserflags |= CHANUSERFLAG_VOICED;
217                     break;
218                 case '*':
219                     userflags |= USERFLAG_ISIRCOP;
220                     break;
221                 case '<':
222                     chanuserflags |= CHANUSERFLAG_INVISIBLE;
223                     break;
224                 default:
225                     break;
226             }
227         }
228         
229         struct UserNode *user;
230         if(chanuserflags & CHANUSERFLAG_INVISIBLE) {
231             user = createTempUser(argv[4]);
232             user->flags |= USERFLAG_ISTMPUSER;
233             chan->flags |= CHANFLAG_HAVE_INVISIBLES;
234             struct ChanUser *chanuser = addInvisibleChanUser(chan, user);
235             chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
236         } else {
237             user = getUserByNick(argv[4]);
238             if(user == NULL) {
239                 user = addUser(argv[4]);
240             }
241             if(!isUserOnChan(user, chan)) {
242                 struct ChanUser *chanuser = addChanUser(chan, user);
243                 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
244             }
245         }
246         user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
247         user->last_who = time(0);
248         if(!*user->ident)
249             strcpy(user->ident, argv[2]);
250         if(!*user->host)
251             strcpy(user->host, argv[3]);
252         if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
253             strcpy(user->auth, argv[6]);
254             user->flags |= USERFLAG_ISAUTHED;
255         }
256     } else if(type & WHOQUEUETYPE_USERAUTH) {
257         //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
258         entry->type |= WHOQUEUETYPE_FOUND;
259         entry->user->last_who = time(0);
260         if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
261             strcpy(entry->user->auth, argv[5]);
262             entry->user->flags |= USERFLAG_ISAUTHED;
263         }
264         for(i = 0; i < MAXCALLBACKS; i++) {
265             userauth_callback_t *callback = entry->callback[i];
266             if(!callback) break;
267             callback(client, entry->user->nick, entry->user, entry->data[i]);
268         }
269     }
270 }
271
272 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
273     if(argc < 2) return;
274     char *typestr = strstr(argv[1], ",");
275     if(!typestr) return;
276     typestr++;
277     int type = atoi(typestr);
278     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
279     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
280     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
281     if(type & WHOQUEUETYPE_USERLIST) {
282         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
283         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
284         userlist_callback_t *callback;
285         int i;
286         for(i = 0; i < MAXCALLBACKS; i++) {
287             callback = entry->callback[i];
288             if(!callback) break;
289             callback(client, entry->chan, entry->data[i]);
290         }
291         if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
292             //remove all invisible users again
293             struct ChanUser *chanuser, *next;
294             for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
295                 next = getChannelUsers(entry->chan, chanuser);
296                 if(chanuser->flags & CHANUSERFLAG_INVISIBLE) {
297                     delChanUser(chanuser, 1);
298                 }
299             }
300         }
301     } else if(type & WHOQUEUETYPE_USERAUTH) {
302         if(!(entry->type & WHOQUEUETYPE_FOUND)) {
303             userauth_callback_t *callback;
304             int i;
305             for(i = 0; i < MAXCALLBACKS; i++) {
306                 callback = entry->callback[i];
307                 if(!callback) break;
308                 callback(client, entry->user->nick, NULL, entry->data[i]);
309             }
310         }
311     }
312     free(entry);
313 }
314
315 void free_whoqueue() {
316     struct WHOQueueEntry *entry, *next;
317     for(entry = first_entry; entry; entry = next) {
318         next = entry->next;
319         free(entry);
320     }
321     first_entry = NULL;
322     last_entry = NULL;
323 }