1 /* main.c - NeonServ v5.3
2 * Copyright (C) 2011-2012 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 running, hard_restart;
42 static int statistics_requested_lusers = 0;
43 int statistics_enabled;
44 TIMEQ_CALLBACK(main_statistics);
47 pthread_mutex_t cache_sync;
48 pthread_mutex_t whohandler_sync, whohandler_mass_sync;
61 free_handleinfohandler();
65 static int load_mysql_config() {
66 char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
68 if(loadConfig("neonserv.conf")) {
69 mysql_host = get_string_field("MySQL.host");
71 perror("invalid neonserv.conf: missing MySQL.host");
74 mysql_serverport = get_int_field("MySQL.port");
76 mysql_serverport = 3306;
77 mysql_user = get_string_field("MySQL.user");
79 perror("invalid neonserv.conf: missing MySQL.user");
82 mysql_pass = get_string_field("MySQL.pass");
84 perror("invalid neonserv.conf: missing MySQL.pass");
87 mysql_base = get_string_field("MySQL.base");
89 perror("invalid neonserv.conf: missing MySQL base");
93 perror("Unable to load neonserv.conf");
96 init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
101 void * thread_main(void *arg) {
104 socket_wait = time(0) + SOCKET_SELECT_TIME;
106 socket_loop(SOCKET_SELECT_TIME);
107 } while(time(0) < socket_wait);
116 int main(int argc, char *argv[]) {
119 start_time = time(0);
124 // Start Windows Sockets.
125 res = WSAStartup(MAKEWORD(2, 0), &wsadata);
128 perror("Unable to start Windows Sockets");
133 if(!load_mysql_config()) return 0;
135 statistics_enabled = get_int_field("statistics.enable");
138 THREAD_MUTEX_INIT(cache_sync);
139 THREAD_MUTEX_INIT(whohandler_sync);
140 THREAD_MUTEX_INIT(whohandler_mass_sync);
154 init_handleinfohandler();
161 int update_minutes = get_int_field("statistics.frequency");
162 if(!update_minutes) update_minutes = 2;
163 timeq_add(update_minutes * 60 + 10, main_statistics, NULL);
165 int worker_threads = get_int_field("General.worker_threads");
166 if(!worker_threads) worker_threads = 1;
170 pthread_t tid[worker_threads];
172 for(tid_id = 0; tid_id < worker_threads; tid_id++) {
174 pthread_create(&tid[tid_id], NULL, thread_main, NULL);
176 int usleep_delay = 1000000 / TICKS_PER_SECOND;
181 usleep(usleep_delay);
183 for(tid_id = 0; tid_id < worker_threads; tid_id++) {
184 pthread_join(tid[tid_id], NULL);
190 socket_wait = time(0) + SOCKET_SELECT_TIME;
192 socket_loop(SOCKET_SELECT_TIME);
193 } while(time(0) < socket_wait);
203 /* Append a NULL to the end of argv[]. */
204 char **restart_argv = (char **)alloca((argc + 1) * sizeof(char *));
205 memcpy(restart_argv, argv, argc * sizeof(char *));
206 restart_argv[argc] = NULL;
209 execv(argv[0], (const char * const*)restart_argv);
211 execv(argv[0], restart_argv);
217 int stricmp (const char *s1, const char *s2)
219 if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
220 if (s2 == NULL) return *s1;
222 while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
224 if (*s1 == '\0') break;
230 int stricmplen (const char *s1, const char *s2, int len)
232 if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
233 if (s2 == NULL) return *s1;
236 while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
239 if (*s1 == '\0') break;
246 void restart_bot(int do_hard_restart) {
247 hard_restart = do_hard_restart;
256 void reload_config() {
257 loadConfig("neonserv.conf");
260 TIMEQ_CALLBACK(main_statistics) {
261 int update_minutes = get_int_field("statistics.frequency");
262 if(!update_minutes) update_minutes = 2;
263 timeq_add(update_minutes * 60, main_statistics, NULL);
264 if(get_int_field("statistics.enable")) {
265 statistics_enabled = 1;
266 statistics_requested_lusers = 1;
267 if(get_int_field("statistics.include_lusers")) {
268 struct ClientSocket *bot, *lusersbot = NULL;
269 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
270 if(bot->flags & SOCKET_FLAG_PREFERRED)
274 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
275 putsock(bot, "LUSERS");
280 statistics_enabled = 0;
283 void statistics_update() {
284 if(get_int_field("statistics.enable") && statistics_requested_lusers && get_string_field("statistics.execute")) {
285 statistics_requested_lusers = 0;
286 char command[MAXLEN];
292 - commands per minute
296 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);
297 statistics_privmsg = 0;
298 statistics_commands = 0;