*** VERSION 5.2.0 ***
[NeonServV5.git] / src / event_neonserv_kick.c
1 /* event_neonserv_kick.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 struct neonserv_event_kick_cache {
19     struct ClientSocket *client;
20     struct UserNode *user;
21     struct ChanUser *target;
22     int userauth_pending;
23 };
24
25 static USERAUTH_CALLBACK(neonserv_event_kick_nick_lookup);
26 static void neonserv_event_kick_async1(struct neonserv_event_kick_cache *cache);
27 static void neonserv_event_kick_async2(struct ClientSocket *client, struct UserNode *user, struct ChanUser *target);
28
29 static void neonserv_event_kick(struct UserNode *user, struct ChanUser *target, char *reason) {
30     struct ChanNode *chan = target->chan;
31     struct ClientSocket *client = getBotForChannel(chan);
32     if(!client) return; //we can't "see" this event
33     if(isNetworkService(user)) return; 
34     loadChannelSettings(chan);
35     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
36     struct neonserv_event_kick_cache *cache = malloc(sizeof(*cache));
37     if (!cache) {
38         perror("malloc() failed");
39         return;
40     }
41     cache->client = client;
42     cache->user = user;
43     cache->target = target;
44     cache->userauth_pending++;
45     if(!(user->flags & USERFLAG_ISAUTHED)) {
46         get_userauth(user, neonserv_event_kick_nick_lookup, cache);
47         cache->userauth_pending++;
48     }
49     if(!(target->user->flags & USERFLAG_ISAUTHED)) {
50         get_userauth(target->user, neonserv_event_kick_nick_lookup, cache);
51         cache->userauth_pending++;
52     }
53     neonserv_event_kick_async1(cache);
54 }
55
56 static USERAUTH_CALLBACK(neonserv_event_kick_nick_lookup) {
57     struct neonserv_event_kick_cache *cache = data;
58     cache->userauth_pending--;
59     neonserv_event_kick_async1(cache);
60 }
61
62 static void neonserv_event_kick_async1(struct neonserv_event_kick_cache *cache) {
63     if(cache->userauth_pending == 0) {
64         neonserv_event_kick_async2(cache->client, cache->user, cache->target);
65         free(cache);
66     }
67 }
68
69 static void neonserv_event_kick_async2(struct ClientSocket *client, struct UserNode *user, struct ChanUser *target) {
70     if(isUserProtected(target->chan, target->user, user)) {
71         char buf[MAXLEN];
72         putsock(client, "KICK %s %s :%s", target->chan->name, user->nick, build_language_string(user, buf, "NS_USER_PROTECTED", target->user->nick));
73         putsock(client, "INVITE %s %s", target->user->nick, target->chan->name);
74     }
75 }