added new multi log system
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_unsuspend.c
1 /* cmd_neonserv_unsuspend.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 */
23 static USERAUTH_CALLBACK(neonserv_cmd_unsuspend_nick_lookup);
24 static void neonserv_cmd_unsuspend_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_unsuspend_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_unsuspend) {
35     if(argv[0][0] == '*') {
36         //we've got an auth
37         argv[0]++;
38         neonserv_cmd_unsuspend_async1(client, textclient, 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                         if(!cuser) {
44                 reply(textclient, user, "NS_USER_UNKNOWN", argv[0]);
45                 return;
46             }
47             cuser->flags |= USERFLAG_ISTMPUSER;
48         }
49         if(cuser->flags & USERFLAG_ISAUTHED) {
50             neonserv_cmd_unsuspend_async1(client, textclient, user, chan, event, argv[0], cuser->auth);
51         } else {
52             struct neonserv_cmd_unsuspend_cache *cache = malloc(sizeof(*cache));
53             if (!cache) {
54                 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
55                 return;
56             }
57             cache->client = client;
58             cache->textclient = textclient;
59             cache->user = user;
60             cache->chan = chan;
61             cache->event = event;
62             cache->nick = strdup(argv[0]);
63             get_userauth(cuser, module_id, neonserv_cmd_unsuspend_nick_lookup, cache);
64         }
65     }
66 }
67
68 static USERAUTH_CALLBACK(neonserv_cmd_unsuspend_nick_lookup) {
69     struct neonserv_cmd_unsuspend_cache *cache = data;
70     if(!user) {
71         //USER_DOES_NOT_EXIST
72         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
73     }
74     else if(!(user->flags & USERFLAG_ISAUTHED)) {
75         //USER_NOT_AUTHED
76         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
77     }
78     else
79         neonserv_cmd_unsuspend_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth);
80     free(cache->nick);
81     free(cache);
82 }
83
84 static void neonserv_cmd_unsuspend_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth) {
85     //we've got a valid auth now...
86     MYSQL_RES *res;
87     MYSQL_ROW row;
88     int userid, cflags;
89     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
90     res = mysql_use();
91     if ((row = mysql_fetch_row(res)) != NULL) {
92         userid = atoi(row[0]);
93         //check if the user is added
94         printf_mysql_query("SELECT `chanuser_access`, `chanuser_id`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
95         res = mysql_use();
96         if ((row = mysql_fetch_row(res)) != NULL) {
97             if(atoi(row[0]) >= getChannelAccess(user, chan)) {
98                 if(isGodMode(user)) {
99                     event->flags |= CMDFLAG_OPLOG;
100                 } else {
101                     reply(textclient, user, "NS_USER_OUTRANKED", nick);
102                     return;
103                 }
104             }
105             //unsuspend
106             cflags = atoi(row[2]);
107             if(!(cflags & DB_CHANUSER_SUSPENDED)) {
108                 reply(textclient, user, "NS_SUSPEND_NOT", nick);
109                 return;
110             }
111             cflags &= ~DB_CHANUSER_SUSPENDED;
112             printf_mysql_query("UPDATE `chanusers` SET `chanuser_flags` = '%d' WHERE `chanuser_id` = '%s'", cflags, row[1]);
113             reply(textclient, user, "NS_SUSPEND_RESTORED", nick, chan->name);
114             logEvent(event);
115             return;
116         }
117     }
118     reply(textclient, user, "NS_NOT_ON_USERLIST", nick, chan->name);
119 }