1 /* WHOHandler.c - NeonServ v5.4
2 * Copyright (C) 2011-2012 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 "WHOHandler.h"
23 #include "ClientSocket.h"
27 #define WHOQUEUETYPE_ISONQUEUE 0x01
28 #define WHOQUEUETYPE_USERLIST 0x02
29 #define WHOQUEUETYPE_USERAUTH 0x04
30 #define WHOQUEUETYPE_CHECKTYPE 0x07
31 #define WHOQUEUETYPE_FOUND 0x08
33 #define MAXCALLBACKS 3
35 struct WHOQueueEntry {
38 struct ChanNode *chan;
39 struct UserNode *user;
40 struct WHOQueueEntry *next;
41 void *callback[MAXCALLBACKS];
42 int module_id[MAXCALLBACKS];
43 void *data[MAXCALLBACKS];
46 static int checkWHOID(struct ClientSocket *client, int whoid) {
47 struct WHOQueueEntry *entry;
48 for(entry = client->whoqueue_first; entry; entry = entry->next) {
49 if(entry->whoid == whoid)
55 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
56 SYNCHRONIZE(whohandler_sync);
60 } while(checkWHOID(client, whoid) && whoid < 1000);
62 DESYNCHRONIZE(whohandler_sync);
65 struct WHOQueueEntry *entry = malloc(sizeof(*entry));
68 perror("malloc() failed");
69 DESYNCHRONIZE(whohandler_sync);
74 if(client->whoqueue_last) {
75 client->whoqueue_last->next = entry;
77 client->whoqueue_first = entry;
79 client->whoqueue_last = entry;
80 DESYNCHRONIZE(whohandler_sync);
84 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int whoid, int freeEntry) {
85 if(!client->whoqueue_first) return NULL;
86 SYNCHRONIZE(whohandler_sync);
87 struct WHOQueueEntry *entry;
88 for(entry = client->whoqueue_first; entry; entry = entry->next) {
89 if(entry->whoid == whoid)
93 DESYNCHRONIZE(whohandler_sync);
97 client->whoqueue_first = entry->next;
98 if(entry == client->whoqueue_last) {
99 client->whoqueue_last = NULL;
102 DESYNCHRONIZE(whohandler_sync);
106 void clear_whoqueue(struct ClientSocket *client) {
107 if(!client->whoqueue_first) return;
108 SYNCHRONIZE(whohandler_sync);
109 struct WHOQueueEntry *entry, *next;
110 for(entry = client->whoqueue_first; entry; entry = next) {
114 client->whoqueue_last = NULL;
115 client->whoqueue_first = NULL;
116 DESYNCHRONIZE(whohandler_sync);
119 void get_userlist(struct ChanNode *chan, int module_id, userlist_callback_t callback, void *data) {
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))
125 if(bot == NULL) return;
126 //check if we really need to who the channel
127 int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST));
129 struct ChanUser *chanuser;
130 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
131 if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) {
138 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
139 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
141 entry->callback[0] = callback;
142 entry->module_id[0] = module_id;
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 %%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
151 callback(bot, chan, data);
154 void _get_userlist_with_invisible(struct ChanNode *chan, int module_id, userlist_callback_t callback, void *data, int force) {
155 struct ClientSocket *bot;
156 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
157 if(isUserOnChan(bot->user, chan))
160 if(bot == NULL) return;
161 //check if we really need to who the channel
162 //invisible users can only be present if chanmode +D or +d is set!
163 int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST)) || (isModeSet(chan->modes, 'd') || isModeSet(chan->modes, 'D'));
164 if(!do_who && force) {
165 struct ChanUser *chanuser;
166 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
167 if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) {
174 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
175 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
177 entry->callback[0] = callback;
178 entry->module_id[0] = module_id;
180 for(i = 1; i < MAXCALLBACKS; i++)
181 entry->callback[i] = NULL;
182 entry->data[0] = data;
183 for(i = 1; i < MAXCALLBACKS; i++)
184 entry->data[i] = NULL;
185 putsock(bot, "WHO %s,%d d%%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
187 callback(bot, chan, data);
190 void get_userauth(struct UserNode *user, int module_id, userauth_callback_t callback, void *data) {
191 //check if we have already an active WHO for this user
192 struct ClientSocket *bot, *whobot = NULL;
193 struct WHOQueueEntry *entry;
194 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
195 for(entry = bot->whoqueue_first; entry; entry = entry->next) {
196 if((entry->type & WHOQUEUETYPE_USERAUTH) && entry->user == user) {
198 for(i = 1; i < MAXCALLBACKS; i++) {
199 if(!entry->callback[i]) {
200 entry->callback[i] = callback;
201 entry->module_id[i] = module_id;
202 entry->data[i] = data;
208 if(bot->flags & SOCKET_FLAG_PREFERRED)
212 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
213 //check if we really need to who the user
214 if(!is_valid_nick(user->nick)) {
215 callback(bot, user->nick, NULL, data);
218 if((user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT | USERFLAG_ISSERVER)) || (time(0) - user->last_who) <= REWHO_TIMEOUT) {
219 callback(bot, user->nick, user, data);
222 entry = addWHOQueueEntry(bot);
223 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
225 user->flags |= USERFLAG_IS_ON_WHO_QUEUE;
226 entry->callback[0] = callback;
227 entry->module_id[0] = module_id;
229 for(i = 1; i < MAXCALLBACKS; i++)
230 entry->callback[i] = NULL;
231 entry->data[0] = data;
232 for(i = 1; i < MAXCALLBACKS; i++)
233 entry->data[i] = NULL;
234 //WHO ".$user->getNick().",".$id." %tuhna,".$id
235 putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->whoid, entry->whoid);
238 static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc);
239 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
240 _recv_whohandler_354(client, argv, argc);
243 static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
246 int type = atoi(argv[1]);
248 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
249 if(entry == NULL) return;
251 unsigned int tid = (unsigned int) pthread_self_tid();
252 while(!clientsocket_parseorder_top(tid)) {
256 if(entry->type & WHOQUEUETYPE_USERLIST) {
258 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
259 struct ChanNode *chan = entry->chan;
260 //add the user toe the channel if he isn't added, yet and update its user data
263 int chanuserflags = 0;
264 for(i = 0; i < strlen(argv[6]); i++) {
265 switch (argv[6][i]) {
267 chanuserflags |= CHANUSERFLAG_OPPED;
270 chanuserflags |= CHANUSERFLAG_HALFOPPED;
273 chanuserflags |= CHANUSERFLAG_VOICED;
276 userflags |= USERFLAG_ISIRCOP;
279 chanuserflags |= CHANUSERFLAG_INVISIBLE;
286 struct UserNode *user = getUserByNick(argv[5]);
287 struct ChanUser *chanuser;
288 if((chanuserflags & CHANUSERFLAG_INVISIBLE)) {
290 user = createTempUser(argv[5]);
291 user->flags |= USERFLAG_ISTMPUSER;
293 chan->flags |= CHANFLAG_HAVE_INVISIBLES;
294 chanuser = addInvisibleChanUser(chan, user);
295 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
298 user = addUser(argv[5]);
300 if(!(chanuser = getChanUser(user, chan))) {
301 chanuser = addChanUser(chan, user);
303 chanuser->flags = (chanuser->flags & ~(CHANUSERFLAG_OPPED_OR_VOICED | CHANUSERFLAG_INVISIBLE)) | chanuserflags;
305 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
306 user->last_who = time(0);
308 strcpy(user->ident, argv[2]);
310 user->ip = createIPNode(argv[3]);
312 strcpy(user->host, argv[4]);
313 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[7], "0")) {
314 strcpy(user->auth, argv[7]);
315 user->flags |= USERFLAG_ISAUTHED;
317 } else if((entry->type & WHOQUEUETYPE_USERAUTH) && !(entry->type & WHOQUEUETYPE_FOUND)) {
318 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
319 entry->type |= WHOQUEUETYPE_FOUND;
320 entry->user->last_who = time(0);
321 if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
322 strcpy(entry->user->auth, argv[5]);
323 entry->user->flags |= USERFLAG_ISAUTHED;
325 for(i = 0; i < MAXCALLBACKS; i++) {
326 userauth_callback_t *callback = entry->callback[i];
328 if(!entry->module_id[i] || module_loaded(entry->module_id[i]))
329 callback(client, entry->user->nick, entry->user, entry->data[i]);
334 static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc);
335 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
336 _recv_whohandler_315(client, argv, argc);
339 static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
341 char *typestr = strstr(argv[1], ",");
344 int type = atoi(typestr);
345 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
346 if(entry == NULL) return;
348 unsigned int tid = (unsigned int) pthread_self_tid();
349 while(!clientsocket_parseorder_top(tid)) {
353 getNextWHOQueueEntry(client, type, 1);
354 if(entry->type & WHOQUEUETYPE_USERLIST) {
355 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
356 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
357 userlist_callback_t *callback;
359 for(i = 0; i < MAXCALLBACKS; i++) {
360 callback = entry->callback[i];
362 if(!entry->module_id[i] || module_loaded(entry->module_id[i]))
363 callback(client, entry->chan, entry->data[i]);
365 if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
366 //remove all invisible users again
367 struct ChanUser *chanuser, *next;
368 for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
369 next = getChannelUsers(entry->chan, chanuser);
370 if((chanuser->flags & CHANUSERFLAG_INVISIBLE) && !isBot(chanuser->user)) {
371 delChanUser(chanuser, 1);
375 } else if(entry->type & WHOQUEUETYPE_USERAUTH) {
376 if(!(entry->type & WHOQUEUETYPE_FOUND)) {
377 userauth_callback_t *callback;
379 for(i = 0; i < MAXCALLBACKS; i++) {
380 callback = entry->callback[i];
382 if(!entry->module_id[i] || module_loaded(entry->module_id[i]))
383 callback(client, entry->user->nick, NULL, entry->data[i]);
386 entry->user->flags &= ~USERFLAG_IS_ON_WHO_QUEUE;
387 if(entry->user->flags & USERFLAG_FREE_AFTER_WHO) {
388 delUser(entry->user, 1);
394 void free_whoqueue() {