1 /* cmd_neonserv_unvisited.c - NeonServ v5.2
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "cmd_neonserv.h"
20 struct neonserv_cmd_unvisited_cache {
21 struct ClientSocket *client, *textclient;
22 struct UserNode *user;
24 int who_count, matches;
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);
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;
36 struct ChanNode *channel;
37 struct neonserv_cmd_unvisited_cache *cache = malloc(sizeof(*cache));
39 perror("malloc() failed");
42 cache->client = client;
43 cache->textclient = getTextBot();
45 cache->duration = duration;
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);
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]);
54 row2 = mysql_fetch_row(res);
55 if(row2 && atol(row2[0]) > (time(0) - duration)) continue;
56 channel = getChanByName(row[1]);
59 channel->channel_id = atoi(row[0]);
60 get_userlist_with_invisible(channel, neonserv_cmd_unvisited_userlist_lookup, cache);
62 reply(getTextBot(), user, "%s", row[1]);
66 if(cache->who_count == 0) {
67 neonserv_cmd_unvisited_async2(cache);
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))
76 if(cache->who_count == 0) {
77 neonserv_cmd_unvisited_async2(cache);
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;
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));
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]);
99 reply(textclient, user, "%s", chan->name);
104 static void neonserv_cmd_unvisited_async2(struct neonserv_cmd_unvisited_cache *cache) {
105 reply(cache->textclient, cache->user, "NS_TABLE_COUNT", cache->matches);