1 /* cmd_neonserv_clvl.c - NeonServ v5.6
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(textclient, user, "NS_INVALID_ACCESS", caccess);
43 if(caccess >= getChannelAccess(user, chan)) {
45 event->flags |= CMDFLAG_OPLOG;
47 reply(textclient, user, "NS_ACCESS_OUTRANKED");
51 if(argv[0][0] == '*') {
54 neonserv_cmd_clvl_async1(client, textclient, user, chan, event, argv[0], argv[0], caccess);
56 struct UserNode *cuser = getUserByNick(argv[0]);
58 cuser = createTempUser(argv[0]);
60 reply(textclient, user, "NS_USER_UNKNOWN", argv[0]);
63 cuser->flags |= USERFLAG_ISTMPUSER;
65 if(cuser->flags & USERFLAG_ISAUTHED) {
66 neonserv_cmd_clvl_async1(client, textclient, user, chan, event, argv[0], cuser->auth, caccess);
68 struct neonserv_cmd_clvl_cache *cache = malloc(sizeof(*cache));
70 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
73 cache->client = client;
74 cache->textclient = textclient;
78 cache->nick = strdup(argv[0]);
79 cache->access = caccess;
80 get_userauth(cuser, module_id, neonserv_cmd_clvl_nick_lookup, cache);
85 static USERAUTH_CALLBACK(neonserv_cmd_clvl_nick_lookup) {
86 struct neonserv_cmd_clvl_cache *cache = data;
89 reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
91 else if(!(user->flags & USERFLAG_ISAUTHED)) {
93 reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
96 neonserv_cmd_clvl_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth, cache->access);
101 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) {
102 //we've got a valid auth now...
106 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
108 if ((row = mysql_fetch_row(res)) != NULL) {
109 userid = atoi(row[0]);
110 //check if the user is already added
111 printf_mysql_query("SELECT `chanuser_access`, `chanuser_id` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
113 if ((row = mysql_fetch_row(res)) != NULL) {
115 if(atoi(row[0]) >= getChannelAccess(user, chan)) {
116 if(isGodMode(user)) {
117 event->flags |= CMDFLAG_OPLOG;
119 reply(textclient, user, "NS_USER_OUTRANKED", nick);
123 printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d' WHERE `chanuser_id` = '%s'", caccess, row[1]);
124 reply(textclient, user, "NS_CLVL_DONE", nick, caccess, chan->name);
129 reply(textclient, user, "NS_NOT_ON_USERLIST", nick, chan->name);