added some code & compiler information to cmd_netinfo
[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     check_mysql();
19     if(stricmp(argv[0], "bans") && !checkChannelAccess(user, chan, "channel_candel", 1, 0)) {
20         reply(getTextBot(), user, "NS_ACCESS_DENIED");
21         return;
22     }
23     int min_access, max_access;
24     if(!stricmp(argv[0], "users")) {
25         min_access = 1;
26         max_access = getChannelAccess(user, chan, 0) - 1;
27     } else if(!stricmp(argv[0], "bans")) {
28         if(!checkChannelAccess(user, chan, "channel_staticban", 1, 0)) {
29             reply(getTextBot(), user, "NS_ACCESS_DENIED");
30             return;
31         }
32         //TODO: TRIM BANS
33         return;
34     } else {
35         char *seperator = strstr(argv[0], "-");
36         if(seperator) {
37             *seperator = '\0';
38             seperator++;
39             min_access = atoi(argv[0]);
40             max_access = atoi(seperator);
41             if(max_access > min_access) {
42                 reply(getTextBot(), user, "NS_INVALID_ACCESS_RANGE", min_access, max_access);
43                 return;
44             }
45         } else {
46             min_access = atoi(argv[0]);
47             max_access = min_access;
48         }
49         if(max_access >= getChannelAccess(user, chan, 1)) {
50             reply(getTextBot(), user, "NS_NO_ACCESS");
51             return;
52         }
53     }
54     //parse duration...
55     int duration = strToTime(user, argv[1]);
56     if(duration < 30) {
57         reply(getTextBot(), user, "NS_TRIM_DURATION_TOO_SHORT", 30);
58         return;
59     }
60     struct neonserv_cmd_trim_cache *cache = malloc(sizeof(*cache));
61     if (!cache) {
62         perror("malloc() failed");
63         return;
64     }
65     cache->client = client;
66     cache->textclient = getTextBot();
67     cache->user = user;
68     cache->min_access = min_access;
69     cache->max_access = max_access;
70     cache->duration = duration;
71     get_userlist(chan, neonserv_cmd_trim_userlist_lookup, cache);
72 }
73
74 static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup) {
75     struct neonserv_cmd_trim_cache *cache = data;
76     //got userlist
77     neonserv_cmd_trim_async1(cache->client, cache->textclient, cache->user, chan, cache->min_access, cache->max_access, cache->duration);
78     free(cache);
79 }
80
81 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) {
82     MYSQL_RES *res;
83     MYSQL_ROW row;
84     int trim_count = 0, is_here;
85     struct ChanUser *chanuser;
86     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);
87     res = mysql_use();
88     while ((row = mysql_fetch_row(res)) != NULL) {
89         if(!strcmp(row[0], "0") || time(0) - atoi(row[0]) >= duration) {
90             //check if the user is currently in the channel
91             is_here = 0;
92             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
93                 if((chanuser->user->flags & USERFLAG_ISAUTHED) && !strcmp(chanuser->user->auth, row[1])) {
94                     is_here = 1;
95                     break;
96                 }
97             }
98             if(!is_here) {
99                 //delete the user
100                 trim_count++;
101                 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[2]);
102             }
103         }
104     }
105     char timeBuf[MAXLEN];
106     reply(getTextBot(), user, "NS_TRIM_DONE", trim_count, min_access, max_access, chan->name, timeToStr(user, duration, 3, timeBuf));
107 }