2 #include "WHOHandler.h"
6 #define WHOQUEUETYPE_ISONQUEUE 0x01
7 #define WHOQUEUETYPE_USERLIST 0x02
11 struct ClientSocket *client;
12 struct ChanNode *chan;
13 struct UserNode *user;
14 struct WHOQueueEntry *next;
15 userlist_callback_t *callback;
18 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
20 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
21 struct WHOQueueEntry *entry = malloc(sizeof(*entry));
24 perror("malloc() failed");
28 entry->client = client;
30 last_entry->next = entry;
35 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
36 if(!first_entry) return NULL;
37 struct WHOQueueEntry *entry;
38 for(entry = first_entry; entry; entry = entry->next) {
39 if(entry->client == client)
42 if(entry == NULL) return NULL;
44 first_entry = first_entry->next;
45 if(last_entry == first_entry)
51 void get_userlist(struct ChanNode *chan, userlist_callback_t callback) {
52 struct ClientSocket *bot;
53 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
54 if(isUserOnChan(bot->user, chan))
57 if(bot == NULL) return;
58 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
59 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
61 entry->callback = callback;
62 //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
63 putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
66 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
69 int type = atoi(argv[1]);
70 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
71 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
72 if(entry == NULL || entry->type != type) return;
73 if(type & WHOQUEUETYPE_USERLIST) {
75 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
76 struct ChanNode *chan = entry->chan;
77 //add the user toe the channel if he isn't added, yet and update its user data
78 struct UserNode *user = getUserByNick(argv[4]);
80 user = addUser(argv[4]);
84 int chanuserflags = 0;
85 for(i = 0; i < strlen(argv[5]); i++) {
88 chanuserflags |= CHANUSERFLAG_OPPED;
91 chanuserflags |= CHANUSERFLAG_VOICED;
94 userflags |= USERFLAG_ISIRCOP;
100 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
101 if(!isUserOnChan(user, chan)) {
102 struct ChanUser *chanuser = addChanUser(chan, user);
103 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
106 strcpy(user->ident, argv[2]);
108 strcpy(user->host, argv[3]);
109 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
110 strcpy(user->auth, argv[6]);
111 user->flags |= USERFLAG_ISAUTHED;
116 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
118 char *typestr = strstr(argv[1], ",") + 1;
119 int type = atoi(typestr);
120 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
121 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
122 if(entry == NULL || entry->type != type) return;
123 if(type & WHOQUEUETYPE_USERLIST) {
124 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
125 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
126 entry->callback(client, entry->chan);