2 #include "WHOHandler.h"
6 #include "ClientSocket.h"
8 #define WHOQUEUETYPE_ISONQUEUE 0x01
9 #define WHOQUEUETYPE_USERLIST 0x02
10 #define WHOQUEUETYPE_USERAUTH 0x04
12 struct WHOQueueEntry {
14 struct ClientSocket *client;
15 struct ChanNode *chan;
16 struct UserNode *user;
17 struct WHOQueueEntry *next;
22 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
24 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
25 struct WHOQueueEntry *entry = malloc(sizeof(*entry));
28 perror("malloc() failed");
32 entry->client = client;
34 last_entry->next = entry;
40 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
41 if(!first_entry) return NULL;
42 struct WHOQueueEntry *entry;
43 for(entry = first_entry; entry; entry = entry->next) {
44 if(entry->client == client)
47 if(entry == NULL) return NULL;
49 first_entry = first_entry->next;
50 if(last_entry == first_entry)
56 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
57 struct ClientSocket *bot;
58 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
59 if(isUserOnChan(bot->user, chan))
62 if(bot == NULL) return;
63 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
64 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
66 entry->callback = callback;
68 //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
69 putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
72 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
73 struct ClientSocket *bot;
74 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
75 if(isUserOnChan(bot->user, chan))
78 if(bot == NULL) return;
79 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
80 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
82 entry->callback = callback;
84 //WHO ".$user->getNick().",".$id." %tuhna,".$id
85 putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
88 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
91 int type = atoi(argv[1]);
92 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
93 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
94 if(entry == NULL || entry->type != type) return;
95 if(type & WHOQUEUETYPE_USERLIST) {
97 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
98 struct ChanNode *chan = entry->chan;
99 //add the user toe the channel if he isn't added, yet and update its user data
100 struct UserNode *user = getUserByNick(argv[4]);
102 user = addUser(argv[4]);
106 int chanuserflags = 0;
107 for(i = 0; i < strlen(argv[5]); i++) {
108 switch (argv[5][i]) {
110 chanuserflags |= CHANUSERFLAG_OPPED;
113 chanuserflags |= CHANUSERFLAG_VOICED;
116 userflags |= USERFLAG_ISIRCOP;
122 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
123 if(!isUserOnChan(user, chan)) {
124 struct ChanUser *chanuser = addChanUser(chan, user);
125 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
128 strcpy(user->ident, argv[2]);
130 strcpy(user->host, argv[3]);
131 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
132 strcpy(user->auth, argv[6]);
133 user->flags |= USERFLAG_ISAUTHED;
135 } else if(type & WHOQUEUETYPE_USERAUTH) {
136 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
137 if(!strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
138 strcpy(entry->user->auth, argv[5]);
139 entry->user->flags |= USERFLAG_ISAUTHED;
141 userauth_callback_t *callback = entry->callback;
142 callback(client, entry->user, entry->data);
146 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
148 char *typestr = strstr(argv[1], ",") + 1;
149 int type = atoi(typestr);
150 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
151 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
152 if(entry == NULL || entry->type != type) return;
153 if(type & WHOQUEUETYPE_USERLIST) {
154 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
155 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
156 userlist_callback_t *callback = entry->callback;
157 callback(client, entry->chan, entry->data);
162 void free_whoqueue() {
163 struct WHOQueueEntry *entry, *next;
164 for(entry = first_entry; entry; entry = next) {