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;
42 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
43 if(!first_entry) return NULL;
44 struct WHOQueueEntry *entry;
45 for(entry = first_entry; entry; entry = entry->next) {
46 if(entry->client == client)
49 if(entry == NULL) return NULL;
51 if(entry == first_entry)
52 first_entry = entry->next;
53 if(entry == last_entry) {
54 struct WHOQueueEntry *last;
55 for(last = first_entry; last; last = last->next)
56 if(last->next == NULL) break;
63 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
64 struct ClientSocket *bot;
65 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
66 if(isUserOnChan(bot->user, chan))
69 if(bot == NULL) return;
70 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
71 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
73 entry->callback = callback;
75 //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
76 putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
79 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
80 struct ClientSocket *bot;
81 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
82 if(bot->flags & SOCKET_FLAG_PREFERRED)
85 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
86 struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
87 entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
89 entry->callback = callback;
91 //WHO ".$user->getNick().",".$id." %tuhna,".$id
92 putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
95 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
98 int type = atoi(argv[1]);
99 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
100 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
101 if(entry == NULL || entry->type != type) return;
102 if(type & WHOQUEUETYPE_USERLIST) {
104 //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
105 struct ChanNode *chan = entry->chan;
106 //add the user toe the channel if he isn't added, yet and update its user data
107 struct UserNode *user = getUserByNick(argv[4]);
109 user = addUser(argv[4]);
113 int chanuserflags = 0;
114 for(i = 0; i < strlen(argv[5]); i++) {
115 switch (argv[5][i]) {
117 chanuserflags |= CHANUSERFLAG_OPPED;
120 chanuserflags |= CHANUSERFLAG_VOICED;
123 userflags |= USERFLAG_ISIRCOP;
129 user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
130 if(!isUserOnChan(user, chan)) {
131 struct ChanUser *chanuser = addChanUser(chan, user);
132 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
135 strcpy(user->ident, argv[2]);
137 strcpy(user->host, argv[3]);
138 if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
139 strcpy(user->auth, argv[6]);
140 user->flags |= USERFLAG_ISAUTHED;
142 } else if(type & WHOQUEUETYPE_USERAUTH) {
143 //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
144 if(!strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
145 strcpy(entry->user->auth, argv[5]);
146 entry->user->flags |= USERFLAG_ISAUTHED;
148 userauth_callback_t *callback = entry->callback;
149 callback(client, entry->user, entry->data);
153 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
155 char *typestr = strstr(argv[1], ",") + 1;
156 int type = atoi(typestr);
157 if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
158 struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
159 if(entry == NULL || entry->type != type) return;
160 if(type & WHOQUEUETYPE_USERLIST) {
161 //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
162 entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
163 userlist_callback_t *callback = entry->callback;
164 callback(client, entry->chan, entry->data);
169 void free_whoqueue() {
170 struct WHOQueueEntry *entry, *next;
171 for(entry = first_entry; entry; entry = next) {