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