*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_kick.c
1 /* cmd_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 #include "cmd_neonserv.h"
19
20 /*
21 * argv[0]    nick[,*auth[,*!*@mask[...]]]
22 * argv[1-*]  reason
23 */
24 static USERLIST_CALLBACK(neonserv_cmd_kick_userlist_lookup);
25 static void neonserv_cmd_kick_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nicks, char *reason);
26
27 struct neonserv_cmd_kick_cache {
28     struct ClientSocket *client, *textclient;
29     struct UserNode *user;
30     struct Event *event;
31     char *nicks;
32     char *reason;
33 };
34
35 CMD_BIND(neonserv_cmd_kick) {
36     struct neonserv_cmd_kick_cache *cache = malloc(sizeof(*cache));
37     if (!cache) {
38         perror("malloc() failed");
39         return;
40     }
41     cache->client = client;
42     cache->textclient = getTextBot();
43     cache->user = user;
44     cache->event = event;
45     cache->nicks = strdup(argv[0]);
46     if(argc > 1) {
47         cache->reason = strdup(merge_argv(argv, 1, argc));
48     } else
49         cache->reason = NULL;
50     get_userlist_with_invisible(chan, neonserv_cmd_kick_userlist_lookup, cache);
51 }
52
53 static USERLIST_CALLBACK(neonserv_cmd_kick_userlist_lookup) {
54     struct neonserv_cmd_kick_cache *cache = data;
55     neonserv_cmd_kick_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->nicks, (cache->reason ? cache->reason : "Bye."));
56     free(cache->nicks);
57     if(cache->reason)
58         free(cache->reason);
59     free(cache);
60 }
61
62 static void neonserv_cmd_kick_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nicks, char *reason) {
63     int i, kicked_users = 0, provided_nicks = 0;
64     char *nick, *nextnick;
65     struct UserNode *cuser;
66     struct ChanUser *chanuser;
67     nextnick = nicks;
68     while((nick = nextnick)) {
69         nextnick = strstr(nick, ",");
70         if(nextnick) {
71             *nextnick = '\0';
72             nextnick++;
73         }
74         if(!*nick) continue;
75         if(is_ircmask(nick)) {
76             //KICK HOSTMASK
77             char usermask[NICKLEN+USERLEN+HOSTLEN+3];
78             struct ChanUser *kick_chanuser[chan->usercount];
79             int kick_chanuser_pos = 0;
80             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
81                 cuser = chanuser->user;
82                 sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
83                 if(!match(nick, usermask)) {
84                     provided_nicks++;
85                     if(isNetworkService(chanuser->user)) {
86                         reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
87                         continue;
88                     }
89                     if(cuser == user || ((cuser->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cuser->auth))) {
90                         reply(textclient, user, "NS_YOU_PROTECTED");
91                         continue;
92                     }
93                     if(isUserProtected(chan, cuser, user)) {
94                         reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
95                         continue;
96                     }
97                     kick_chanuser[kick_chanuser_pos++] = chanuser;
98                     if(kick_chanuser_pos > 4 && (kick_chanuser_pos * 3) > chan->usercount && !isGodMode(user)) {
99                         kick_chanuser_pos = 0;
100                         reply(textclient, user, "NS_LAME_MASK", nick);
101                         break;
102                     }
103                 }
104             }
105             for(i = 0; i < kick_chanuser_pos; i++) {
106                 kicked_users++;
107                 putsock(client, "KICK %s %s :%s", chan->name, kick_chanuser[i]->user->nick, reason);
108             }
109         } else if(*nick == '*') {
110             //KICK AUTH
111             nick++;
112             cuser = NULL;
113             if(!stricmp(user->auth, nick)) {
114                 reply(textclient, user, "NS_YOU_PROTECTED");
115                 continue;
116             }
117             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
118                 if((chanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(chanuser->user->auth, nick)) {
119                     provided_nicks++;
120                     if(isNetworkService(chanuser->user)) {
121                         reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
122                         continue;
123                     }
124                     if(!cuser) {
125                         //check if the user is protected
126                         if(isUserProtected(chan, chanuser->user, user)) {
127                             reply(textclient, user, "NS_USER_PROTECTED", chanuser->user->nick);
128                             break; //all other users are also protected...
129                         }
130                         cuser = chanuser->user;
131                     }
132                     kicked_users++;
133                     putsock(client, "KICK %s %s :(%s) %s", chan->name, cuser->nick, user->nick, reason);
134                 }
135             }
136         } else {
137             provided_nicks++;
138             cuser = NULL;
139             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
140                 if(!stricmp(chanuser->user->nick, nick)) {
141                     cuser = chanuser->user;
142                 }
143             }
144             if(!cuser) continue;
145             if(isNetworkService(cuser)) {
146                 reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
147                 continue;
148             }
149             if(cuser == user || ((cuser->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cuser->auth))) {
150                 reply(textclient, user, "NS_YOU_PROTECTED");
151                 continue;
152             }
153             if(isUserProtected(chan, cuser, user)) {
154                 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
155                 continue;
156             }
157             kicked_users++;
158             putsock(client, "KICK %s %s :(%s) %s", chan->name, cuser->nick, user->nick, reason);
159         }
160     }
161     if(kicked_users == provided_nicks)
162         reply(getTextBot(), user, "NS_KICK_DONE", kicked_users, chan->name);
163     else
164         reply(getTextBot(), user, "NS_KICK_FAIL", client->user->nick);
165     if(kicked_users)
166         logEvent(event);
167 }