added basic halfop support
[NeonServV5.git] / src / WHOHandler.c
1 /* WHOHandler.c - NeonServ v5.2
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 "ModeNode.h"
23 #include "ClientSocket.h"
24
25 #define WHOQUEUETYPE_ISONQUEUE 0x01
26 #define WHOQUEUETYPE_USERLIST  0x02
27 #define WHOQUEUETYPE_USERAUTH  0x04
28 #define WHOQUEUETYPE_CHECKTYPE 0x07
29 #define WHOQUEUETYPE_FOUND     0x08
30
31 #define MAXCALLBACKS 3
32
33 struct WHOQueueEntry {
34     char type;
35     struct ChanNode *chan;
36     struct UserNode *user;
37     struct WHOQueueEntry *next;
38     void *callback[MAXCALLBACKS];
39     void *data[MAXCALLBACKS];
40 };
41 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
42     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
43     if (!entry)
44     {
45         perror("malloc() failed");
46         return NULL;
47     }
48     entry->next = NULL;
49     if(client->whoqueue_last) {
50         client->whoqueue_last->next = entry;
51     } else {
52         client->whoqueue_first = entry;
53     }
54     client->whoqueue_last = entry;
55     return entry;
56 }
57
58 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
59     if(!client->whoqueue_first) return NULL;
60     struct WHOQueueEntry *entry = client->whoqueue_first;
61     if(freeEntry) {
62         client->whoqueue_first = entry->next;
63         if(entry == client->whoqueue_last) {
64             client->whoqueue_last = NULL;
65         }
66     }
67     return entry;
68 }
69
70 void clear_whoqueue(struct ClientSocket *client) {
71     if(!client->whoqueue_first) return;
72     struct WHOQueueEntry *entry, *next;
73     for(entry = client->whoqueue_first; entry; entry = next) {
74         next = entry->next;
75         free(entry);
76     }
77     client->whoqueue_last = NULL;
78 }
79
80 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
81     struct ClientSocket *bot;
82     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
83         if(isUserOnChan(bot->user, chan))
84             break;
85     }
86     if(bot == NULL) return;
87     //check if we really need to who the channel
88     int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST));
89     if(!do_who) {
90         struct ChanUser *chanuser;
91         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
92             if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) {
93                 do_who = 1;
94                 break;
95             }
96         }
97     }
98     if(do_who) {
99         struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
100         entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
101         entry->chan = chan;
102         entry->callback[0] = callback;
103         int i;
104         for(i = 1; i < MAXCALLBACKS; i++)
105             entry->callback[i] = NULL;
106         entry->data[0] = data;
107         for(i = 1; i < MAXCALLBACKS; i++)
108             entry->data[i] = NULL;
109         putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
110     } else
111         callback(bot, chan, data);
112 }
113
114 void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data, int force) {
115     struct ClientSocket *bot;
116     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
117         if(isUserOnChan(bot->user, chan))
118             break;
119     }
120     if(bot == NULL) return;
121     //check if we really need to who the channel
122     //invisible users can only be present if chanmode +D or +d is set!
123     int do_who = (!(chan->flags & CHANFLAG_RECEIVED_USERLIST))  || (isModeSet(chan->modes, 'd') || isModeSet(chan->modes, 'D'));
124     if(!do_who && force) {
125         struct ChanUser *chanuser;
126         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
127             if(!(chanuser->user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT)) && (time(0) - chanuser->user->last_who) > REWHO_TIMEOUT) {
128                 do_who = 1;
129                 break;
130             }
131         }
132     }
133     if(do_who) {
134         struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
135         entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
136         entry->chan = chan;
137         entry->callback[0] = callback;
138         int i;
139         for(i = 1; i < MAXCALLBACKS; i++)
140             entry->callback[i] = NULL;
141         entry->data[0] = data;
142         for(i = 1; i < MAXCALLBACKS; i++)
143             entry->data[i] = NULL;
144         putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type);
145     } else
146         callback(bot, chan, data);
147 }
148
149 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
150     //check if we have already an active WHO for this user
151     struct ClientSocket *bot, *whobot = NULL;
152     struct WHOQueueEntry *entry;
153     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
154         for(entry = bot->whoqueue_first; entry; entry = entry->next) {
155             if((entry->type & WHOQUEUETYPE_USERAUTH) && entry->user == user) {
156                 int i = 0;
157                 for(i = 1; i < MAXCALLBACKS; i++) {
158                     if(!entry->callback[i]) {
159                         entry->callback[i] = callback;
160                         entry->data[i] = data;
161                         return;
162                     }
163                 }
164             }
165         }
166         if(bot->flags & SOCKET_FLAG_PREFERRED)
167             whobot = bot;
168     }
169     bot = whobot;
170     if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
171     //check if we really need to who the user
172     if((user->flags & (USERFLAG_ISAUTHED | USERFLAG_ISIRCOP | USERFLAG_ISBOT | USERFLAG_ISSERVER)) || (time(0) - user->last_who) <= REWHO_TIMEOUT) {
173         callback(bot, user->nick, user, data);
174         return;
175     }
176     entry = addWHOQueueEntry(bot);
177     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
178     entry->user = user;
179     entry->callback[0] = callback;
180     int i;
181     for(i = 1; i < MAXCALLBACKS; i++)
182         entry->callback[i] = NULL;
183     entry->data[0] = data;
184     for(i = 1; i < MAXCALLBACKS; i++)
185             entry->data[i] = NULL;
186     //WHO ".$user->getNick().",".$id." %tuhna,".$id
187     putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
188 }
189
190 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
191     int i;
192     if(argc < 2) return;
193     int type = atoi(argv[1]);
194     if(!type) return;
195     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
196     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
197     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
198     if(type & WHOQUEUETYPE_USERLIST) {
199         if(argc < 7) return;
200         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
201         struct ChanNode *chan = entry->chan;
202         //add the user toe the channel if he isn't added, yet and update its user data
203         //parse flags
204         int userflags = 0;
205         int chanuserflags = 0;
206         for(i = 0; i < strlen(argv[5]); i++) {
207             switch (argv[5][i]) {
208                 case '@':
209                     chanuserflags |= CHANUSERFLAG_OPPED;
210                     break;
211                 case '%':
212                     chanuserflags |= CHANUSERFLAG_HALFOPPED;
213                     break;
214                 case '+':
215                     chanuserflags |= CHANUSERFLAG_VOICED;
216                     break;
217                 case '*':
218                     userflags |= USERFLAG_ISIRCOP;
219                     break;
220                 case '<':
221                     chanuserflags |= CHANUSERFLAG_INVISIBLE;
222                     break;
223                 default:
224                     break;
225             }
226         }
227         
228         struct UserNode *user = getUserByNick(argv[4]);
229         struct ChanUser *chanuser;
230         if((chanuserflags & CHANUSERFLAG_INVISIBLE) && (!user || !isBot(user))) {
231             user = createTempUser(argv[4]);
232             user->flags |= USERFLAG_ISTMPUSER;
233             chan->flags |= CHANFLAG_HAVE_INVISIBLES;
234             chanuser = addInvisibleChanUser(chan, user);
235             chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
236         } else {
237             if(user == NULL) {
238                 user = addUser(argv[4]);
239             }
240             if(!(chanuser = getChanUser(user, chan))) {
241                 chanuser = addChanUser(chan, user);
242             }
243             chanuser->flags = (chanuser->flags & ~(CHANUSERFLAG_OPPED_OR_VOICED | CHANUSERFLAG_INVISIBLE)) | chanuserflags;
244         }
245         user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
246         user->last_who = time(0);
247         if(!*user->ident)
248             strcpy(user->ident, argv[2]);
249         if(!*user->host)
250             strcpy(user->host, argv[3]);
251         if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
252             strcpy(user->auth, argv[6]);
253             user->flags |= USERFLAG_ISAUTHED;
254         }
255     } else if(type & WHOQUEUETYPE_USERAUTH) {
256         //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
257         entry->type |= WHOQUEUETYPE_FOUND;
258         entry->user->last_who = time(0);
259         if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
260             strcpy(entry->user->auth, argv[5]);
261             entry->user->flags |= USERFLAG_ISAUTHED;
262         }
263         for(i = 0; i < MAXCALLBACKS; i++) {
264             userauth_callback_t *callback = entry->callback[i];
265             if(!callback) break;
266             callback(client, entry->user->nick, entry->user, entry->data[i]);
267         }
268     }
269 }
270
271 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
272     if(argc < 2) return;
273     char *typestr = strstr(argv[1], ",");
274     if(!typestr) return;
275     typestr++;
276     int type = atoi(typestr);
277     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
278     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
279     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
280     if(type & WHOQUEUETYPE_USERLIST) {
281         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
282         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
283         userlist_callback_t *callback;
284         int i;
285         for(i = 0; i < MAXCALLBACKS; i++) {
286             callback = entry->callback[i];
287             if(!callback) break;
288             callback(client, entry->chan, entry->data[i]);
289         }
290         if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
291             //remove all invisible users again
292             struct ChanUser *chanuser, *next;
293             for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
294                 next = getChannelUsers(entry->chan, chanuser);
295                 if((chanuser->flags & CHANUSERFLAG_INVISIBLE) && !isBot(chanuser->user)) {
296                     delChanUser(chanuser, 1);
297                 }
298             }
299         }
300     } else if(type & WHOQUEUETYPE_USERAUTH) {
301         if(!(entry->type & WHOQUEUETYPE_FOUND)) {
302             userauth_callback_t *callback;
303             int i;
304             for(i = 0; i < MAXCALLBACKS; i++) {
305                 callback = entry->callback[i];
306                 if(!callback) break;
307                 callback(client, entry->user->nick, NULL, entry->data[i]);
308             }
309         }
310     }
311     free(entry);
312 }
313
314 void free_whoqueue() {
315     
316 }