execute mysql_check only if a query fails.
[NeonServV5.git] / cmd_neonserv_trim.c
1
2 /*
3 * argv[0]  target (format: minaccess-maxaccess/users/bans)
4 * argv[1]  duration
5 */
6 static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup);
7 static void neonserv_cmd_trim_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, int min_access, int max_access, int duration);
8
9 struct neonserv_cmd_trim_cache {
10     struct ClientSocket *client, *textclient;
11     struct UserNode *user;
12     int min_access;
13     int max_access;
14     int duration;
15 };
16
17 static CMD_BIND(neonserv_cmd_trim) {
18     if(stricmp(argv[0], "bans") && !checkChannelAccess(user, chan, "channel_candel", 1, 0)) {
19         reply(getTextBot(), user, "NS_ACCESS_DENIED");
20         return;
21     }
22     int min_access, max_access;
23     if(!stricmp(argv[0], "users")) {
24         min_access = 1;
25         max_access = getChannelAccess(user, chan, 0) - 1;
26     } else if(!stricmp(argv[0], "bans")) {
27         if(!checkChannelAccess(user, chan, "channel_staticban", 1, 0)) {
28             reply(getTextBot(), user, "NS_ACCESS_DENIED");
29             return;
30         }
31         //TODO: TRIM BANS
32         return;
33     } else {
34         char *seperator = strstr(argv[0], "-");
35         if(seperator) {
36             *seperator = '\0';
37             seperator++;
38             min_access = atoi(argv[0]);
39             max_access = atoi(seperator);
40             if(max_access > min_access) {
41                 reply(getTextBot(), user, "NS_INVALID_ACCESS_RANGE", min_access, max_access);
42                 return;
43             }
44         } else {
45             min_access = atoi(argv[0]);
46             max_access = min_access;
47         }
48         if(max_access >= getChannelAccess(user, chan, 1)) {
49             reply(getTextBot(), user, "NS_NO_ACCESS");
50             return;
51         }
52     }
53     //parse duration...
54     int duration = strToTime(user, argv[1]);
55     if(duration < 30) {
56         reply(getTextBot(), user, "NS_TRIM_DURATION_TOO_SHORT", 30);
57         return;
58     }
59     struct neonserv_cmd_trim_cache *cache = malloc(sizeof(*cache));
60     if (!cache) {
61         perror("malloc() failed");
62         return;
63     }
64     cache->client = client;
65     cache->textclient = getTextBot();
66     cache->user = user;
67     cache->min_access = min_access;
68     cache->max_access = max_access;
69     cache->duration = duration;
70     get_userlist(chan, neonserv_cmd_trim_userlist_lookup, cache);
71 }
72
73 static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup) {
74     struct neonserv_cmd_trim_cache *cache = data;
75     //got userlist
76     neonserv_cmd_trim_async1(cache->client, cache->textclient, cache->user, chan, cache->min_access, cache->max_access, cache->duration);
77     free(cache);
78 }
79
80 static void neonserv_cmd_trim_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, int min_access, int max_access, int duration) {
81     MYSQL_RES *res;
82     MYSQL_ROW row;
83     int trim_count = 0, is_here;
84     struct ChanUser *chanuser;
85     printf_mysql_query("SELECT `chanuser_seen`, `user_user`, `chanuser_id` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d' AND `chanuser_access` >= '%d' AND `chanuser_access` <= '%d'", chan->channel_id, min_access, max_access);
86     res = mysql_use();
87     while ((row = mysql_fetch_row(res)) != NULL) {
88         if(!strcmp(row[0], "0") || time(0) - atoi(row[0]) >= duration) {
89             //check if the user is currently in the channel
90             is_here = 0;
91             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
92                 if((chanuser->user->flags & USERFLAG_ISAUTHED) && !strcmp(chanuser->user->auth, row[1])) {
93                     is_here = 1;
94                     break;
95                 }
96             }
97             if(!is_here) {
98                 //delete the user
99                 trim_count++;
100                 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[2]);
101             }
102         }
103     }
104     char timeBuf[MAXLEN];
105     reply(getTextBot(), user, "NS_TRIM_DONE", trim_count, min_access, max_access, chan->name, timeToStr(user, duration, 3, timeBuf));
106 }