181923c45e611760fefbfc60c5bc28260e0824b3
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_nicklist.c
1 /* cmd_neonserv_nicklist.c - NeonServ v5.3
2  * Copyright (C) 2011-2012  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]    "force"
22 * argv[1]    (optional) nick mask
23 */
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);
27
28 struct neonserv_cmd_nicklist_cache {
29     struct ClientSocket *client, *textclient;
30     struct UserNode *user;
31     struct Event *event;
32     char *nickmask;
33     int syncusers;
34 };
35
36 CMD_BIND(neonserv_cmd_nicklist) {
37     int syncusers = 0;
38     if(argc && !stricmp(argv[0], "sync")) {
39         if(!checkChannelAccess(user, chan, "channel_canadd", 0)) {
40             if(isGodMode(user)) {
41                 event->flags |= CMDFLAG_OPLOG;
42             } else {
43                 reply(getTextBot(), user, "NS_ACCESS_DENIED");
44                 return;
45             }
46         }
47         argv++;
48         argc--;
49         syncusers = 1;
50         event->flags |= CMDFLAG_LOG;
51     }
52     if(argc && !stricmp(argv[0], "nowho") && isGodMode(user)) {
53         argv++;
54         argc--;
55         neonserv_cmd_nicklist_async1(client, getTextBot(), user, chan, event, (argc ? argv[0] : NULL), syncusers);
56         return;
57     }
58     struct neonserv_cmd_nicklist_cache *cache = malloc(sizeof(*cache));
59     if (!cache) {
60         perror("malloc() failed");
61         return;
62     }
63     cache->client = client;
64     cache->textclient = getTextBot();
65     cache->user = user;
66     cache->event = event;
67     if(argc) {
68         cache->nickmask = strdup(argv[0]);
69     } else
70         cache->nickmask = NULL;
71     cache->syncusers = syncusers;
72     get_userlist_with_invisible(chan, neonserv_cmd_nicklist_userlist_lookup, cache);
73 }
74
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);
78     if(cache->nickmask)
79         free(cache->nickmask);
80     free(cache);
81 }
82
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) {
84     MYSQL_RES *res;
85     MYSQL_ROW row, defaults = NULL;
86     struct Table *table;
87     table = table_init(3, chan->usercount + 1, 0);
88     char *content[3];
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);
94     res = mysql_use();
95     int userlistlen = mysql_num_rows(res);
96     int i = 0;
97     MYSQL_ROW userlist[userlistlen];
98     while ((row = mysql_fetch_row(res)) != NULL) {
99         userlist[i++] = row;
100     }
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());
107     }
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);
111     int synced_user = 0;
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,
117             CHANUSERFLAG_OPPED,
118             CHANUSERFLAG_HALFOPPED | CHANUSERFLAG_VOICED,
119             CHANUSERFLAG_HALFOPPED,
120             CHANUSERFLAG_VOICED,
121             CHANUSERFLAG_INVISIBLE,
122             0
123         };
124     int *sort_pos = sort_nicklist;
125     int sort_flags;
126     do {
127         sort_flags = *(sort_pos++);
128         char statebuf[5];
129         char accessbuf[9];
130         int uaccess;
131         int stateset = 0;
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;
135             if(!stateset) {
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';
141             }
142             content[0] = chanuser->user->nick;
143             content[1] = statebuf;
144             uaccess = 0;
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)) {
150                             if(syncusers) {
151                                 if((chanuser->flags & CHANUSERFLAG_OPPED) && (db_enfops < caccess || isGodMode(user))) {
152                                     if(db_enfops >= caccess)
153                                         event->flags |= CMDFLAG_OPLOG;
154                                     uaccess = db_enfops;
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;
159                                 } else {
160                                     //fail...
161                                     sprintf(accessbuf, "\00307%d\003", uaccess);
162                                     break;
163                                 }
164                                 neonserv_cmd_nicklist_synchronize_user(chan, chanuser->user, uaccess);
165                                 sprintf(accessbuf, "\00309%d\003", uaccess);
166                                 synced_user = 1;
167                             } else {
168                                 synced_user = 1;
169                                 sprintf(accessbuf, "\00307%d\003", uaccess);
170                             }
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);
173                         else
174                             sprintf(accessbuf, "%d", uaccess);
175                         break;
176                     }
177                 }
178             }
179             if(!uaccess && (chanuser->flags & CHANUSERFLAG_OPPED_OR_VOICED) && !isNetworkService(chanuser->user)) {
180                 if(syncusers) {
181                     if((chanuser->flags & CHANUSERFLAG_OPPED) && (db_enfops < caccess || isGodMode(user))) {
182                         if(db_enfops >= caccess)
183                             event->flags |= CMDFLAG_OPLOG;
184                         uaccess = db_enfops;
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;
189                     } else {
190                         uaccess = 0;
191                         sprintf(accessbuf, "\003040\003");
192                     }
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);
196                         synced_user = 1;
197                     } else if(uaccess) {
198                         sprintf(accessbuf, "\003040\003");
199                     }
200                 } else {
201                     synced_user = 1;
202                     sprintf(accessbuf, "\003040\003");
203                 }
204             } else if(!uaccess)
205                 sprintf(accessbuf, "0");
206             content[2] = accessbuf;
207             table_add(table, content);
208         }
209     } while(sort_flags != 0);
210     //send the table
211     char **table_lines = table_end(table);
212     for(i = 0; i < table->entrys; i++) {
213         reply(textclient, user, table_lines[i]);
214     }
215     if(table->length == 1)
216         reply(textclient, user, "NS_TABLE_NONE");
217     reply(textclient, user, "NS_TABLE_COUNT", table->length - 1);
218     table_free(table);
219     if(synced_user) {
220         if(!syncusers)
221             reply(textclient, user, "NS_NICKLIST_SYNC", db_enfops, db_enfvoice);
222         else
223             logEvent(event);
224     }
225 }
226
227 static void neonserv_cmd_nicklist_synchronize_user(struct ChanNode *chan, struct UserNode *user, int caccess) {
228     if(!(user->flags & USERFLAG_ISAUTHED)) return;
229     MYSQL_RES *res;
230     MYSQL_ROW row;
231     int userid;
232     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
233     res = mysql_use();
234     if ((row = mysql_fetch_row(res)) != NULL) {
235         userid = atoi(row[0]);
236     } else {
237         printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(user->auth));
238         userid = (int) mysql_insert_id(get_mysql_conn());
239     }
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);
242     res = mysql_use();
243     if ((row = mysql_fetch_row(res)) != NULL) {
244         //clvl
245         if(atoi(row[0]) >= caccess) return;
246         printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d' WHERE `chanuser_id` = '%s'", caccess, row[1]);
247     } else 
248         printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chan->channel_id, userid, caccess);
249 }