1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Main file (Windows specific part) */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #define BITLBEE_CORE |
---|
27 | #include "bitlbee.h" |
---|
28 | #include "commands.h" |
---|
29 | #include "crypting.h" |
---|
30 | #include "protocols/nogaim.h" |
---|
31 | #include "help.h" |
---|
32 | #include <signal.h> |
---|
33 | #include <windows.h> |
---|
34 | |
---|
35 | global_t global; /* Against global namespace pollution */ |
---|
36 | |
---|
37 | static void WINAPI service_ctrl (DWORD dwControl) |
---|
38 | { |
---|
39 | switch (dwControl) |
---|
40 | { |
---|
41 | case SERVICE_CONTROL_STOP: |
---|
42 | /* FIXME */ |
---|
43 | break; |
---|
44 | |
---|
45 | case SERVICE_CONTROL_INTERROGATE: |
---|
46 | break; |
---|
47 | |
---|
48 | default: |
---|
49 | break; |
---|
50 | |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | static void bitlbee_init(int argc, char **argv) |
---|
55 | { |
---|
56 | int i = -1; |
---|
57 | memset( &global, 0, sizeof( global_t ) ); |
---|
58 | |
---|
59 | b_main_init(); |
---|
60 | |
---|
61 | global.conf = conf_load( argc, argv ); |
---|
62 | if( global.conf == NULL ) |
---|
63 | return; |
---|
64 | |
---|
65 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
66 | { |
---|
67 | i = bitlbee_inetd_init(); |
---|
68 | log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION ); |
---|
69 | |
---|
70 | } |
---|
71 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
72 | { |
---|
73 | i = bitlbee_daemon_init(); |
---|
74 | log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION ); |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | log_message( LOGLVL_INFO, "No bitlbee mode specified..."); |
---|
79 | } |
---|
80 | |
---|
81 | if( i != 0 ) |
---|
82 | return; |
---|
83 | |
---|
84 | if( access( global.conf->configdir, F_OK ) != 0 ) |
---|
85 | log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", global.conf->configdir ); |
---|
86 | else if( access( global.conf->configdir, 06 ) != 0 ) |
---|
87 | log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir ); |
---|
88 | if( help_init( &(global.help), HELP_FILE ) == NULL ) |
---|
89 | log_message( LOGLVL_WARNING, "Error opening helpfile %s.", global.helpfile ); |
---|
90 | } |
---|
91 | |
---|
92 | void service_main (DWORD argc, LPTSTR *argv) |
---|
93 | { |
---|
94 | SERVICE_STATUS_HANDLE handle; |
---|
95 | SERVICE_STATUS status; |
---|
96 | |
---|
97 | handle = RegisterServiceCtrlHandler("bitlbee", service_ctrl); |
---|
98 | |
---|
99 | if (!handle) |
---|
100 | return; |
---|
101 | |
---|
102 | status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; |
---|
103 | status.dwServiceSpecificExitCode = 0; |
---|
104 | |
---|
105 | bitlbee_init(argc, argv); |
---|
106 | |
---|
107 | SetServiceStatus(handle, &status); |
---|
108 | |
---|
109 | b_main_run( ); |
---|
110 | } |
---|
111 | |
---|
112 | SERVICE_TABLE_ENTRY dispatch_table[] = |
---|
113 | { |
---|
114 | { TEXT("bitlbee"), (LPSERVICE_MAIN_FUNCTION)service_main }, |
---|
115 | { NULL, NULL } |
---|
116 | }; |
---|
117 | |
---|
118 | static int debug = 0; |
---|
119 | |
---|
120 | static void usage() |
---|
121 | { |
---|
122 | printf("Options:\n"); |
---|
123 | printf("-h Show this help message\n"); |
---|
124 | printf("-d Debug mode (simple console program)\n"); |
---|
125 | } |
---|
126 | |
---|
127 | int main( int argc, char **argv) |
---|
128 | { |
---|
129 | int i; |
---|
130 | WSADATA WSAData; |
---|
131 | |
---|
132 | nogaim_init( ); |
---|
133 | |
---|
134 | for (i = 1; i < argc; i++) { |
---|
135 | if (!strcmp(argv[i], "-d")) debug = 1; |
---|
136 | if (!strcmp(argv[i], "-h")) { |
---|
137 | usage(); |
---|
138 | return 0; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | WSAStartup(MAKEWORD(1,1), &WSAData); |
---|
143 | |
---|
144 | if (!debug) { |
---|
145 | if (!StartServiceCtrlDispatcher(dispatch_table)) |
---|
146 | log_message( LOGLVL_ERROR, "StartServiceCtrlDispatcher failed."); |
---|
147 | } else { |
---|
148 | bitlbee_init(argc, argv); |
---|
149 | b_main_run(); |
---|
150 | } |
---|
151 | |
---|
152 | return 0; |
---|
153 | } |
---|
154 | |
---|
155 | double gettime() |
---|
156 | { |
---|
157 | return (GetTickCount() / 1000); |
---|
158 | } |
---|
159 | |
---|
160 | void conf_get_string(HKEY section, const char *name, const char *def, char **dest) |
---|
161 | { |
---|
162 | char buf[4096]; |
---|
163 | long x; |
---|
164 | if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) { |
---|
165 | *dest = g_strdup(buf); |
---|
166 | } else if (!def) { |
---|
167 | *dest = NULL; |
---|
168 | } else { |
---|
169 | *dest = g_strdup(def); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | void conf_get_int(HKEY section, const char *name, int def, int *dest) |
---|
175 | { |
---|
176 | char buf[20]; |
---|
177 | long x; |
---|
178 | DWORD y; |
---|
179 | if (RegQueryValue(section, name, buf, &x) == ERROR_SUCCESS) { |
---|
180 | memcpy(&y, buf, sizeof(DWORD)); |
---|
181 | *dest = y; |
---|
182 | } else { |
---|
183 | *dest = def; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | conf_t *conf_load( int argc, char *argv[] ) |
---|
188 | { |
---|
189 | conf_t *conf; |
---|
190 | HKEY key, key_main, key_proxy; |
---|
191 | char *tmp; |
---|
192 | |
---|
193 | RegOpenKey(HKEY_CURRENT_USER, "SOFTWARE\\Bitlbee", &key); |
---|
194 | RegOpenKey(key, "main", &key_main); |
---|
195 | RegOpenKey(key, "proxy", &key_proxy); |
---|
196 | |
---|
197 | memset( &global, 0, sizeof( global_t ) ); |
---|
198 | b_main_init(); |
---|
199 | |
---|
200 | conf = g_new0( conf_t,1 ); |
---|
201 | global.conf = conf; |
---|
202 | conf_get_string(key_main, "interface_in", "0.0.0.0", &global.conf->iface_in); |
---|
203 | conf_get_string(key_main, "interface_out", "0.0.0.0", &global.conf->iface_out); |
---|
204 | conf_get_string(key_main, "port", "6667", &global.conf->port); |
---|
205 | conf_get_int(key_main, "verbose", 0, &global.conf->verbose); |
---|
206 | conf_get_string(key_main, "auth_pass", "", &global.conf->auth_pass); |
---|
207 | conf_get_string(key_main, "oper_pass", "", &global.conf->oper_pass); |
---|
208 | conf_get_int(key_main, "ping_interval_timeout", 60, &global.conf->ping_interval); |
---|
209 | conf_get_string(key_main, "hostname", "localhost", &global.conf->hostname); |
---|
210 | conf_get_string(key_main, "configdir", NULL, &global.conf->configdir); |
---|
211 | conf_get_string(key_main, "motdfile", NULL, &global.conf->motdfile); |
---|
212 | conf_get_string(key_main, "helpfile", NULL, &global.helpfile); |
---|
213 | global.conf->runmode = RUNMODE_DAEMON; |
---|
214 | conf_get_int(key_main, "AuthMode", AUTHMODE_OPEN, (int *)&global.conf->authmode); |
---|
215 | conf_get_string(key_proxy, "host", "", &tmp); strcpy(proxyhost, tmp); |
---|
216 | conf_get_string(key_proxy, "user", "", &tmp); strcpy(proxyuser, tmp); |
---|
217 | conf_get_string(key_proxy, "password", "", &tmp); strcpy(proxypass, tmp); |
---|
218 | conf_get_int(key_proxy, "type", PROXY_NONE, &proxytype); |
---|
219 | conf_get_int(key_proxy, "port", 3128, &proxyport); |
---|
220 | |
---|
221 | RegCloseKey(key); |
---|
222 | RegCloseKey(key_main); |
---|
223 | RegCloseKey(key_proxy); |
---|
224 | |
---|
225 | return conf; |
---|
226 | } |
---|
227 | |
---|
228 | void conf_loaddefaults( irc_t *irc ) |
---|
229 | { |
---|
230 | HKEY key_defaults; |
---|
231 | int i; |
---|
232 | char name[4096], data[4096]; |
---|
233 | DWORD namelen = sizeof(name), datalen = sizeof(data); |
---|
234 | DWORD type; |
---|
235 | if (RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee\\defaults", &key_defaults) != ERROR_SUCCESS) { |
---|
236 | return; |
---|
237 | } |
---|
238 | |
---|
239 | for (i = 0; RegEnumValue(key_defaults, i, name, &namelen, NULL, &type, data, &datalen) == ERROR_SUCCESS; i++) { |
---|
240 | set_t *s = set_find( &irc->set, name ); |
---|
241 | |
---|
242 | if( s ) |
---|
243 | { |
---|
244 | if( s->def ) g_free( s->def ); |
---|
245 | s->def = g_strdup( data ); |
---|
246 | } |
---|
247 | |
---|
248 | namelen = sizeof(name); |
---|
249 | datalen = sizeof(data); |
---|
250 | } |
---|
251 | |
---|
252 | RegCloseKey(key_defaults); |
---|
253 | } |
---|
254 | |
---|
255 | #ifndef INADDR_NONE |
---|
256 | #define INADDR_NONE 0xffffffff |
---|
257 | #endif |
---|
258 | |
---|
259 | int |
---|
260 | inet_aton(const char *cp, struct in_addr *addr) |
---|
261 | { |
---|
262 | addr->s_addr = inet_addr(cp); |
---|
263 | return (addr->s_addr == INADDR_NONE) ? 0 : 1; |
---|
264 | } |
---|
265 | |
---|
266 | void log_error(char *msg) |
---|
267 | { |
---|
268 | log_message(LOGLVL_ERROR, "%s", msg); |
---|
269 | } |
---|
270 | |
---|
271 | void log_message(int level, char *message, ...) |
---|
272 | { |
---|
273 | HANDLE hEventSource; |
---|
274 | LPTSTR lpszStrings[2]; |
---|
275 | WORD elevel; |
---|
276 | va_list ap; |
---|
277 | |
---|
278 | va_start(ap, message); |
---|
279 | |
---|
280 | if (debug) { |
---|
281 | vprintf(message, ap); |
---|
282 | putchar('\n'); |
---|
283 | va_end(ap); |
---|
284 | return; |
---|
285 | } |
---|
286 | |
---|
287 | hEventSource = RegisterEventSource(NULL, TEXT("bitlbee")); |
---|
288 | |
---|
289 | lpszStrings[0] = TEXT("bitlbee"); |
---|
290 | lpszStrings[1] = g_strdup_vprintf(message, ap); |
---|
291 | va_end(ap); |
---|
292 | |
---|
293 | switch (level) { |
---|
294 | case LOGLVL_ERROR: elevel = EVENTLOG_ERROR_TYPE; break; |
---|
295 | case LOGLVL_WARNING: elevel = EVENTLOG_WARNING_TYPE; break; |
---|
296 | case LOGLVL_INFO: elevel = EVENTLOG_INFORMATION_TYPE; break; |
---|
297 | #ifdef DEBUG |
---|
298 | case LOGLVL_DEBUG: elevel = EVENTLOG_AUDIT_SUCCESS; break; |
---|
299 | #endif |
---|
300 | } |
---|
301 | |
---|
302 | if (hEventSource != NULL) { |
---|
303 | ReportEvent(hEventSource, |
---|
304 | elevel, |
---|
305 | 0, |
---|
306 | 0, |
---|
307 | NULL, |
---|
308 | 2, |
---|
309 | 0, |
---|
310 | lpszStrings, |
---|
311 | NULL); |
---|
312 | |
---|
313 | DeregisterEventSource(hEventSource); |
---|
314 | } |
---|
315 | |
---|
316 | g_free(lpszStrings[1]); |
---|
317 | } |
---|
318 | |
---|
319 | void log_link(int level, int output) { /* FIXME */ } |
---|
320 | |
---|
321 | struct tm * |
---|
322 | gmtime_r (const time_t *timer, struct tm *result) |
---|
323 | { |
---|
324 | struct tm *local_result; |
---|
325 | local_result = gmtime (timer); |
---|
326 | |
---|
327 | if (local_result == NULL || result == NULL) |
---|
328 | return NULL; |
---|
329 | |
---|
330 | memcpy (result, local_result, sizeof (result)); |
---|
331 | return result; |
---|
332 | } |
---|