fa4a9173dc199b5eaf7bdf94077c0956f2af2450
[NeonServV5.git] / src / cmd_neonserv_clvl.c
1 /* cmd_neonserv_clvl.c - NeonServ v5.1
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 * argv[1] - access
23 */
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);
26
27 struct neonserv_cmd_clvl_cache {
28     struct ClientSocket *client, *textclient;
29     struct UserNode *user;
30     struct ChanNode *chan;
31     struct Event *event;
32     char *nick;
33     int access;
34 };
35
36 CMD_BIND(neonserv_cmd_clvl) {
37     int caccess;
38     caccess = atoi(argv[1]);
39     if(caccess <= 0 || caccess > 500) {
40         reply(getTextBot(), user, "NS_INVALID_ACCESS", caccess);
41         return;
42     }
43     if(caccess >= getChannelAccess(user, chan, 0)) {
44         if(isGodMode(user)) {
45             event->flags |= CMDFLAG_OPLOG;
46         } else {
47             reply(getTextBot(), user, "NS_ACCESS_OUTRANKED");
48             return;
49         }
50     }
51     if(argv[0][0] == '*') {
52         //we've got an auth
53         argv[0]++;
54         neonserv_cmd_clvl_async1(client, getTextBot(), user, chan, event, argv[0], argv[0], caccess);
55     } else {
56         struct UserNode *cuser = getUserByNick(argv[0]);
57         if(!cuser) {
58             cuser = createTempUser(argv[0]);
59             cuser->flags |= USERFLAG_ISTMPUSER;
60         }
61         if(cuser->flags & USERFLAG_ISAUTHED) {
62             neonserv_cmd_clvl_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth, caccess);
63         } else {
64             struct neonserv_cmd_clvl_cache *cache = malloc(sizeof(*cache));
65             if (!cache) {
66                 perror("malloc() failed");
67                 return;
68             }
69             cache->client = client;
70             cache->textclient = getTextBot();
71             cache->user = user;
72             cache->chan = chan;
73             cache->event = event;
74             cache->nick = strdup(argv[0]);
75             cache->access = caccess;
76             get_userauth(cuser, neonserv_cmd_clvl_nick_lookup, cache);
77         }
78     }
79 }
80
81 static USERAUTH_CALLBACK(neonserv_cmd_clvl_nick_lookup) {
82     struct neonserv_cmd_clvl_cache *cache = data;
83     if(!user) {
84         //USER_DOES_NOT_EXIST
85         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
86     }
87     else if(!(user->flags & USERFLAG_ISAUTHED)) {
88         //USER_NOT_AUTHED
89         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
90     }
91     else
92         neonserv_cmd_clvl_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth, cache->access);
93     free(cache->nick);
94     free(cache);
95 }
96
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...
99     MYSQL_RES *res;
100     MYSQL_ROW row;
101     int userid;
102     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
103     res = mysql_use();
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);
108         res = mysql_use();
109         if ((row = mysql_fetch_row(res)) != NULL) {
110             //clvl
111             if(atoi(row[0]) >= getChannelAccess(user, chan, 1)) {
112                 reply(textclient, user, "NS_USER_OUTRANKED", nick);
113                 return;
114             }
115             printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d' WHERE `chanuser_id` = '%s'", caccess, row[1]);
116             reply(textclient, user, "NS_CLVL_DONE", nick, caccess, chan->name);
117             logEvent(event);
118             return;
119         }
120     }
121     reply(textclient, user, "NS_NOT_ON_USERLIST", nick, chan->name);
122 }