1 /* cmd_neonserv_search.c - NeonServ v5.0
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 #define CMD_SEARCH_FLAG_HAS_NODELETE 0x01
21 #define CMD_SEARCH_FLAG_NOT_NODELETE 0x02
22 #define CMD_SEARCH_FLAG_HAS_SUSPENDED 0x04
23 #define CMD_SEARCH_FLAG_NOT_SUSPENDED 0x08
25 struct neonserv_cmd_search_criteria {
28 unsigned int flags : 16;
29 unsigned int unvisited;
30 unsigned int registered;
31 unsigned int limit : 16;
34 CMD_BIND(neonserv_cmd_search) {
35 //ok parse the criterias
36 struct neonserv_cmd_search_criteria *criteria = malloc(sizeof(*criteria));
38 perror("malloc() failed");
41 memset(criteria, 0, sizeof(*criteria));
43 int i, show_chans = 0, positive;
44 if(!stricmp(argv[0], "print")) {
47 for(i = 1; i < argc; i += 2) {
49 reply(getTextBot(), user, "MODCMD_LESS_PARAM_COUNT");
52 if(!stricmp(argv[i], "name")) criteria->name = argv[i+1];
53 else if(!stricmp(argv[i], "registrar")) criteria->registrar = argv[i+1];
54 else if(!stricmp(argv[i], "unvisited")) criteria->unvisited = strToTime(user, argv[i+1]);
55 else if(!stricmp(argv[i], "registered")) criteria->registered = strToTime(user, argv[i+1]);
56 else if(!stricmp(argv[i], "flags")) {
57 if(argv[i+1][0] == '+') {
60 } else if(argv[i+1][0] == '-') {
65 if(!stricmp(argv[i+1], "nodelete")) {
67 criteria->flags |= CMD_SEARCH_FLAG_HAS_NODELETE;
69 criteria->flags |= CMD_SEARCH_FLAG_NOT_NODELETE;
70 } else if(!stricmp(argv[i+1], "suspended")) {
72 criteria->flags |= CMD_SEARCH_FLAG_HAS_SUSPENDED;
74 criteria->flags |= CMD_SEARCH_FLAG_NOT_SUSPENDED;
77 else if(!stricmp(argv[i], "limit")) {
78 criteria->limit = atoi(argv[i+1]);
82 reply(getTextBot(), user, "NS_SEARCH_HEADER");
83 MYSQL_RES *res, *res2;
85 printf_mysql_query("SELECT `channel_name`, `user_user`, `channel_registered`, `channel_nodelete`, `suspended`, `channel_id` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` LEFT JOIN `users` ON `channel_registrator` = `user_id` WHERE `botid` = '%d'", client->botid);
87 while ((row = mysql_fetch_row(res)) != NULL) {
88 if(show_chans && matches == criteria->limit) {
92 if(criteria->name && match(criteria->name, row[0])) continue;
93 if(criteria->registrar && row[1] && match(criteria->registrar, row[1])) continue;
94 if(criteria->unvisited) {
95 printf_mysql_query("SELECT `chanuser_seen` FROM `chanusers` WHERE `chanuser_cid` = '%s' ORDER BY `chanuser_seen` DESC LIMIT 1", row[5]);
97 row2 = mysql_fetch_row(res);
99 if((time(0) - atoi(row2[0])) < criteria->unvisited) continue;
101 if(criteria->registered && (time(0) - atoi(row[2])) < criteria->registered) continue;
103 if((criteria->flags & CMD_SEARCH_FLAG_HAS_NODELETE) && strcmp(row[3], "1")) continue;
104 if((criteria->flags & CMD_SEARCH_FLAG_NOT_NODELETE) && strcmp(row[3], "0")) continue;
105 if((criteria->flags & CMD_SEARCH_FLAG_HAS_SUSPENDED) && strcmp(row[4], "1")) continue;
106 if((criteria->flags & CMD_SEARCH_FLAG_NOT_SUSPENDED) && strcmp(row[4], "0")) continue;
110 reply(getTextBot(), user, "%s", row[0]);
113 reply(getTextBot(), user, "NS_TABLE_COUNT", matches);