2 #include "cmd_neonserv.h"
5 * argv[0] - nick / *auth
6 * argv[1] - chan access
8 static AUTHLOOKUP_CALLBACK(neonserv_cmd_adduser_auth_lookup);
9 static USERAUTH_CALLBACK(neonserv_cmd_adduser_nick_lookup);
10 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);
12 struct neonserv_cmd_adduser_cache {
13 struct ClientSocket *client, *textclient;
14 struct UserNode *user;
15 struct ChanNode *chan;
21 CMD_BIND(neonserv_cmd_adduser) {
25 caccess = atoi(argv[1]);
26 if(caccess <= 0 || caccess > 500) {
27 reply(getTextBot(), user, "NS_INVALID_ACCESS", caccess);
30 if(caccess >= getChannelAccess(user, chan, 0)) {
32 event->flags |= CMDFLAG_OPLOG;
34 reply(getTextBot(), user, "NS_ACCESS_OUTRANKED");
39 if(argv[0][0] == '*') {
42 printf_mysql_query("SELECT `user_user` FROM `users` WHERE `user_user` = '%s'", escape_string(argv[0]));
44 if ((row = mysql_fetch_row(res)) != NULL) {
45 neonserv_cmd_adduser_async1(client, getTextBot(), user, chan, event, argv[0], row[0], caccess);
47 //we need to create a new user...
48 //but first lookup the auth to check if it really exists
49 struct neonserv_cmd_adduser_cache *cache = malloc(sizeof(*cache));
51 perror("malloc() failed");
54 cache->client = client;
55 cache->textclient = getTextBot();
59 cache->access = caccess;
60 cache->nick = strdup(argv[0]);
61 lookup_authname(argv[0], neonserv_cmd_adduser_auth_lookup, cache);
64 struct UserNode *cuser = getUserByNick(argv[0]);
66 cuser = createTempUser(argv[0]);
67 cuser->flags |= USERFLAG_ISTMPUSER;
69 if(cuser->flags & USERFLAG_ISAUTHED) {
70 neonserv_cmd_adduser_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth, caccess);
72 struct neonserv_cmd_adduser_cache *cache = malloc(sizeof(*cache));
74 perror("malloc() failed");
77 cache->client = client;
78 cache->textclient = getTextBot();
82 cache->access = caccess;
83 cache->nick = strdup(argv[0]);
84 get_userauth(cuser, neonserv_cmd_adduser_nick_lookup, cache);
89 static AUTHLOOKUP_CALLBACK(neonserv_cmd_adduser_auth_lookup) {
90 struct neonserv_cmd_adduser_cache *cache = data;
93 reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
95 neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, cache->nick, auth, cache->access);
100 static USERAUTH_CALLBACK(neonserv_cmd_adduser_nick_lookup) {
101 struct neonserv_cmd_adduser_cache *cache = data;
103 //USER_DOES_NOT_EXIST
104 reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
106 else if(!(user->flags & USERFLAG_ISAUTHED)) {
108 reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
111 neonserv_cmd_adduser_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth, cache->access);
116 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) {
117 //we've got a valid auth now...
121 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
123 if ((row = mysql_fetch_row(res)) != NULL) {
124 userid = atoi(row[0]);
125 //check if the user is already added
126 printf_mysql_query("SELECT `chanuser_access` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
128 if ((row = mysql_fetch_row(res)) != NULL) {
129 reply(textclient, user, "NS_ADDUSER_ALREADY_ADDED", nick, chan->name, atoi(row[0]));
133 printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
134 userid = (int) mysql_insert_id(mysql_conn);
136 printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chan->channel_id, userid, caccess);
137 reply(textclient, user, "NS_ADDUSER_DONE", nick, chan->name, caccess);