fixed WHOHandler.c
[NeonServV5.git] / src / WHOHandler.c
1 /* WHOHandler.c - NeonServ v5.1
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "WHOHandler.h"
19 #include "ChanNode.h"
20 #include "UserNode.h"
21 #include "ChanUser.h"
22 #include "ClientSocket.h"
23
24 #define WHOQUEUETYPE_ISONQUEUE 0x01
25 #define WHOQUEUETYPE_USERLIST  0x02
26 #define WHOQUEUETYPE_USERAUTH  0x04
27 #define WHOQUEUETYPE_CHECKTYPE 0x07
28 #define WHOQUEUETYPE_FOUND     0x08
29
30 struct WHOQueueEntry {
31     char type;
32     struct ClientSocket *client;
33     struct ChanNode *chan;
34     struct UserNode *user;
35     struct WHOQueueEntry *next;
36     void *callback;
37     void *data;
38 };
39
40 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
41
42 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
43     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
44     if (!entry)
45     {
46         perror("malloc() failed");
47         return NULL;
48     }
49     entry->next = NULL;
50     entry->client = client;
51     if(last_entry) {
52         last_entry->next = entry;
53         last_entry = entry;
54     } else {
55         last_entry = entry;
56         first_entry = entry;
57     }
58     return entry;
59 }
60
61 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
62     if(!first_entry) return NULL;
63     struct WHOQueueEntry *entry;
64     for(entry = first_entry; entry; entry = entry->next) {
65         if(entry->client == client)
66             break;
67     }
68     if(entry == NULL) return NULL;
69     if(freeEntry) {
70         if(entry == first_entry)
71             first_entry = entry->next;
72         if(entry == last_entry) {
73             struct WHOQueueEntry *last = NULL;
74             for(last = first_entry; last; last = last->next)
75                 if(last->next == NULL) break;
76             last_entry = last;
77         }
78     }
79     return entry;
80 }
81
82 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
83     struct ClientSocket *bot;
84     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
85         if(isUserOnChan(bot->user, chan))
86             break;
87     }
88     if(bot == NULL) return;
89     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
90     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
91     entry->chan = chan;
92     entry->callback = callback;
93     entry->data = data;
94     //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
95     putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
96 }
97
98 void get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data) {
99     struct ClientSocket *bot;
100     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
101         if(isUserOnChan(bot->user, chan))
102             break;
103     }
104     if(bot == NULL) return;
105     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
106     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
107     entry->chan = chan;
108     entry->callback = callback;
109     entry->data = data;
110     //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
111     putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type);
112 }
113
114 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
115     struct ClientSocket *bot;
116     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
117         if(bot->flags & SOCKET_FLAG_PREFERRED)
118             break;
119     }
120     if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
121     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
122     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
123     entry->user = user;
124     entry->callback = callback;
125     entry->data = data;
126     //WHO ".$user->getNick().",".$id." %tuhna,".$id
127     putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
128 }
129
130 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
131     int i;
132     if(argc < 2) return;
133     int type = atoi(argv[1]);
134     if(!type) return;
135     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
136     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
137     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
138     if(type & WHOQUEUETYPE_USERLIST) {
139         if(argc < 7) return;
140         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
141         struct ChanNode *chan = entry->chan;
142         //add the user toe the channel if he isn't added, yet and update its user data
143         //parse flags
144         int userflags = 0;
145         int chanuserflags = 0;
146         for(i = 0; i < strlen(argv[5]); i++) {
147             switch (argv[5][i]) {
148                 case '@':
149                     chanuserflags |= CHANUSERFLAG_OPPED;
150                     break;
151                 case '+':
152                     chanuserflags |= CHANUSERFLAG_VOICED;
153                     break;
154                 case '*':
155                     userflags |= USERFLAG_ISIRCOP;
156                     break;
157                 case '<':
158                     chanuserflags |= CHANUSERFLAG_INVISIBLE;
159                     break;
160                 default:
161                     break;
162             }
163         }
164         
165         struct UserNode *user;
166         if(chanuserflags & CHANUSERFLAG_INVISIBLE) {
167             user = createTempUser(argv[4]);
168             user->flags |= USERFLAG_ISTMPUSER;
169             chan->flags |= CHANFLAG_HAVE_INVISIBLES;
170             struct ChanUser *chanuser = addInvisibleChanUser(chan, user);
171             chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
172         } else {
173             user = getUserByNick(argv[4]);
174             if(user == NULL) {
175                 user = addUser(argv[4]);
176             }
177             if(!isUserOnChan(user, chan)) {
178                 struct ChanUser *chanuser = addChanUser(chan, user);
179                 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
180             }
181         }
182         user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
183         if(!*user->ident)
184             strcpy(user->ident, argv[2]);
185         if(!*user->host)
186             strcpy(user->host, argv[3]);
187         if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
188             strcpy(user->auth, argv[6]);
189             user->flags |= USERFLAG_ISAUTHED;
190         }
191     } else if(type & WHOQUEUETYPE_USERAUTH) {
192         //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
193         entry->type |= WHOQUEUETYPE_FOUND;
194         if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
195             strcpy(entry->user->auth, argv[5]);
196             entry->user->flags |= USERFLAG_ISAUTHED;
197         }
198         userauth_callback_t *callback = entry->callback;
199         callback(client, entry->user->nick, entry->user, entry->data);
200     }
201 }
202
203 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
204     if(argc < 2) return;
205     char *typestr = strstr(argv[1], ",") + 1;
206     if(!typestr) return;
207     int type = atoi(typestr);
208     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
209     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
210     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
211     if(type & WHOQUEUETYPE_USERLIST) {
212         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
213         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
214         userlist_callback_t *callback = entry->callback;
215         callback(client, entry->chan, entry->data);
216         if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
217             //remove all invisible users again
218             struct ChanUser *chanuser, *next;
219             for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
220                 next = getChannelUsers(entry->chan, chanuser);
221                 if(chanuser->flags & CHANUSERFLAG_INVISIBLE) {
222                     delChanUser(chanuser, 1);
223                 }
224             }
225         }
226     } else if(type & WHOQUEUETYPE_USERAUTH) {
227         if(!(entry->type & WHOQUEUETYPE_FOUND)) {
228             userauth_callback_t *callback = entry->callback;
229             callback(client, entry->user->nick, NULL, entry->data);
230         }
231     }
232     free(entry);
233 }
234
235 void free_whoqueue() {
236     struct WHOQueueEntry *entry, *next;
237     for(entry = first_entry; entry; entry = next) {
238         next = entry->next;
239         free(entry);
240     }
241     first_entry = NULL;
242     last_entry = NULL;
243 }