2 #include "WHOHandler.h"
6 #include "ClientSocket.h"
8 #define WHOQUEUETYPE_ISONQUEUE 0x01
9 #define WHOQUEUETYPE_USERLIST 0x02
11 struct WHOQueueEntry {
13 struct ClientSocket *client;
14 struct ChanNode *chan;
15 struct UserNode *user;
16 struct WHOQueueEntry *next;
17 userlist_callback_t *callback;
21 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
23 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
24 struct WHOQueueEntry *entry = malloc(sizeof(*entry));
27 perror("malloc() failed");
31 entry->client = client;
33 last_entry->next = entry;
39 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
40 if(!first_entry) return NULL;
41 struct WHOQueueEntry *entry;
42 for(entry = first_entry; entry; entry = entry->next) {
43 if(entry->client == client)
46 if(entry == NULL) return NULL;
48 first_entry = first_entry->next;
49 if(last_entry == first_entry)
55 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
56 struct ClientSocket *bot;
57 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
58 if(isUserOnChan(bot->user, chan))
61 if(bot == NULL) return;
62 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
63 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
65 entry->callback = callback;
67 //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
68 putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
71 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
74 int type = atoi(argv[1]);
75 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
76 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
77 if(entry == NULL || entry->type != type) return;
78 if(type & WHOQUEUETYPE_USERLIST) {
80 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
81 struct ChanNode *chan = entry->chan;
82 //add the user toe the channel if he isn't added, yet and update its user data
83 struct UserNode *user = getUserByNick(argv[4]);
85 user = addUser(argv[4]);
89 int chanuserflags = 0;
90 for(i = 0; i < strlen(argv[5]); i++) {
93 chanuserflags |= CHANUSERFLAG_OPPED;
96 chanuserflags |= CHANUSERFLAG_VOICED;
99 userflags |= USERFLAG_ISIRCOP;
105 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
106 if(!isUserOnChan(user, chan)) {
107 struct ChanUser *chanuser = addChanUser(chan, user);
108 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
111 strcpy(user->ident, argv[2]);
113 strcpy(user->host, argv[3]);
114 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
115 strcpy(user->auth, argv[6]);
116 user->flags |= USERFLAG_ISAUTHED;
121 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
123 char *typestr = strstr(argv[1], ",") + 1;
124 int type = atoi(typestr);
125 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
126 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
127 if(entry == NULL || entry->type != type) return;
128 if(type & WHOQUEUETYPE_USERLIST) {
129 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
130 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
131 entry->callback(client, entry->chan, entry->data);
136 void free_whoqueue() {
137 struct WHOQueueEntry *entry, *next;
138 for(entry = first_entry; entry; entry = next) {