1 /* main.c - NeonServ v5.2
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
19 #include "ClientSocket.h"
22 #include "IRCEvents.h"
23 #include "IRCParser.h"
25 #include "WHOHandler.h"
27 #include "mysqlConn.h"
28 #include "HandleInfoHandler.h"
32 #include "EventLogger.h"
37 #include "ConfigParser.h"
41 static int statistics_requested_lusers = 0;
42 int statistics_enabled;
43 TIMEQ_CALLBACK(main_statistics);
55 free_handleinfohandler();
59 static int load_mysql_config() {
60 char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
62 if(loadConfig("neonserv.conf")) {
63 mysql_host = get_string_field("MySQL.host");
65 perror("invalid neonserv.conf: missing MySQL.host");
68 mysql_serverport = get_int_field("MySQL.port");
70 mysql_serverport = 3306;
71 mysql_user = get_string_field("MySQL.user");
73 perror("invalid neonserv.conf: missing MySQL.user");
76 mysql_pass = get_string_field("MySQL.pass");
78 perror("invalid neonserv.conf: missing MySQL.pass");
81 mysql_base = get_string_field("MySQL.base");
83 perror("invalid neonserv.conf: missing MySQL base");
87 perror("Unable to load neonserv.conf");
90 init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
102 // Start Windows Sockets.
103 res = WSAStartup(MAKEWORD(2, 0), &wsadata);
106 perror("Unable to start Windows Sockets");
111 if(!load_mysql_config()) return 0;
113 statistics_enabled = get_int_field("statistics.enable");
123 init_handleinfohandler();
130 int update_minutes = get_int_field("statistics.frequency");
131 if(!update_minutes) update_minutes = 2;
132 timeq_add(update_minutes * 60 + 10, main_statistics, NULL);
137 socket_wait = time(0) + SOCKET_SELECT_TIME;
139 socket_loop(SOCKET_SELECT_TIME);
140 } while(time(0) < socket_wait);
151 int stricmp (const char *s1, const char *s2)
153 if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
154 if (s2 == NULL) return *s1;
156 while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
158 if (*s1 == '\0') break;
164 int stricmplen (const char *s1, const char *s2, int len)
166 if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
167 if (s2 == NULL) return *s1;
170 while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
173 if (*s1 == '\0') break;
180 void restart_bot(int hard_restart) {
189 void reload_config() {
190 loadConfig("neonserv.conf");
193 TIMEQ_CALLBACK(main_statistics) {
194 int update_minutes = get_int_field("statistics.frequency");
195 if(!update_minutes) update_minutes = 2;
196 timeq_add(update_minutes * 60, main_statistics, NULL);
197 if(get_int_field("statistics.enable")) {
198 statistics_enabled = 1;
199 statistics_requested_lusers = 1;
200 if(get_int_field("statistics.include_lusers")) {
201 struct ClientSocket *bot, *lusersbot = NULL;
202 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
203 if(bot->flags & SOCKET_FLAG_PREFERRED)
207 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
208 putsock(bot, "LUSERS");
213 statistics_enabled = 0;
216 void statistics_update() {
217 if(get_int_field("statistics.enable") && statistics_requested_lusers && get_string_field("statistics.execute")) {
218 statistics_requested_lusers = 0;
219 char command[MAXLEN];
225 - commands per minute
229 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);
230 statistics_privmsg = 0;
231 statistics_commands = 0;