1 /* WHOHandler.c - NeonServ v5.2
2 * Copyright (C) 2011 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"
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
31 #define MAXCALLBACKS 3
33 struct WHOQueueEntry {
35 struct ClientSocket *client;
36 struct ChanNode *chan;
37 struct UserNode *user;
38 struct WHOQueueEntry *next;
39 void *callback[MAXCALLBACKS];
40 void *data[MAXCALLBACKS];
43 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
45 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
46 struct WHOQueueEntry *entry = malloc(sizeof(*entry));
49 perror("malloc() failed");
53 entry->client = client;
55 last_entry->next = entry;
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)
71 if(entry == NULL) return NULL;
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;
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))
91 if(bot == NULL) return;
92 //check if we really need to who the channel
93 int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST));
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) {
104 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
105 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
107 entry->callback[0] = callback;
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);
116 callback(bot, chan, data);
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))
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) {
139 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
140 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
142 entry->callback[0] = callback;
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);
151 callback(bot, chan, data);
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) {
160 for(i = 1; i < MAXCALLBACKS; i++) {
161 if(!entry->callback[i]) {
162 entry->callback[i] = callback;
163 entry->data[i] = data;
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)
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);
180 entry = addWHOQueueEntry(bot);
181 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
183 entry->callback[0] = callback;
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);
194 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
197 int type = atoi(argv[1]);
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) {
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
209 int chanuserflags = 0;
210 for(i = 0; i < strlen(argv[5]); i++) {
211 switch (argv[5][i]) {
213 chanuserflags |= CHANUSERFLAG_OPPED;
216 chanuserflags |= CHANUSERFLAG_VOICED;
219 userflags |= USERFLAG_ISIRCOP;
222 chanuserflags |= CHANUSERFLAG_INVISIBLE;
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;
237 user = getUserByNick(argv[4]);
239 user = addUser(argv[4]);
241 if(!isUserOnChan(user, chan)) {
242 struct ChanUser *chanuser = addChanUser(chan, user);
243 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
246 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
247 user->last_who = time(0);
249 strcpy(user->ident, argv[2]);
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;
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;
264 for(i = 0; i < MAXCALLBACKS; i++) {
265 userauth_callback_t *callback = entry->callback[i];
267 callback(client, entry->user->nick, entry->user, entry->data[i]);
272 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
274 char *typestr = strstr(argv[1], ",");
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;
286 for(i = 0; i < MAXCALLBACKS; i++) {
287 callback = entry->callback[i];
289 callback(client, entry->chan, entry->data[i]);
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);
301 } else if(type & WHOQUEUETYPE_USERAUTH) {
302 if(!(entry->type & WHOQUEUETYPE_FOUND)) {
303 userauth_callback_t *callback;
305 for(i = 0; i < MAXCALLBACKS; i++) {
306 callback = entry->callback[i];
308 callback(client, entry->user->nick, NULL, entry->data[i]);
315 void free_whoqueue() {
316 struct WHOQueueEntry *entry, *next;
317 for(entry = first_entry; entry; entry = next) {