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;
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]);
134 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
135 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
136 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
137 if(type & WHOQUEUETYPE_USERLIST) {
139 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
140 struct ChanNode *chan = entry->chan;
141 //add the user toe the channel if he isn't added, yet and update its user data
144 int chanuserflags = 0;
145 for(i = 0; i < strlen(argv[5]); i++) {
146 switch (argv[5][i]) {
148 chanuserflags |= CHANUSERFLAG_OPPED;
151 chanuserflags |= CHANUSERFLAG_VOICED;
154 userflags |= USERFLAG_ISIRCOP;
157 chanuserflags |= CHANUSERFLAG_INVISIBLE;
164 struct UserNode *user;
165 if(chanuserflags & CHANUSERFLAG_INVISIBLE) {
166 user = createTempUser(argv[4]);
167 user->flags |= USERFLAG_ISTMPUSER;
168 chan->flags |= CHANFLAG_HAVE_INVISIBLES;
169 struct ChanUser *chanuser = addInvisibleChanUser(chan, user);
170 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
172 user = getUserByNick(argv[4]);
174 user = addUser(argv[4]);
176 if(!isUserOnChan(user, chan)) {
177 struct ChanUser *chanuser = addChanUser(chan, user);
178 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
181 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
183 strcpy(user->ident, argv[2]);
185 strcpy(user->host, argv[3]);
186 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
187 strcpy(user->auth, argv[6]);
188 user->flags |= USERFLAG_ISAUTHED;
190 } else if(type & WHOQUEUETYPE_USERAUTH) {
191 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
192 entry->type |= WHOQUEUETYPE_FOUND;
193 if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
194 strcpy(entry->user->auth, argv[5]);
195 entry->user->flags |= USERFLAG_ISAUTHED;
197 userauth_callback_t *callback = entry->callback;
198 callback(client, entry->user->nick, entry->user, entry->data);
202 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
204 char *typestr = strstr(argv[1], ",") + 1;
205 int type = atoi(typestr);
206 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
207 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
208 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
209 if(type & WHOQUEUETYPE_USERLIST) {
210 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
211 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
212 userlist_callback_t *callback = entry->callback;
213 callback(client, entry->chan, entry->data);
214 if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
215 //remove all invisible users again
216 struct ChanUser *chanuser, *next;
217 for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
218 next = getChannelUsers(entry->chan, chanuser);
219 if(chanuser->flags & CHANUSERFLAG_INVISIBLE) {
220 delChanUser(chanuser, 1);
224 } else if(type & WHOQUEUETYPE_USERAUTH) {
225 if(!(entry->type & WHOQUEUETYPE_FOUND)) {
226 userauth_callback_t *callback = entry->callback;
227 callback(client, entry->user->nick, NULL, entry->data);
233 void free_whoqueue() {
234 struct WHOQueueEntry *entry, *next;
235 for(entry = first_entry; entry; entry = next) {