d1204f4055ffb028db2d2c82fb4e9b146e4ad085
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_myaccess.c
1 /* cmd_neonserv_myaccess.c - NeonServ v5.6
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] - nick / *auth
22 */
23 static USERAUTH_CALLBACK(neonserv_cmd_myaccess_nick_lookup);
24 static void neonserv_cmd_myaccess_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *nick, char *auth, char *chanmatch);
25
26 struct neonserv_cmd_myaccess_cache {
27     struct ClientSocket *client, *textclient;
28     struct UserNode *user;
29     struct ChanNode *chan;
30     char *nick;
31     char *chanmatch;
32 };
33
34 CMD_BIND(neonserv_cmd_myaccess) {
35     char *chanmatch = NULL;
36     if(argc == 0 || argv[0][0] == '#') {
37         if(argc != 0) {
38             chanmatch = argv[0];
39         }
40         if(!(user->flags & USERFLAG_ISAUTHED)) {
41             struct neonserv_cmd_myaccess_cache *cache = malloc(sizeof(*cache));
42             if (!cache) {
43                 perror("malloc() failed");
44                 return;
45             }
46             cache->client = client;
47             cache->textclient = textclient;
48             cache->user = user;
49             cache->chan = chan;
50             cache->nick = strdup(argv[0]);
51             cache->chanmatch = (chanmatch ? strdup(chanmatch) : NULL);
52             get_userauth(user, module_id, neonserv_cmd_myaccess_nick_lookup, cache);
53         } else
54             neonserv_cmd_myaccess_async1(client, textclient, user, chan, user->nick, user->auth, chanmatch);
55     }
56     else if(argv[0][0] == '*') {
57         //we've got an auth
58         if(argc > 1 && argv[1][0] == '#') {
59             chanmatch = argv[1];
60         }
61         argv[0]++;
62         neonserv_cmd_myaccess_async1(client, textclient, user, chan, NULL, argv[0], chanmatch);
63     } else {
64         if(argc > 1 && argv[1][0] == '#') {
65             chanmatch = argv[1];
66         }
67         struct UserNode *cuser = getUserByNick(argv[0]);
68         if(!cuser) {
69             cuser = createTempUser(argv[0]);
70                         if(!cuser) {
71                 reply(textclient, user, "NS_USER_UNKNOWN", argv[0]);
72                 return;
73             }
74             cuser->flags |= USERFLAG_ISTMPUSER;
75         }
76         if(cuser->flags & USERFLAG_ISAUTHED) {
77             neonserv_cmd_myaccess_async1(client, textclient, user, chan, argv[0], cuser->auth, chanmatch);
78         } else {
79             struct neonserv_cmd_myaccess_cache *cache = malloc(sizeof(*cache));
80             if (!cache) {
81                 perror("malloc() failed");
82                 return;
83             }
84             cache->client = client;
85             cache->textclient = textclient;
86             cache->user = user;
87             cache->chan = chan;
88             cache->nick = strdup(argv[0]);
89             cache->chanmatch = (chanmatch ? strdup(chanmatch) : NULL);
90             get_userauth(cuser, module_id, neonserv_cmd_myaccess_nick_lookup, cache);
91         }
92     }
93 }
94
95 static USERAUTH_CALLBACK(neonserv_cmd_myaccess_nick_lookup) {
96     struct neonserv_cmd_myaccess_cache *cache = data;
97     if(!user) {
98         //USER_DOES_NOT_EXIST
99         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
100     }
101     else if(!(user->flags & USERFLAG_ISAUTHED)) {
102         //USER_NOT_AUTHED
103         if(!strcmp(cache->nick, cache->user->nick))
104             reply(cache->textclient, cache->user, "NS_YOU_NEED_AUTH");
105         else
106             reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
107     }
108     else
109         neonserv_cmd_myaccess_async1(cache->client, cache->textclient, cache->user, cache->chan, user->nick, user->auth, cache->chanmatch);
110     if(cache->chanmatch)
111         free(cache->chanmatch);
112     free(cache->nick);
113     free(cache);
114 }
115
116 static void neonserv_cmd_myaccess_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *nick, char *auth, char *chanmatch) {
117     //we've got a valid auth now...
118     if(stricmp(user->auth, auth) && !isGodMode(user)) {
119         reply(textclient, user, "NS_MYACCESS_SELF_ONLY");
120         return;
121     }
122     MYSQL_RES *res, *default_res;
123     MYSQL_ROW user_row, chanuser_row, default_chan = NULL;
124     char flagBuf[5];
125     int userid, cflags, caccess, flagPos;
126     int i, total_count = 0, match_count = 0, owner_count = 0;
127     struct Table *table = NULL;
128     printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
129     res = mysql_use();
130     char *content[4];
131     content[0] = get_language_string(user, "NS_MYACCESS_HEADER_NAME");
132     content[1] = get_language_string(user, "NS_MYACCESS_HEADER_ACCESS");
133     content[2] = get_language_string(user, "NS_MYACCESS_HEADER_FLAGS");
134     content[3] = get_language_string(user, "NS_MYACCESS_HEADER_INFO");
135     if(chanmatch)
136         reply(textclient, user, "NS_MYACCESS_HEADER_MATCH", auth, chanmatch);
137     else
138         reply(textclient, user, "NS_MYACCESS_HEADER", auth);
139     if ((user_row = mysql_fetch_row(res)) != NULL) {
140         userid = atoi(user_row[0]);
141         //check if the user is already added
142         printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags`, `chanuser_infoline`, `channel_name`, `channel_getop`, `channel_getvoice`, `botid` FROM `chanusers` LEFT JOIN `channels` ON `chanuser_cid` = `channel_id` LEFT JOIN `bot_channels` ON `chanuser_cid` = `chanid` LEFT JOIN `bots` ON `bots`.`id` = `botid` WHERE `chanuser_uid` = '%d' AND `botclass` = '%d' AND `active` = '1' ORDER BY `chanuser_access` DESC, `channel_name` ASC", userid, client->botid);
143         res = mysql_use();
144         total_count = mysql_num_rows(res);
145         table = table_init(4, total_count + 1, 0);
146         table_add(table, content);
147         while ((chanuser_row = mysql_fetch_row(res)) != NULL) {
148             if(!strcmp(chanuser_row[0], "500")) owner_count++;
149             if(chanmatch && match(chanmatch, chanuser_row[0])) continue;
150             match_count++;
151             if((!chanuser_row[4] || !chanuser_row[5]) && !default_chan) {
152                 printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
153                 default_res = mysql_use();
154                 default_chan = mysql_fetch_row(default_res);
155             }
156             flagPos = 0;
157             content[0] = chanuser_row[3];
158             content[1] = chanuser_row[0];
159             cflags = atoi(chanuser_row[1]);
160             caccess = atoi(chanuser_row[0]);
161             if((cflags & DB_CHANUSER_SUSPENDED))
162                 flagPos += sprintf(flagBuf + flagPos, "s");
163             if(caccess >= (chanuser_row[4] ? atoi(chanuser_row[4]) : atoi(default_chan[0])))
164                 flagPos += sprintf(flagBuf + flagPos, "o");
165             if(caccess >= (chanuser_row[5] ? atoi(chanuser_row[5]) : atoi(default_chan[1])))
166                 flagPos += sprintf(flagBuf + flagPos, "v");
167             if((cflags & DB_CHANUSER_AUTOINVITE))
168                 flagPos += sprintf(flagBuf + flagPos, "i");
169             content[2] = (flagPos ? flagBuf : "");
170             content[3] = chanuser_row[2];
171             table_add(table, content);
172         }
173     } else {
174         table = table_init(4, 1, 0);
175         table_add(table, content);
176     }
177     //send the table
178     char **table_lines = table_end(table);
179     for(i = 0; i < table->entrys; i++) {
180         reply(textclient, user, table_lines[i]);
181     }
182     if(!match_count)
183         reply(textclient, user, "NS_TABLE_NONE");
184     if(chanmatch) {
185         reply(textclient, user, "NS_MYACCESS_COUNT_MATCH", auth, total_count, owner_count, match_count, chanmatch);
186     } else {
187         reply(textclient, user, "NS_MYACCESS_COUNT", auth, total_count, owner_count);
188     }
189     table_free(table);
190 }