added GPL header to all files and added INSTALL AUTHORS COPYING files.
[NeonServV5.git] / src / main.c
1 /* main.c - NeonServ v5.0
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 "lib/ini.h"
35
36 time_t start_time;
37
38 void cleanup() {
39     free_sockets();
40     free_parser();
41     free_UserNode();
42     free_ChanNode();
43     free_bind();
44     free_modcmd();
45     free_whoqueue();
46     free_bots();
47     free_mysql();
48     free_handleinfohandler();
49     free_lang();
50 }
51
52 static int load_mysql_config() {
53     char mysql_host[MAXLEN], mysql_port_str[MAXLEN], mysql_user[MAXLEN], mysql_pass[MAXLEN], mysql_base[MAXLEN];
54     int mysql_serverport;
55     if(loadINI("neonserv.ini") == FILE_SUCCESS) {
56         mysql_host[0] = '\0';
57         ReadString("MySQL", "host", mysql_host);
58         if(!*mysql_host) {
59             perror("invalid neonserv.ini: missing MySQL host");
60             return 0;
61         }
62         mysql_port_str[0] = '\0';
63         ReadString("MySQL", "port", mysql_port_str);
64         mysql_serverport = atoi(mysql_port_str);
65         if(!mysql_serverport)
66             mysql_serverport = 3306;
67         mysql_user[0] = '\0';
68         ReadString("MySQL", "user", mysql_user);
69         if(!*mysql_user) {
70             perror("invalid neonserv.ini: missing MySQL user");
71             return 0;
72         }
73         mysql_pass[0] = '\0';
74         ReadString("MySQL", "pass", mysql_pass);
75         if(!*mysql_pass) {
76             perror("invalid neonserv.ini: missing MySQL pass");
77             return 0;
78         }
79         mysql_base[0] = '\0';
80         ReadString("MySQL", "base", mysql_base);
81         if(!*mysql_base) {
82             perror("invalid neonserv.ini: missing MySQL base");
83             return 0;
84         }
85     } else {
86         perror("Unable to load neonserv.ini");
87         return 0;
88     }
89     init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
90     return 1;
91 }
92
93 int main(void)
94 {
95     
96     start_time = time(0);
97     
98     #ifdef WIN32
99     int res;
100     WSADATA wsadata;
101     // Start Windows Sockets.
102     res = WSAStartup(MAKEWORD(2, 0), &wsadata);
103     if (res)
104     {
105         perror("Unable to start Windows Sockets");
106         return 0;
107     }
108     #endif
109     
110     if(!load_mysql_config()) return 0;
111     
112     init_lang();
113     init_parser();
114     init_UserNode();
115     init_ChanNode();
116     init_ModeNode();
117     init_bind();
118         init_modcmd();
119     init_handleinfohandler();
120     init_tools();
121     init_bots();
122     
123     load_languages();
124     
125     time_t socket_wait;
126     while(1) {
127         socket_wait = time(0) + SOCKET_SELECT_TIME;
128         do {
129             socket_loop(SOCKET_SELECT_TIME);
130         } while(time(0) < socket_wait);
131         timeq_tick();
132         loop_bots();
133         clearTempUsers();
134         destroyEvents();
135     }
136 }
137
138 int stricmp (const char *s1, const char *s2)
139 {
140    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
141    if (s2 == NULL) return *s1;
142    char c1, c2;
143    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
144    {
145      if (*s1 == '\0') break;
146      ++s1; ++s2;
147    }
148    return c1 - c2;
149 }
150
151 int stricmplen (const char *s1, const char *s2, int len)
152 {
153    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
154    if (s2 == NULL) return *s1;
155    char c1, c2;
156    int i = 0;
157    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
158    {
159      i++;
160      if (*s1 == '\0') break;
161      ++s1; ++s2;
162      if(i == len) break;
163    }
164    return c1 - c2;
165 }
166