c9d5aca119256e2de9d3c18338a85ee2398cfb24
[NeonServV5.git] / cmd_neonserv_unsuspend.c
1
2 #include "cmd_neonserv.h"
3
4 /*
5 * argv[0] - nick / *auth
6 */
7 static USERAUTH_CALLBACK(neonserv_cmd_unsuspend_nick_lookup);
8 static void neonserv_cmd_unsuspend_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth);
9
10 struct neonserv_cmd_unsuspend_cache {
11     struct ClientSocket *client, *textclient;
12     struct UserNode *user;
13     struct ChanNode *chan;
14     struct Event *event;
15     char *nick;
16 };
17
18 CMD_BIND(neonserv_cmd_unsuspend) {
19     if(argv[0][0] == '*') {
20         //we've got an auth
21         argv[0]++;
22         neonserv_cmd_unsuspend_async1(client, getTextBot(), user, chan, event, argv[0], argv[0]);
23     } else {
24         struct UserNode *cuser = getUserByNick(argv[0]);
25         if(!cuser) {
26             cuser = createTempUser(argv[0]);
27             cuser->flags |= USERFLAG_ISTMPUSER;
28         }
29         if(cuser->flags & USERFLAG_ISAUTHED) {
30             neonserv_cmd_unsuspend_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth);
31         } else {
32             struct neonserv_cmd_unsuspend_cache *cache = malloc(sizeof(*cache));
33             if (!cache) {
34                 perror("malloc() failed");
35                 return;
36             }
37             cache->client = client;
38             cache->textclient = getTextBot();
39             cache->user = user;
40             cache->chan = chan;
41             cache->event = event;
42             cache->nick = strdup(argv[0]);
43             get_userauth(cuser, neonserv_cmd_unsuspend_nick_lookup, cache);
44         }
45     }
46 }
47
48 static USERAUTH_CALLBACK(neonserv_cmd_unsuspend_nick_lookup) {
49     struct neonserv_cmd_unsuspend_cache *cache = data;
50     if(!user) {
51         //USER_DOES_NOT_EXIST
52         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
53     }
54     else if(!(user->flags & USERFLAG_ISAUTHED)) {
55         //USER_NOT_AUTHED
56         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
57     }
58     else
59         neonserv_cmd_unsuspend_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, user->auth);
60     free(cache->nick);
61     free(cache);
62 }
63
64 static void neonserv_cmd_unsuspend_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth) {
65     //we've got a valid auth now...
66     MYSQL_RES *res;
67     MYSQL_ROW row;
68     int userid, cflags;
69     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
70     res = mysql_use();
71     if ((row = mysql_fetch_row(res)) != NULL) {
72         userid = atoi(row[0]);
73         //check if the user is added
74         printf_mysql_query("SELECT `chanuser_access`, `chanuser_id`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
75         res = mysql_use();
76         if ((row = mysql_fetch_row(res)) != NULL) {
77             if(atoi(row[0]) >= getChannelAccess(user, chan, 0)) {
78                 if(isGodMode(user)) {
79                     event->flags |= CMDFLAG_OPLOG;
80                 } else {
81                     reply(textclient, user, "NS_USER_OUTRANKED", nick);
82                     return;
83                 }
84             }
85             //unsuspend
86             cflags = atoi(row[2]);
87             if(!(cflags & DB_CHANUSER_SUSPENDED)) {
88                 reply(textclient, user, "NS_SUSPEND_NOT", nick);
89                 return;
90             }
91             cflags &= ~DB_CHANUSER_SUSPENDED;
92             printf_mysql_query("UPDATE `chanusers` SET `chanuser_flags` = '%d' WHERE `chanuser_id` = '%s'", cflags, row[1]);
93             reply(textclient, user, "NS_SUSPEND_RESTORED", nick, chan->name);
94             logEvent(event);
95             return;
96         }
97     }
98     reply(textclient, user, "NS_NOT_ON_USERLIST", nick, chan->name);
99 }