tidied up main.c code
[NeonServV5.git] / src / statistics.c
diff --git a/src/statistics.c b/src/statistics.c
new file mode 100644 (file)
index 0000000..5d102ca
--- /dev/null
@@ -0,0 +1,78 @@
+/* statistics.c - NeonServ v5.6
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+#include "statistics.h"
+#include "timeq.h"
+#include "ConfigParser.h"
+#include "ClientSocket.h"
+#include "bots.h"
+#include "UserNode.h"
+#include "ChanNode.h"
+#include "ChanUser.h"
+#include "IRCParser.h"
+#include "modcmd.h"
+
+int statistics_requested_lusers = 0;
+
+static TIMEQ_CALLBACK(main_statistics) {
+    int update_minutes = get_int_field("statistics.frequency");
+    if(!update_minutes) update_minutes = 2;
+    timeq_add(update_minutes * 60, 0, main_statistics, NULL);
+    if(get_int_field("statistics.enable")) {
+        statistics_requested_lusers = 1;
+        if(get_int_field("statistics.include_lusers")) {
+            struct ClientSocket *bot, *lusersbot = NULL;
+            for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+                if(bot->flags & SOCKET_FLAG_PREFERRED)
+                    lusersbot = bot;
+            }
+            bot = lusersbot;
+            if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
+            putsock(bot, "LUSERS");
+        } else
+            statistics_update();
+    } else {
+        statistics_privmsg = 0;
+        statistics_commands = 0;
+    }
+}
+
+int statistics_update() {
+    if(get_int_field("statistics.enable") && statistics_requested_lusers && get_string_field("statistics.execute")) {
+        statistics_requested_lusers = 0;
+        char command[MAXLEN];
+        /* parameters:
+         - visible users
+         - visible chanusers
+         - visible channels
+         - privmsg per minute
+         - commands per minute
+         - network users
+         - network channels
+        */
+        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);
+        statistics_privmsg = 0;
+        statistics_commands = 0;
+        return system(command);
+    }
+       return -1;
+}
+
+void init_statistics() {
+    int update_minutes = get_int_field("statistics.frequency");
+    if(!update_minutes) update_minutes = 2;
+    timeq_add(update_minutes * 60 + 10, 0, main_statistics, NULL);
+}