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