changed Makefile; build all commands as an own file
[NeonServV5.git] / cmd_neonserv_trim.c
1
2 #include "cmd_neonserv.h"
3
4 /*
5 * argv[0]  target (format: minaccess-maxaccess/users/bans)
6 * argv[1]  duration
7 */
8 static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup);
9 static void neonserv_cmd_trim_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, int min_access, int max_access, int duration);
10
11 struct neonserv_cmd_trim_cache {
12     struct ClientSocket *client, *textclient;
13     struct UserNode *user;
14     struct Event *event;
15     int min_access;
16     int max_access;
17     int duration;
18 };
19
20 CMD_BIND(neonserv_cmd_trim) {
21     if(stricmp(argv[0], "bans") && !checkChannelAccess(user, chan, "channel_candel", 0, 0)) {
22         if(isGodMode(user)) {
23             event->flags |= CMDFLAG_OPLOG;
24         } else {
25             reply(getTextBot(), user, "NS_ACCESS_DENIED");
26             return;
27         }
28     }
29     int min_access, max_access;
30     if(!stricmp(argv[0], "users")) {
31         min_access = 1;
32         max_access = getChannelAccess(user, chan, 0) - 1;
33     } else if(!stricmp(argv[0], "bans")) {
34         if(!checkChannelAccess(user, chan, "channel_staticban", 0, 0)) {
35             if(isGodMode(user)) {
36                 event->flags |= CMDFLAG_OPLOG;
37             } else {
38                 reply(getTextBot(), user, "NS_ACCESS_DENIED");
39                 return;
40             }
41         }
42         //TODO: TRIM BANS
43         return;
44     } else {
45         char *seperator = strstr(argv[0], "-");
46         if(seperator) {
47             *seperator = '\0';
48             seperator++;
49             min_access = atoi(argv[0]);
50             max_access = atoi(seperator);
51             if(max_access < min_access) {
52                 reply(getTextBot(), user, "NS_INVALID_ACCESS_RANGE", min_access, max_access);
53                 return;
54             }
55         } else {
56             min_access = atoi(argv[0]);
57             max_access = min_access;
58         }
59         if(max_access >= getChannelAccess(user, chan, 0)) {
60             if(isGodMode(user)) {
61                 event->flags |= CMDFLAG_OPLOG;
62             } else {
63                 reply(getTextBot(), user, "NS_NO_ACCESS");
64                 return;
65             }
66         }
67     }
68     //parse duration...
69     int duration = strToTime(user, argv[1]);
70     if(duration < 30) {
71         reply(getTextBot(), user, "NS_TRIM_DURATION_TOO_SHORT", 30);
72         return;
73     }
74     struct neonserv_cmd_trim_cache *cache = malloc(sizeof(*cache));
75     if (!cache) {
76         perror("malloc() failed");
77         return;
78     }
79     cache->client = client;
80     cache->textclient = getTextBot();
81     cache->user = user;
82     cache->event = event;
83     cache->min_access = min_access;
84     cache->max_access = max_access;
85     cache->duration = duration;
86     get_userlist_with_invisible(chan, neonserv_cmd_trim_userlist_lookup, cache);
87 }
88
89 static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup) {
90     struct neonserv_cmd_trim_cache *cache = data;
91     //got userlist
92     neonserv_cmd_trim_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->min_access, cache->max_access, cache->duration);
93     free(cache);
94 }
95
96 static void neonserv_cmd_trim_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, int min_access, int max_access, int duration) {
97     MYSQL_RES *res;
98     MYSQL_ROW row;
99     int trim_count = 0, is_here;
100     struct ChanUser *chanuser;
101     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);
102     res = mysql_use();
103     while ((row = mysql_fetch_row(res)) != NULL) {
104         if(!strcmp(row[0], "0") || time(0) - atoi(row[0]) >= duration) {
105             //check if the user is currently in the channel
106             is_here = 0;
107             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
108                 if((chanuser->user->flags & USERFLAG_ISAUTHED) && !strcmp(chanuser->user->auth, row[1])) {
109                     is_here = 1;
110                     break;
111                 }
112             }
113             if(!is_here) {
114                 //delete the user
115                 trim_count++;
116                 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[2]);
117             }
118         }
119     }
120     char timeBuf[MAXLEN];
121     reply(getTextBot(), user, "NS_TRIM_DONE", trim_count, min_access, max_access, chan->name, timeToStr(user, duration, 3, timeBuf));
122     if(trim_count)
123         logEvent(event);
124 }