1 /* WHOHandler.c - NeonServ v5.3
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"
26 #define WHOQUEUETYPE_ISONQUEUE 0x01
27 #define WHOQUEUETYPE_USERLIST 0x02
28 #define WHOQUEUETYPE_USERAUTH 0x04
29 #define WHOQUEUETYPE_CHECKTYPE 0x07
30 #define WHOQUEUETYPE_FOUND 0x08
32 #define MAXCALLBACKS 3
34 struct WHOQueueEntry {
37 struct ChanNode *chan;
38 struct UserNode *user;
39 struct WHOQueueEntry *next;
40 void *callback[MAXCALLBACKS];
41 void *data[MAXCALLBACKS];
43 unsigned long lock_tid;
44 pthread_mutex_t lock_mutex;
49 static int checkWHOID(struct ClientSocket *client, int whoid) {
50 struct WHOQueueEntry *entry;
51 for(entry = client->whoqueue_first; entry; entry = entry->next) {
52 if(entry->whoid == whoid)
58 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
59 SYNCHRONIZE(whohandler_sync);
63 } while(checkWHOID(client, whoid) && whoid < 1000);
65 DESYNCHRONIZE(whohandler_sync);
68 struct WHOQueueEntry *entry = malloc(sizeof(*entry));
71 perror("malloc() failed");
72 DESYNCHRONIZE(whohandler_sync);
79 THREAD_MUTEX_INIT(entry->lock_mutex);
80 entry->lock_count = 0;
82 if(client->whoqueue_last) {
83 client->whoqueue_last->next = entry;
85 client->whoqueue_first = entry;
87 client->whoqueue_last = entry;
88 DESYNCHRONIZE(whohandler_sync);
92 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int whoid, int freeEntry) {
93 if(!client->whoqueue_first) return NULL;
94 SYNCHRONIZE(whohandler_sync);
95 struct WHOQueueEntry *entry;
96 for(entry = client->whoqueue_first; entry; entry = entry->next) {
97 if(entry->whoid == whoid)
101 DESYNCHRONIZE(whohandler_sync);
105 client->whoqueue_first = entry->next;
106 if(entry == client->whoqueue_last) {
107 client->whoqueue_last = NULL;
110 entry->lock_tid = -1;
111 if(entry->lock_count) {
113 for(i = 0; i < entry->lock_count; i++) {
114 DESYNCHRONIZE(entry->lock_mutex); //unlock ALL
119 DESYNCHRONIZE(whohandler_sync);
123 void clear_whoqueue(struct ClientSocket *client) {
124 if(!client->whoqueue_first) return;
125 SYNCHRONIZE(whohandler_sync);
126 struct WHOQueueEntry *entry, *next;
127 for(entry = client->whoqueue_first; entry; entry = next) {
130 entry->lock_tid = -1;
131 if(entry->lock_count) {
133 for(i = 0; i < entry->lock_count; i++) {
134 DESYNCHRONIZE(entry->lock_mutex); //unlock ALL
140 client->whoqueue_last = NULL;
141 client->whoqueue_first = NULL;
142 DESYNCHRONIZE(whohandler_sync);
146 void whohandler_end_of_recv(struct ClientSocket *client) {
147 SYNCHRONIZE(whohandler_sync);
148 unsigned long tid = syscall(SYS_gettid);
149 struct WHOQueueEntry *entry;
150 for(entry = client->whoqueue_first; entry; entry = entry->next) {
151 if(entry->lock_tid == tid) {
153 DESYNCHRONIZE(entry->lock_mutex);
156 DESYNCHRONIZE(whohandler_sync);
160 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
161 struct ClientSocket *bot;
162 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
163 if(isUserOnChan(bot->user, chan))
166 if(bot == NULL) return;
167 //check if we really need to who the channel
168 int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST));
170 struct ChanUser *chanuser;
171 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
172 if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) {
179 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
180 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
182 entry->callback[0] = callback;
184 for(i = 1; i < MAXCALLBACKS; i++)
185 entry->callback[i] = NULL;
186 entry->data[0] = data;
187 for(i = 1; i < MAXCALLBACKS; i++)
188 entry->data[i] = NULL;
189 putsock(bot, "WHO %s,%d %%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
191 callback(bot, chan, data);
194 void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data, int force) {
195 struct ClientSocket *bot;
196 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
197 if(isUserOnChan(bot->user, chan))
200 if(bot == NULL) return;
201 //check if we really need to who the channel
202 //invisible users can only be present if chanmode +D or +d is set!
203 int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST)) || (isModeSet(chan->modes, 'd') || isModeSet(chan->modes, 'D'));
204 if(!do_who && force) {
205 struct ChanUser *chanuser;
206 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
207 if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) {
214 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
215 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
217 entry->callback[0] = callback;
219 for(i = 1; i < MAXCALLBACKS; i++)
220 entry->callback[i] = NULL;
221 entry->data[0] = data;
222 for(i = 1; i < MAXCALLBACKS; i++)
223 entry->data[i] = NULL;
224 putsock(bot, "WHO %s,%d d%%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
226 callback(bot, chan, data);
229 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
230 //check if we have already an active WHO for this user
231 struct ClientSocket *bot, *whobot = NULL;
232 struct WHOQueueEntry *entry;
233 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
234 for(entry = bot->whoqueue_first; entry; entry = entry->next) {
235 if((entry->type & WHOQUEUETYPE_USERAUTH) && entry->user == user) {
237 for(i = 1; i < MAXCALLBACKS; i++) {
238 if(!entry->callback[i]) {
239 entry->callback[i] = callback;
240 entry->data[i] = data;
246 if(bot->flags & SOCKET_FLAG_PREFERRED)
250 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
251 //check if we really need to who the user
252 if(!is_valid_nick(user->nick)) {
253 callback(bot, user->nick, NULL, data);
256 if((user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT | USERFLAG_ISSERVER)) || (time(0) - user->last_who) <= REWHO_TIMEOUT) {
257 callback(bot, user->nick, user, data);
260 entry = addWHOQueueEntry(bot);
261 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
263 user->flags |= USERFLAG_IS_ON_WHO_QUEUE;
264 entry->callback[0] = callback;
266 for(i = 1; i < MAXCALLBACKS; i++)
267 entry->callback[i] = NULL;
268 entry->data[0] = data;
269 for(i = 1; i < MAXCALLBACKS; i++)
270 entry->data[i] = NULL;
271 //WHO ".$user->getNick().",".$id." %tuhna,".$id
272 putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->whoid, entry->whoid);
275 static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc);
276 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
277 _recv_whohandler_354(client, argv, argc);
280 static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
283 int type = atoi(argv[1]);
285 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
286 if(entry == NULL) return;
288 unsigned long tid = syscall(SYS_gettid);
289 if(entry->lock_tid != tid) {
291 SYNCHRONIZE(entry->lock_mutex);
292 if(entry->lock_tid == -1) {
293 return; //entry has been destroyed
295 entry->lock_tid = tid;
298 if(entry->type & WHOQUEUETYPE_USERLIST) {
300 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
301 struct ChanNode *chan = entry->chan;
302 //add the user toe the channel if he isn't added, yet and update its user data
305 int chanuserflags = 0;
306 for(i = 0; i < strlen(argv[6]); i++) {
307 switch (argv[6][i]) {
309 chanuserflags |= CHANUSERFLAG_OPPED;
312 chanuserflags |= CHANUSERFLAG_HALFOPPED;
315 chanuserflags |= CHANUSERFLAG_VOICED;
318 userflags |= USERFLAG_ISIRCOP;
321 chanuserflags |= CHANUSERFLAG_INVISIBLE;
328 struct UserNode *user = getUserByNick(argv[5]);
329 struct ChanUser *chanuser;
330 if((chanuserflags & CHANUSERFLAG_INVISIBLE) && (!user || !isBot(user))) {
331 user = createTempUser(argv[5]);
332 user->flags |= USERFLAG_ISTMPUSER;
333 chan->flags |= CHANFLAG_HAVE_INVISIBLES;
334 chanuser = addInvisibleChanUser(chan, user);
335 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
338 user = addUser(argv[5]);
340 if(!(chanuser = getChanUser(user, chan))) {
341 chanuser = addChanUser(chan, user);
343 chanuser->flags = (chanuser->flags & ~(CHANUSERFLAG_OPPED_OR_VOICED | CHANUSERFLAG_INVISIBLE)) | chanuserflags;
345 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
346 user->last_who = time(0);
348 strcpy(user->ident, argv[2]);
350 user->ip = createIPNode(argv[3]);
352 strcpy(user->host, argv[4]);
353 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[7], "0")) {
354 strcpy(user->auth, argv[7]);
355 user->flags |= USERFLAG_ISAUTHED;
357 } else if((entry->type & WHOQUEUETYPE_USERAUTH) && !(entry->type & WHOQUEUETYPE_FOUND)) {
358 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
359 entry->type |= WHOQUEUETYPE_FOUND;
360 entry->user->last_who = time(0);
361 if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
362 strcpy(entry->user->auth, argv[5]);
363 entry->user->flags |= USERFLAG_ISAUTHED;
365 for(i = 0; i < MAXCALLBACKS; i++) {
366 userauth_callback_t *callback = entry->callback[i];
368 callback(client, entry->user->nick, entry->user, entry->data[i]);
373 static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc);
374 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
375 _recv_whohandler_315(client, argv, argc);
378 static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
380 char *typestr = strstr(argv[1], ",");
383 int type = atoi(typestr);
384 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
385 if(entry == NULL) return;
387 unsigned long tid = syscall(SYS_gettid);
388 if(entry->lock_tid != tid) {
390 SYNCHRONIZE(entry->lock_mutex);
391 if(entry->lock_tid == -1) {
392 return; //entry has been destroyed
394 entry->lock_tid = tid;
397 getNextWHOQueueEntry(client, type, 1);
398 if(entry->type & WHOQUEUETYPE_USERLIST) {
399 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
400 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
401 userlist_callback_t *callback;
403 for(i = 0; i < MAXCALLBACKS; i++) {
404 callback = entry->callback[i];
406 callback(client, entry->chan, entry->data[i]);
408 if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
409 //remove all invisible users again
410 struct ChanUser *chanuser, *next;
411 for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
412 next = getChannelUsers(entry->chan, chanuser);
413 if((chanuser->flags & CHANUSERFLAG_INVISIBLE) && !isBot(chanuser->user)) {
414 delChanUser(chanuser, 1);
418 } else if(entry->type & WHOQUEUETYPE_USERAUTH) {
419 if(!(entry->type & WHOQUEUETYPE_FOUND)) {
420 userauth_callback_t *callback;
422 for(i = 0; i < MAXCALLBACKS; i++) {
423 callback = entry->callback[i];
425 callback(client, entry->user->nick, NULL, entry->data[i]);
428 entry->user->flags &= ~USERFLAG_IS_ON_WHO_QUEUE;
429 if(entry->user->flags & USERFLAG_FREE_AFTER_WHO) {
430 delUser(entry->user, 1);
436 void free_whoqueue() {