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