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"
22 #include "ClientSocket.h"
24 #define WHOQUEUETYPE_ISONQUEUE 0x01
25 #define WHOQUEUETYPE_USERLIST 0x02
26 #define WHOQUEUETYPE_USERAUTH 0x04
27 #define WHOQUEUETYPE_CHECKTYPE 0x07
28 #define WHOQUEUETYPE_FOUND 0x08
30 struct WHOQueueEntry {
32 struct ClientSocket *client;
33 struct ChanNode *chan;
34 struct UserNode *user;
35 struct WHOQueueEntry *next;
40 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
42 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
43 struct WHOQueueEntry *entry = malloc(sizeof(*entry));
46 perror("malloc() failed");
50 entry->client = client;
52 last_entry->next = entry;
61 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
62 if(!first_entry) return NULL;
63 struct WHOQueueEntry *entry;
64 for(entry = first_entry; entry; entry = entry->next) {
65 if(entry->client == client)
68 if(entry == NULL) return NULL;
70 if(entry == first_entry)
71 first_entry = entry->next;
72 if(entry == last_entry) {
73 struct WHOQueueEntry *last = NULL;
74 for(last = first_entry; last; last = last->next)
75 if(last->next == NULL) break;
82 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
83 struct ClientSocket *bot;
84 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
85 if(isUserOnChan(bot->user, chan))
88 if(bot == NULL) return;
89 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
90 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
92 entry->callback = callback;
94 //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
95 putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
98 void get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data) {
99 struct ClientSocket *bot;
100 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
101 if(isUserOnChan(bot->user, chan))
104 if(bot == NULL) return;
105 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
106 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
108 entry->callback = callback;
110 //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
111 putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type);
114 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
115 struct ClientSocket *bot;
116 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
117 if(bot->flags & SOCKET_FLAG_PREFERRED)
120 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
121 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
122 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
124 entry->callback = callback;
126 //WHO ".$user->getNick().",".$id." %tuhna,".$id
127 putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
130 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
133 int type = atoi(argv[1]);
135 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
136 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
137 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
138 if(type & WHOQUEUETYPE_USERLIST) {
140 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
141 struct ChanNode *chan = entry->chan;
142 //add the user toe the channel if he isn't added, yet and update its user data
145 int chanuserflags = 0;
146 for(i = 0; i < strlen(argv[5]); i++) {
147 switch (argv[5][i]) {
149 chanuserflags |= CHANUSERFLAG_OPPED;
152 chanuserflags |= CHANUSERFLAG_VOICED;
155 userflags |= USERFLAG_ISIRCOP;
158 chanuserflags |= CHANUSERFLAG_INVISIBLE;
165 struct UserNode *user;
166 if(chanuserflags & CHANUSERFLAG_INVISIBLE) {
167 user = createTempUser(argv[4]);
168 user->flags |= USERFLAG_ISTMPUSER;
169 chan->flags |= CHANFLAG_HAVE_INVISIBLES;
170 struct ChanUser *chanuser = addInvisibleChanUser(chan, user);
171 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
173 user = getUserByNick(argv[4]);
175 user = addUser(argv[4]);
177 if(!isUserOnChan(user, chan)) {
178 struct ChanUser *chanuser = addChanUser(chan, user);
179 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
182 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
184 strcpy(user->ident, argv[2]);
186 strcpy(user->host, argv[3]);
187 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
188 strcpy(user->auth, argv[6]);
189 user->flags |= USERFLAG_ISAUTHED;
191 } else if(type & WHOQUEUETYPE_USERAUTH) {
192 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
193 entry->type |= WHOQUEUETYPE_FOUND;
194 if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
195 strcpy(entry->user->auth, argv[5]);
196 entry->user->flags |= USERFLAG_ISAUTHED;
198 userauth_callback_t *callback = entry->callback;
199 callback(client, entry->user->nick, entry->user, entry->data);
203 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
205 char *typestr = strstr(argv[1], ",") + 1;
207 int type = atoi(typestr);
208 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
209 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
210 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
211 if(type & WHOQUEUETYPE_USERLIST) {
212 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
213 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
214 userlist_callback_t *callback = entry->callback;
215 callback(client, entry->chan, entry->data);
216 if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
217 //remove all invisible users again
218 struct ChanUser *chanuser, *next;
219 for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
220 next = getChannelUsers(entry->chan, chanuser);
221 if(chanuser->flags & CHANUSERFLAG_INVISIBLE) {
222 delChanUser(chanuser, 1);
226 } else if(type & WHOQUEUETYPE_USERAUTH) {
227 if(!(entry->type & WHOQUEUETYPE_FOUND)) {
228 userauth_callback_t *callback = entry->callback;
229 callback(client, entry->user->nick, NULL, entry->data);
235 void free_whoqueue() {
236 struct WHOQueueEntry *entry, *next;
237 for(entry = first_entry; entry; entry = next) {