d8fe67a0bc3c52a1a08db348a55bf92867f53666
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_nicklist.c
1 /* cmd_neonserv_nicklist.c - NeonServ v5.5
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 flags);
26 static void neonserv_cmd_nicklist_synchronize_user(struct ChanNode *chan, struct UserNode *user, int access);
27
28 #define NEONSERV_CMD_NICKLIST_FLAG_SYNCUSERS  0x01
29 #define NEONSERV_CMD_NICKLIST_FLAG_NOWHO      0x02
30 #define NEONSERV_CMD_NICKLIST_FLAG_VISCOUNT   0x04
31
32 struct neonserv_cmd_nicklist_cache {
33     struct ClientSocket *client, *textclient;
34     struct UserNode *user;
35     struct Event *event;
36     char *nickmask;
37     int flags;
38 };
39
40 CMD_BIND(neonserv_cmd_nicklist) {
41     int flags = 0;
42     while(argc) {
43         if(!stricmp(argv[0], "sync")) {
44             if(!checkChannelAccess(user, chan, "channel_canadd", 0)) {
45                 if(isGodMode(user)) {
46                     event->flags |= CMDFLAG_OPLOG;
47                 } else {
48                     reply(textclient, user, "NS_ACCESS_DENIED");
49                     return;
50                 }
51             }
52             flags |= NEONSERV_CMD_NICKLIST_FLAG_SYNCUSERS;
53             event->flags |= CMDFLAG_LOG;
54         } else if(argc && !stricmp(argv[0], "nowho") && isGodMode(user)) {
55             flags |= NEONSERV_CMD_NICKLIST_FLAG_NOWHO;
56         } else if(argc && !stricmp(argv[0], "viscount") && isGodMode(user)) {
57             flags |= NEONSERV_CMD_NICKLIST_FLAG_VISCOUNT;
58         } else
59             break;
60         argv++;
61         argc--;
62     }
63     if(flags & NEONSERV_CMD_NICKLIST_FLAG_NOWHO) {
64         neonserv_cmd_nicklist_async1(client, textclient, user, chan, event, (argc ? argv[0] : NULL), flags);
65         return;
66     }
67     struct neonserv_cmd_nicklist_cache *cache = malloc(sizeof(*cache));
68     if (!cache) {
69         perror("malloc() failed");
70         return;
71     }
72     cache->client = client;
73     cache->textclient = textclient;
74     cache->user = user;
75     cache->event = event;
76     if(argc) {
77         cache->nickmask = strdup(argv[0]);
78     } else
79         cache->nickmask = NULL;
80     cache->flags = flags;
81     get_userlist_with_invisible(chan, module_id, neonserv_cmd_nicklist_userlist_lookup, cache);
82 }
83
84 static USERLIST_CALLBACK(neonserv_cmd_nicklist_userlist_lookup) {
85     struct neonserv_cmd_nicklist_cache *cache = data;
86     neonserv_cmd_nicklist_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->nickmask, cache->flags);
87     if(cache->nickmask)
88         free(cache->nickmask);
89     free(cache);
90 }
91
92 static int neonserv_cmd_nicklist_sort_flags[] = { 
93     CHANUSERFLAG_OPPED | CHANUSERFLAG_HALFOPPED | CHANUSERFLAG_VOICED,
94     CHANUSERFLAG_OPPED | CHANUSERFLAG_HALFOPPED,
95     CHANUSERFLAG_OPPED | CHANUSERFLAG_VOICED,
96     CHANUSERFLAG_OPPED,
97     CHANUSERFLAG_HALFOPPED | CHANUSERFLAG_VOICED,
98     CHANUSERFLAG_HALFOPPED,
99     CHANUSERFLAG_VOICED,
100     CHANUSERFLAG_INVISIBLE,
101     0
102 };
103
104 static int neonserv_cmd_nicklist_sort(const void *a, const void *b) {
105     const struct ChanUser *chanuser_a = *((struct ChanUser * const *) a);
106     const struct ChanUser *chanuser_b = *((struct ChanUser * const *) b); 
107     int i_a = 0, i_b = 0;
108     while((chanuser_a->flags & (CHANUSERFLAG_OPPED | CHANUSERFLAG_VOICED | CHANUSERFLAG_INVISIBLE)) != neonserv_cmd_nicklist_sort_flags[i_a] && neonserv_cmd_nicklist_sort_flags[i_a])
109         i_a++;
110     while((chanuser_b->flags & (CHANUSERFLAG_OPPED | CHANUSERFLAG_VOICED | CHANUSERFLAG_INVISIBLE)) != neonserv_cmd_nicklist_sort_flags[i_b] && neonserv_cmd_nicklist_sort_flags[i_b])
111         i_b++;
112     if(i_a == i_b) {
113         return stricmp(chanuser_a->user->nick, chanuser_b->user->nick);
114     } else
115         return i_a - i_b;
116 }
117
118 static void neonserv_cmd_nicklist_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nickmask, int flags) {
119     MYSQL_RES *res;
120     MYSQL_ROW row, defaults = NULL;
121     struct Table *table;
122     char *content[4];
123     int userlistlen, i, j;
124     int db_enfops, db_enfvoice;
125     int caccess, synced_user;
126     struct ChanUser *chanusers[chan->usercount];
127     struct ChanUser *chanuser;
128     int chanuser_count;
129     char statebuf[5];
130     char accessbuf[9];
131     char viscountbuf[5];
132     int uaccess;
133     
134     i = 3;
135     if(flags & NEONSERV_CMD_NICKLIST_FLAG_VISCOUNT) {
136         i++;
137         content[3] = "VisCount";
138     }
139     table = table_init(i, chan->usercount + 1, 0);
140     content[0] = get_language_string(user, "NS_NICKLIST_NICK");
141     content[1] = get_language_string(user, "NS_NICKLIST_STATE");
142     content[2] = get_language_string(user, "NS_NICKLIST_ACCESS");
143     table_add(table, content);
144     
145     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);
146     res = mysql_use();
147     userlistlen = mysql_num_rows(res);
148     i = 0;
149     MYSQL_ROW userlist[userlistlen];
150     while ((row = mysql_fetch_row(res)) != NULL) {
151         userlist[i++] = row;
152     }
153     
154     printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
155     row = mysql_fetch_row(mysql_use());
156     if(row[0] == NULL || row[1] == NULL) {
157         printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
158         defaults = mysql_fetch_row(mysql_use());
159     }
160     db_enfops = atoi((row[0] ? row[0] : defaults[0]));
161     db_enfvoice = atoi((row[1] ? row[1] : defaults[1]));
162     
163     chanuser_count = 0;
164     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
165         if(nickmask && match(nickmask, chanuser->user->nick)) continue;
166         chanusers[chanuser_count++] = chanuser;
167     }
168     qsort(chanusers, chanuser_count, sizeof(struct ChanUser *), neonserv_cmd_nicklist_sort);
169     
170     caccess = getChannelAccess(user, chan);
171     synced_user = 0;
172     for(i = 0; i < chanuser_count; i++) {
173         chanuser = chanusers[i];
174         
175         content[0] = chanuser->user->nick;
176         
177         j = 0;
178         if((chanuser->flags & CHANUSERFLAG_INVISIBLE)) statebuf[j++] = '<';
179         if((chanuser->flags & CHANUSERFLAG_OPPED)) statebuf[j++] = '@';
180         if((chanuser->flags & CHANUSERFLAG_HALFOPPED)) statebuf[j++] = '%';
181         if((chanuser->flags & CHANUSERFLAG_VOICED)) statebuf[j++] = '+';
182         statebuf[j++] = '\0';
183         content[1] = statebuf;
184         
185         uaccess = 0;
186         if(chanuser->user->flags & USERFLAG_ISAUTHED) {
187             for(j = 0; j < userlistlen; j++) {
188                 if(!stricmp(chanuser->user->auth, userlist[j][1])) {
189                     uaccess = atoi(userlist[j][0]);
190                     if((((chanuser->flags & CHANUSERFLAG_OPPED) && uaccess < db_enfops) || ((chanuser->flags & CHANUSERFLAG_VOICED) && uaccess < db_enfvoice)) && !isNetworkService(chanuser->user)) {
191                         if(flags & NEONSERV_CMD_NICKLIST_FLAG_SYNCUSERS) {
192                             if((chanuser->flags & CHANUSERFLAG_OPPED) && (db_enfops < caccess || isGodMode(user))) {
193                                 if(db_enfops >= caccess)
194                                     event->flags |= CMDFLAG_OPLOG;
195                                 uaccess = db_enfops;
196                             } else if((chanuser->flags & CHANUSERFLAG_VOICED) && (caccess < db_enfvoice || isGodMode(user))) {
197                                 if(db_enfvoice >= caccess)
198                                     event->flags |= CMDFLAG_OPLOG;
199                                 uaccess = db_enfvoice;
200                             } else {
201                                 //fail...
202                                 sprintf(accessbuf, "\00307%d\003", uaccess);
203                                 break;
204                             }
205                             neonserv_cmd_nicklist_synchronize_user(chan, chanuser->user, uaccess);
206                             sprintf(accessbuf, "\00309%d\003", uaccess);
207                             synced_user = 1;
208                         } else {
209                             synced_user = 1;
210                             sprintf(accessbuf, "\00307%d\003", uaccess);
211                         }
212                     } else if((uaccess >= db_enfops && !(chanuser->flags & CHANUSERFLAG_OPPED)) || (uaccess >= db_enfvoice && !(chanuser->flags & CHANUSERFLAG_OPPED_OR_VOICED)))
213                         sprintf(accessbuf, "\00303%d\003", uaccess);
214                     else
215                         sprintf(accessbuf, "%d", uaccess);
216                     break;
217                 }
218             }
219         }
220         if(!uaccess && (chanuser->flags & CHANUSERFLAG_OPPED_OR_VOICED) && !isNetworkService(chanuser->user)) {
221             if(flags & NEONSERV_CMD_NICKLIST_FLAG_SYNCUSERS) {
222                 if((chanuser->flags & CHANUSERFLAG_OPPED) && (db_enfops < caccess || isGodMode(user))) {
223                     if(db_enfops >= caccess)
224                         event->flags |= CMDFLAG_OPLOG;
225                     uaccess = db_enfops;
226                 } else if((chanuser->flags & CHANUSERFLAG_VOICED) && (db_enfvoice < caccess || isGodMode(user))) {
227                     if(db_enfvoice >= caccess)
228                         event->flags |= CMDFLAG_OPLOG;
229                     uaccess = db_enfvoice;
230                 } else {
231                     uaccess = 0;
232                     sprintf(accessbuf, "\003040\003");
233                 }
234                 if(uaccess && (chanuser->user->flags & USERFLAG_ISAUTHED)) {
235                     neonserv_cmd_nicklist_synchronize_user(chan, chanuser->user, uaccess);
236                     sprintf(accessbuf, "\00309%d\003", uaccess);
237                     synced_user = 1;
238                 } else if(uaccess) {
239                     sprintf(accessbuf, "\003040\003");
240                 }
241             } else {
242                 synced_user = 1;
243                 sprintf(accessbuf, "\003040\003");
244             }
245         } else if(!uaccess)
246             sprintf(accessbuf, "0");
247         content[2] = accessbuf;
248         if(flags & NEONSERV_CMD_NICKLIST_FLAG_VISCOUNT) {
249             sprintf(viscountbuf, "%d", chanuser->visCount);
250             content[3] = viscountbuf;
251         }
252         table_add(table, content);
253     }
254     
255     //send the table
256     char **table_lines = table_end(table);
257     for(i = 0; i < table->entrys; i++) {
258         reply(textclient, user, table_lines[i]);
259     }
260     if(table->length == 1)
261         reply(textclient, user, "NS_TABLE_NONE");
262     reply(textclient, user, "NS_TABLE_COUNT", table->length - 1);
263     table_free(table);
264     if(synced_user) {
265         if(!(flags & NEONSERV_CMD_NICKLIST_FLAG_SYNCUSERS))
266             reply(textclient, user, "NS_NICKLIST_SYNC", db_enfops, db_enfvoice);
267         else
268             logEvent(event);
269     }
270 }
271
272 static void neonserv_cmd_nicklist_synchronize_user(struct ChanNode *chan, struct UserNode *user, int caccess) {
273     if(!(user->flags & USERFLAG_ISAUTHED)) return;
274     MYSQL_RES *res;
275     MYSQL_ROW row;
276     int userid;
277     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
278     res = mysql_use();
279     if ((row = mysql_fetch_row(res)) != NULL) {
280         userid = atoi(row[0]);
281     } else {
282         printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(user->auth));
283         userid = (int) mysql_insert_id(get_mysql_conn());
284     }
285     //check if already added
286     printf_mysql_query("SELECT `chanuser_access`, `chanuser_id`, `chanuser_seen` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
287     res = mysql_use();
288     if ((row = mysql_fetch_row(res)) != NULL) {
289         //clvl
290         if(atoi(row[0]) >= caccess) return;
291         printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d' WHERE `chanuser_id` = '%s'", caccess, row[1]);
292     } else 
293         printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chan->channel_id, userid, caccess);
294 }