added new multi log system
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_clvl.c
1 /* cmd_neonserv_clvl.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 * 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(textclient, user, "NS_INVALID_ACCESS", caccess);
41         return;
42     }
43     if(caccess >= getChannelAccess(user, chan)) {
44         if(isGodMode(user)) {
45             event->flags |= CMDFLAG_OPLOG;
46         } else {
47             reply(textclient, 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, textclient, 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                         if(!cuser) {
60                 reply(textclient, user, "NS_USER_UNKNOWN", argv[0]);
61                 return;
62             }
63             cuser->flags |= USERFLAG_ISTMPUSER;
64         }
65         if(cuser->flags & USERFLAG_ISAUTHED) {
66             neonserv_cmd_clvl_async1(client, textclient, user, chan, event, argv[0], cuser->auth, caccess);
67         } else {
68             struct neonserv_cmd_clvl_cache *cache = malloc(sizeof(*cache));
69             if (!cache) {
70                 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
71                 return;
72             }
73             cache->client = client;
74             cache->textclient = textclient;
75             cache->user = user;
76             cache->chan = chan;
77             cache->event = event;
78             cache->nick = strdup(argv[0]);
79             cache->access = caccess;
80             get_userauth(cuser, module_id, neonserv_cmd_clvl_nick_lookup, cache);
81         }
82     }
83 }
84
85 static USERAUTH_CALLBACK(neonserv_cmd_clvl_nick_lookup) {
86     struct neonserv_cmd_clvl_cache *cache = data;
87     if(!user) {
88         //USER_DOES_NOT_EXIST
89         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
90     }
91     else if(!(user->flags & USERFLAG_ISAUTHED)) {
92         //USER_NOT_AUTHED
93         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
94     }
95     else
96         neonserv_cmd_clvl_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth, cache->access);
97     free(cache->nick);
98     free(cache);
99 }
100
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...
103     MYSQL_RES *res;
104     MYSQL_ROW row;
105     int userid;
106     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
107     res = mysql_use();
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);
112         res = mysql_use();
113         if ((row = mysql_fetch_row(res)) != NULL) {
114             //clvl
115             if(atoi(row[0]) >= getChannelAccess(user, chan)) {
116                 if(isGodMode(user)) {
117                     event->flags |= CMDFLAG_OPLOG;
118                 } else {
119                     reply(textclient, user, "NS_USER_OUTRANKED", nick);
120                     return;
121                 }
122             }
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);
125             logEvent(event);
126             return;
127         }
128     }
129     reply(textclient, user, "NS_NOT_ON_USERLIST", nick, chan->name);
130 }