added GPL header to all files and added INSTALL AUTHORS COPYING files.
[NeonServV5.git] / src / cmd_neonserv_kickban.c
1 /* cmd_neonserv_kickban.c - NeonServ v5.0
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_kickban_userlist_lookup);
25 static void neonserv_cmd_kickban_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_kickban_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_kickban) {
36     struct neonserv_cmd_kickban_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_kickban_userlist_lookup, cache);
51 }
52
53 static USERLIST_CALLBACK(neonserv_cmd_kickban_userlist_lookup) {
54     struct neonserv_cmd_kickban_cache *cache = data;
55     neonserv_cmd_kickban_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_kickban_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     char usermask[NICKLEN+USERLEN+HOSTLEN+3];
68     nextnick = nicks;
69     while((nick = nextnick)) {
70         nextnick = strstr(nick, ",");
71         if(nextnick) {
72             *nextnick = '\0';
73             nextnick++;
74         }
75         if(!*nick) continue;
76         if(is_ircmask(nick)) {
77             //KICK HOSTMASK
78             struct ChanUser *kickban_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(isUserProtected(chan, cuser, user)) {
90                         reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
91                         continue;
92                     }
93                     kickban_chanuser[kick_chanuser_pos++] = chanuser;
94                     if(kick_chanuser_pos > 4 && (kick_chanuser_pos * 3) > chan->usercount && !isGodMode(user)) {
95                         kick_chanuser_pos = 0;
96                         reply(textclient, user, "NS_LAME_MASK", nick);
97                         break;
98                     }
99                 }
100             }
101             for(i = 0; i < kick_chanuser_pos; i++) {
102                 if(i == 0) {
103                     putsock(client, "MODE %s +b %s", chan->name, nick);
104                 }
105                 kicked_users++;
106                 putsock(client, "KICK %s %s :(%s) %s", chan->name, kickban_chanuser[i]->user->nick, user->nick, reason);
107             }
108         } else if(*nick == '*') {
109             //KICK AUTH
110             nick++;
111             cuser = NULL;
112             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
113                 if((chanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(chanuser->user->auth, nick)) {
114                     provided_nicks++;
115                     if(isNetworkService(chanuser->user)) {
116                         reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
117                         continue;
118                     }
119                     if(!cuser) {
120                         //check if the user is protected
121                         if(isUserProtected(chan, chanuser->user, user)) {
122                             reply(textclient, user, "NS_USER_PROTECTED", chanuser->user->nick);
123                             break; //all other users are also protected...
124                         }
125                         cuser = chanuser->user;
126                     }
127                     kicked_users++;
128                     putsock(client, "MODE %s +b %s", chan->name, generate_banmask(cuser, usermask));
129                     putsock(client, "KICK %s %s :(%s) %s", chan->name, cuser->nick, user->nick, reason);
130                 }
131             }
132         } else {
133             provided_nicks++;
134             cuser = searchUserByNick(nick);
135             if(!cuser) continue;
136             chanuser = getChanUser(cuser, chan);
137             if(!chanuser) continue;
138             if(isNetworkService(cuser)) {
139                 reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
140                 continue;
141             }
142             if(isUserProtected(chan, cuser, user)) {
143                 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
144                 continue;
145             }
146             kicked_users++;
147             putsock(client, "MODE %s +b %s", chan->name, generate_banmask(cuser, usermask));
148             putsock(client, "KICK %s %s :(%s) %s", chan->name, cuser->nick, user->nick, reason);
149         }
150     }
151     if(kicked_users == provided_nicks)
152         reply(getTextBot(), user, "NS_KICKBAN_DONE", kicked_users, chan->name);
153     else
154         reply(getTextBot(), user, "NS_KICKBAN_FAIL", client->user->nick);
155     if(kicked_users)
156         logEvent(event);
157 }