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, struct Event *event, 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;
19 static CMD_BIND(neonserv_cmd_adduser) {
23 caccess = atoi(argv[1]);
24 if(caccess <= 0 || caccess > 500) {
25 reply(getTextBot(), user, "NS_INVALID_ACCESS", caccess);
28 if(caccess >= getChannelAccess(user, chan, 0)) {
30 event->flags |= CMDFLAG_OPLOG;
32 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, event, 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();
57 cache->access = caccess;
58 cache->nick = strdup(argv[0]);
59 lookup_authname(argv[0], neonserv_cmd_adduser_auth_lookup, cache);
62 struct UserNode *cuser = getUserByNick(argv[0]);
64 cuser = createTempUser(argv[0]);
65 cuser->flags |= USERFLAG_ISTMPUSER;
67 if(cuser->flags & USERFLAG_ISAUTHED) {
68 neonserv_cmd_adduser_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth, caccess);
70 struct neonserv_cmd_adduser_cache *cache = malloc(sizeof(*cache));
72 perror("malloc() failed");
75 cache->client = client;
76 cache->textclient = getTextBot();
80 cache->access = caccess;
81 cache->nick = strdup(argv[0]);
82 get_userauth(cuser, neonserv_cmd_adduser_nick_lookup, cache);
87 static AUTHLOOKUP_CALLBACK(neonserv_cmd_adduser_auth_lookup) {
88 struct neonserv_cmd_adduser_cache *cache = data;
91 reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
93 neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, cache->nick, auth, cache->access);
98 static USERAUTH_CALLBACK(neonserv_cmd_adduser_nick_lookup) {
99 struct neonserv_cmd_adduser_cache *cache = data;
101 //USER_DOES_NOT_EXIST
102 reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
104 else if(!(user->flags & USERFLAG_ISAUTHED)) {
106 reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
109 neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth, cache->access);
114 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) {
115 //we've got a valid auth now...
119 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
121 if ((row = mysql_fetch_row(res)) != NULL) {
122 userid = atoi(row[0]);
123 //check if the user is already added
124 printf_mysql_query("SELECT `chanuser_access` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
126 if ((row = mysql_fetch_row(res)) != NULL) {
127 reply(textclient, user, "NS_ADDUSER_ALREADY_ADDED", nick, chan->name, atoi(row[0]));
131 printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
132 userid = (int) mysql_insert_id(mysql_conn);
134 printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chan->channel_id, userid, caccess);
135 reply(textclient, user, "NS_ADDUSER_DONE", nick, chan->name, caccess);