tried to reorder the program structure and build process
[NeonServV5.git] / src / cmd_neonserv_peek.c
diff --git a/src/cmd_neonserv_peek.c b/src/cmd_neonserv_peek.c
new file mode 100644 (file)
index 0000000..6724403
--- /dev/null
@@ -0,0 +1,67 @@
+
+#include "cmd_neonserv.h"
+
+/*
+* no parameters
+*/
+static USERLIST_CALLBACK(neonserv_cmd_peek_userlist_lookup);
+static void neonserv_cmd_peek_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan);
+
+struct neonserv_cmd_peek_cache {
+    struct ClientSocket *client, *textclient;
+    struct UserNode *user;
+};
+
+CMD_BIND(neonserv_cmd_peek) {
+    struct neonserv_cmd_peek_cache *cache = malloc(sizeof(*cache));
+    if (!cache) {
+        perror("malloc() failed");
+        return;
+    }
+    cache->client = client;
+    cache->textclient = getTextBot();
+    cache->user = user;
+    get_userlist_with_invisible(chan, neonserv_cmd_peek_userlist_lookup, cache);
+}
+
+static USERLIST_CALLBACK(neonserv_cmd_peek_userlist_lookup) {
+    struct neonserv_cmd_peek_cache *cache = data;
+    neonserv_cmd_peek_async1(cache->client, cache->textclient, cache->user, chan);
+    free(cache);
+}
+
+static void neonserv_cmd_peek_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan) {
+    reply(textclient, user, "NS_PEEK_HEADER", chan->name);
+    reply(textclient, user, "NS_PEEK_TOPIC", chan->topic);
+    char tmpStr[MAXLEN];
+    getModeString(chan->modes, tmpStr);
+    reply(textclient, user, "NS_PEEK_MODES", tmpStr);
+    struct ChanUser *chanuser;
+    int op_count = 0, voice_count = 0, normal_count = 0, invi_count = 0;
+    for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+        if(chanuser->flags & CHANUSERFLAG_OPPED)
+            op_count++;
+        else if(chanuser->flags & CHANUSERFLAG_VOICED)
+            voice_count++;
+        else if(chanuser->flags & CHANUSERFLAG_VOICED)
+            invi_count++;
+        else
+            normal_count++;
+    }
+    reply(textclient, user, "NS_PEEK_USERS", op_count+voice_count+invi_count+normal_count, op_count, voice_count, normal_count, invi_count);
+    int tmpStrPos = 0;
+    int headerlen = 10 + strlen(user->nick);
+    for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+        if(chanuser->flags & CHANUSERFLAG_OPPED) {
+            if(tmpStrPos + headerlen + strlen(chanuser->user->nick) + 2 >= 512) {
+                //clear buffer
+                reply(textclient, user, "%s", tmpStr);
+                tmpStrPos = 0;
+            }
+            tmpStrPos += sprintf(tmpStr + tmpStrPos, (tmpStrPos ? ", %s" : "%s"), chanuser->user->nick);
+        }
+    }
+    if(tmpStrPos) {
+        reply(textclient, user, "%s", tmpStr);
+    }
+}