1 /* cmd_neonserv_users.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 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"
22 * argv[1] - min access
23 * argv[2] - max access
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);
28 struct neonserv_cmd_users_cache {
29 struct ClientSocket *client, *textclient;
30 struct UserNode *user;
36 CMD_BIND(neonserv_cmd_users) {
37 int min_access = 1, max_access = 500;
38 char *usermask = NULL;
42 min_access = atoi(argv[1]);
43 max_access = atoi(argv[2]);
45 struct neonserv_cmd_users_cache *cache = malloc(sizeof(*cache));
47 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
50 cache->client = client;
51 cache->textclient = textclient;
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, module_id, neonserv_cmd_users_userlist_lookup, cache);
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);
63 free(cache->usermask);
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) {
70 int content_count = 0, cflags, is_here, caccess, i;
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);
76 table = table_init(4, mysql_num_rows(res) + 1, 0);
78 reply(textclient, user, "NS_USERS_HEADER_MATCH", chan->name, min_access, max_access, usermask);
80 reply(textclient, user, "NS_USERS_HEADER", chan->name, min_access, max_access);
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])) && (min_access == 1 || caccess >= min_access) && (max_access == 500 || caccess <= max_access)) {
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))
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");
108 timeToStr(user, (time(0) - atoi(row[2])), 2, seenstr);
109 content[2] = seenstr; //generate time
111 cflags = atoi(row[3]);
112 if(cflags & DB_CHANUSER_SUSPENDED)
113 content[3] = get_language_string(user, "NS_USERS_STATE_SUSPENDED");
115 content[3] = get_language_string(user, "NS_USERS_STATE_NORMAL");
117 table_add(table, content);
121 char **table_lines = table_end(table);
122 for(i = 0; i < table->entrys; i++) {
123 reply(textclient, user, table_lines[i]);
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);
130 reply(textclient, user, (table->length == 2 ? "NS_USERS_COUNT_1" : "NS_USERS_COUNT"), table->length - 1, chan->name);