2 #include "WHOHandler.h"
6 #include "ClientSocket.h"
8 #define WHOQUEUETYPE_ISONQUEUE 0x01
9 #define WHOQUEUETYPE_USERLIST 0x02
10 #define WHOQUEUETYPE_USERAUTH 0x04
11 #define WHOQUEUETYPE_CHECKTYPE 0x07
12 #define WHOQUEUETYPE_FOUND 0x08
14 struct WHOQueueEntry {
16 struct ClientSocket *client;
17 struct ChanNode *chan;
18 struct UserNode *user;
19 struct WHOQueueEntry *next;
24 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
26 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
27 struct WHOQueueEntry *entry = malloc(sizeof(*entry));
30 perror("malloc() failed");
34 entry->client = client;
36 last_entry->next = entry;
45 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
46 if(!first_entry) return NULL;
47 struct WHOQueueEntry *entry;
48 for(entry = first_entry; entry; entry = entry->next) {
49 if(entry->client == client)
52 if(entry == NULL) return NULL;
54 if(entry == first_entry)
55 first_entry = entry->next;
56 if(entry == last_entry) {
57 struct WHOQueueEntry *last;
58 for(last = first_entry; last; last = last->next)
59 if(last->next == NULL) break;
66 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
67 struct ClientSocket *bot;
68 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
69 if(isUserOnChan(bot->user, chan))
72 if(bot == NULL) return;
73 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
74 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
76 entry->callback = callback;
78 //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
79 putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
82 void get_userlist_with_invisible(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 d%%tuhnaf,%d", chan->name, entry->type, entry->type);
98 void get_userauth(struct UserNode *user, userauth_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(bot->flags & SOCKET_FLAG_PREFERRED)
104 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
105 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
106 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
108 entry->callback = callback;
110 //WHO ".$user->getNick().",".$id." %tuhna,".$id
111 putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
114 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
117 int type = atoi(argv[1]);
118 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
119 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
120 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
121 if(type & WHOQUEUETYPE_USERLIST) {
123 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
124 struct ChanNode *chan = entry->chan;
125 //add the user toe the channel if he isn't added, yet and update its user data
128 int chanuserflags = 0;
129 for(i = 0; i < strlen(argv[5]); i++) {
130 switch (argv[5][i]) {
132 chanuserflags |= CHANUSERFLAG_OPPED;
135 chanuserflags |= CHANUSERFLAG_VOICED;
138 userflags |= USERFLAG_ISIRCOP;
141 chanuserflags |= CHANUSERFLAG_INVISIBLE;
148 struct UserNode *user;
149 if(chanuserflags & CHANUSERFLAG_INVISIBLE) {
150 user = createTempUser(argv[4]);
151 user->flags |= USERFLAG_ISTMPUSER;
152 chan->flags |= CHANFLAG_HAVE_INVISIBLES;
153 struct ChanUser *chanuser = addInvisibleChanUser(chan, user);
154 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
156 user = getUserByNick(argv[4]);
158 user = addUser(argv[4]);
160 if(!isUserOnChan(user, chan)) {
161 struct ChanUser *chanuser = addChanUser(chan, user);
162 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
165 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
167 strcpy(user->ident, argv[2]);
169 strcpy(user->host, argv[3]);
170 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
171 strcpy(user->auth, argv[6]);
172 user->flags |= USERFLAG_ISAUTHED;
174 } else if(type & WHOQUEUETYPE_USERAUTH) {
175 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
176 entry->type |= WHOQUEUETYPE_FOUND;
177 if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
178 strcpy(entry->user->auth, argv[5]);
179 entry->user->flags |= USERFLAG_ISAUTHED;
181 userauth_callback_t *callback = entry->callback;
182 callback(client, entry->user, entry->data);
186 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
188 char *typestr = strstr(argv[1], ",") + 1;
189 int type = atoi(typestr);
190 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
191 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
192 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
193 if(type & WHOQUEUETYPE_USERLIST) {
194 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
195 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
196 userlist_callback_t *callback = entry->callback;
197 callback(client, entry->chan, entry->data);
198 if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
199 //remove all invisible users again
200 struct ChanUser *chanuser, *next;
201 for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
202 next = getChannelUsers(entry->chan, chanuser);
203 if(chanuser->flags & CHANUSERFLAG_INVISIBLE) {
204 delChanUser(chanuser, 1);
208 } else if(type & WHOQUEUETYPE_USERAUTH) {
209 if(!(entry->type & WHOQUEUETYPE_FOUND)) {
210 userauth_callback_t *callback = entry->callback;
211 callback(client, NULL, entry->data);
217 void free_whoqueue() {
218 struct WHOQueueEntry *entry, *next;
219 for(entry = first_entry; entry; entry = next) {