added cmd_unvisited
[NeonServV5.git] / src / cmd_neonserv_unvisited.c
diff --git a/src/cmd_neonserv_unvisited.c b/src/cmd_neonserv_unvisited.c
new file mode 100644 (file)
index 0000000..3cfaed9
--- /dev/null
@@ -0,0 +1,107 @@
+/* cmd_neonserv_unvisited.c - NeonServ v5.1
+ * Copyright (C) 2011  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"
+
+struct neonserv_cmd_unvisited_cache {
+    struct ClientSocket *client, *textclient;
+    struct UserNode *user;
+    int duration;
+    int who_count, matches;
+};
+
+static USERLIST_CALLBACK(neonserv_cmd_unvisited_userlist_lookup);
+static int neonserv_cmd_unvisited_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, int duration);
+static void neonserv_cmd_unvisited_async2(struct neonserv_cmd_unvisited_cache *cache);
+
+CMD_BIND(neonserv_cmd_unvisited) {
+    int duration = (argc ? strToTime(user, argv[0]) : 60*60*24*7*3);
+    reply(getTextBot(), user, "NS_SEARCH_HEADER");
+    MYSQL_RES *res, *res2;
+    MYSQL_ROW row, row2;
+    struct ChanNode *channel;
+    struct neonserv_cmd_unvisited_cache *cache = malloc(sizeof(*cache));
+    if (!cache) {
+        perror("malloc() failed");
+        return;
+    }
+    cache->client = client;
+    cache->textclient = getTextBot();
+    cache->user = user;
+    cache->duration = duration;
+    cache->who_count = 0;
+    cache->matches = 0;
+    printf_mysql_query("SELECT `channel_id`, `channel_name`, `channel_nodelete` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` LEFT JOIN `users` ON `channel_registrator` = `user_id` WHERE `botid` = '%d'", client->botid);
+    res = mysql_use();
+    while ((row = mysql_fetch_row(res)) != NULL) {
+        if(!strcmp(row[2], "1")) continue;
+        printf_mysql_query("SELECT `chanuser_seen` FROM `chanusers` WHERE `chanuser_cid` = '%s' AND `chanuser_access` >= 300 ORDER BY `chanuser_seen` DESC LIMIT 1", row[0]);
+        res2 = mysql_use();
+        row2 = mysql_fetch_row(res);
+        if(row2 && (time(0) - atoi(row2[0])) < duration) continue;
+        channel = getChanByName(row[1]);
+        if(channel) {
+            cache->who_count++;
+            channel->channel_id = atoi(row[0]);
+            get_userlist_with_invisible(channel, neonserv_cmd_unvisited_userlist_lookup, cache);
+        } else {
+            reply(getTextBot(), user, "%s", row[1]);
+            cache->matches++;
+        }   
+    }
+    if(cache->who_count == 0) {
+        neonserv_cmd_unvisited_async2(cache);
+    }
+}
+
+static USERLIST_CALLBACK(neonserv_cmd_unvisited_userlist_lookup) {
+    struct neonserv_cmd_unvisited_cache *cache = data;
+    if(neonserv_cmd_unvisited_async1(cache->client, cache->textclient, cache->user, chan, cache->duration))
+        cache->matches++;
+    cache->who_count--;
+    if(cache->who_count == 0) {
+        neonserv_cmd_unvisited_async2(cache);
+    }
+}
+
+static int neonserv_cmd_unvisited_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, int duration) {
+    struct ChanUser *chanuser;
+    MYSQL_RES *res2;
+    MYSQL_ROW row2;
+    int active = 0;
+    for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+        if(isBot(chanuser->user)) continue;
+        if((chanuser->user->flags & USERFLAG_ISAUTHED)) {
+            printf_mysql_query("SELECT `chanuser_id`, `chanuser_access` FROM `chanusers` LEFT JOIN `users` ON `user_id` = `chanuser_uid` WHERE `chanuser_cid` = '%d' AND `user_user` = '%s'", chan->channel_id, escape_string(chanuser->user->auth));
+            res2 = mysql_use();
+            row2 = mysql_fetch_row(res2);
+            if(row2 && atoi(row2[1]) >= 300) {
+                printf_mysql_query("UPDATE `chanusers` SET `chanuser_seen` = UNIX_TIMESTAMP() WHERE `chanuser_id` = '%s'", row2[0]);
+                active = 1;
+            }
+        }
+    }
+    if(!active) {
+        reply(textclient, user, "%s", chan->name);
+    }
+    return !active;
+}
+
+static void neonserv_cmd_unvisited_async2(struct neonserv_cmd_unvisited_cache *cache) {
+    reply(cache->textclient, cache->user, "NS_TABLE_COUNT", cache->matches);
+    free(cache);
+}