Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / cmd_neonserv_unvisited.c
1 /* cmd_neonserv_unvisited.c - NeonServ v5.3
2  * Copyright (C) 2011-2012  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 = 1; /* small fake to prevent the cache to be freed too early */
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(res2);
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     cache->who_count--; //see fix on line 46
67     if(cache->who_count == 0) {
68         neonserv_cmd_unvisited_async2(cache);
69     }
70 }
71
72 static USERLIST_CALLBACK(neonserv_cmd_unvisited_userlist_lookup) {
73     struct neonserv_cmd_unvisited_cache *cache = data;
74     if(neonserv_cmd_unvisited_async1(cache->client, cache->textclient, cache->user, chan, cache->duration))
75         cache->matches++;
76     cache->who_count--;
77     if(cache->who_count == 0) {
78         neonserv_cmd_unvisited_async2(cache);
79     }
80 }
81
82 static int neonserv_cmd_unvisited_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, int duration) {
83     struct ChanUser *chanuser;
84     MYSQL_RES *res2;
85     MYSQL_ROW row2;
86     int active = 0;
87     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
88         if(isBot(chanuser->user)) continue;
89         if((chanuser->user->flags & USERFLAG_ISAUTHED)) {
90             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));
91             res2 = mysql_use();
92             row2 = mysql_fetch_row(res2);
93             if(row2 && atoi(row2[1]) >= 300) {
94                 printf_mysql_query("UPDATE `chanusers` SET `chanuser_seen` = UNIX_TIMESTAMP() WHERE `chanuser_id` = '%s'", row2[0]);
95                 active = 1;
96             }
97         }
98     }
99     if(!active) {
100         reply(textclient, user, "%s", chan->name);
101     }
102     return !active;
103 }
104
105 static void neonserv_cmd_unvisited_async2(struct neonserv_cmd_unvisited_cache *cache) {
106     reply(cache->textclient, cache->user, "NS_TABLE_COUNT", cache->matches);
107     free(cache);
108 }