1 /* cmd_neonserv_clvl.c - NeonServ v5.3
2 * Copyright (C) 2011-2012 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
24 static USERAUTH_CALLBACK(neonserv_cmd_clvl_nick_lookup);
25 static void neonserv_cmd_clvl_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth, int caccess);
27 struct neonserv_cmd_clvl_cache {
28 struct ClientSocket *client, *textclient;
29 struct UserNode *user;
30 struct ChanNode *chan;
36 CMD_BIND(neonserv_cmd_clvl) {
38 caccess = atoi(argv[1]);
39 if(caccess <= 0 || caccess > 500) {
40 reply(getTextBot(), user, "NS_INVALID_ACCESS", caccess);
43 if(caccess >= getChannelAccess(user, chan)) {
45 event->flags |= CMDFLAG_OPLOG;
47 reply(getTextBot(), user, "NS_ACCESS_OUTRANKED");
51 if(argv[0][0] == '*') {
54 neonserv_cmd_clvl_async1(client, getTextBot(), user, chan, event, argv[0], argv[0], caccess);
56 struct UserNode *cuser = getUserByNick(argv[0]);
58 cuser = createTempUser(argv[0]);
59 cuser->flags |= USERFLAG_ISTMPUSER;
61 if(cuser->flags & USERFLAG_ISAUTHED) {
62 neonserv_cmd_clvl_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth, caccess);
64 struct neonserv_cmd_clvl_cache *cache = malloc(sizeof(*cache));
66 perror("malloc() failed");
69 cache->client = client;
70 cache->textclient = getTextBot();
74 cache->nick = strdup(argv[0]);
75 cache->access = caccess;
76 get_userauth(cuser, neonserv_cmd_clvl_nick_lookup, cache);
81 static USERAUTH_CALLBACK(neonserv_cmd_clvl_nick_lookup) {
82 struct neonserv_cmd_clvl_cache *cache = data;
85 reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
87 else if(!(user->flags & USERFLAG_ISAUTHED)) {
89 reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
92 neonserv_cmd_clvl_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth, cache->access);
97 static void neonserv_cmd_clvl_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth, int caccess) {
98 //we've got a valid auth now...
102 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
104 if ((row = mysql_fetch_row(res)) != NULL) {
105 userid = atoi(row[0]);
106 //check if the user is already added
107 printf_mysql_query("SELECT `chanuser_access`, `chanuser_id` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
109 if ((row = mysql_fetch_row(res)) != NULL) {
111 if(atoi(row[0]) >= getChannelAccess(user, chan)) {
112 if(isGodMode(user)) {
113 event->flags |= CMDFLAG_OPLOG;
115 reply(textclient, user, "NS_USER_OUTRANKED", nick);
119 printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d' WHERE `chanuser_id` = '%s'", caccess, row[1]);
120 reply(textclient, user, "NS_CLVL_DONE", nick, caccess, chan->name);
125 reply(textclient, user, "NS_NOT_ON_USERLIST", nick, chan->name);