Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / event_neonserv_kick.c
1 /* event_neonserv_kick.c - NeonServ v5.3
2  * Copyright (C) 2011-2012  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;
32     if(isBot(target->user)) {
33         client = getChannelBot(chan, 0);
34         struct ClientSocket *bot = client;
35         for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
36             if(client->user == target->user) {
37                 break;
38             }
39         }
40         if(!client) return;
41         if(bot && bot != client && (isModeSet(chan->modes, 'i') || isModeSet(chan->modes, 'a') || isModeSet(chan->modes, 'l'))) {
42             struct ChanUser *chanuser = getChanUser(bot->user, chan);
43             if(chanuser && chanuser->flags & CHANUSERFLAG_OPPED)
44                 putsock(bot, "INVITE %s %s", target->user->nick, chan->name);
45         }
46         char *key = "";
47         if(isModeSet(chan->modes, 'k')) {
48             key = getModeValue(chan->modes, 'k');
49         }
50         putsock(client, "JOIN %s %s", chan->name, key);
51         return;
52     }
53     client = getBotForChannel(chan);
54     if(!client) return; //we can't "see" this event
55     if(isNetworkService(user)) return; 
56     loadChannelSettings(chan);
57     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
58     struct neonserv_event_kick_cache *cache = malloc(sizeof(*cache));
59     if (!cache) {
60         perror("malloc() failed");
61         return;
62     }
63     cache->client = client;
64     cache->user = user;
65     cache->target = target;
66     cache->userauth_pending++;
67     if(!(user->flags & USERFLAG_ISAUTHED)) {
68         get_userauth(user, neonserv_event_kick_nick_lookup, cache);
69         cache->userauth_pending++;
70     }
71     if(!(target->user->flags & USERFLAG_ISAUTHED)) {
72         get_userauth(target->user, neonserv_event_kick_nick_lookup, cache);
73         cache->userauth_pending++;
74     }
75     neonserv_event_kick_async1(cache);
76 }
77
78 static USERAUTH_CALLBACK(neonserv_event_kick_nick_lookup) {
79     struct neonserv_event_kick_cache *cache = data;
80     cache->userauth_pending--;
81     neonserv_event_kick_async1(cache);
82 }
83
84 static void neonserv_event_kick_async1(struct neonserv_event_kick_cache *cache) {
85     if(cache->userauth_pending == 0) {
86         neonserv_event_kick_async2(cache->client, cache->user, cache->target);
87         free(cache);
88     }
89 }
90
91 static void neonserv_event_kick_async2(struct ClientSocket *client, struct UserNode *user, struct ChanUser *target) {
92     if(isUserProtected(target->chan, target->user, user)) {
93         char buf[MAXLEN];
94         putsock(client, "KICK %s %s :%s", target->chan->name, user->nick, build_language_string(user, buf, "NS_USER_PROTECTED", target->user->nick));
95         putsock(client, "INVITE %s %s", target->user->nick, target->chan->name);
96     }
97 }