1 /* cmd_neonserv_nicklist.c - NeonServ v5.3
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] (optional) nick mask
24 static USERLIST_CALLBACK(neonserv_cmd_nicklist_userlist_lookup);
25 static void neonserv_cmd_nicklist_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nickmask, int syncusers);
26 static void neonserv_cmd_nicklist_synchronize_user(struct ChanNode *chan, struct UserNode *user, int access);
28 struct neonserv_cmd_nicklist_cache {
29 struct ClientSocket *client, *textclient;
30 struct UserNode *user;
36 CMD_BIND(neonserv_cmd_nicklist) {
38 if(argc && !stricmp(argv[0], "sync")) {
39 if(!checkChannelAccess(user, chan, "channel_canadd", 0)) {
41 event->flags |= CMDFLAG_OPLOG;
43 reply(getTextBot(), user, "NS_ACCESS_DENIED");
50 event->flags |= CMDFLAG_LOG;
52 if(argc && !stricmp(argv[0], "nowho") && isGodMode(user)) {
55 neonserv_cmd_nicklist_async1(client, getTextBot(), user, chan, event, (argc ? argv[0] : NULL), syncusers);
58 struct neonserv_cmd_nicklist_cache *cache = malloc(sizeof(*cache));
60 perror("malloc() failed");
63 cache->client = client;
64 cache->textclient = getTextBot();
68 cache->nickmask = strdup(argv[0]);
70 cache->nickmask = NULL;
71 cache->syncusers = syncusers;
72 get_userlist_with_invisible(chan, neonserv_cmd_nicklist_userlist_lookup, cache);
75 static USERLIST_CALLBACK(neonserv_cmd_nicklist_userlist_lookup) {
76 struct neonserv_cmd_nicklist_cache *cache = data;
77 neonserv_cmd_nicklist_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->nickmask, cache->syncusers);
79 free(cache->nickmask);
83 static void neonserv_cmd_nicklist_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nickmask, int syncusers) {
85 MYSQL_ROW row, defaults = NULL;
87 table = table_init(3, chan->usercount + 1, 0);
89 content[0] = get_language_string(user, "NS_NICKLIST_NICK");
90 content[1] = get_language_string(user, "NS_NICKLIST_STATE");
91 content[2] = get_language_string(user, "NS_NICKLIST_ACCESS");
92 table_add(table, content);
93 printf_mysql_query("SELECT `chanuser_access`, `user_user`, `chanuser_flags` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d'", chan->channel_id);
95 int userlistlen = mysql_num_rows(res);
97 MYSQL_ROW userlist[userlistlen];
98 while ((row = mysql_fetch_row(res)) != NULL) {
101 int db_enfops, db_enfvoice;
102 printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
103 row = mysql_fetch_row(mysql_use());
104 if(row[0] == NULL || row[1] == NULL) {
105 printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
106 defaults = mysql_fetch_row(mysql_use());
108 db_enfops = atoi((row[0] ? row[0] : defaults[0]));
109 db_enfvoice = atoi((row[1] ? row[1] : defaults[1]));
110 int caccess = getChannelAccess(user, chan);
112 struct ChanUser *chanuser;
113 int sort_nicklist[] = {
114 CHANUSERFLAG_OPPED | CHANUSERFLAG_HALFOPPED | CHANUSERFLAG_VOICED,
115 CHANUSERFLAG_OPPED | CHANUSERFLAG_HALFOPPED,
116 CHANUSERFLAG_OPPED | CHANUSERFLAG_VOICED,
118 CHANUSERFLAG_HALFOPPED | CHANUSERFLAG_VOICED,
119 CHANUSERFLAG_HALFOPPED,
121 CHANUSERFLAG_INVISIBLE,
124 int *sort_pos = sort_nicklist;
127 sort_flags = *(sort_pos++);
132 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
133 if((chanuser->flags & (CHANUSERFLAG_OPPED | CHANUSERFLAG_VOICED | CHANUSERFLAG_INVISIBLE)) != sort_flags) continue;
134 if(nickmask && match(nickmask, chanuser->user->nick)) continue;
136 if((chanuser->flags & CHANUSERFLAG_INVISIBLE)) statebuf[stateset++] = '<';
137 if((chanuser->flags & CHANUSERFLAG_OPPED)) statebuf[stateset++] = '@';
138 if((chanuser->flags & CHANUSERFLAG_HALFOPPED)) statebuf[stateset++] = '%';
139 if((chanuser->flags & CHANUSERFLAG_VOICED)) statebuf[stateset++] = '+';
140 statebuf[stateset++] = '\0';
142 content[0] = chanuser->user->nick;
143 content[1] = statebuf;
145 if(chanuser->user->flags & USERFLAG_ISAUTHED) {
146 for(i = 0; i < userlistlen; i++) {
147 if(!stricmp(chanuser->user->auth, userlist[i][1])) {
148 uaccess = atoi(userlist[i][0]);
149 if((((chanuser->flags & CHANUSERFLAG_OPPED) && uaccess < db_enfops) || ((chanuser->flags & CHANUSERFLAG_VOICED) && uaccess < db_enfvoice)) && !isNetworkService(chanuser->user)) {
151 if((chanuser->flags & CHANUSERFLAG_OPPED) && (db_enfops < caccess || isGodMode(user))) {
152 if(db_enfops >= caccess)
153 event->flags |= CMDFLAG_OPLOG;
155 } else if((chanuser->flags & CHANUSERFLAG_VOICED) && (caccess < db_enfvoice || isGodMode(user))) {
156 if(db_enfvoice >= caccess)
157 event->flags |= CMDFLAG_OPLOG;
158 uaccess = db_enfvoice;
161 sprintf(accessbuf, "\00307%d\003", uaccess);
164 neonserv_cmd_nicklist_synchronize_user(chan, chanuser->user, uaccess);
165 sprintf(accessbuf, "\00309%d\003", uaccess);
169 sprintf(accessbuf, "\00307%d\003", uaccess);
171 } else if((uaccess >= db_enfops && !(chanuser->flags & CHANUSERFLAG_OPPED)) || (uaccess >= db_enfvoice && !(chanuser->flags & CHANUSERFLAG_OPPED_OR_VOICED)))
172 sprintf(accessbuf, "\00303%d\003", uaccess);
174 sprintf(accessbuf, "%d", uaccess);
179 if(!uaccess && (chanuser->flags & CHANUSERFLAG_OPPED_OR_VOICED) && !isNetworkService(chanuser->user)) {
181 if((chanuser->flags & CHANUSERFLAG_OPPED) && (db_enfops < caccess || isGodMode(user))) {
182 if(db_enfops >= caccess)
183 event->flags |= CMDFLAG_OPLOG;
185 } else if((chanuser->flags & CHANUSERFLAG_VOICED) && (db_enfvoice < caccess || isGodMode(user))) {
186 if(db_enfvoice >= caccess)
187 event->flags |= CMDFLAG_OPLOG;
188 uaccess = db_enfvoice;
191 sprintf(accessbuf, "\003040\003");
193 if(uaccess && (chanuser->user->flags & USERFLAG_ISAUTHED)) {
194 neonserv_cmd_nicklist_synchronize_user(chan, chanuser->user, uaccess);
195 sprintf(accessbuf, "\00309%d\003", uaccess);
198 sprintf(accessbuf, "\003040\003");
202 sprintf(accessbuf, "\003040\003");
205 sprintf(accessbuf, "0");
206 content[2] = accessbuf;
207 table_add(table, content);
209 } while(sort_flags != 0);
211 char **table_lines = table_end(table);
212 for(i = 0; i < table->entrys; i++) {
213 reply(textclient, user, table_lines[i]);
215 if(table->length == 1)
216 reply(textclient, user, "NS_TABLE_NONE");
217 reply(textclient, user, "NS_TABLE_COUNT", table->length - 1);
221 reply(textclient, user, "NS_NICKLIST_SYNC", db_enfops, db_enfvoice);
227 static void neonserv_cmd_nicklist_synchronize_user(struct ChanNode *chan, struct UserNode *user, int caccess) {
228 if(!(user->flags & USERFLAG_ISAUTHED)) return;
232 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
234 if ((row = mysql_fetch_row(res)) != NULL) {
235 userid = atoi(row[0]);
237 printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(user->auth));
238 userid = (int) mysql_insert_id(get_mysql_conn());
240 //check if already added
241 printf_mysql_query("SELECT `chanuser_access`, `chanuser_id`, `chanuser_seen` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
243 if ((row = mysql_fetch_row(res)) != NULL) {
245 if(atoi(row[0]) >= caccess) return;
246 printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d' WHERE `chanuser_id` = '%s'", caccess, row[1]);
248 printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chan->channel_id, userid, caccess);