added new multi log system
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_adduser.c
1 /* cmd_neonserv_adduser.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] - chan access
23 */
24 static AUTHLOOKUP_CALLBACK(neonserv_cmd_adduser_auth_lookup);
25 static USERAUTH_CALLBACK(neonserv_cmd_adduser_nick_lookup);
26 static void neonserv_cmd_adduser_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth, int access);
27
28 struct neonserv_cmd_adduser_cache {
29     struct ClientSocket *client, *textclient;
30     struct UserNode *user;
31     struct ChanNode *chan;
32     struct Event *event;
33     int access;
34     char *nick;
35 };
36
37 CMD_BIND(neonserv_cmd_adduser) {
38     int caccess;
39     MYSQL_RES *res;
40     MYSQL_ROW row;
41     caccess = atoi(argv[1]);
42     if(caccess <= 0 || caccess > 500) {
43         reply(textclient, user, "NS_INVALID_ACCESS", caccess);
44         return;
45     }
46     if(caccess >= getChannelAccess(user, chan)) {
47         if(isGodMode(user)) {
48             event->flags |= CMDFLAG_OPLOG;
49         } else {
50             reply(textclient, user, "NS_ACCESS_OUTRANKED");
51             return;
52         }
53     }
54     //check own access
55     if(argv[0][0] == '*') {
56         //we've got an auth
57         argv[0]++;
58         printf_mysql_query("SELECT `user_user` FROM `users` WHERE `user_user` = '%s'", escape_string(argv[0]));
59         res = mysql_use();
60         if ((row = mysql_fetch_row(res)) != NULL) {
61             neonserv_cmd_adduser_async1(client, textclient, user, chan, event, argv[0], row[0], caccess);
62         } else {
63             //we need to create a new user...
64             //but first lookup the auth to check if it really exists
65             struct neonserv_cmd_adduser_cache *cache = malloc(sizeof(*cache));
66             if (!cache) {
67                 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
68                 return;
69             }
70             cache->client = client;
71             cache->textclient = textclient;
72             cache->user = user;
73             cache->chan = chan;
74             cache->event = event;
75             cache->access = caccess;
76             cache->nick = strdup(argv[0]);
77             lookup_authname(argv[0], module_id, neonserv_cmd_adduser_auth_lookup, cache);
78         }
79     } else {
80         struct UserNode *cuser = getUserByNick(argv[0]);
81         if(!cuser) {
82             cuser = createTempUser(argv[0]);
83                         if(!cuser) {
84                 reply(textclient, user, "NS_USER_UNKNOWN", argv[0]);
85                 return;
86             }
87             cuser->flags |= USERFLAG_ISTMPUSER;
88         }
89         if(cuser->flags & USERFLAG_ISAUTHED) {
90             neonserv_cmd_adduser_async1(client, textclient, user, chan, event, argv[0], cuser->auth, caccess);
91         } else {
92             struct neonserv_cmd_adduser_cache *cache = malloc(sizeof(*cache));
93             if (!cache) {
94                 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
95                 return;
96             }
97             cache->client = client;
98             cache->textclient = textclient;
99             cache->user = user;
100             cache->chan = chan;
101             cache->event = event;
102             cache->access = caccess;
103             cache->nick = strdup(argv[0]);
104             get_userauth(cuser, module_id, neonserv_cmd_adduser_nick_lookup, cache);
105         }
106     }
107 }
108
109 static AUTHLOOKUP_CALLBACK(neonserv_cmd_adduser_auth_lookup) {
110     struct neonserv_cmd_adduser_cache *cache = data;
111     if(!exists) {
112         //AUTH_DOES_NOT_EXIST
113         reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
114     } else
115         neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, cache->nick, auth, cache->access);
116     free(cache->nick);
117     free(cache);
118 }
119
120 static USERAUTH_CALLBACK(neonserv_cmd_adduser_nick_lookup) {
121     struct neonserv_cmd_adduser_cache *cache = data;
122     if(!user) {
123         //USER_DOES_NOT_EXIST
124         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
125     }
126     else if(!(user->flags & USERFLAG_ISAUTHED)) {
127         //USER_NOT_AUTHED
128         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
129     }
130     else
131         neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth, cache->access);
132     free(cache->nick);
133     free(cache);
134 }
135
136 static void neonserv_cmd_adduser_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth, int caccess) {
137     //we've got a valid auth now...
138     MYSQL_RES *res;
139     MYSQL_ROW row;
140     int userid;
141     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
142     res = mysql_use();
143     if ((row = mysql_fetch_row(res)) != NULL) {
144         userid = atoi(row[0]);
145         //check if the user is already added
146         printf_mysql_query("SELECT `chanuser_access` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
147         res = mysql_use();
148         if ((row = mysql_fetch_row(res)) != NULL) {
149             reply(textclient, user, "NS_ADDUSER_ALREADY_ADDED", nick, chan->name, atoi(row[0]));
150             return;
151         }
152     } else {
153         printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
154         userid = (int) mysql_insert_id(get_mysql_conn());
155     }
156     printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chan->channel_id, userid, caccess);
157     reply(textclient, user, "NS_ADDUSER_DONE", nick, chan->name, caccess);
158     logEvent(event);
159 }