210312afc7a5b8f30ccb6d826545d76a0dbdba75
[NeonServV5.git] / src / cmd_neonserv_peek.c
1 /* cmd_neonserv_peek.c - NeonServ v5.1
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 * 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 op_count = 0, voice_count = 0, normal_count = 0, invi_count = 0;
57     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
58         if(chanuser->flags & CHANUSERFLAG_OPPED)
59             op_count++;
60         else if(chanuser->flags & CHANUSERFLAG_VOICED)
61             voice_count++;
62         else if(chanuser->flags & CHANUSERFLAG_VOICED)
63             invi_count++;
64         else
65             normal_count++;
66     }
67     reply(textclient, user, "NS_PEEK_USERS", op_count+voice_count+invi_count+normal_count, op_count, voice_count, normal_count, invi_count);
68     int tmpStrPos = 0;
69     int headerlen = 10 + strlen(user->nick);
70     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
71         if(chanuser->flags & CHANUSERFLAG_OPPED) {
72             if(tmpStrPos + headerlen + strlen(chanuser->user->nick) + 2 >= 512) {
73                 //clear buffer
74                 reply(textclient, user, "%s", tmpStr);
75                 tmpStrPos = 0;
76             }
77             tmpStrPos += sprintf(tmpStr + tmpStrPos, (tmpStrPos ? ", %s" : "%s"), chanuser->user->nick);
78         }
79     }
80     if(tmpStrPos) {
81         reply(textclient, user, "%s", tmpStr);
82     }
83 }