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