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_userauth(struct UserNode *user, userauth_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(bot->flags & SOCKET_FLAG_PREFERRED)
87 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
88 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
89 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
91 entry->callback = callback;
93 //WHO ".$user->getNick().",".$id." %tuhna,".$id
94 putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
97 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
100 int type = atoi(argv[1]);
101 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
102 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
103 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
104 if(type & WHOQUEUETYPE_USERLIST) {
106 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
107 struct ChanNode *chan = entry->chan;
108 //add the user toe the channel if he isn't added, yet and update its user data
109 struct UserNode *user = getUserByNick(argv[4]);
111 user = addUser(argv[4]);
115 int chanuserflags = 0;
116 for(i = 0; i < strlen(argv[5]); i++) {
117 switch (argv[5][i]) {
119 chanuserflags |= CHANUSERFLAG_OPPED;
122 chanuserflags |= CHANUSERFLAG_VOICED;
125 userflags |= USERFLAG_ISIRCOP;
131 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
132 if(!isUserOnChan(user, chan)) {
133 struct ChanUser *chanuser = addChanUser(chan, user);
134 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
137 strcpy(user->ident, argv[2]);
139 strcpy(user->host, argv[3]);
140 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
141 strcpy(user->auth, argv[6]);
142 user->flags |= USERFLAG_ISAUTHED;
144 } else if(type & WHOQUEUETYPE_USERAUTH) {
145 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
146 entry->flags |= WHOQUEUETYPE_FOUND;
147 if(!strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
148 strcpy(entry->user->auth, argv[5]);
149 entry->user->flags |= USERFLAG_ISAUTHED;
151 userauth_callback_t *callback = entry->callback;
152 callback(client, entry->user, entry->data);
156 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
158 char *typestr = strstr(argv[1], ",") + 1;
159 int type = atoi(typestr);
160 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
161 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
162 if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
163 if(type & WHOQUEUETYPE_USERLIST) {
164 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
165 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
166 userlist_callback_t *callback = entry->callback;
167 callback(client, entry->chan, entry->data);
168 } else if(type & WHOQUEUETYPE_USERAUTH) {
169 if(!(entry->flags & WHOQUEUETYPE_FOUND)) {
170 userauth_callback_t *callback = entry->callback;
171 callback(client, NULL, entry->data);
177 void free_whoqueue() {
178 struct WHOQueueEntry *entry, *next;
179 for(entry = first_entry; entry; entry = next) {