9677e19cc028f0ee34ad5a79d2e43e94f6de1f06
[NeonServV5.git] / src / cmd_neonserv_users.c
1 /* cmd_neonserv_users.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 /*
21 * argv[0] - usermask
22 * argv[1] - min access
23 * argv[2] - max access
24 */
25 static USERLIST_CALLBACK(neonserv_cmd_users_userlist_lookup);
26 static void neonserv_cmd_users_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *usermask, int min_access, int max_access);
27
28 struct neonserv_cmd_users_cache {
29     struct ClientSocket *client, *textclient;
30     struct UserNode *user;
31     char *usermask;
32     int min_access;
33     int max_access;
34 };
35
36 CMD_BIND(neonserv_cmd_users) {
37     int min_access = 1, max_access = 500;
38     char *usermask = NULL;
39     if(argc > 0)
40         usermask = argv[0];
41     if(argc > 2) {
42         min_access = atoi(argv[1]);
43         max_access = atoi(argv[2]);
44     }
45     struct neonserv_cmd_users_cache *cache = malloc(sizeof(*cache));
46     if (!cache) {
47         perror("malloc() failed");
48         return;
49     }
50     cache->client = client;
51     cache->textclient = getTextBot();
52     cache->user = user;
53     cache->usermask = (usermask ? strdup(usermask) : NULL);
54     cache->min_access = min_access;
55     cache->max_access = max_access;
56     get_userlist_with_invisible(chan, neonserv_cmd_users_userlist_lookup, cache);
57 }
58
59 static USERLIST_CALLBACK(neonserv_cmd_users_userlist_lookup) {
60     struct neonserv_cmd_users_cache *cache = data;
61     neonserv_cmd_users_async1(cache->client, cache->textclient, cache->user, chan, cache->usermask, cache->min_access, cache->max_access);
62     if(cache->usermask)
63         free(cache->usermask);
64     free(cache);
65 }
66
67 static void neonserv_cmd_users_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *usermask, int min_access, int max_access) {
68     MYSQL_RES *res;
69     MYSQL_ROW row;
70     int content_count = 0, cflags, is_here, caccess, i;
71     char seenstr[MAXLEN];
72     struct Table *table;
73     struct ChanUser *chanuser;
74     printf_mysql_query("SELECT `chanuser_access`, `user_user`, `chanuser_seen`, `chanuser_flags` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d' ORDER BY `chanuser_access` DESC, `user_user` ASC", chan->channel_id);
75     res = mysql_use();
76     table = table_init(4, mysql_num_rows(res) + 1, 0);
77     if(usermask)
78         reply(textclient, user, "NS_USERS_HEADER_MATCH", chan->name, min_access, max_access, usermask);
79     else
80         reply(textclient, user, "NS_USERS_HEADER", chan->name, min_access, max_access);
81     char *content[4];
82     content[0] = get_language_string(user, "NS_USERS_HEADER_ACCESS");
83     content[1] = get_language_string(user, "NS_USERS_HEADER_ACCOUNT");
84     content[2] = get_language_string(user, "NS_USERS_HEADER_SEEN");
85     content[3] = get_language_string(user, "NS_USERS_HEADER_STATE");
86     table_add(table, content);
87     while ((row = mysql_fetch_row(res)) != NULL) {
88         caccess = atoi(row[0]);
89         if((!usermask || !match(usermask, row[1])) && caccess >= min_access && caccess <= max_access) {
90             content[0] = row[0];
91             content[1] = row[1];
92             is_here = 0;
93             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
94                 if((chanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(chanuser->user->auth, row[1])) {
95                     if((chanuser->flags & CHANUSERFLAG_INVISIBLE))
96                         is_here = 2;
97                     else {
98                         is_here = 1;
99                         break;
100                     }
101                 }
102             }
103             if(is_here) {
104                 content[2] = get_language_string(user, (is_here == 2 ? "NS_USERS_SEEN_INVISIBLE" : "NS_USERS_SEEN_HERE"));
105             } else if(!strcmp(row[2], "0")) {
106                 content[2] = get_language_string(user, "NS_USERS_SEEN_NEVER");
107             } else {
108                 timeToStr(user, (time(0) - atoi(row[2])), 2, seenstr);
109                 content[2] = seenstr; //generate time
110             }
111             cflags = atoi(row[3]);
112             if(cflags & DB_CHANUSER_SUSPENDED)
113                 content[3] = get_language_string(user, "NS_USERS_STATE_SUSPENDED");
114             else
115                 content[3] = get_language_string(user, "NS_USERS_STATE_NORMAL");
116             content_count++;
117             table_add(table, content);
118         }
119     }
120     //send the table
121     char **table_lines = table_end(table);
122     for(i = 0; i < table->entrys; i++) {
123         reply(textclient, user, table_lines[i]);
124     }
125     if(!content_count)
126         reply(textclient, user, "NS_TABLE_NONE");
127     if(usermask || min_access != 1 || max_access != 500)
128         reply(textclient, user, (table->length == 2 ? "NS_USERS_COUNT_MATCH_1" : "NS_USERS_COUNT_MATCH"), table->length - 1, chan->name, content_count);
129     else
130         reply(textclient, user, (table->length == 2 ? "NS_USERS_COUNT_1" : "NS_USERS_COUNT"), table->length - 1, chan->name);
131     table_free(table);
132 }