e8d366aa0d7141696c51b302cc900f2b961fa3c2
[NeonServV5.git] / src / main.c
1 /* main.c - NeonServ v5.3
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17
18 #include "main.h"
19 #include "ClientSocket.h"
20 #include "UserNode.h"
21 #include "ChanNode.h"
22 #include "IRCEvents.h"
23 #include "IRCParser.h"
24 #include "modcmd.h"
25 #include "WHOHandler.h"
26 #include "bots.h"
27 #include "mysqlConn.h"
28 #include "HandleInfoHandler.h"
29 #include "lang.h"
30 #include "tools.h"
31 #include "timeq.h"
32 #include "EventLogger.h"
33 #include "ModeNode.h"
34 #include "IRCQueue.h"
35 #include "DBHelper.h"
36 #include "commands.h"
37 #include "ConfigParser.h"
38 #include "ssl.h"
39 #include "QServer.h"
40
41 time_t start_time;
42 static int running, hard_restart;
43 static int statistics_requested_lusers = 0;
44 int statistics_enabled;
45 TIMEQ_CALLBACK(main_statistics);
46 #ifdef HAVE_THREADS
47 int running_threads;
48 pthread_mutex_t cache_sync;
49 pthread_mutex_t whohandler_sync, whohandler_mass_sync;
50 #endif
51
52 void cleanup() {
53     free_sockets();
54     qserver_free();
55     free_parser();
56     free_UserNode();
57     free_ChanNode();
58     free_bind();
59     free_modcmd();
60     free_whoqueue();
61     free_bots();
62     free_mysql();
63     free_handleinfohandler();
64     free_lang();
65 }
66
67 static int load_mysql_config() {
68     char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
69     int mysql_serverport;
70     if(loadConfig("neonserv.conf")) {
71         mysql_host = get_string_field("MySQL.host");
72         if(!mysql_host) {
73             perror("invalid neonserv.conf: missing MySQL.host");
74             return 0;
75         }
76         mysql_serverport = get_int_field("MySQL.port");
77         if(!mysql_serverport)
78             mysql_serverport = 3306;
79         mysql_user = get_string_field("MySQL.user");
80         if(!mysql_user) {
81             perror("invalid neonserv.conf: missing MySQL.user");
82             return 0;
83         }
84         mysql_pass = get_string_field("MySQL.pass");
85         if(!mysql_pass) {
86             perror("invalid neonserv.conf: missing MySQL.pass");
87             return 0;
88         }
89         mysql_base = get_string_field("MySQL.base");
90         if(!mysql_base) {
91             perror("invalid neonserv.conf: missing MySQL base");
92             return 0;
93         }
94     } else {
95         perror("Unable to load neonserv.conf");
96         return 0;
97     }
98     init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
99     return 1;
100 }
101
102 #ifdef HAVE_THREADS
103 void * thread_main(void *arg) {
104     time_t socket_wait;
105     while(running) {
106         socket_wait = time(0) + SOCKET_SELECT_TIME;
107         do {
108             socket_loop(SOCKET_SELECT_TIME);
109         } while(time(0) < socket_wait);
110         clearTempUsers();
111         destroyEvents();
112     }
113     running_threads--;
114     return NULL;
115 }
116 #endif
117
118 int main(int argc, char *argv[]) {
119 main:
120     
121     start_time = time(0);
122     
123     #ifdef WIN32
124     int res;
125     WSADATA wsadata;
126     // Start Windows Sockets.
127     res = WSAStartup(MAKEWORD(2, 0), &wsadata);
128     if (res)
129     {
130         perror("Unable to start Windows Sockets");
131         return 0;
132     }
133     #endif
134     
135     if(!load_mysql_config()) return 0;
136     
137     statistics_enabled = get_int_field("statistics.enable");
138     
139     #ifdef HAVE_THREADS
140     THREAD_MUTEX_INIT(cache_sync);
141     THREAD_MUTEX_INIT(whohandler_sync);
142     THREAD_MUTEX_INIT(whohandler_mass_sync);
143     #endif
144     
145     queue_init();
146     init_sockets();
147     init_timeq();
148     init_lang();
149     ssl_init();
150     init_parser();
151     init_UserNode();
152     init_ChanNode();
153     init_ModeNode();
154     init_bind();
155         init_modcmd();
156     init_handleinfohandler();
157     init_tools();
158     register_commands();
159     init_bots();
160     init_DBHelper();
161     qserver_init();
162     
163     load_languages();
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);
167     
168     int worker_threads = get_int_field("General.worker_threads");
169     if(!worker_threads) worker_threads = 1;
170     
171     running = 1;
172     #ifdef HAVE_THREADS
173     pthread_t tid[worker_threads];
174     int tid_id = 0;
175     for(tid_id = 0; tid_id < worker_threads; tid_id++) {
176         running_threads++;
177         pthread_create(&tid[tid_id], NULL, thread_main, NULL);
178     }
179     int usleep_delay = 1000000 / TICKS_PER_SECOND;
180     while(running) {
181         timeq_tick();
182         loop_bots();
183         qserver_loop();
184         queue_loop();
185         usleep(usleep_delay);
186     }
187     for(tid_id = 0; tid_id < worker_threads; tid_id++) {
188         pthread_join(tid[tid_id], NULL);
189     }
190     running_threads = 0;
191     #else
192     time_t socket_wait;
193     while(running) {
194         socket_wait = time(0) + SOCKET_SELECT_TIME;
195         do {
196             socket_loop(SOCKET_SELECT_TIME);
197         } while(time(0) < socket_wait);
198         timeq_tick();
199         loop_bots();
200         clearTempUsers();
201         destroyEvents();
202         qserver_loop();
203         queue_loop();
204     }
205     #endif
206     cleanup();
207     if(hard_restart) {
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;
212         
213         #ifdef WIN32
214         execv(argv[0], (const char * const*)restart_argv);
215         #else
216         execv(argv[0], restart_argv);
217         #endif
218     }
219     goto main;
220 }
221
222 int stricmp (const char *s1, const char *s2)
223 {
224    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
225    if (s2 == NULL) return *s1;
226    char c1, c2;
227    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
228    {
229      if (*s1 == '\0') break;
230      ++s1; ++s2;
231    }
232    return c1 - c2;
233 }
234
235 int stricmplen (const char *s1, const char *s2, int len)
236 {
237    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
238    if (s2 == NULL) return *s1;
239    char c1, c2;
240    int i = 0;
241    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
242    {
243      i++;
244      if (*s1 == '\0') break;
245      ++s1; ++s2;
246      if(i == len) break;
247    }
248    return c1 - c2;
249 }
250
251 void restart_bot(int do_hard_restart) {
252     hard_restart = do_hard_restart;
253     running = 0;
254 }
255
256 void stop_bot() {
257     cleanup();
258     exit(0);
259 }
260
261 void reload_config() {
262     loadConfig("neonserv.conf");
263 }
264
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)
276                     lusersbot = bot;
277             }
278             bot = lusersbot;
279             if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
280             putsock(bot, "LUSERS");
281         } else {
282             statistics_update();
283         }
284     } else
285         statistics_enabled = 0;
286 }
287
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];
292         /* parameters:
293          - visible users
294          - visible chanusers
295          - visible channels
296          - privmsg per minute
297          - commands per minute
298          - network users
299          - network channels
300         */
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;
304         system(command);
305     }
306 }