47de2620ac71a63571a354b65a89ff16daba0d80
[NeonServV5.git] / src / main.c
1
2 #include "main.h"
3 #include "ClientSocket.h"
4 #include "UserNode.h"
5 #include "ChanNode.h"
6 #include "IRCEvents.h"
7 #include "IRCParser.h"
8 #include "modcmd.h"
9 #include "WHOHandler.h"
10 #include "bots.h"
11 #include "mysqlConn.h"
12 #include "HandleInfoHandler.h"
13 #include "lang.h"
14 #include "tools.h"
15 #include "timeq.h"
16 #include "EventLogger.h"
17 #include "ModeNode.h"
18 #include "lib/ini.h"
19
20 time_t start_time;
21
22 void cleanup() {
23     free_sockets();
24     free_parser();
25     free_UserNode();
26     free_ChanNode();
27     free_bind();
28     free_modcmd();
29     free_whoqueue();
30     free_bots();
31     free_mysql();
32     free_handleinfohandler();
33     free_lang();
34 }
35
36 static int load_mysql_config() {
37     char mysql_host[MAXLEN], mysql_port_str[MAXLEN], mysql_user[MAXLEN], mysql_pass[MAXLEN], mysql_base[MAXLEN];
38     int mysql_serverport;
39     if(loadINI("neonserv.ini") == FILE_SUCCESS) {
40         mysql_host[0] = '\0';
41         ReadString("MySQL", "host", mysql_host);
42         if(!*mysql_host) {
43             perror("invalid neonserv.ini: missing MySQL host");
44             return 0;
45         }
46         mysql_port_str[0] = '\0';
47         ReadString("MySQL", "port", mysql_port_str);
48         mysql_serverport = atoi(mysql_port_str);
49         if(!mysql_serverport)
50             mysql_serverport = 3306;
51         mysql_user[0] = '\0';
52         ReadString("MySQL", "user", mysql_user);
53         if(!*mysql_user) {
54             perror("invalid neonserv.ini: missing MySQL user");
55             return 0;
56         }
57         mysql_pass[0] = '\0';
58         ReadString("MySQL", "pass", mysql_pass);
59         if(!*mysql_pass) {
60             perror("invalid neonserv.ini: missing MySQL pass");
61             return 0;
62         }
63         mysql_base[0] = '\0';
64         ReadString("MySQL", "base", mysql_base);
65         if(!*mysql_base) {
66             perror("invalid neonserv.ini: missing MySQL base");
67             return 0;
68         }
69     } else {
70         perror("Unable to load neonserv.ini");
71         return 0;
72     }
73     init_mysql(mysql_host, mysql_serverport, mysql_user, mysql_pass, mysql_base);
74     return 1;
75 }
76
77 int main(void)
78 {
79     
80     start_time = time(0);
81     
82     #ifdef WIN32
83     int res;
84     WSADATA wsadata;
85     // Start Windows Sockets.
86     res = WSAStartup(MAKEWORD(2, 0), &wsadata);
87     if (res)
88     {
89         perror("Unable to start Windows Sockets");
90         return 0;
91     }
92     #endif
93     
94     if(!load_mysql_config()) return 0;
95     
96     init_lang();
97     init_parser();
98     init_UserNode();
99     init_ChanNode();
100     init_ModeNode();
101     init_bind();
102         init_modcmd();
103     init_handleinfohandler();
104     init_tools();
105     init_bots();
106     
107     load_languages();
108     
109     time_t socket_wait;
110     while(1) {
111         socket_wait = time(0) + SOCKET_SELECT_TIME;
112         do {
113             socket_loop(SOCKET_SELECT_TIME);
114         } while(time(0) < socket_wait);
115         timeq_tick();
116         loop_bots();
117         clearTempUsers();
118         destroyEvents();
119     }
120 }
121
122 int stricmp (const char *s1, const char *s2)
123 {
124    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
125    if (s2 == NULL) return *s1;
126    char c1, c2;
127    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
128    {
129      if (*s1 == '\0') break;
130      ++s1; ++s2;
131    }
132    return c1 - c2;
133 }
134
135 int stricmplen (const char *s1, const char *s2, int len)
136 {
137    if (s1 == NULL) return s2 == NULL ? 0 : -(*s2);
138    if (s2 == NULL) return *s1;
139    char c1, c2;
140    int i = 0;
141    while ((c1 = tolower (*s1)) == (c2 = tolower (*s2)))
142    {
143      i++;
144      if (*s1 == '\0') break;
145      ++s1; ++s2;
146      if(i == len) break;
147    }
148    return c1 - c2;
149 }
150