1 /* cmd_neonserv_setaccess.c - NeonServ v5.1
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "cmd_neonserv.h"
21 * argv[0] - nick / *auth
22 * argv[1] - global access
24 static AUTHLOOKUP_CALLBACK(neonserv_cmd_setaccess_auth_lookup);
25 static USERAUTH_CALLBACK(neonserv_cmd_setaccess_nick_lookup);
26 static void neonserv_cmd_setaccess_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct Event *event, char *nick, char *auth, int access);
28 struct neonserv_cmd_setaccess_cache {
29 struct ClientSocket *client, *textclient;
30 struct UserNode *user;
36 CMD_BIND(neonserv_cmd_setaccess) {
40 caccess = atoi(argv[1]);
41 if(caccess < 0 || caccess > 1000) {
42 reply(getTextBot(), user, "NS_INVALID_ACCESS", caccess);
45 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
47 if ((row = mysql_fetch_row(res)) == NULL || atoi(row[0]) < caccess) {
48 reply(getTextBot(), user, "NS_ACCESS_OUTRANKED");
51 if(argv[0][0] == '*') {
54 printf_mysql_query("SELECT `user_user` FROM `users` WHERE `user_user` = '%s'", escape_string(argv[0]));
56 if ((row = mysql_fetch_row(res)) != NULL) {
57 neonserv_cmd_setaccess_async1(client, getTextBot(), user, event, argv[0], row[0], caccess);
59 //we need to create a new user...
60 //but first lookup the auth to check if it really exists
61 struct neonserv_cmd_setaccess_cache *cache = malloc(sizeof(*cache));
63 perror("malloc() failed");
66 cache->client = client;
67 cache->textclient = getTextBot();
70 cache->access = caccess;
71 cache->nick = strdup(argv[0]);
72 lookup_authname(argv[0], neonserv_cmd_setaccess_auth_lookup, cache);
75 struct UserNode *cuser = getUserByNick(argv[0]);
77 cuser = createTempUser(argv[0]);
78 cuser->flags |= USERFLAG_ISTMPUSER;
80 if(cuser->flags & USERFLAG_ISAUTHED) {
81 neonserv_cmd_setaccess_async1(client, getTextBot(), user, event, argv[0], cuser->auth, caccess);
83 struct neonserv_cmd_setaccess_cache *cache = malloc(sizeof(*cache));
85 perror("malloc() failed");
88 cache->client = client;
89 cache->textclient = getTextBot();
92 cache->access = caccess;
93 cache->nick = strdup(argv[0]);
94 get_userauth(cuser, neonserv_cmd_setaccess_nick_lookup, cache);
99 static AUTHLOOKUP_CALLBACK(neonserv_cmd_setaccess_auth_lookup) {
100 struct neonserv_cmd_setaccess_cache *cache = data;
102 //AUTH_DOES_NOT_EXIST
103 reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
105 neonserv_cmd_setaccess_async1(cache->client, cache->textclient, cache->user, cache->event, cache->nick, auth, cache->access);
110 static USERAUTH_CALLBACK(neonserv_cmd_setaccess_nick_lookup) {
111 struct neonserv_cmd_setaccess_cache *cache = data;
113 //USER_DOES_NOT_EXIST
114 reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
116 else if(!(user->flags & USERFLAG_ISAUTHED)) {
118 reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
121 neonserv_cmd_setaccess_async1(cache->client, cache->textclient, cache->user, cache->event, user->nick, user->auth, cache->access);
126 static void neonserv_cmd_setaccess_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct Event *event, char *nick, char *auth, int caccess) {
127 //we've got a valid auth now...
130 printf_mysql_query("SELECT `user_id`, `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
132 if ((row = mysql_fetch_row(res)) != NULL) {
133 if(atoi(row[1]) != caccess)
134 printf_mysql_query("UPDATE `users` SET `user_access` = '%d' WHERE `user_id` = '%s'", caccess, row[0]);
136 printf_mysql_query("INSERT INTO `users` (`user_user`, `user_access`) VALUES ('%s', '%d')", escape_string(auth), caccess);
138 reply(textclient, user, "NS_SETACCESS_DONE", auth, caccess);