added thread count to cmd_netinfo
[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
40 time_t start_time;
41 static int running, hard_restart;
42 static int statistics_requested_lusers = 0;
43 int statistics_enabled;
44 TIMEQ_CALLBACK(main_statistics);
45 #ifdef HAVE_THREADS
46 int running_threads;
47 #endif
48
49 void cleanup() {
50     free_sockets();
51     free_parser();
52     free_UserNode();
53     free_ChanNode();
54     free_bind();
55     free_modcmd();
56     free_whoqueue();
57     free_bots();
58     free_mysql();
59     free_handleinfohandler();
60     free_lang();
61 }
62
63 static int load_mysql_config() {
64     char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
65     int mysql_serverport;
66     if(loadConfig("neonserv.conf")) {
67         mysql_host = get_string_field("MySQL.host");
68         if(!mysql_host) {
69             perror("invalid neonserv.conf: missing MySQL.host");
70             return 0;
71         }
72         mysql_serverport = get_int_field("MySQL.port");
73         if(!mysql_serverport)
74             mysql_serverport = 3306;
75         mysql_user = get_string_field("MySQL.user");
76         if(!mysql_user) {
77             perror("invalid neonserv.conf: missing MySQL.user");
78             return 0;
79         }
80         mysql_pass = get_string_field("MySQL.pass");
81         if(!mysql_pass) {
82             perror("invalid neonserv.conf: missing MySQL.pass");
83             return 0;
84         }
85         mysql_base = get_string_field("MySQL.base");
86         if(!mysql_base) {
87             perror("invalid neonserv.conf: missing MySQL base");
88             return 0;
89         }
90     } else {
91         perror("Unable to load neonserv.conf");
92         return 0;
93     }
94     init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
95     return 1;
96 }
97
98 #ifdef HAVE_THREADS
99 void * thread_main(void *arg) {
100     time_t socket_wait;
101     while(running) {
102         socket_wait = time(0) + SOCKET_SELECT_TIME;
103         do {
104             socket_loop(SOCKET_SELECT_TIME);
105         } while(time(0) < socket_wait);
106         timeq_tick();
107         loop_bots();
108         clearTempUsers();
109         destroyEvents();
110         queue_loop();
111     }
112     running_threads--;
113     return NULL;
114 }
115 #endif
116
117 int main(int argc, char *argv[]) {
118 main:
119     
120     start_time = time(0);
121     
122     #ifdef WIN32
123     int res;
124     WSADATA wsadata;
125     // Start Windows Sockets.
126     res = WSAStartup(MAKEWORD(2, 0), &wsadata);
127     if (res)
128     {
129         perror("Unable to start Windows Sockets");
130         return 0;
131     }
132     #endif
133     
134     if(!load_mysql_config()) return 0;
135     
136     statistics_enabled = get_int_field("statistics.enable");
137     
138     queue_init();
139     init_timeq();
140     init_lang();
141     ssl_init();
142     init_parser();
143     init_UserNode();
144     init_ChanNode();
145     init_ModeNode();
146     init_bind();
147         init_modcmd();
148     init_handleinfohandler();
149     init_tools();
150     register_commands();
151     init_bots();
152     init_DBHelper();
153     
154     load_languages();
155     int update_minutes = get_int_field("statistics.frequency");
156     if(!update_minutes) update_minutes = 2;
157     timeq_add(update_minutes * 60 + 10, main_statistics, NULL);
158     
159     int worker_threads = get_int_field("General.worker_threads");
160     if(!worker_threads) worker_threads = 1;
161     
162     running = 1;
163     #ifdef HAVE_THREADS
164     pthread_t tid[worker_threads];
165     int tid_id = 0;
166     for(tid_id = 0; tid_id < worker_threads; tid_id++) {
167         running_threads++;
168         pthread_create(&tid[tid_id], NULL, thread_main, NULL);
169     }
170     for(tid_id = 0; tid_id < worker_threads; tid_id++) {
171         pthread_join(tid[tid_id], NULL);
172     }
173     running_threads = 0;
174     #else
175     time_t socket_wait;
176     while(running) {
177         socket_wait = time(0) + SOCKET_SELECT_TIME;
178         do {
179             socket_loop(SOCKET_SELECT_TIME);
180         } while(time(0) < socket_wait);
181         timeq_tick();
182         loop_bots();
183         clearTempUsers();
184         destroyEvents();
185         queue_loop();
186     }
187     #endif
188     cleanup();
189     if(hard_restart) {
190         /* Append a NULL to the end of argv[]. */
191         char **restart_argv = (char **)alloca((argc + 1) * sizeof(char *));
192         memcpy(restart_argv, argv, argc * sizeof(char *));
193         restart_argv[argc] = NULL;
194         
195         #ifdef WIN32
196         execv(argv[0], (const char * const*)restart_argv);
197         #else
198         execv(argv[0], restart_argv);
199         #endif
200     }
201     goto main;
202 }
203
204 int stricmp (const char *s1, const char *s2)
205 {
206    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
207    if (s2 == NULL) return *s1;
208    char c1, c2;
209    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
210    {
211      if (*s1 == '\0') break;
212      ++s1; ++s2;
213    }
214    return c1 - c2;
215 }
216
217 int stricmplen (const char *s1, const char *s2, int len)
218 {
219    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
220    if (s2 == NULL) return *s1;
221    char c1, c2;
222    int i = 0;
223    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
224    {
225      i++;
226      if (*s1 == '\0') break;
227      ++s1; ++s2;
228      if(i == len) break;
229    }
230    return c1 - c2;
231 }
232
233 void restart_bot(int do_hard_restart) {
234     hard_restart = do_hard_restart;
235     running = 0;
236 }
237
238 void stop_bot() {
239     cleanup();
240     exit(0);
241 }
242
243 void reload_config() {
244     loadConfig("neonserv.conf");
245 }
246
247 TIMEQ_CALLBACK(main_statistics) {
248     int update_minutes = get_int_field("statistics.frequency");
249     if(!update_minutes) update_minutes = 2;
250     timeq_add(update_minutes * 60, main_statistics, NULL);
251     if(get_int_field("statistics.enable")) {
252         statistics_enabled = 1;
253         statistics_requested_lusers = 1;
254         if(get_int_field("statistics.include_lusers")) {
255             struct ClientSocket *bot, *lusersbot = NULL;
256             for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
257                 if(bot->flags & SOCKET_FLAG_PREFERRED)
258                     lusersbot = bot;
259             }
260             bot = lusersbot;
261             if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
262             putsock(bot, "LUSERS");
263         } else {
264             statistics_update();
265         }
266     } else
267         statistics_enabled = 0;
268 }
269
270 void statistics_update() {
271     if(get_int_field("statistics.enable") && statistics_requested_lusers && get_string_field("statistics.execute")) {
272         statistics_requested_lusers = 0;
273         char command[MAXLEN];
274         /* parameters:
275          - visible users
276          - visible chanusers
277          - visible channels
278          - privmsg per minute
279          - commands per minute
280          - network users
281          - network channels
282         */
283         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);
284         statistics_privmsg = 0;
285         statistics_commands = 0;
286         system(command);
287     }
288 }