added cmd_peek and added example arguments for language strings with placeholders
[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     reply(textclient, user, "NS_PEEK_MODES", getModeString(chan->modes, tmpStr));
36     struct ChanUser *chanuser;
37     int op_count = 0, voice_count = 0, normal_count = 0, invi_count = 0;
38     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
39         if(chanuser->flags & CHANUSERFLAG_OPPED)
40             op_count++;
41         else if(chanuser->flags & CHANUSERFLAG_VOICED)
42             voice_count++;
43         else if(chanuser->flags & CHANUSERFLAG_VOICED)
44             invi_count++;
45         else
46             normal_count++;
47     }
48     reply(textclient, user, "NS_PEEK_USERS", op_count+voice_count+invi_count+normal_count, op_count, voice_count, normal_count, invi_count);
49     int tmpStrPos = 0;
50     int headerlen = 10 + strlen(user->nick);
51     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
52         if(chanuser->flags & CHANUSERFLAG_OPPED) {
53             if(tmpStrPos + headerlen + strlen(chanuser->user->nick) + 2 >= 512) {
54                 //clear buffer
55                 reply(textclient, user, "%s", tmpStr);
56                 tmpStrPos = 0;
57             }
58             tmpStrPos += sprintf(tmpStr + tmpStrPos, (tmpStrPos ? ", %s" : "%s"), chanuser->user->nick);
59         }
60     }
61     if(tmpStrPos) {
62         reply(textclient, user, "%s", tmpStr);
63     }
64 }