*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_adduser.c
1 /* cmd_neonserv_adduser.c - NeonServ v5.2
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] - 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(getTextBot(), user, "NS_INVALID_ACCESS", caccess);
44         return;
45     }
46     if(caccess >= getChannelAccess(user, chan, 0)) {
47         if(isGodMode(user)) {
48             event->flags |= CMDFLAG_OPLOG;
49         } else {
50             reply(getTextBot(), 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, getTextBot(), 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                 perror("malloc() failed");
68                 return;
69             }
70             cache->client = client;
71             cache->textclient = getTextBot();
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], 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             cuser->flags |= USERFLAG_ISTMPUSER;
84         }
85         if(cuser->flags & USERFLAG_ISAUTHED) {
86             neonserv_cmd_adduser_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth, caccess);
87         } else {
88             struct neonserv_cmd_adduser_cache *cache = malloc(sizeof(*cache));
89             if (!cache) {
90                 perror("malloc() failed");
91                 return;
92             }
93             cache->client = client;
94             cache->textclient = getTextBot();
95             cache->user = user;
96             cache->chan = chan;
97             cache->event = event;
98             cache->access = caccess;
99             cache->nick = strdup(argv[0]);
100             get_userauth(cuser, neonserv_cmd_adduser_nick_lookup, cache);
101         }
102     }
103 }
104
105 static AUTHLOOKUP_CALLBACK(neonserv_cmd_adduser_auth_lookup) {
106     struct neonserv_cmd_adduser_cache *cache = data;
107     if(!exists) {
108         //AUTH_DOES_NOT_EXIST
109         reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
110     } else
111         neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, cache->nick, auth, cache->access);
112     free(cache->nick);
113     free(cache);
114 }
115
116 static USERAUTH_CALLBACK(neonserv_cmd_adduser_nick_lookup) {
117     struct neonserv_cmd_adduser_cache *cache = data;
118     if(!user) {
119         //USER_DOES_NOT_EXIST
120         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
121     }
122     else if(!(user->flags & USERFLAG_ISAUTHED)) {
123         //USER_NOT_AUTHED
124         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
125     }
126     else
127         neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth, cache->access);
128     free(cache->nick);
129     free(cache);
130 }
131
132 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) {
133     //we've got a valid auth now...
134     MYSQL_RES *res;
135     MYSQL_ROW row;
136     int userid;
137     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
138     res = mysql_use();
139     if ((row = mysql_fetch_row(res)) != NULL) {
140         userid = atoi(row[0]);
141         //check if the user is already added
142         printf_mysql_query("SELECT `chanuser_access` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
143         res = mysql_use();
144         if ((row = mysql_fetch_row(res)) != NULL) {
145             reply(textclient, user, "NS_ADDUSER_ALREADY_ADDED", nick, chan->name, atoi(row[0]));
146             return;
147         }
148     } else {
149         printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
150         userid = (int) mysql_insert_id(mysql_conn);
151     }
152     printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chan->channel_id, userid, caccess);
153     reply(textclient, user, "NS_ADDUSER_DONE", nick, chan->name, caccess);
154     logEvent(event);
155 }