9274edbd2f17abf7e8b7df64dc0d4980cd34daa6
[NeonServV5.git] / src / cmd_neonserv_search.c
1 /* cmd_neonserv_search.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 #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
24
25 struct neonserv_cmd_search_criteria {
26     char *name;
27     char *registrar;
28     unsigned int flags : 16;
29     unsigned int unvisited;
30     unsigned int registered;
31     unsigned int limit : 16;
32 };
33
34 CMD_BIND(neonserv_cmd_search) {
35     //ok parse the criterias
36     struct neonserv_cmd_search_criteria *criteria = malloc(sizeof(*criteria));
37     if (!criteria) {
38         perror("malloc() failed");
39         return;
40     }
41     memset(criteria, 0, sizeof(*criteria));
42     criteria->limit = 50;
43     int i, show_chans = 0, positive;
44     if(!stricmp(argv[0], "print")) {
45         show_chans = 1;
46     }
47     for(i = 1; i < argc; i += 2) {
48         if(argc <= i+1) {
49             reply(getTextBot(), user, "MODCMD_LESS_PARAM_COUNT");
50             return;
51         }
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] == '+') {
58                 positive = 1;
59                 argv[i+1]++;
60             } else if(argv[i+1][0] == '-') {
61                 positive = 0;
62                 argv[i+1]++;
63             } else
64                 positive = 1;
65             if(!stricmp(argv[i+1], "nodelete")) {
66                 if(positive)
67                     criteria->flags |= CMD_SEARCH_FLAG_HAS_NODELETE;
68                 else
69                     criteria->flags |= CMD_SEARCH_FLAG_NOT_NODELETE;
70             } else if(!stricmp(argv[i+1], "suspended")) {
71                 if(positive)
72                     criteria->flags |= CMD_SEARCH_FLAG_HAS_SUSPENDED;
73                 else
74                     criteria->flags |= CMD_SEARCH_FLAG_NOT_SUSPENDED;
75             }
76         }
77         else if(!stricmp(argv[i], "limit")) {
78             criteria->limit = atoi(argv[i+1]);
79         }
80     }
81     int matches = 0;
82     reply(getTextBot(), user, "NS_SEARCH_HEADER");
83     MYSQL_RES *res, *res2;
84     MYSQL_ROW row, row2;
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);
86     res = mysql_use();
87     while ((row = mysql_fetch_row(res)) != NULL) {
88         if(show_chans && matches == criteria->limit) {
89             //too many
90             break;
91         }
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]);
96             res2 = mysql_use();
97             row2 = mysql_fetch_row(res);
98             if(!row2) continue;
99             if((time(0) - atoi(row2[0])) < criteria->unvisited) continue;
100         }
101         if(criteria->registered && (time(0) - atoi(row[2])) < criteria->registered) continue;
102         
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;
107         matches++;
108         //output
109         if(show_chans) {
110             reply(getTextBot(), user, "%s", row[0]);
111         }
112     }
113     reply(getTextBot(), user, "NS_TABLE_COUNT", matches);
114 }