*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_myaccess.c
1 /* cmd_neonserv_myaccess.c - NeonServ v5.2
2  * Copyright (C) 2011  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 = getTextBot();
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, neonserv_cmd_myaccess_nick_lookup, cache);
53         } else
54             neonserv_cmd_myaccess_async1(client, getTextBot(), 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, getTextBot(), 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             cuser->flags |= USERFLAG_ISTMPUSER;
71         }
72         if(cuser->flags & USERFLAG_ISAUTHED) {
73             neonserv_cmd_myaccess_async1(client, getTextBot(), user, chan, argv[0], cuser->auth, chanmatch);
74         } else {
75             struct neonserv_cmd_myaccess_cache *cache = malloc(sizeof(*cache));
76             if (!cache) {
77                 perror("malloc() failed");
78                 return;
79             }
80             cache->client = client;
81             cache->textclient = getTextBot();
82             cache->user = user;
83             cache->chan = chan;
84             cache->nick = strdup(argv[0]);
85             cache->chanmatch = (chanmatch ? strdup(chanmatch) : NULL);
86             get_userauth(cuser, neonserv_cmd_myaccess_nick_lookup, cache);
87         }
88     }
89 }
90
91 static USERAUTH_CALLBACK(neonserv_cmd_myaccess_nick_lookup) {
92     struct neonserv_cmd_myaccess_cache *cache = data;
93     if(!user) {
94         //USER_DOES_NOT_EXIST
95         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
96     }
97     else if(!(user->flags & USERFLAG_ISAUTHED)) {
98         //USER_NOT_AUTHED
99         if(!strcmp(cache->nick, cache->user->nick))
100             reply(cache->textclient, cache->user, "NS_YOU_NEED_AUTH");
101         else
102             reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
103     }
104     else
105         neonserv_cmd_myaccess_async1(cache->client, cache->textclient, cache->user, cache->chan, user->nick, user->auth, cache->chanmatch);
106     if(cache->chanmatch)
107         free(cache->chanmatch);
108     free(cache->nick);
109     free(cache);
110 }
111
112 static void neonserv_cmd_myaccess_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *nick, char *auth, char *chanmatch) {
113     //we've got a valid auth now...
114     MYSQL_RES *res, *default_res;
115     MYSQL_ROW user_row, chanuser_row, default_chan = NULL;
116     char flagBuf[5];
117     int userid, cflags, caccess, flagPos;
118     int i, total_count = 0, match_count = 0, owner_count = 0;
119     struct Table *table = NULL;
120     printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
121     res = mysql_use();
122     char *content[4];
123     content[0] = get_language_string(user, "NS_MYACCESS_HEADER_NAME");
124     content[1] = get_language_string(user, "NS_MYACCESS_HEADER_ACCESS");
125     content[2] = get_language_string(user, "NS_MYACCESS_HEADER_FLAGS");
126     content[3] = get_language_string(user, "NS_MYACCESS_HEADER_INFO");
127     if(chanmatch)
128         reply(textclient, user, "NS_MYACCESS_HEADER_MATCH", auth, chanmatch);
129     else
130         reply(textclient, user, "NS_MYACCESS_HEADER", auth);
131     if ((user_row = mysql_fetch_row(res)) != NULL) {
132         userid = atoi(user_row[0]);
133         //check if the user is already added
134         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' ORDER BY `chanuser_access` DESC, `channel_name` ASC", userid, client->botid);
135         res = mysql_use();
136         total_count = mysql_num_rows(res);
137         table = table_init(4, total_count + 1, 0);
138         table_add(table, content);
139         while ((chanuser_row = mysql_fetch_row(res)) != NULL) {
140             if(!strcmp(chanuser_row[0], "500")) owner_count++;
141             if(chanmatch && match(chanmatch, chanuser_row[0])) continue;
142             match_count++;
143             if((!chanuser_row[4] || !chanuser_row[5]) && !default_chan) {
144                 printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
145                 default_res = mysql_use();
146                 default_chan = mysql_fetch_row(default_res);
147             }
148             flagPos = 0;
149             content[0] = chanuser_row[3];
150             content[1] = chanuser_row[0];
151             cflags = atoi(chanuser_row[1]);
152             caccess = atoi(chanuser_row[0]);
153             if((cflags & DB_CHANUSER_SUSPENDED))
154                 flagPos += sprintf(flagBuf + flagPos, "s");
155             if(caccess >= (chanuser_row[4] ? atoi(chanuser_row[4]) : atoi(default_chan[0])))
156                 flagPos += sprintf(flagBuf + flagPos, "o");
157             if(caccess >= (chanuser_row[5] ? atoi(chanuser_row[5]) : atoi(default_chan[1])))
158                 flagPos += sprintf(flagBuf + flagPos, "v");
159             if((cflags & DB_CHANUSER_AUTOINVITE))
160                 flagPos += sprintf(flagBuf + flagPos, "i");
161             content[2] = flagBuf;
162             content[3] = chanuser_row[2];
163             table_add(table, content);
164         }
165     } else {
166         table = table_init(4, 1, 0);
167         table_add(table, content);
168     }
169     //send the table
170     char **table_lines = table_end(table);
171     for(i = 0; i < table->entrys; i++) {
172         reply(textclient, user, table_lines[i]);
173     }
174     if(!match_count)
175         reply(textclient, user, "NS_TABLE_NONE");
176     if(chanmatch) {
177         reply(textclient, user, "NS_MYACCESS_COUNT_MATCH", auth, total_count, owner_count, match_count, chanmatch);
178     } else {
179         reply(textclient, user, "NS_MYACCESS_COUNT", auth, total_count, owner_count);
180     }
181 }