6c881fb00bfef702b9254c2cba82b247953db0ca
[NeonServV5.git] / src / main.c
1 /* main.c - NeonServ v5.2
2  * Copyright (C) 2011  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 "lib/ini.h"
37
38 time_t start_time;
39
40 void cleanup() {
41     free_sockets();
42     free_parser();
43     free_UserNode();
44     free_ChanNode();
45     free_bind();
46     free_modcmd();
47     free_whoqueue();
48     free_bots();
49     free_mysql();
50     free_handleinfohandler();
51     free_lang();
52 }
53
54 static int load_mysql_config() {
55     char mysql_host[MAXLEN], mysql_port_str[MAXLEN], mysql_user[MAXLEN], mysql_pass[MAXLEN], mysql_base[MAXLEN];
56     int mysql_serverport;
57     if(loadINI("neonserv.ini") == FILE_SUCCESS) {
58         mysql_host[0] = '\0';
59         ReadString("MySQL", "host", mysql_host);
60         if(!*mysql_host) {
61             perror("invalid neonserv.ini: missing MySQL host");
62             return 0;
63         }
64         mysql_port_str[0] = '\0';
65         ReadString("MySQL", "port", mysql_port_str);
66         mysql_serverport = atoi(mysql_port_str);
67         if(!mysql_serverport)
68             mysql_serverport = 3306;
69         mysql_user[0] = '\0';
70         ReadString("MySQL", "user", mysql_user);
71         if(!*mysql_user) {
72             perror("invalid neonserv.ini: missing MySQL user");
73             return 0;
74         }
75         mysql_pass[0] = '\0';
76         ReadString("MySQL", "pass", mysql_pass);
77         if(!*mysql_pass) {
78             perror("invalid neonserv.ini: missing MySQL pass");
79             return 0;
80         }
81         mysql_base[0] = '\0';
82         ReadString("MySQL", "base", mysql_base);
83         if(!*mysql_base) {
84             perror("invalid neonserv.ini: missing MySQL base");
85             return 0;
86         }
87     } else {
88         perror("Unable to load neonserv.ini");
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(void)
96 {
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     queue_init();
115     init_lang();
116     init_parser();
117     init_UserNode();
118     init_ChanNode();
119     init_ModeNode();
120     init_bind();
121         init_modcmd();
122     init_handleinfohandler();
123     init_tools();
124     init_bots();
125     init_DBHelper();
126     
127     load_languages();
128     
129     time_t socket_wait;
130     while(1) {
131         socket_wait = time(0) + SOCKET_SELECT_TIME;
132         do {
133             socket_loop(SOCKET_SELECT_TIME);
134         } while(time(0) < socket_wait);
135         timeq_tick();
136         loop_bots();
137         clearTempUsers();
138         destroyEvents();
139         queue_loop();
140     }
141 }
142
143 int stricmp (const char *s1, const char *s2)
144 {
145    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
146    if (s2 == NULL) return *s1;
147    char c1, c2;
148    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
149    {
150      if (*s1 == '\0') break;
151      ++s1; ++s2;
152    }
153    return c1 - c2;
154 }
155
156 int stricmplen (const char *s1, const char *s2, int len)
157 {
158    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
159    if (s2 == NULL) return *s1;
160    char c1, c2;
161    int i = 0;
162    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
163    {
164      i++;
165      if (*s1 == '\0') break;
166      ++s1; ++s2;
167      if(i == len) break;
168    }
169    return c1 - c2;
170 }
171