3 * argv[0] - nick / *auth
4 * argv[1] - chan access
6 static AUTHLOOKUP_CALLBACK(neonserv_cmd_adduser_auth_lookup);
7 static USERAUTH_CALLBACK(neonserv_cmd_adduser_nick_lookup);
8 static void neonserv_cmd_adduser_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *nick, char *auth, int access);
10 struct neonserv_cmd_adduser_cache {
11 struct ClientSocket *client, *textclient;
12 struct UserNode *user;
13 struct ChanNode *chan;
18 static CMD_BIND(neonserv_cmd_adduser) {
23 if(!checkChannelAccess(user, chan, "channel_canadd", 1, 0)) {
24 reply(getTextBot(), user, "NS_ACCESS_DENIED");
27 caccess = atoi(argv[1]);
28 if(caccess <= 0 || caccess > 500) {
29 reply(getTextBot(), user, "NS_INVALID_ACCESS", caccess);
32 if(caccess >= getChannelAccess(user, chan, 1)) {
33 reply(getTextBot(), user, "NS_ACCESS_OUTRANKED");
37 if(argv[0][0] == '*') {
40 printf_mysql_query("SELECT `user_user` FROM `users` WHERE `user_user` = '%s'", escape_string(argv[0]));
42 if ((row = mysql_fetch_row(res)) != NULL) {
43 neonserv_cmd_adduser_async1(client, getTextBot(), user, chan, argv[0], row[0], caccess);
45 //we need to create a new user...
46 //but first lookup the auth to check if it really exists
47 struct neonserv_cmd_adduser_cache *cache = malloc(sizeof(*cache));
49 perror("malloc() failed");
52 cache->client = client;
53 cache->textclient = getTextBot();
56 cache->access = caccess;
57 cache->nick = strdup(argv[0]);
58 lookup_authname(argv[0], neonserv_cmd_adduser_auth_lookup, cache);
61 struct UserNode *cuser = getUserByNick(argv[0]);
63 cuser = createTempUser(argv[0]);
64 cuser->flags |= USERFLAG_ISTMPUSER;
66 if(cuser->flags & USERFLAG_ISAUTHED) {
67 neonserv_cmd_adduser_async1(client, getTextBot(), user, chan, argv[0], cuser->auth, caccess);
69 struct neonserv_cmd_adduser_cache *cache = malloc(sizeof(*cache));
71 perror("malloc() failed");
74 cache->client = client;
75 cache->textclient = getTextBot();
78 cache->access = caccess;
79 cache->nick = strdup(argv[0]);
80 get_userauth(cuser, neonserv_cmd_adduser_nick_lookup, cache);
85 static AUTHLOOKUP_CALLBACK(neonserv_cmd_adduser_auth_lookup) {
86 struct neonserv_cmd_adduser_cache *cache = data;
89 reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
91 neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->nick, auth, cache->access);
96 static USERAUTH_CALLBACK(neonserv_cmd_adduser_nick_lookup) {
97 struct neonserv_cmd_adduser_cache *cache = data;
100 reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
102 else if(!(user->flags & USERFLAG_ISAUTHED)) {
104 reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
107 neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, user->nick, user->auth, cache->access);
112 static void neonserv_cmd_adduser_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *nick, char *auth, int caccess) {
113 //we've got a valid auth now...
117 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
119 if ((row = mysql_fetch_row(res)) != NULL) {
120 userid = atoi(row[0]);
121 //check if the user is already added
122 printf_mysql_query("SELECT `chanuser_access` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
124 if ((row = mysql_fetch_row(res)) != NULL) {
125 reply(textclient, user, "NS_ADDUSER_ALREADY_ADDED", nick, chan->name, atoi(row[0]));
129 printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
130 userid = (int) mysql_insert_id(mysql_conn);
132 printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chan->channel_id, userid, caccess);
133 reply(textclient, user, "NS_ADDUSER_DONE", nick, chan->name, caccess);