c65c220ff34ab7078d3b701884646e329370f507
[NeonServV5.git] / src / cmd_neonserv_deluser.c
1 /* cmd_neonserv_deluser.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 */
23 static USERAUTH_CALLBACK(neonserv_cmd_deluser_nick_lookup);
24 static void neonserv_cmd_deluser_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth);
25
26 struct neonserv_cmd_deluser_cache {
27     struct ClientSocket *client, *textclient;
28     struct UserNode *user;
29     struct ChanNode *chan;
30     struct Event *event;
31     char *nick;
32 };
33
34 CMD_BIND(neonserv_cmd_deluser) {
35     if(argv[0][0] == '*') {
36         //we've got an auth
37         argv[0]++;
38         neonserv_cmd_deluser_async1(client, getTextBot(), user, chan, event, argv[0], argv[0]);
39     } else {
40         struct UserNode *cuser = getUserByNick(argv[0]);
41         if(!cuser) {
42             cuser = createTempUser(argv[0]);
43             cuser->flags |= USERFLAG_ISTMPUSER;
44         }
45         if(cuser->flags & USERFLAG_ISAUTHED) {
46             neonserv_cmd_deluser_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth);
47         } else {
48             struct neonserv_cmd_deluser_cache *cache = malloc(sizeof(*cache));
49             if (!cache) {
50                 perror("malloc() failed");
51                 return;
52             }
53             cache->client = client;
54             cache->textclient = getTextBot();
55             cache->user = user;
56             cache->chan = chan;
57             cache->event = event;
58             cache->nick = strdup(argv[0]);
59             get_userauth(cuser, neonserv_cmd_deluser_nick_lookup, cache);
60         }
61     }
62 }
63
64 static USERAUTH_CALLBACK(neonserv_cmd_deluser_nick_lookup) {
65     struct neonserv_cmd_deluser_cache *cache = data;
66     if(!user) {
67         //USER_DOES_NOT_EXIST
68         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
69     }
70     else if(!(user->flags & USERFLAG_ISAUTHED)) {
71         //USER_NOT_AUTHED
72         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
73     }
74     else
75         neonserv_cmd_deluser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth);
76     free(cache->nick);
77     free(cache);
78 }
79
80 static void neonserv_cmd_deluser_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth) {
81     //we've got a valid auth now...
82     MYSQL_RES *res;
83     MYSQL_ROW row;
84     int userid;
85     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
86     res = mysql_use();
87     if ((row = mysql_fetch_row(res)) != NULL) {
88         userid = atoi(row[0]);
89         //check if the user is already added
90         printf_mysql_query("SELECT `chanuser_access`, `chanuser_id` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
91         res = mysql_use();
92         if ((row = mysql_fetch_row(res)) != NULL) {
93             if(atoi(row[0]) >= getChannelAccess(user, chan, 0)) {
94                 if(isGodMode(user)) {
95                     event->flags |= CMDFLAG_OPLOG;
96                 } else {
97                     reply(textclient, user, "NS_USER_OUTRANKED", nick);
98                     return;
99                 }
100             }
101             //delete
102             printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[1]);
103             reply(textclient, user, "NS_DELUSER_DONE", nick, atoi(row[0]), chan->name);
104             logEvent(event);
105             return;
106         }
107     }
108     reply(textclient, user, "NS_NOT_ON_USERLIST", nick, chan->name);
109 }