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/>.
20 #include "ClientSocket.h"
23 #include "IRCEvents.h"
24 #include "IRCParser.h"
26 #include "WHOHandler.h"
28 #include "mysqlConn.h"
29 #include "HandleInfoHandler.h"
33 #include "EventLogger.h"
38 #include "ConfigParser.h"
43 static int running, hard_restart;
44 static int statistics_requested_lusers = 0;
45 int statistics_enabled;
46 TIMEQ_CALLBACK(main_statistics);
47 TIMEQ_CALLBACK(main_checkauths);
48 static int process_argc;
49 static char **process_argv;
52 pthread_mutex_t cache_sync;
53 pthread_mutex_t whohandler_sync, whohandler_mass_sync;
67 free_handleinfohandler();
71 static int load_mysql_config() {
72 char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
74 if(loadConfig("neonserv.conf")) {
75 mysql_host = get_string_field("MySQL.host");
77 perror("invalid neonserv.conf: missing MySQL.host");
80 mysql_serverport = get_int_field("MySQL.port");
82 mysql_serverport = 3306;
83 mysql_user = get_string_field("MySQL.user");
85 perror("invalid neonserv.conf: missing MySQL.user");
88 mysql_pass = get_string_field("MySQL.pass");
90 perror("invalid neonserv.conf: missing MySQL.pass");
93 mysql_base = get_string_field("MySQL.base");
95 perror("invalid neonserv.conf: missing MySQL base");
99 perror("Unable to load neonserv.conf");
102 init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
107 pthread_t *current_threads = NULL;
109 void * thread_main(void *arg) {
112 socket_wait = time(0) + SOCKET_SELECT_TIME;
114 socket_loop(SOCKET_SELECT_TIME);
115 } while(time(0) < socket_wait);
123 int getCurrentThreadID() {
124 if(!current_threads) return 0;
126 unsigned int my_tid = (unsigned int) pthread_self_tid();
127 for(i = 0; i < running_threads; i++) {
129 if((unsigned int) current_threads[i].p == my_tid)
131 if((unsigned int) current_threads[i] == my_tid)
140 int main(int argc, char *argv[]) {
145 signal(SIGABRT, sighandler);
146 signal(SIGFPE, sighandler);
147 signal(SIGILL, sighandler);
148 signal(SIGINT, sighandler);
149 signal(SIGSEGV, sighandler);
150 signal(SIGTERM, sighandler);
152 start_time = time(0);
157 // Start Windows Sockets.
158 res = WSAStartup(MAKEWORD(2, 0), &wsadata);
161 perror("Unable to start Windows Sockets");
166 if(!load_mysql_config()) return 0;
168 statistics_enabled = get_int_field("statistics.enable");
171 THREAD_MUTEX_INIT(cache_sync);
172 THREAD_MUTEX_INIT(whohandler_sync);
173 THREAD_MUTEX_INIT(whohandler_mass_sync);
187 init_handleinfohandler();
195 int update_minutes = get_int_field("statistics.frequency");
196 if(!update_minutes) update_minutes = 2;
197 timeq_add(update_minutes * 60 + 10, main_statistics, NULL);
199 timeq_add(90, main_checkauths, NULL);
201 int worker_threads = get_int_field("General.worker_threads");
202 if(!worker_threads) worker_threads = 1;
206 current_threads = calloc(worker_threads, sizeof(*current_threads));
207 for(tid_id = 0; tid_id < worker_threads; tid_id++) {
209 pthread_create(¤t_threads[tid_id], NULL, thread_main, NULL);
211 int usleep_delay = 1000000 / TICKS_PER_SECOND;
217 usleep(usleep_delay);
219 for(tid_id = 0; tid_id < worker_threads; tid_id++) {
220 pthread_join(current_threads[tid_id], NULL);
226 socket_wait = time(0) + SOCKET_SELECT_TIME;
228 socket_loop(SOCKET_SELECT_TIME);
229 } while(time(0) < socket_wait);
245 void restart_process() {
246 /* Append a NULL to the end of argv[]. */
247 char **restart_argv = (char **)alloca((process_argc + 1) * sizeof(char *));
248 memcpy(restart_argv, process_argv, process_argc * sizeof(char *));
249 restart_argv[process_argc] = NULL;
251 execv(process_argv[0], (const char * const*)restart_argv);
253 execv(process_argv[0], restart_argv);
257 int stricmp (const char *s1, const char *s2)
259 if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
260 if (s2 == NULL) return *s1;
262 while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
264 if (*s1 == '\0') break;
270 int stricmplen (const char *s1, const char *s2, int len)
272 if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
273 if (s2 == NULL) return *s1;
276 while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
279 if (*s1 == '\0') break;
286 void restart_bot(int do_hard_restart) {
287 hard_restart = do_hard_restart;
296 void reload_config() {
297 loadConfig("neonserv.conf");
300 static int getCurrentSecondsOfDay() {
301 time_t now = time(0);
302 struct tm *timeofday = localtime(&now);
304 seconds += timeofday->tm_hour * 3600;
305 seconds += timeofday->tm_min * 60;
306 seconds += timeofday->tm_sec;
310 static AUTHLOOKUP_CALLBACK(main_checkauths_callback) {
311 //check if registered is still valid
314 printf_mysql_query("SELECT `user_id`, `user_registered` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
316 if ((row = mysql_fetch_row(res)) != NULL) {
317 if(!exists || (strcmp(row[1], "0") && registered != atoi(row[1]))) {
318 //User is no longer valid! Delete it...
319 deleteUser(atoi(row[0]));
320 char *alertchan = get_string_field("General.CheckAuths.alertchan");
322 struct ChanNode *alertchan_chan = getChanByName(alertchan);
323 struct ClientSocket *alertclient;
324 if(alertchan_chan && (alertclient = getChannelBot(alertchan_chan, 0)) != NULL) {
325 putsock(alertclient, "PRIVMSG %s :Deleted User %s", alertchan_chan->name, auth);
328 } else if(exists && !strcmp(row[1], "0")) {
329 printf_mysql_query("UPDATE `users` SET `user_registered` = '%lu', `user_lastcheck` = UNIX_TIMESTAMP() WHERE `user_id` = '%s'", (unsigned long) registered, row[0]);
331 printf_mysql_query("UPDATE `users` SET `user_lastcheck` = UNIX_TIMESTAMP() WHERE `user_id` = '%s'", row[0]);
336 TIMEQ_CALLBACK(main_checkauths) {
338 if(get_int_field("General.CheckAuths.enabled")) {
339 int check_start_time = get_int_field("General.CheckAuths.start_time") * 3600;
340 int duration = get_int_field("General.CheckAuths.duration") * 60;
341 int now = getCurrentSecondsOfDay();
342 if(now < check_start_time && check_start_time+duration >= 86400) {
343 check_start_time -= 86400;
345 if(now >= check_start_time && now < (check_start_time + duration)) {
346 next_call = get_int_field("General.CheckAuths.interval");
347 //get the "longest-unchecked-user"
351 time_t unixtime = time(0);
352 int min_unckecked = get_int_field("General.CheckAuths.min_unckecked");
353 printf_mysql_query("SELECT `user_user`, `user_lastcheck` FROM `users` ORDER BY `user_lastcheck` ASC LIMIT 1");
355 if ((row = mysql_fetch_row(res)) != NULL) {
356 lastcheck = atoi(row[1]);
357 if(!lastcheck || unixtime - lastcheck >= min_unckecked) {
358 lookup_authname(row[0], main_checkauths_callback, NULL);
364 if(now > check_start_time)
365 pending = 86400 - now + check_start_time;
367 pending = check_start_time - now;
373 timeq_add(next_call, main_checkauths, NULL);
376 TIMEQ_CALLBACK(main_statistics) {
377 int update_minutes = get_int_field("statistics.frequency");
378 if(!update_minutes) update_minutes = 2;
379 timeq_add(update_minutes * 60, main_statistics, NULL);
380 if(get_int_field("statistics.enable")) {
381 statistics_enabled = 1;
382 statistics_requested_lusers = 1;
383 if(get_int_field("statistics.include_lusers")) {
384 struct ClientSocket *bot, *lusersbot = NULL;
385 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
386 if(bot->flags & SOCKET_FLAG_PREFERRED)
390 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
391 putsock(bot, "LUSERS");
396 statistics_enabled = 0;
399 void statistics_update() {
400 if(get_int_field("statistics.enable") && statistics_requested_lusers && get_string_field("statistics.execute")) {
401 statistics_requested_lusers = 0;
402 char command[MAXLEN];
408 - commands per minute
412 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);
413 statistics_privmsg = 0;
414 statistics_commands = 0;