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