fixed WHOHandler.c WHO queue
[NeonServV5.git] / WHOHandler.c
1
2 #include "WHOHandler.h"
3 #include "ChanNode.h"
4 #include "UserNode.h"
5 #include "ChanUser.h"
6 #include "ClientSocket.h"
7
8 #define WHOQUEUETYPE_ISONQUEUE 0x01
9 #define WHOQUEUETYPE_USERLIST  0x02
10 #define WHOQUEUETYPE_USERAUTH  0x04
11
12 struct WHOQueueEntry {
13     char type;
14     struct ClientSocket *client;
15     struct ChanNode *chan;
16     struct UserNode *user;
17     struct WHOQueueEntry *next;
18     void *callback;
19     void *data;
20 };
21
22 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
23
24 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
25     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
26     if (!entry)
27     {
28         perror("malloc() failed");
29         return NULL;
30     }
31     entry->next = NULL;
32     entry->client = client;
33     if(last_entry)
34         last_entry->next = entry;
35     else
36         last_entry = entry;
37     if(!first_entry)
38         first_entry = entry;
39     return entry;
40 }
41
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)
47             break;
48     }
49     if(entry == NULL) return NULL;
50     if(freeEntry) {
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;
57             last_entry = last;
58         }
59     }
60     return entry;
61 }
62
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))
67             break;
68     }
69     if(bot == NULL) return;
70     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
71     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
72     entry->chan = chan;
73     entry->callback = callback;
74     entry->data = data;
75     //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
76     putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
77 }
78
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)
83             break;
84     }
85     if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
86     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
87     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
88     entry->user = user;
89     entry->callback = callback;
90     entry->data = data;
91     //WHO ".$user->getNick().",".$id." %tuhna,".$id
92     putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
93 }
94
95 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
96     int i;
97     if(argc < 2) return;
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) {
103         if(argc < 7) return;
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]);
108         if(user == NULL) {
109             user = addUser(argv[4]);
110         }
111         //parse flags
112         int userflags = 0;
113         int chanuserflags = 0;
114         for(i = 0; i < strlen(argv[5]); i++) {
115             switch (argv[5][i]) {
116                 case '@':
117                     chanuserflags |= CHANUSERFLAG_OPPED;
118                     break;
119                 case '+':
120                     chanuserflags |= CHANUSERFLAG_VOICED;
121                     break;
122                 case '*':
123                     userflags |= USERFLAG_ISIRCOP;
124                     break;
125                 default:
126                     break;
127             }
128         }
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;
133         }
134         if(!*user->ident)
135             strcpy(user->ident, argv[2]);
136         if(!*user->host)
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;
141         }
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;
147         }
148         userauth_callback_t *callback = entry->callback;
149         callback(client, entry->user, entry->data);
150     }
151 }
152
153 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
154     if(argc < 2) return;
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);
165     }
166     free(entry);
167 }
168
169 void free_whoqueue() {
170     struct WHOQueueEntry *entry, *next;
171     for(entry = first_entry; entry; entry = next) {
172         next = entry->next;
173         free(entry);
174     }
175     first_entry = NULL;
176     last_entry = NULL;
177 }