1 /* WHOHandler.c - NeonServ v5.1
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;
168 struct ClientSocket *bot;
169 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
170 if(bot->flags & SOCKET_FLAG_PREFERRED)
173 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
174 //check if we really need to who the user
175 if((user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT | USERFLAG_ISSERVER)) || (time(0) - user->last_who) <= REWHO_TIMEOUT) {
176 callback(bot, user->nick, user, data);
179 entry = addWHOQueueEntry(bot);
180 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
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 //WHO ".$user->getNick().",".$id." %tuhna,".$id
190 putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
193 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
196 int type = atoi(argv[1]);
198 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
199 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
200 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
201 if(type & WHOQUEUETYPE_USERLIST) {
203 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
204 struct ChanNode *chan = entry->chan;
205 //add the user toe the channel if he isn't added, yet and update its user data
208 int chanuserflags = 0;
209 for(i = 0; i < strlen(argv[5]); i++) {
210 switch (argv[5][i]) {
212 chanuserflags |= CHANUSERFLAG_OPPED;
215 chanuserflags |= CHANUSERFLAG_VOICED;
218 userflags |= USERFLAG_ISIRCOP;
221 chanuserflags |= CHANUSERFLAG_INVISIBLE;
228 struct UserNode *user;
229 if(chanuserflags & CHANUSERFLAG_INVISIBLE) {
230 user = createTempUser(argv[4]);
231 user->flags |= USERFLAG_ISTMPUSER;
232 chan->flags |= CHANFLAG_HAVE_INVISIBLES;
233 struct ChanUser *chanuser = addInvisibleChanUser(chan, user);
234 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
236 user = getUserByNick(argv[4]);
238 user = addUser(argv[4]);
240 if(!isUserOnChan(user, chan)) {
241 struct ChanUser *chanuser = addChanUser(chan, user);
242 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
245 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
246 user->last_who = time(0);
248 strcpy(user->ident, argv[2]);
250 strcpy(user->host, argv[3]);
251 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
252 strcpy(user->auth, argv[6]);
253 user->flags |= USERFLAG_ISAUTHED;
255 } else if(type & WHOQUEUETYPE_USERAUTH) {
256 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
257 entry->type |= WHOQUEUETYPE_FOUND;
258 entry->user->last_who = time(0);
259 if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
260 strcpy(entry->user->auth, argv[5]);
261 entry->user->flags |= USERFLAG_ISAUTHED;
263 for(i = 0; i < MAXCALLBACKS; i++) {
264 userauth_callback_t *callback = entry->callback[i];
266 callback(client, entry->user->nick, entry->user, entry->data[i]);
271 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
273 char *typestr = strstr(argv[1], ",");
276 int type = atoi(typestr);
277 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
278 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
279 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
280 if(type & WHOQUEUETYPE_USERLIST) {
281 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
282 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
283 userlist_callback_t *callback;
285 for(i = 0; i < MAXCALLBACKS; i++) {
286 callback = entry->callback[i];
288 callback(client, entry->chan, entry->data[i]);
290 if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
291 //remove all invisible users again
292 struct ChanUser *chanuser, *next;
293 for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
294 next = getChannelUsers(entry->chan, chanuser);
295 if(chanuser->flags & CHANUSERFLAG_INVISIBLE) {
296 delChanUser(chanuser, 1);
300 } else if(type & WHOQUEUETYPE_USERAUTH) {
301 if(!(entry->type & WHOQUEUETYPE_FOUND)) {
302 userauth_callback_t *callback;
304 for(i = 0; i < MAXCALLBACKS; i++) {
305 callback = entry->callback[i];
307 callback(client, entry->user->nick, NULL, entry->data[i]);
314 void free_whoqueue() {
315 struct WHOQueueEntry *entry, *next;
316 for(entry = first_entry; entry; entry = next) {