added full half-op support
[NeonServV5.git] / src / cmd_neonserv_peek.c
1 /* cmd_neonserv_peek.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 * no parameters
22 */
23 static USERLIST_CALLBACK(neonserv_cmd_peek_userlist_lookup);
24 static void neonserv_cmd_peek_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan);
25
26 struct neonserv_cmd_peek_cache {
27     struct ClientSocket *client, *textclient;
28     struct UserNode *user;
29 };
30
31 CMD_BIND(neonserv_cmd_peek) {
32     struct neonserv_cmd_peek_cache *cache = malloc(sizeof(*cache));
33     if (!cache) {
34         perror("malloc() failed");
35         return;
36     }
37     cache->client = client;
38     cache->textclient = getTextBot();
39     cache->user = user;
40     get_userlist_if_invisible(chan, neonserv_cmd_peek_userlist_lookup, cache);
41 }
42
43 static USERLIST_CALLBACK(neonserv_cmd_peek_userlist_lookup) {
44     struct neonserv_cmd_peek_cache *cache = data;
45     neonserv_cmd_peek_async1(cache->client, cache->textclient, cache->user, chan);
46     free(cache);
47 }
48
49 static void neonserv_cmd_peek_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan) {
50     reply(textclient, user, "NS_PEEK_HEADER", chan->name);
51     reply(textclient, user, "NS_PEEK_TOPIC", chan->topic);
52     char tmpStr[MAXLEN];
53     getModeString(chan->modes, tmpStr);
54     reply(textclient, user, "NS_PEEK_MODES", tmpStr);
55     struct ChanUser *chanuser;
56     int with_halfops = get_int_field("General.have_halfop");
57     int op_count = 0, halfop_count = 0, voice_count = 0, normal_count = 0, invi_count = 0;
58     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
59         if(chanuser->flags & CHANUSERFLAG_OPPED)
60             op_count++;
61         else if(with_halfops && (chanuser->flags & CHANUSERFLAG_HALFOPPED))
62             halfop_count++;
63         else if(chanuser->flags & CHANUSERFLAG_VOICED)
64             voice_count++;
65         else if(chanuser->flags & CHANUSERFLAG_INVISIBLE)
66             invi_count++;
67         else
68             normal_count++;
69     }
70     if(with_halfops)
71         reply(textclient, user, "NS_PEEK_USERS_HALFOP", op_count+halfop_count+voice_count+invi_count+normal_count, op_count, halfop_count, voice_count, normal_count, invi_count);
72     else
73         reply(textclient, user, "NS_PEEK_USERS", op_count+voice_count+invi_count+normal_count, op_count, voice_count, normal_count, invi_count);
74     int tmpStrPos = 0;
75     int headerlen = 10 + strlen(user->nick);
76     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
77         if(chanuser->flags & CHANUSERFLAG_OPPED) {
78             if(tmpStrPos + headerlen + strlen(chanuser->user->nick) + 2 >= 512) {
79                 //clear buffer
80                 reply(textclient, user, "%s", tmpStr);
81                 tmpStrPos = 0;
82             }
83             tmpStrPos += sprintf(tmpStr + tmpStrPos, (tmpStrPos ? ", %s" : "%s"), chanuser->user->nick);
84         }
85     }
86     if(tmpStrPos) {
87         reply(textclient, user, "%s", tmpStr);
88     }
89 }