1 /* cmd_neonserv_adduser.c - NeonServ v5.2
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "cmd_neonserv.h"
21 * argv[0] - nick / *auth
22 * argv[1] - chan access
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);
28 struct neonserv_cmd_adduser_cache {
29 struct ClientSocket *client, *textclient;
30 struct UserNode *user;
31 struct ChanNode *chan;
37 CMD_BIND(neonserv_cmd_adduser) {
41 caccess = atoi(argv[1]);
42 if(caccess <= 0 || caccess > 500) {
43 reply(getTextBot(), user, "NS_INVALID_ACCESS", caccess);
46 if(caccess >= getChannelAccess(user, chan)) {
48 event->flags |= CMDFLAG_OPLOG;
50 reply(getTextBot(), user, "NS_ACCESS_OUTRANKED");
55 if(argv[0][0] == '*') {
58 printf_mysql_query("SELECT `user_user` FROM `users` WHERE `user_user` = '%s'", escape_string(argv[0]));
60 if ((row = mysql_fetch_row(res)) != NULL) {
61 neonserv_cmd_adduser_async1(client, getTextBot(), user, chan, event, argv[0], row[0], caccess);
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));
67 perror("malloc() failed");
70 cache->client = client;
71 cache->textclient = getTextBot();
75 cache->access = caccess;
76 cache->nick = strdup(argv[0]);
77 lookup_authname(argv[0], neonserv_cmd_adduser_auth_lookup, cache);
80 struct UserNode *cuser = getUserByNick(argv[0]);
82 cuser = createTempUser(argv[0]);
83 cuser->flags |= USERFLAG_ISTMPUSER;
85 if(cuser->flags & USERFLAG_ISAUTHED) {
86 neonserv_cmd_adduser_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth, caccess);
88 struct neonserv_cmd_adduser_cache *cache = malloc(sizeof(*cache));
90 perror("malloc() failed");
93 cache->client = client;
94 cache->textclient = getTextBot();
98 cache->access = caccess;
99 cache->nick = strdup(argv[0]);
100 get_userauth(cuser, neonserv_cmd_adduser_nick_lookup, cache);
105 static AUTHLOOKUP_CALLBACK(neonserv_cmd_adduser_auth_lookup) {
106 struct neonserv_cmd_adduser_cache *cache = data;
108 //AUTH_DOES_NOT_EXIST
109 reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
111 neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, cache->nick, auth, cache->access);
116 static USERAUTH_CALLBACK(neonserv_cmd_adduser_nick_lookup) {
117 struct neonserv_cmd_adduser_cache *cache = data;
119 //USER_DOES_NOT_EXIST
120 reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
122 else if(!(user->flags & USERFLAG_ISAUTHED)) {
124 reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
127 neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth, cache->access);
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...
137 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
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);
144 if ((row = mysql_fetch_row(res)) != NULL) {
145 reply(textclient, user, "NS_ADDUSER_ALREADY_ADDED", nick, chan->name, atoi(row[0]));
149 printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
150 userid = (int) mysql_insert_id(mysql_conn);
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);