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"
42 static int running, hard_restart;
43 static int statistics_requested_lusers = 0;
44 int statistics_enabled;
45 TIMEQ_CALLBACK(main_statistics);
48 pthread_mutex_t cache_sync;
49 pthread_mutex_t whohandler_sync, whohandler_mass_sync;
63 free_handleinfohandler();
67 static int load_mysql_config() {
68 char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
70 if(loadConfig("neonserv.conf")) {
71 mysql_host = get_string_field("MySQL.host");
73 perror("invalid neonserv.conf: missing MySQL.host");
76 mysql_serverport = get_int_field("MySQL.port");
78 mysql_serverport = 3306;
79 mysql_user = get_string_field("MySQL.user");
81 perror("invalid neonserv.conf: missing MySQL.user");
84 mysql_pass = get_string_field("MySQL.pass");
86 perror("invalid neonserv.conf: missing MySQL.pass");
89 mysql_base = get_string_field("MySQL.base");
91 perror("invalid neonserv.conf: missing MySQL base");
95 perror("Unable to load neonserv.conf");
98 init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
103 void * thread_main(void *arg) {
106 socket_wait = time(0) + SOCKET_SELECT_TIME;
108 socket_loop(SOCKET_SELECT_TIME);
109 } while(time(0) < socket_wait);
118 int main(int argc, char *argv[]) {
121 start_time = time(0);
126 // Start Windows Sockets.
127 res = WSAStartup(MAKEWORD(2, 0), &wsadata);
130 perror("Unable to start Windows Sockets");
135 if(!load_mysql_config()) return 0;
137 statistics_enabled = get_int_field("statistics.enable");
140 THREAD_MUTEX_INIT(cache_sync);
141 THREAD_MUTEX_INIT(whohandler_sync);
142 THREAD_MUTEX_INIT(whohandler_mass_sync);
156 init_handleinfohandler();
164 int update_minutes = get_int_field("statistics.frequency");
165 if(!update_minutes) update_minutes = 2;
166 timeq_add(update_minutes * 60 + 10, main_statistics, NULL);
168 int worker_threads = get_int_field("General.worker_threads");
169 if(!worker_threads) worker_threads = 1;
173 pthread_t tid[worker_threads];
175 for(tid_id = 0; tid_id < worker_threads; tid_id++) {
177 pthread_create(&tid[tid_id], NULL, thread_main, NULL);
179 int usleep_delay = 1000000 / TICKS_PER_SECOND;
185 usleep(usleep_delay);
187 for(tid_id = 0; tid_id < worker_threads; tid_id++) {
188 pthread_join(tid[tid_id], NULL);
194 socket_wait = time(0) + SOCKET_SELECT_TIME;
196 socket_loop(SOCKET_SELECT_TIME);
197 } while(time(0) < socket_wait);
208 /* Append a NULL to the end of argv[]. */
209 char **restart_argv = (char **)alloca((argc + 1) * sizeof(char *));
210 memcpy(restart_argv, argv, argc * sizeof(char *));
211 restart_argv[argc] = NULL;
214 execv(argv[0], (const char * const*)restart_argv);
216 execv(argv[0], restart_argv);
222 int stricmp (const char *s1, const char *s2)
224 if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
225 if (s2 == NULL) return *s1;
227 while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
229 if (*s1 == '\0') break;
235 int stricmplen (const char *s1, const char *s2, int len)
237 if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
238 if (s2 == NULL) return *s1;
241 while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
244 if (*s1 == '\0') break;
251 void restart_bot(int do_hard_restart) {
252 hard_restart = do_hard_restart;
261 void reload_config() {
262 loadConfig("neonserv.conf");
265 TIMEQ_CALLBACK(main_statistics) {
266 int update_minutes = get_int_field("statistics.frequency");
267 if(!update_minutes) update_minutes = 2;
268 timeq_add(update_minutes * 60, main_statistics, NULL);
269 if(get_int_field("statistics.enable")) {
270 statistics_enabled = 1;
271 statistics_requested_lusers = 1;
272 if(get_int_field("statistics.include_lusers")) {
273 struct ClientSocket *bot, *lusersbot = NULL;
274 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
275 if(bot->flags & SOCKET_FLAG_PREFERRED)
279 if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
280 putsock(bot, "LUSERS");
285 statistics_enabled = 0;
288 void statistics_update() {
289 if(get_int_field("statistics.enable") && statistics_requested_lusers && get_string_field("statistics.execute")) {
290 statistics_requested_lusers = 0;
291 char command[MAXLEN];
297 - commands per minute
301 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);
302 statistics_privmsg = 0;
303 statistics_commands = 0;