increased MAXLANGUAGES definition
[NeonServV5.git] / src / statistics.c
1 /* statistics.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "statistics.h"
18 #include "timeq.h"
19 #include "ConfigParser.h"
20 #include "ClientSocket.h"
21 #include "bots.h"
22 #include "UserNode.h"
23 #include "ChanNode.h"
24 #include "ChanUser.h"
25 #include "IRCParser.h"
26 #include "modcmd.h"
27
28 int statistics_requested_lusers = 0;
29
30 static TIMEQ_CALLBACK(main_statistics) {
31     int update_minutes = get_int_field("statistics.frequency");
32     if(!update_minutes) update_minutes = 2;
33     timeq_add(update_minutes * 60, 0, main_statistics, NULL);
34     if(get_int_field("statistics.enable")) {
35         statistics_requested_lusers = 1;
36         if(get_int_field("statistics.include_lusers")) {
37             struct ClientSocket *bot, *lusersbot = NULL;
38             for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
39                 if(bot->flags & SOCKET_FLAG_PREFERRED)
40                     lusersbot = bot;
41             }
42             bot = lusersbot;
43             if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
44             putsock(bot, "LUSERS");
45         } else
46             statistics_update();
47     } else {
48         statistics_privmsg = 0;
49         statistics_commands = 0;
50     }
51 }
52
53 int statistics_update() {
54     if(get_int_field("statistics.enable") && statistics_requested_lusers && get_string_field("statistics.execute")) {
55         statistics_requested_lusers = 0;
56         char command[MAXLEN];
57         /* parameters:
58          - visible users
59          - visible chanusers
60          - visible channels
61          - privmsg per minute
62          - commands per minute
63          - network users
64          - network channels
65         */
66         sprintf(command, "%s %d %d %d %d %d %d %d", get_string_field("statistics.execute"), getUserCount(), getChanUserCount(), getChannelCount(), statistics_privmsg, statistics_commands, statistics_network_users, statistics_network_channels);
67         statistics_privmsg = 0;
68         statistics_commands = 0;
69         return system(command);
70     }
71         return -1;
72 }
73
74 void init_statistics() {
75     int update_minutes = get_int_field("statistics.frequency");
76     if(!update_minutes) update_minutes = 2;
77     timeq_add(update_minutes * 60 + 10, 0, main_statistics, NULL);
78 }