67244037f9ca045a071b491d3abcb6dbb1a718f8
[NeonServV5.git] / cmd_neonserv_peek.c
1
2 #include "cmd_neonserv.h"
3
4 /*
5 * no parameters
6 */
7 static USERLIST_CALLBACK(neonserv_cmd_peek_userlist_lookup);
8 static void neonserv_cmd_peek_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan);
9
10 struct neonserv_cmd_peek_cache {
11     struct ClientSocket *client, *textclient;
12     struct UserNode *user;
13 };
14
15 CMD_BIND(neonserv_cmd_peek) {
16     struct neonserv_cmd_peek_cache *cache = malloc(sizeof(*cache));
17     if (!cache) {
18         perror("malloc() failed");
19         return;
20     }
21     cache->client = client;
22     cache->textclient = getTextBot();
23     cache->user = user;
24     get_userlist_with_invisible(chan, neonserv_cmd_peek_userlist_lookup, cache);
25 }
26
27 static USERLIST_CALLBACK(neonserv_cmd_peek_userlist_lookup) {
28     struct neonserv_cmd_peek_cache *cache = data;
29     neonserv_cmd_peek_async1(cache->client, cache->textclient, cache->user, chan);
30     free(cache);
31 }
32
33 static void neonserv_cmd_peek_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan) {
34     reply(textclient, user, "NS_PEEK_HEADER", chan->name);
35     reply(textclient, user, "NS_PEEK_TOPIC", chan->topic);
36     char tmpStr[MAXLEN];
37     getModeString(chan->modes, tmpStr);
38     reply(textclient, user, "NS_PEEK_MODES", tmpStr);
39     struct ChanUser *chanuser;
40     int op_count = 0, voice_count = 0, normal_count = 0, invi_count = 0;
41     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
42         if(chanuser->flags & CHANUSERFLAG_OPPED)
43             op_count++;
44         else if(chanuser->flags & CHANUSERFLAG_VOICED)
45             voice_count++;
46         else if(chanuser->flags & CHANUSERFLAG_VOICED)
47             invi_count++;
48         else
49             normal_count++;
50     }
51     reply(textclient, user, "NS_PEEK_USERS", op_count+voice_count+invi_count+normal_count, op_count, voice_count, normal_count, invi_count);
52     int tmpStrPos = 0;
53     int headerlen = 10 + strlen(user->nick);
54     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
55         if(chanuser->flags & CHANUSERFLAG_OPPED) {
56             if(tmpStrPos + headerlen + strlen(chanuser->user->nick) + 2 >= 512) {
57                 //clear buffer
58                 reply(textclient, user, "%s", tmpStr);
59                 tmpStrPos = 0;
60             }
61             tmpStrPos += sprintf(tmpStr + tmpStrPos, (tmpStrPos ? ", %s" : "%s"), chanuser->user->nick);
62         }
63     }
64     if(tmpStrPos) {
65         reply(textclient, user, "%s", tmpStr);
66     }
67 }