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