rearranged NeonServ code to be modular
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_users.c
diff --git a/src/modules/NeonServ.mod/cmd_neonserv_users.c b/src/modules/NeonServ.mod/cmd_neonserv_users.c
new file mode 100644 (file)
index 0000000..cd8d375
--- /dev/null
@@ -0,0 +1,132 @@
+/* cmd_neonserv_users.c - NeonServ v5.3
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+#include "cmd_neonserv.h"
+
+/*
+* argv[0] - usermask
+* argv[1] - min access
+* argv[2] - max access
+*/
+static USERLIST_CALLBACK(neonserv_cmd_users_userlist_lookup);
+static void neonserv_cmd_users_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *usermask, int min_access, int max_access);
+
+struct neonserv_cmd_users_cache {
+    struct ClientSocket *client, *textclient;
+    struct UserNode *user;
+    char *usermask;
+    int min_access;
+    int max_access;
+};
+
+CMD_BIND(neonserv_cmd_users) {
+    int min_access = 1, max_access = 500;
+    char *usermask = NULL;
+    if(argc > 0)
+        usermask = argv[0];
+    if(argc > 2) {
+        min_access = atoi(argv[1]);
+        max_access = atoi(argv[2]);
+    }
+    struct neonserv_cmd_users_cache *cache = malloc(sizeof(*cache));
+    if (!cache) {
+        perror("malloc() failed");
+        return;
+    }
+    cache->client = client;
+    cache->textclient = getTextBot();
+    cache->user = user;
+    cache->usermask = (usermask ? strdup(usermask) : NULL);
+    cache->min_access = min_access;
+    cache->max_access = max_access;
+    get_userlist_with_invisible(chan, neonserv_cmd_users_userlist_lookup, cache);
+}
+
+static USERLIST_CALLBACK(neonserv_cmd_users_userlist_lookup) {
+    struct neonserv_cmd_users_cache *cache = data;
+    neonserv_cmd_users_async1(cache->client, cache->textclient, cache->user, chan, cache->usermask, cache->min_access, cache->max_access);
+    if(cache->usermask)
+        free(cache->usermask);
+    free(cache);
+}
+
+static void neonserv_cmd_users_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *usermask, int min_access, int max_access) {
+    MYSQL_RES *res;
+    MYSQL_ROW row;
+    int content_count = 0, cflags, is_here, caccess, i;
+    char seenstr[MAXLEN];
+    struct Table *table;
+    struct ChanUser *chanuser;
+    printf_mysql_query("SELECT `chanuser_access`, `user_user`, `chanuser_seen`, `chanuser_flags` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d' ORDER BY `chanuser_access` DESC, `user_user` ASC", chan->channel_id);
+    res = mysql_use();
+    table = table_init(4, mysql_num_rows(res) + 1, 0);
+    if(usermask)
+        reply(textclient, user, "NS_USERS_HEADER_MATCH", chan->name, min_access, max_access, usermask);
+    else
+        reply(textclient, user, "NS_USERS_HEADER", chan->name, min_access, max_access);
+    char *content[4];
+    content[0] = get_language_string(user, "NS_USERS_HEADER_ACCESS");
+    content[1] = get_language_string(user, "NS_USERS_HEADER_ACCOUNT");
+    content[2] = get_language_string(user, "NS_USERS_HEADER_SEEN");
+    content[3] = get_language_string(user, "NS_USERS_HEADER_STATE");
+    table_add(table, content);
+    while ((row = mysql_fetch_row(res)) != NULL) {
+        caccess = atoi(row[0]);
+        if((!usermask || !match(usermask, row[1])) && caccess >= min_access && caccess <= max_access) {
+            content[0] = row[0];
+            content[1] = row[1];
+            is_here = 0;
+            for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+                if((chanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(chanuser->user->auth, row[1])) {
+                    if((chanuser->flags & CHANUSERFLAG_INVISIBLE))
+                        is_here = 2;
+                    else {
+                        is_here = 1;
+                        break;
+                    }
+                }
+            }
+            if(is_here) {
+                content[2] = get_language_string(user, (is_here == 2 ? "NS_USERS_SEEN_INVISIBLE" : "NS_USERS_SEEN_HERE"));
+            } else if(!strcmp(row[2], "0")) {
+                content[2] = get_language_string(user, "NS_USERS_SEEN_NEVER");
+            } else {
+                timeToStr(user, (time(0) - atoi(row[2])), 2, seenstr);
+                content[2] = seenstr; //generate time
+            }
+            cflags = atoi(row[3]);
+            if(cflags & DB_CHANUSER_SUSPENDED)
+                content[3] = get_language_string(user, "NS_USERS_STATE_SUSPENDED");
+            else
+                content[3] = get_language_string(user, "NS_USERS_STATE_NORMAL");
+            content_count++;
+            table_add(table, content);
+        }
+    }
+    //send the table
+    char **table_lines = table_end(table);
+    for(i = 0; i < table->entrys; i++) {
+        reply(textclient, user, table_lines[i]);
+    }
+    if(!content_count)
+        reply(textclient, user, "NS_TABLE_NONE");
+    if(usermask || min_access != 1 || max_access != 500)
+        reply(textclient, user, (table->length == 2 ? "NS_USERS_COUNT_MATCH_1" : "NS_USERS_COUNT_MATCH"), table->length - 1, chan->name, content_count);
+    else
+        reply(textclient, user, (table->length == 2 ? "NS_USERS_COUNT_1" : "NS_USERS_COUNT"), table->length - 1, chan->name);
+    table_free(table);
+}