added full half-op support
[NeonServV5.git] / src / cmd_neonserv_halfop.c
1 /* cmd_neonserv_halfop.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 #include "cmd_neonserv.h"
19
20 /*
21 * argv[0-*]    nicks
22 */
23 static USERLIST_CALLBACK(neonserv_cmd_halfop_userlist_lookup);
24 static void neonserv_cmd_halfop_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nicks);
25
26 struct neonserv_cmd_halfop_cache {
27     struct ClientSocket *client, *textclient;
28     struct UserNode *user;
29     struct Event *event;
30     char *nicks;
31 };
32
33 CMD_BIND(neonserv_cmd_halfop) {
34     struct neonserv_cmd_halfop_cache *cache = malloc(sizeof(*cache));
35     if (!cache) {
36         perror("malloc() failed");
37         return;
38     }
39     cache->client = client;
40     cache->textclient = getTextBot();
41     cache->user = user;
42     cache->event = event;
43     cache->nicks = strdup(merge_argv(argv, 0, argc));
44     get_userlist_if_invisible(chan, neonserv_cmd_halfop_userlist_lookup, cache);
45 }
46
47 static USERLIST_CALLBACK(neonserv_cmd_halfop_userlist_lookup) {
48     struct neonserv_cmd_halfop_cache *cache = data;
49     neonserv_cmd_halfop_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->nicks);
50     free(cache->nicks);
51     free(cache);
52 }
53
54 static void neonserv_cmd_halfop_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nicks) {
55     int total_users = 0, done_users = 0;
56     struct UserNode *cuser;
57     struct ChanUser *chanuser;
58     struct ModeBuffer *modeBuf;
59     modeBuf = initModeBuffer(client, chan);
60     char *a, *b = nicks;
61     do {
62         a = strstr(b, " ");
63         if(a) *a = '\0';
64         total_users++;
65         cuser = searchUserByNick(b);
66         if(!cuser) {
67             //check for an invisible user
68             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
69                 if(!stricmp(chanuser->user->nick, b)) {
70                     cuser = chanuser->user;
71                     break;
72                 }
73             }
74             if(!cuser) continue;
75         } else {
76             chanuser = getChanUser(cuser, chan);
77             if(!chanuser) continue;
78         }
79         done_users++;
80         if(chanuser->flags & CHANUSERFLAG_OPPED) continue;
81         modeBufferHalfop(modeBuf, b);
82         if(a) {
83             b = a+1;
84         }
85     } while(a);
86     freeModeBuffer(modeBuf);
87     if(done_users == total_users)
88         reply(textclient, user, "NS_HALFOP_DONE", chan->name);
89     else
90         reply(textclient, user, "NS_HALFOP_FAIL", client->user->nick);
91     if(done_users) 
92         logEvent(event);
93 }