added new self-made config parser with new config style
[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 "commands.h"
37 #include "ConfigParser.h"
38
39 time_t start_time;
40
41 void cleanup() {
42     free_sockets();
43     free_parser();
44     free_UserNode();
45     free_ChanNode();
46     free_bind();
47     free_modcmd();
48     free_whoqueue();
49     free_bots();
50     free_mysql();
51     free_handleinfohandler();
52     free_lang();
53 }
54
55 static int load_mysql_config() {
56     char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
57     int mysql_serverport;
58     if(loadConfig("neonserv.conf")) {
59         mysql_host = get_string_field("MySQL.host");
60         if(!mysql_host) {
61             perror("invalid neonserv.conf: missing MySQL.host");
62             return 0;
63         }
64         mysql_serverport = get_int_field("MySQL.port");
65         if(!mysql_serverport)
66             mysql_serverport = 3306;
67         mysql_user = get_string_field("MySQL.user");
68         if(!mysql_user) {
69             perror("invalid neonserv.conf: missing MySQL.user");
70             return 0;
71         }
72         mysql_pass = get_string_field("MySQL.pass");
73         if(!mysql_pass) {
74             perror("invalid neonserv.conf: missing MySQL.pass");
75             return 0;
76         }
77         mysql_base = get_string_field("MySQL.base");
78         if(!mysql_base) {
79             perror("invalid neonserv.conf: missing MySQL base");
80             return 0;
81         }
82     } else {
83         perror("Unable to load neonserv.conf");
84         return 0;
85     }
86     init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
87     return 1;
88 }
89
90 int main(void)
91 {
92     
93     start_time = time(0);
94     
95     #ifdef WIN32
96     int res;
97     WSADATA wsadata;
98     // Start Windows Sockets.
99     res = WSAStartup(MAKEWORD(2, 0), &wsadata);
100     if (res)
101     {
102         perror("Unable to start Windows Sockets");
103         return 0;
104     }
105     #endif
106     
107     if(!load_mysql_config()) return 0;
108     
109     queue_init();
110     init_lang();
111     init_parser();
112     init_UserNode();
113     init_ChanNode();
114     init_ModeNode();
115     init_bind();
116         init_modcmd();
117     init_handleinfohandler();
118     init_tools();
119     register_commands();
120     init_bots();
121     init_DBHelper();
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         queue_loop();
136     }
137 }
138
139 int stricmp (const char *s1, const char *s2)
140 {
141    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
142    if (s2 == NULL) return *s1;
143    char c1, c2;
144    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
145    {
146      if (*s1 == '\0') break;
147      ++s1; ++s2;
148    }
149    return c1 - c2;
150 }
151
152 int stricmplen (const char *s1, const char *s2, int len)
153 {
154    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
155    if (s2 == NULL) return *s1;
156    char c1, c2;
157    int i = 0;
158    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
159    {
160      i++;
161      if (*s1 == '\0') break;
162      ++s1; ++s2;
163      if(i == len) break;
164    }
165    return c1 - c2;
166 }
167