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;
44 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
45 if(!first_entry) return NULL;
46 struct WHOQueueEntry *entry;
47 for(entry = first_entry; entry; entry = entry->next) {
48 if(entry->client == client)
51 if(entry == NULL) return NULL;
53 if(entry == first_entry)
54 first_entry = entry->next;
55 if(entry == last_entry) {
56 struct WHOQueueEntry *last;
57 for(last = first_entry; last; last = last->next)
58 if(last->next == NULL) break;
65 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
66 struct ClientSocket *bot;
67 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
68 if(isUserOnChan(bot->user, chan))
71 if(bot == NULL) return;
72 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
73 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
75 entry->callback = callback;
77 //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
78 putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
81 void get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data) {
82 struct ClientSocket *bot;
83 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
84 if(isUserOnChan(bot->user, chan))
87 if(bot == NULL) return;
88 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
89 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
91 entry->callback = callback;
93 //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
94 putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type);
97 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
98 struct ClientSocket *bot;
99 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
100 if(bot->flags & SOCKET_FLAG_PREFERRED)
103 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
104 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
105 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
107 entry->callback = callback;
109 //WHO ".$user->getNick().",".$id." %tuhna,".$id
110 putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
113 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
116 int type = atoi(argv[1]);
117 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
118 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
119 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
120 if(type & WHOQUEUETYPE_USERLIST) {
122 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
123 struct ChanNode *chan = entry->chan;
124 //add the user toe the channel if he isn't added, yet and update its user data
127 int chanuserflags = 0;
128 for(i = 0; i < strlen(argv[5]); i++) {
129 switch (argv[5][i]) {
131 chanuserflags |= CHANUSERFLAG_OPPED;
134 chanuserflags |= CHANUSERFLAG_VOICED;
137 userflags |= USERFLAG_ISIRCOP;
140 chanuserflags |= CHANUSERFLAG_INVISIBLE;
147 struct UserNode *user;
148 if(chanuserflags & CHANUSERFLAG_INVISIBLE) {
149 user = createTempUser(argv[4]);
150 user->flags |= USERFLAG_ISTMPUSER;
151 chan->flags |= CHANFLAG_HAVE_INVISIBLES;
152 struct ChanUser *chanuser = addInvisibleChanUser(chan, user);
153 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
155 user = getUserByNick(argv[4]);
157 user = addUser(argv[4]);
159 if(!isUserOnChan(user, chan)) {
160 struct ChanUser *chanuser = addChanUser(chan, user);
161 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
164 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
166 strcpy(user->ident, argv[2]);
168 strcpy(user->host, argv[3]);
169 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
170 strcpy(user->auth, argv[6]);
171 user->flags |= USERFLAG_ISAUTHED;
173 } else if(type & WHOQUEUETYPE_USERAUTH) {
174 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
175 entry->type |= WHOQUEUETYPE_FOUND;
176 if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
177 strcpy(entry->user->auth, argv[5]);
178 entry->user->flags |= USERFLAG_ISAUTHED;
180 userauth_callback_t *callback = entry->callback;
181 callback(client, entry->user, entry->data);
185 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
187 char *typestr = strstr(argv[1], ",") + 1;
188 int type = atoi(typestr);
189 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
190 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
191 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
192 if(type & WHOQUEUETYPE_USERLIST) {
193 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
194 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
195 userlist_callback_t *callback = entry->callback;
196 callback(client, entry->chan, entry->data);
197 if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
198 //remove all invisible users again
199 struct ChanUser *chanuser, *next;
200 for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
201 next = getChannelUsers(entry->chan, chanuser);
202 if(chanuser->flags & CHANUSERFLAG_INVISIBLE) {
203 delChanUser(chanuser, 1);
207 } else if(type & WHOQUEUETYPE_USERAUTH) {
208 if(!(entry->type & WHOQUEUETYPE_FOUND)) {
209 userauth_callback_t *callback = entry->callback;
210 callback(client, NULL, entry->data);
216 void free_whoqueue() {
217 struct WHOQueueEntry *entry, *next;
218 for(entry = first_entry; entry; entry = next) {