14ecce80e1f0552258a9a997fde40be1b89c248f
[NeonServV5.git] / src / cmd_neonserv_unvisited.c
1 /* cmd_neonserv_unvisited.c - NeonServ v5.1
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_neonserv.h"
19
20 struct neonserv_cmd_unvisited_cache {
21     struct ClientSocket *client, *textclient;
22     struct UserNode *user;
23     int duration;
24     int who_count, matches;
25 };
26
27 static USERLIST_CALLBACK(neonserv_cmd_unvisited_userlist_lookup);
28 static int neonserv_cmd_unvisited_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, int duration);
29 static void neonserv_cmd_unvisited_async2(struct neonserv_cmd_unvisited_cache *cache);
30
31 CMD_BIND(neonserv_cmd_unvisited) {
32     int duration = (argc ? strToTime(user, argv[0]) : 60*60*24*7*3);
33     reply(getTextBot(), user, "NS_SEARCH_HEADER");
34     MYSQL_RES *res, *res2;
35     MYSQL_ROW row, row2;
36     struct ChanNode *channel;
37     struct neonserv_cmd_unvisited_cache *cache = malloc(sizeof(*cache));
38     if (!cache) {
39         perror("malloc() failed");
40         return;
41     }
42     cache->client = client;
43     cache->textclient = getTextBot();
44     cache->user = user;
45     cache->duration = duration;
46     cache->who_count = 0;
47     cache->matches = 0;
48     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);
49     res = mysql_use();
50     while ((row = mysql_fetch_row(res)) != NULL) {
51         if(!strcmp(row[2], "1")) continue;
52         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]);
53         res2 = mysql_use();
54         row2 = mysql_fetch_row(res);
55         if(row2 && atol(row2[0]) > (time(0) - duration)) continue;
56         channel = getChanByName(row[1]);
57         if(channel) {
58             cache->who_count++;
59             channel->channel_id = atoi(row[0]);
60             get_userlist_with_invisible(channel, neonserv_cmd_unvisited_userlist_lookup, cache);
61         } else {
62             reply(getTextBot(), user, "%s", row[1]);
63             cache->matches++;
64         }   
65     }
66     if(cache->who_count == 0) {
67         neonserv_cmd_unvisited_async2(cache);
68     }
69 }
70
71 static USERLIST_CALLBACK(neonserv_cmd_unvisited_userlist_lookup) {
72     struct neonserv_cmd_unvisited_cache *cache = data;
73     if(neonserv_cmd_unvisited_async1(cache->client, cache->textclient, cache->user, chan, cache->duration))
74         cache->matches++;
75     cache->who_count--;
76     if(cache->who_count == 0) {
77         neonserv_cmd_unvisited_async2(cache);
78     }
79 }
80
81 static int neonserv_cmd_unvisited_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, int duration) {
82     struct ChanUser *chanuser;
83     MYSQL_RES *res2;
84     MYSQL_ROW row2;
85     int active = 0;
86     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
87         if(isBot(chanuser->user)) continue;
88         if((chanuser->user->flags & USERFLAG_ISAUTHED)) {
89             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));
90             res2 = mysql_use();
91             row2 = mysql_fetch_row(res2);
92             if(row2 && atoi(row2[1]) >= 300) {
93                 printf_mysql_query("UPDATE `chanusers` SET `chanuser_seen` = UNIX_TIMESTAMP() WHERE `chanuser_id` = '%s'", row2[0]);
94                 active = 1;
95             }
96         }
97     }
98     if(!active) {
99         reply(textclient, user, "%s", chan->name);
100     }
101     return !active;
102 }
103
104 static void neonserv_cmd_unvisited_async2(struct neonserv_cmd_unvisited_cache *cache) {
105     reply(cache->textclient, cache->user, "NS_TABLE_COUNT", cache->matches);
106     free(cache);
107 }